minor chunk refactor

This commit is contained in:
2020-09-21 14:55:34 -05:00
parent b67a0b6946
commit 4cc1cf4f7e
4 changed files with 32 additions and 16 deletions

View File

@@ -27,7 +27,7 @@ void ChunkManager::addNPC(int posX, int posY, int32_t id) {
chunk->NPCs.insert(id);
NPCManager::addNPC(grabChunks(pos.first, pos.second), id);
NPCManager::addNPC(grabChunks(pos), id);
}
void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
@@ -45,26 +45,41 @@ void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
chunk->players.insert(sock);
}
void ChunkManager::removePlayer(std::pair<int, int> chunkPos, CNSocket* sock) {
if (!checkChunk(chunkPos))
return; // do nothing if chunk doesn't even exist
Chunk* chunk = chunks[chunkPos];
chunk->players.erase(sock); // gone
// TODO: if players and NPCs are empty, free chunk and remove it from surrounding views
}
bool ChunkManager::checkChunk(std::pair<int, int> chunk) {
return chunks.find(chunk) != chunks.end();
}
std::pair<int, int> ChunkManager::grabChunk(int posX, int posY) {
return std::make_pair<int, int>(posX / (settings::PLAYERDISTANCE / 3), posY / (settings::PLAYERDISTANCE / 3));
}
std::vector<Chunk*> ChunkManager::grabChunks(int chunkX, int chunkY) {
std::vector<Chunk*> delta;
delta.reserve(9);
std::vector<Chunk*> ChunkManager::grabChunks(std::pair<int, int> chunk) {
std::vector<Chunk*> chnks;
chnks.reserve(9);
// grabs surrounding chunks if they exist
for (int i = -1; i < 2; i++) {
for (int z = -1; z < 2; z++) {
std::pair<int, int> pos(chunkX+i, chunkY+z);
std::pair<int, int> pos(chunk.first+i, chunk.second+z);
// if chunk exists, add it to the delta
if (chunks.find(pos) != chunks.end()) {
delta.push_back(chunks[pos]);
}
// if chunk exists, add it to the vector
if (checkChunk(pos))
chnks.push_back(chunks[pos]);
}
}
return delta;
return chnks;
}
// returns the chunks that aren't shared (only from from)