added TileMaps to TSE
This commit is contained in:
@@ -9,9 +9,7 @@ TSE::TileMapChunk::TileMapChunk(int _chunksize, const Vector2 &_pos, SortingOrde
|
||||
|
||||
void TSE::TileMapChunk::SetTile(const Vector2& p, const Vector2& Spriteindex, TileSet* set)
|
||||
{
|
||||
Sprite s;
|
||||
set->GetSpriteAt(Spriteindex, s);
|
||||
sprites[p] = s;
|
||||
sprites[p] = set->GetSpriteIdAt(Spriteindex.x, Spriteindex.y);
|
||||
}
|
||||
|
||||
void TSE::TileMapChunk::RemoveTile(Vector2 p)
|
||||
@@ -24,6 +22,130 @@ void TSE::TileMapChunk::SetOrdering(SortingOrder _order)
|
||||
order = _order;
|
||||
}
|
||||
|
||||
void TSE::TileMapChunk::GetOrderedPositions(Vector2 *array)
|
||||
{
|
||||
switch (order)
|
||||
{
|
||||
case TopLeft:
|
||||
for (int y = 0; y < chunksize; y++)
|
||||
{
|
||||
Vector2 offset = nextLine * y;
|
||||
for (int x = 0; x < chunksize; x++)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->first - offset;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TopRight:
|
||||
for (int y = 0; y < chunksize; y++)
|
||||
{
|
||||
Vector2 offset = nextLine * y;
|
||||
for (int x = chunksize - 1; x >= 0; x--)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->first - offset;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BottomLeft:
|
||||
for (int y = chunksize - 1; y >= 0; y--)
|
||||
{
|
||||
Vector2 offset = nextLine * y;
|
||||
for (int x = 0; x < chunksize; x++)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->first - offset;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BottomRight:
|
||||
for (int y = chunksize - 1; y >= 0; y--)
|
||||
{
|
||||
Vector2 offset = nextLine * y;
|
||||
for (int x = chunksize - 1; x >= 0; x--)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->first - offset;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::TileMapChunk::GetOrderedSpriteIds(int *array)
|
||||
{
|
||||
switch (order)
|
||||
{
|
||||
case TopLeft:
|
||||
for (int y = 0; y < chunksize; y++)
|
||||
{
|
||||
for (int x = 0; x < chunksize; x++)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->second;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TopRight:
|
||||
for (int y = 0; y < chunksize; y++)
|
||||
{
|
||||
for (int x = chunksize - 1; x >= 0; x--)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->second;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BottomLeft:
|
||||
for (int y = chunksize - 1; y >= 0; y--)
|
||||
{
|
||||
for (int x = 0; x < chunksize; x++)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->second;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BottomRight:
|
||||
for (int y = chunksize - 1; y >= 0; y--)
|
||||
{
|
||||
for (int x = chunksize - 1; x >= 0; x--)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = sprites.find(p);
|
||||
if(v != sprites.end())
|
||||
*array++ = v->second;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int TSE::TileMapChunk::GetChunksize()
|
||||
{
|
||||
return chunksize;
|
||||
}
|
||||
|
||||
int TSE::TileMapChunk::GetSpriteCount()
|
||||
{
|
||||
return sprites.size();
|
||||
}
|
||||
|
||||
void TSE::TileMap::RemoveTile(Vector2 p)
|
||||
{
|
||||
Vector2 chunkInnerPos = LocalToChunkPos(p);
|
||||
@@ -39,13 +161,118 @@ void TSE::TileMap::SetTile(Vector2 p, Vector2 Spriteindex)
|
||||
if(!chunks.contains(chunkIndex))
|
||||
{
|
||||
chunks[chunkIndex] = TileMapChunk(chunkSize, chunkIndex, order);
|
||||
chunks[chunkIndex].nextLine = nextLine;
|
||||
CheckBounds(chunkIndex);
|
||||
}
|
||||
chunks[chunkIndex].SetTile(chunkInnerPos, Spriteindex, set);
|
||||
}
|
||||
|
||||
TSE::TileMapChunk* TSE::TileMap::GetChunk(const Vector2 &pos)
|
||||
{
|
||||
auto chunk = chunks.find(pos);
|
||||
if(chunk == chunks.end())
|
||||
return nullptr;
|
||||
return &chunks[pos];
|
||||
}
|
||||
|
||||
void TSE::TileMap::GetChunkPositionsInOrder(Vector2 *arr)
|
||||
{
|
||||
|
||||
switch (order)
|
||||
{
|
||||
case TopLeft:
|
||||
for (int y = bounds.p1.y; y < bounds.p2.y + 1; y++)
|
||||
{
|
||||
for (int x = bounds.p1.x; x < bounds.p2.x + 1; x++)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = chunks.find(p);
|
||||
if(v != chunks.end())
|
||||
*arr++ = v->first;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TopRight:
|
||||
for (int y = bounds.p1.y; y < bounds.p2.y + 1; y++)
|
||||
{
|
||||
for (int x = bounds.p2.x; x > bounds.p1.x - 1; x--)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = chunks.find(p);
|
||||
if(v != chunks.end())
|
||||
*arr++ = v->first;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BottomLeft:
|
||||
for (int y = bounds.p2.y; y > bounds.p1.y - 1; y--)
|
||||
{
|
||||
for (int x = bounds.p1.x; x < bounds.p2.x + 1; x++)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = chunks.find(p);
|
||||
if(v != chunks.end())
|
||||
*arr++ = v->first;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BottomRight:
|
||||
for (int y = bounds.p2.y; y > bounds.p1.y - 1; y--)
|
||||
{
|
||||
for (int x = bounds.p2.x; x > bounds.p1.x - 1; x--)
|
||||
{
|
||||
Vector2 p(x,y);
|
||||
auto v = chunks.find(p);
|
||||
if(v != chunks.end())
|
||||
*arr++ = v->first;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int TSE::TileMap::GetChunkCount()
|
||||
{
|
||||
return chunks.size();
|
||||
}
|
||||
|
||||
TSE::TileSet *TSE::TileMap::GetTileSet()
|
||||
{
|
||||
return set;
|
||||
}
|
||||
|
||||
void TSE::TileMap::SetNextLineOffset(const Vector2 &offset)
|
||||
{
|
||||
nextLine = offset;
|
||||
for(auto& [_, chunk] : chunks)
|
||||
{
|
||||
chunk.nextLine = offset;
|
||||
}
|
||||
}
|
||||
|
||||
TSE::Vector2 TSE::TileMap::GetNextLineOffset()
|
||||
{
|
||||
return nextLine;
|
||||
}
|
||||
|
||||
void TSE::TileMap::CheckBounds(Vector2 pos)
|
||||
{
|
||||
if(pos.x > bounds.p2.x)
|
||||
bounds.p2.x = pos.x;
|
||||
if(pos.y > bounds.p2.y)
|
||||
bounds.p2.y = pos.y;
|
||||
if(pos.x < bounds.p1.x)
|
||||
bounds.p1.x = pos.x;
|
||||
if(pos.y < bounds.p1.y)
|
||||
bounds.p1.y = pos.y;
|
||||
}
|
||||
|
||||
TSE::Vector2 TSE::TileMap::LocalToChunkPos(const Vector2 &v)
|
||||
{
|
||||
return Vector2((int)v.x % chunkSize, (int)v.y % chunkSize);
|
||||
Vector2 p = Vector2((int)v.x % chunkSize, (int)v.y % chunkSize);
|
||||
if(p.x < 0) p.x += chunkSize;
|
||||
if(p.y < 0) p.y += chunkSize;
|
||||
return p;
|
||||
}
|
||||
|
||||
TSE::Vector2 TSE::TileMap::ChunkToLocalPos(const Vector2 &v, const TileMapChunk &chunk)
|
||||
|
||||
@@ -23,33 +23,50 @@ namespace TSE
|
||||
private:
|
||||
SortingOrder order;
|
||||
int chunksize;
|
||||
std::unordered_map<Vector2, Sprite> sprites;
|
||||
std::unordered_map<Vector2, int> sprites;
|
||||
public:
|
||||
Vector2 nextLine;
|
||||
Vector2 pos;
|
||||
TileMapChunk(int _chunksize, const Vector2& _pos, SortingOrder _order);
|
||||
TileMapChunk(int _chunksize = 16, const Vector2& _pos = {0,0}, SortingOrder _order = TopRight);
|
||||
|
||||
void SetTile(const Vector2& p, const Vector2& Spriteindex, TileSet* set);
|
||||
void RemoveTile(Vector2 p);
|
||||
void SetOrdering(SortingOrder _order);
|
||||
void GetOrderedPositions(Vector2* array);
|
||||
void GetOrderedSpriteIds(int* array);
|
||||
int GetChunksize();
|
||||
int GetSpriteCount();
|
||||
|
||||
};
|
||||
|
||||
class TileMap : public BehaviourScript
|
||||
{
|
||||
private:
|
||||
Rect bounds = Rect(0,0,0,0);
|
||||
Vector2 nextLine = Vector2(-0.5f, 1.25f);
|
||||
public:
|
||||
int chunkSize = 16;
|
||||
SortingOrder order = TopRight;
|
||||
Vector2 SpriteScale = Vector2(1,1);
|
||||
TileSet* set;
|
||||
std::unordered_map<Vector2, TileMapChunk> chunks;
|
||||
|
||||
void RemoveTile(Vector2 p);
|
||||
void SetTile(Vector2 p, Vector2 Spriteindex);
|
||||
TileMapChunk* GetChunk(const Vector2& pos);
|
||||
void GetChunkPositionsInOrder(Vector2* arr);
|
||||
int GetChunkCount();
|
||||
TileSet* GetTileSet();
|
||||
const Rect& GetBounds() const { return bounds; }
|
||||
void SetNextLineOffset(const Vector2& offset);
|
||||
Vector2 GetNextLineOffset();
|
||||
|
||||
inline const char* GetName() override
|
||||
{
|
||||
return "Tile Map";
|
||||
}
|
||||
private:
|
||||
void CheckBounds(Vector2 pos);
|
||||
Vector2 LocalToChunkPos(const Vector2& v);
|
||||
Vector2 ChunkToLocalPos(const Vector2& v, const TileMapChunk& chunk);
|
||||
};
|
||||
|
||||
@@ -33,3 +33,24 @@ void TSE::TileSet::GetSpriteAt(int x, int y, Sprite &s)
|
||||
|
||||
s = Sprite(tex, Rect(startpos, endpos));
|
||||
}
|
||||
|
||||
int TSE::TileSet::GetSpriteIdAt(int x, int y)
|
||||
{
|
||||
if(x < 0 || x >= resx || y < 0 || y >= resy)
|
||||
{
|
||||
TSE_ERROR("The sprite you are trying to access is out of range");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return y * resx + x;
|
||||
}
|
||||
|
||||
uint TSE::TileSet::GetTextueID()
|
||||
{
|
||||
return tex->GetTextureId();
|
||||
}
|
||||
|
||||
TSE::Vector2 TSE::TileSet::GetCount()
|
||||
{
|
||||
return Vector2(resx, resy);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ namespace TSE
|
||||
void SetTexture(Texture* tex);
|
||||
|
||||
void GetSpriteAt(int x, int y, Sprite& s);
|
||||
int GetSpriteIdAt(int x, int y);
|
||||
uint GetTextueID();
|
||||
Vector2 GetCount();
|
||||
|
||||
inline void SetCount(Vector2& v)
|
||||
{
|
||||
@@ -27,5 +30,6 @@ namespace TSE
|
||||
{
|
||||
GetSpriteAt(v.x, v.y, s);
|
||||
};
|
||||
|
||||
};
|
||||
} // namespace TSE
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace TSE
|
||||
|
||||
Matrix4x4 Transformable::GetLocalMatrix() const
|
||||
{
|
||||
return Matrix4x4::ToTranslationMatrix(position) * Matrix4x4::ToRotationMatrix(rotation) * Matrix4x4::ToScaleMatrix(scale);;
|
||||
return Matrix4x4::ToTranslationMatrix(position) * Matrix4x4::ToRotationMatrix(rotation) * Matrix4x4::ToScaleMatrix(scale);
|
||||
}
|
||||
|
||||
Matrix4x4 Transformable::GetGlobalMatrix() const
|
||||
|
||||
Reference in New Issue
Block a user