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)
|
||||
|
||||
Reference in New Issue
Block a user