SQCSimulator2023/SQCSim-common/world.cpp
2023-09-18 15:56:17 -04:00

110 lines
2.7 KiB
C++

#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 ||
x >= WORLD_SIZE_X * CHUNK_SIZE_X ||
z >= CHUNK_SIZE_Z * WORLD_SIZE_Y ||
y > CHUNK_SIZE_Y)
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);
}
void World::GetScope(unsigned int& x, unsigned int& y) {
x = m_center[0];
y = m_center[1];
}
void World::ChangeBlockAtPosition(BlockType blockType, Vector3f pos) {
int bx = (int)pos.x % CHUNK_SIZE_X;
int by = (int)pos.y % CHUNK_SIZE_Y;
int bz = (int)pos.z % CHUNK_SIZE_Z;
ChunkAt(pos)->SetBlock(bx, by, bz, blockType, this);
}
void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block) {
Vector3f currentPos = player.GetPosition();
Vector3f currentBlock = currentPos;
Vector3f ray = player.GetDirection();
bool found = false;
if (block) return;
while ((currentPos - currentBlock).Length() <= MAX_SELECTION_DISTANCE && !found) {
currentBlock += ray / 10.f;
BlockType bt = BlockAt(currentBlock);
if (bt != BTYPE_AIR)
found = true;
}
if (found)
if (blockType != BTYPE_AIR) {
found = false;
while ((currentPos - currentBlock).Length() >= 1.7f && !found) {
currentBlock -= ray / 10.f;
BlockType bt = BlockAt(currentBlock);
if (bt == BTYPE_AIR) { // V?rification pour ?tre s?r que le bloc ? changer n'est pas dans le joueur.
int Bx = (int)currentBlock.x;
int By = (int)currentBlock.y;
int Bz = (int)currentBlock.z;
int Px = (int)currentPos.x;
int PyA = (int)currentPos.y;
int PyB = (int)(currentPos.y - .9f);
int PyC = (int)(currentPos.y - 1.7f);
int Pz = (int)currentPos.z;
if (!(Bx == Px &&
(By == PyA ||
By == PyB ||
By == PyC) &&
Bz == Pz))
found = true;
}
}
}
if (found && (int)currentBlock.y < CHUNK_SIZE_Y) {
int bx = (int)currentBlock.x % CHUNK_SIZE_X;
int by = (int)currentBlock.y % CHUNK_SIZE_Y;
int bz = (int)currentBlock.z % CHUNK_SIZE_Z;
ChunkAt(currentBlock)->SetBlock(bx, by, bz, blockType, this);
ChunkAt(currentBlock)->MakeModified();
block = true;
}
}