SQCSimulator2023/SQCSim2021/world.cpp

100 lines
3.3 KiB
C++
Raw Normal View History

2021-11-15 20:58:13 -05:00
#include "world.h"
World::World(){}
World::~World(){}
Array2d<Chunk*>& World::GetChunks() { return m_chunks; }
Chunk* World::ChunkAt(float x, float y, float z) const {
int cx = (int)x / CHUNK_SIZE_X;
int cz = (int)z / CHUNK_SIZE_Z;
if (x < 0 || y < 0 || z < 0 ||
2021-12-01 21:21:45 -05:00
x >= WORLD_SIZE_X * CHUNK_SIZE_X ||
z >= CHUNK_SIZE_Z * WORLD_SIZE_Y ||
y > CHUNK_SIZE_Y)
2021-11-19 09:06:09 -05:00
return 0;
return m_chunks.Get(cx, cz);
}
Chunk* World::ChunkAt(const Vector3f& pos) const { return ChunkAt(pos.x, pos.y, pos.z); }
BlockType World::BlockAt(float x, float y, float z, BlockType defaultBlockType) const {
Chunk* c = ChunkAt(x, y, z);
if (!c)
return defaultBlockType;
int bx = (int)x % CHUNK_SIZE_X;
int by = (int)y % CHUNK_SIZE_Y;
int bz = (int)z % CHUNK_SIZE_Z;
return c->GetBlock(bx, by, bz);
}
BlockType World::BlockAt(const Vector3f& pos, BlockType defaultBlockType) const {
return BlockAt(pos.x, pos.y, pos.z, defaultBlockType);
}
2021-11-30 19:55:11 -05:00
2021-12-01 21:21:45 -05:00
void World::TransposeWorld(Player& player) {
int x = 0;
int y = 0;
if (player.GetPosition().x > (WORLD_SIZE_X * CHUNK_SIZE_X) * .66f) ++x;
else if (player.GetPosition().x < (WORLD_SIZE_X * CHUNK_SIZE_X) * .33f) --x;
if (player.GetPosition().z > (WORLD_SIZE_Y * CHUNK_SIZE_Z) * .66f) ++y;
else if (player.GetPosition().z < (WORLD_SIZE_Y * CHUNK_SIZE_Z) * .33f) --y;
if (!x && !y) return;
if (x != 0)
for (int ay = 0; ay < WORLD_SIZE_Y; ++ay)
for (int ax = 0; ax < abs(x); ++ay)
if (ChunkAt(x < 0 ? (WORLD_SIZE_X - 1 - ax) * WORLD_SIZE_X : ax, 1, ay * WORLD_SIZE_Y))
m_tbDeleted.push_back(std::move(m_chunks.Get(x < 0? WORLD_SIZE_X - 1 - ax: ax, ay)));
if (y != 0)
for (int ax = 0; ax < WORLD_SIZE_X; ++ax)
for (int ay = 0; ay < abs(y); ++ay)
if (ChunkAt(ax * WORLD_SIZE_X , 1, y < 0 ? (WORLD_SIZE_Y - 1 - ay) * WORLD_SIZE_Y : ay))
m_tbDeleted.push_back(std::move(m_chunks.Get(ax, y < 0 ? WORLD_SIZE_Y - 1 - ay: ay)));
for (int ax = 0; ax < WORLD_SIZE_X; ++ax)
for (int ay = 0; ay < WORLD_SIZE_Y; ++ay)
if (ax + x < WORLD_SIZE_X && ax + x > 0 &&
ay + y < WORLD_SIZE_Y && ay + y > 0)
m_chunks.Set(x > 0? ax: ax + x, y > 0? ay: ay + y,
m_chunks.Get(x < 0 ? ax : ax + x, y < 0 ? ay : ay + y));
if (x != 0)
for (int ay = 0; ay < WORLD_SIZE_Y; ++ay)
for (int ax = 0; ax < abs(x); ++ay)
m_chunks.Set(x > 0 ? WORLD_SIZE_X - 1 - ax : ax, ay, nullptr);
if (y != 0)
for (int ax = 0; ax < WORLD_SIZE_X; ++ax)
for (int ay = 0; ay < abs(y); ++ay)
m_chunks.Set(ax, y > 0 ? WORLD_SIZE_Y - 1 - ay : ay, nullptr);
m_center[0] += x; m_center[1] += y;
player.Transpose(x, y);
}
2021-12-01 22:06:47 -05:00
void World::CleanUpWorld(int& frames) {
if (!m_tbDeleted.empty() && !frames) {
m_tbDeleted.pop_back();
frames = FRAMES_DELETE_CHUNKS;
}
}
Chunk* World::RetrieveChunk(int x, int y) {
for (int index = 0; index < m_tbDeleted.size(); ++index) {
int cx, cy;
m_tbDeleted.at(index)->GetPosition(cx, cy);
if (cx == x && cy == y)
return std::move(m_tbDeleted.at(index));
}
return nullptr;
}