-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkUtility.cpp
89 lines (73 loc) · 2.65 KB
/
ChunkUtility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "ChunkUtility.h"
std::vector<std::shared_ptr<Tile> > ChunkUtility::getSurroundingTiles(std::HashMap<glm::ivec2, std::shared_ptr<Chunk> > chunks, int radius, EntityPlayer player)
{
glm::vec2 centerPos = player.getCenterPosition();
std::vector<std::shared_ptr<Tile> > t;
glm::ivec2 centerPosInChunk = Utility::inChunkCoord(centerPos);
glm::ivec2 centerPosInTile = Utility::inTileCoord(centerPos);
for(int x = centerPosInTile.x - radius; x <= centerPosInTile.x + radius; x++)
{
for(int y = centerPosInTile.y - radius; y <= centerPosInTile.y + radius; y++)
{
glm::ivec2 chunkCoord(floor((float)x/TileAmount), floor((float)y/TileAmount));
std::shared_ptr<Chunk> tmpChunk = chunks[chunkCoord];
if(tmpChunk != nullptr)
{
std::shared_ptr<Tile> tmpTile = tmpChunk->getTile(Utility::withinRange(glm::ivec2(x, y)));
if(&tmpTile != nullptr)
{
t.push_back(tmpTile);
}
}
}
}
return t;
}
std::vector<std::shared_ptr<GroundItemStack> > ChunkUtility::getSurroundingItems(std::HashMap<glm::ivec2, std::shared_ptr<Chunk> > chunks, EntityPlayer player)
{
std::vector<std::shared_ptr<GroundItemStack> > t;
glm::ivec2 centerPosInChunk = Utility::inChunkCoord(player.getCenterPosition());
for(int x = centerPosInChunk.x - 2; x <= centerPosInChunk.x + 2; x++)
{
for(int y = centerPosInChunk.y - 2; y <= centerPosInChunk.y + 2; y++)
{
std::shared_ptr<Chunk> tmpChunk = chunks[glm::ivec2(x, y)];
if(tmpChunk != nullptr)
{
std::vector<std::shared_ptr<GroundItemStack> > t2 = tmpChunk->getGroundItemStacks();
t.insert(t.end(), t2.begin(), t2.end());
}
}
}
return t;
}
void ChunkUtility::generateSurroundingChunk(std::HashMap<glm::ivec2, std::shared_ptr<Chunk> >& chunks, int radius, EntityPlayer player, std::vector<std::shared_ptr<TypeTile> > tileTypes, std::vector<std::shared_ptr<TypeItem> > itemTypes)
{
glm::ivec2 centerPosInChunk = Utility::inChunkCoord(player.getCenterPosition());
std::HashMap<glm::ivec2,std::shared_ptr<Chunk> > newchunks;
std::HashMap<glm::ivec2, bool> checker;
for (int x = centerPosInChunk.x - radius; x <= centerPosInChunk.x + radius; x++)
{
for (int y = centerPosInChunk.y - radius; y <= centerPosInChunk.y + radius; y++)
{
if(chunks[glm::ivec2(x, y)] != nullptr)
{
newchunks[glm::ivec2(x, y)] = chunks[glm::ivec2(x, y)];
checker[glm::ivec2(x, y)] = true;
}
else
{
newchunks[glm::ivec2(x, y)] = std::make_shared<Chunk>(glm::ivec2(x, y), tileTypes, itemTypes);
checker[glm::ivec2(x, y)] = true;
}
}
}
for(auto it = chunks.begin(); it != chunks.end();)
{
if(!checker[it->first])
it = chunks.erase(it);
else
it++;
}
chunks = newchunks;
}