Polyphase Game Engine
Loading...
Searching...
No Matches
TileSet.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include "glm/glm.hpp"
6#include "Asset.h"
7#include "AssetRef.h"
8
9class Texture;
10
11// Per-tile collision shape kind. Phase 1 only authors None / FullSolid via the
12// inspector; richer types (Box, Boxes, Slope, Polygon) are populated in Phase 2
13// and beyond when the dedicated TileSet editor lands.
14enum class TileCollisionType : uint8_t
15{
16 None = 0,
18 Box,
19 Boxes,
20 Slope,
21 Polygon,
22
23 Count
24};
25
26// Metadata for a single tile slot in the atlas. Tile indices are stable —
27// every slot in the atlas grid gets a TileDefinition, even if it is unused,
28// so layouts can grow without breaking saved TileMaps.
30{
31 int32_t mTileIndex = -1;
32 glm::ivec2 mAtlasCoord = { 0, 0 };
33 std::string mName;
34 std::vector<std::string> mTags;
35
36 bool mHasCollision = false;
38
39 // Tile-local pixel coords. Phase 2 populates these from the TileSet editor.
40 std::vector<glm::vec4> mCollisionRects; // (x, y, w, h)
41 std::vector<glm::vec2> mCollisionPoly;
42
43 bool mIsAnimated = false;
44 std::vector<int32_t> mAnimFrames;
45 float mAnimFps = 0.0f;
46};
47
48// 9-box brush definition. Authored in the TileSet editor in Phase 3.
50{
51 std::string mName;
52
53 int32_t mTopLeft = -1;
54 int32_t mTop = -1;
55 int32_t mTopRight = -1;
56 int32_t mLeft = -1;
57 int32_t mCenter = -1;
58 int32_t mRight = -1;
59 int32_t mBottomLeft = -1;
60 int32_t mBottom = -1;
61 int32_t mBottomRight = -1;
62
63 std::vector<std::string> mTags;
64};
65
66// Phase 4 — autotile / terrain rules.
67//
68// An autotile rule pattern-matches against the 8 neighbors of a cell. Each
69// neighbor slot is one of three states:
70// DontCare — neighbor is irrelevant
71// MustBeSelf — neighbor MUST be a member of this autotile group
72// MustNotBeSelf — neighbor MUST NOT be a member of this autotile group
73//
74// Slot indices follow a 3x3 grid (with +Y up) skipping the center:
75// [0]=NW [1]=N [2]=NE
76// [3]=W [4]=E
77// [5]=SW [6]=S [7]=SE
78enum class AutotileNeighborState : uint8_t
79{
80 DontCare = 0,
83
84 Count
85};
86
88{
90 std::vector<int32_t> mResultTiles; // pick one (first for now; random in a follow-up)
91};
92
94{
95 std::string mName;
96 std::vector<std::string> mMemberTags; // a tile is "self" if it has any of these tags
97 std::vector<int32_t> mMemberTileIndices; // ...or its index appears in this list
98 std::vector<AutotileRule> mRules;
99};
100
102{
103public:
104
106
107 TileSet();
108 virtual ~TileSet();
109
110 virtual void LoadStream(Stream& stream, Platform platform) override;
111 virtual void SaveStream(Stream& stream, Platform platform) override;
112 virtual void Create() override;
113 virtual void Destroy() override;
114 virtual void GatherProperties(std::vector<Property>& outProps) override;
115 virtual glm::vec4 GetTypeColor() override;
116 virtual const char* GetTypeName() override;
117 virtual bool ShouldSnapshotForPie() const override { return true; }
118
119 Texture* GetTexture() const;
120 void SetTexture(Texture* tex);
121
122 int32_t GetTileWidth() const { return mTileWidth; }
123 int32_t GetTileHeight() const { return mTileHeight; }
124 int32_t GetMarginX() const { return mMarginX; }
125 int32_t GetMarginY() const { return mMarginY; }
126 int32_t GetSpacingX() const { return mSpacingX; }
127 int32_t GetSpacingY() const { return mSpacingY; }
128
129 // Recompute mAtlasColumns / mAtlasRows from the texture and slicing parameters
130 // and resize mTiles to match. Called automatically by HandlePropChange.
131 void RebuildTileGrid();
132
133 // Number of valid tile slots in the atlas (rows * columns).
134 int32_t GetNumTiles() const;
135
136 glm::ivec2 GetAtlasGridSize() const { return { mAtlasColumns, mAtlasRows }; }
137
138 // Convert a tile index to its (column, row) in the atlas. Returns (-1,-1) if out of range.
139 glm::ivec2 TileIndexToAtlasCoord(int32_t tileIndex) const;
140
141 // Compute the [u0,v0]..[u1,v1] UV rectangle for a tile in [0,1] texture space.
142 // Returns false if the index is out of range or no texture is bound.
143 bool GetTileUVs(int32_t tileIndex, glm::vec2& outUV0, glm::vec2& outUV1) const;
144
145 // Const accessors for tile metadata. Returns nullptr if the index is out of range.
146 const TileDefinition* GetTileDef(int32_t tileIndex) const;
147 TileDefinition* GetTileDefMutable(int32_t tileIndex);
148
149 bool IsTileSolid(int32_t tileIndex) const;
150 bool HasTileTag(int32_t tileIndex, const std::string& tag) const;
151
152 const std::vector<TileDefinition>& GetTiles() const { return mTiles; }
153
154 // 9-box brush accessors (Phase 3)
155 const std::vector<NineBoxBrushDef>& GetNineBoxBrushes() const { return mNineBoxBrushes; }
156 std::vector<NineBoxBrushDef>& GetNineBoxBrushesMutable() { return mNineBoxBrushes; }
157 int32_t AddNineBoxBrush(const std::string& name);
158 void RemoveNineBoxBrush(int32_t index);
159
160 // Autotile accessors (Phase 4)
161 const std::vector<AutotileSet>& GetAutotileSets() const { return mAutotileSets; }
162 std::vector<AutotileSet>& GetAutotileSetsMutable() { return mAutotileSets; }
163 int32_t AddAutotileSet(const std::string& name);
164 void RemoveAutotileSet(int32_t index);
165
166 // Returns true if the given tile index belongs to the autotile group
167 // (matches one of its mMemberTileIndices or mMemberTags entries).
168 bool IsTileMemberOfAutotile(int32_t autotileIndex, int32_t tileIndex) const;
169
170 // Walk the autotile rules in order and return the first matching tile
171 // index for the given 8-neighbor mask. The mask uses a single bit per
172 // neighbor slot (1 = neighbor is self, 0 = neighbor is not self), in the
173 // same slot order as AutotileRule::mNeighbors. Returns -1 if no rule matches.
174 int32_t MatchAutotileRule(int32_t autotileIndex, uint8_t selfMask) const;
175
176 static bool HandlePropChange(class Datum* datum, uint32_t index, const void* newValue);
177
178protected:
179
181 int32_t mTileWidth = 16;
182 int32_t mTileHeight = 16;
183 int32_t mMarginX = 0;
184 int32_t mMarginY = 0;
185 int32_t mSpacingX = 0;
186 int32_t mSpacingY = 0;
187
188 int32_t mAtlasColumns = 0;
189 int32_t mAtlasRows = 0;
190
191 std::vector<TileDefinition> mTiles;
192 std::vector<NineBoxBrushDef> mNineBoxBrushes;
193 std::vector<AutotileSet> mAutotileSets;
194};
Platform
Definition EngineTypes.h:31
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
AutotileNeighborState
Definition TileSet.h:79
TileCollisionType
Definition TileSet.h:15
Definition AssetRef.h:18
Definition Asset.h:113
virtual void Create()
Definition Asset.cpp:77
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:236
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:254
virtual const char * GetTypeName()
Definition Asset.cpp:259
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:249
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:222
virtual void Destroy()
Definition Asset.cpp:87
Definition Datum.h:164
Definition Stream.h:21
Definition Texture.h:10
Definition TileSet.h:102
virtual bool ShouldSnapshotForPie() const override
Definition TileSet.h:117
int32_t GetTileHeight() const
Definition TileSet.h:123
int32_t GetTileWidth() const
Definition TileSet.h:122
std::vector< NineBoxBrushDef > & GetNineBoxBrushesMutable()
Definition TileSet.h:156
int32_t GetSpacingY() const
Definition TileSet.h:127
DECLARE_ASSET(TileSet, Asset)
int32_t GetMarginY() const
Definition TileSet.h:125
AssetRef mTexture
Definition TileSet.h:180
std::vector< NineBoxBrushDef > mNineBoxBrushes
Definition TileSet.h:192
std::vector< AutotileSet > & GetAutotileSetsMutable()
Definition TileSet.h:162
const std::vector< AutotileSet > & GetAutotileSets() const
Definition TileSet.h:161
std::vector< AutotileSet > mAutotileSets
Definition TileSet.h:193
int32_t GetSpacingX() const
Definition TileSet.h:126
const std::vector< NineBoxBrushDef > & GetNineBoxBrushes() const
Definition TileSet.h:155
const std::vector< TileDefinition > & GetTiles() const
Definition TileSet.h:152
std::vector< TileDefinition > mTiles
Definition TileSet.h:191
glm::ivec2 GetAtlasGridSize() const
Definition TileSet.h:136
int32_t GetMarginX() const
Definition TileSet.h:124
Definition TileSet.h:88
std::vector< int32_t > mResultTiles
Definition TileSet.h:90
AutotileNeighborState mNeighbors[8]
Definition TileSet.h:89
Definition TileSet.h:94
std::string mName
Definition TileSet.h:95
std::vector< AutotileRule > mRules
Definition TileSet.h:98
std::vector< std::string > mMemberTags
Definition TileSet.h:96
std::vector< int32_t > mMemberTileIndices
Definition TileSet.h:97
Definition TileSet.h:50
std::string mName
Definition TileSet.h:51
int32_t mBottomRight
Definition TileSet.h:61
int32_t mTopLeft
Definition TileSet.h:53
int32_t mTop
Definition TileSet.h:54
int32_t mTopRight
Definition TileSet.h:55
int32_t mCenter
Definition TileSet.h:57
int32_t mLeft
Definition TileSet.h:56
int32_t mBottomLeft
Definition TileSet.h:59
int32_t mBottom
Definition TileSet.h:60
std::vector< std::string > mTags
Definition TileSet.h:63
int32_t mRight
Definition TileSet.h:58
Definition TileSet.h:30
std::vector< std::string > mTags
Definition TileSet.h:34
float mAnimFps
Definition TileSet.h:45
TileCollisionType mCollisionType
Definition TileSet.h:37
std::string mName
Definition TileSet.h:33
std::vector< glm::vec4 > mCollisionRects
Definition TileSet.h:40
std::vector< glm::vec2 > mCollisionPoly
Definition TileSet.h:41
bool mHasCollision
Definition TileSet.h:36
glm::ivec2 mAtlasCoord
Definition TileSet.h:32
std::vector< int32_t > mAnimFrames
Definition TileSet.h:44
bool mIsAnimated
Definition TileSet.h:43
int32_t mTileIndex
Definition TileSet.h:31