Compare commits
2 Commits
145ad027f8
...
0a254808c4
Author | SHA1 | Date | |
---|---|---|---|
0a254808c4 | |||
b8ac6e8d9c |
@@ -43,6 +43,25 @@ namespace CaveGame::Core
|
||||
|
||||
explicit World(const std::string &worldName, int seed = 0, bool overwrite = false);
|
||||
|
||||
Vector2 GetChunkCoordinatesAtCell(int x, int y) const {
|
||||
float chunkX = Math::Floor((float)x / Chunk::ChunkSize);
|
||||
float chunkY = Math::Floor((float)y / Chunk::ChunkSize);
|
||||
|
||||
return Vector2(chunkX, chunkY);
|
||||
}
|
||||
|
||||
Vector2 GetTileCoordinatesAtCell(int x, int y) const {
|
||||
float tileX = Math::Mod(x, Chunk::ChunkSize);
|
||||
float tileY = Math::Mod(y, Chunk::ChunkSize);
|
||||
|
||||
if (tileX < 0)
|
||||
tileX = Chunk::ChunkSize + tileX;
|
||||
if (tileY < 0)
|
||||
tileY = Chunk::ChunkSize + tileY;
|
||||
|
||||
return Vector2(tileX, tileY);
|
||||
}
|
||||
|
||||
TileID GetTile(int x, int y) const override;
|
||||
|
||||
void SetTile(int x, int y, TileID t, bool flag_update = true) override;
|
||||
@@ -79,70 +98,9 @@ namespace CaveGame::Core
|
||||
|
||||
void DoForcedTileTicks();
|
||||
|
||||
void DoRandomTileTick(const Vector2 coords, Chunk* chunk) {
|
||||
Tile* tile = nullptr;
|
||||
|
||||
int max_tries = RandomTileTickCoefficient*5;
|
||||
int max_successes = RandomTileTickCoefficient;
|
||||
|
||||
for (int ticc = 0; ticc < RandomTileTickCoefficient; ticc++)
|
||||
{
|
||||
// Select random x,y coordinates between [0, ChunkSize]
|
||||
int x = rng.Int(0, Chunk::ChunkSize-1);
|
||||
int y = rng.Int(0, Chunk::ChunkSize-1);
|
||||
|
||||
int wx = coords.x*Chunk::ChunkSize + x;
|
||||
int wy = coords.y*Chunk::ChunkSize + y;
|
||||
|
||||
TileID at = chunk->GetTile(x, y);
|
||||
|
||||
if (at == TileID::AIR)
|
||||
continue;
|
||||
|
||||
tile = GetByNumeric(at);
|
||||
|
||||
/*
|
||||
if (tile != nullptr)
|
||||
if (tile->DoesRandomTicc())
|
||||
tile->RandomTicc(this, 0, wx, wy);
|
||||
*/
|
||||
if ((tile != nullptr) && (tile->DoesRandomTicc()))
|
||||
tile->RandomTicc(this, 0, wx, wy);
|
||||
}
|
||||
}
|
||||
|
||||
void DoForcedTileTick(const Vector2 coords, Chunk* chunk) {
|
||||
Tile* tile = nullptr;
|
||||
|
||||
chunk->SwapTileUpdateBuffers();
|
||||
|
||||
for (int x = 0; x < Core::Chunk::ChunkSize; x++) {
|
||||
for (int y = 0; y < Core::Chunk::ChunkSize; y++) {
|
||||
int wx = coords.x*Chunk::ChunkSize + x;
|
||||
int wy = coords.y*Chunk::ChunkSize + y;
|
||||
|
||||
if (!chunk->GetTileUpdateBufferFlag(x, y))
|
||||
continue;
|
||||
|
||||
chunk->SetTileUpdateBufferFlag(x, y, false);
|
||||
|
||||
TileID at = chunk->GetTile(x, y);
|
||||
|
||||
if (at == TileID::AIR)
|
||||
continue;
|
||||
|
||||
tile = GetByNumeric(at);
|
||||
|
||||
if (tile != nullptr) {
|
||||
if (tile->DoesForcedTicc()) {
|
||||
tile->ForcedTicc(this, 0, wx, wy);
|
||||
//chunk->SetTileUpdateFlag(x, y, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void DoRandomTileTick(const Vector2 coords, Chunk* chunk);
|
||||
|
||||
void DoForcedTileTick(const Vector2 coords, Chunk* chunk);
|
||||
|
||||
virtual void Update(float elapsed);
|
||||
|
||||
|
@@ -11,64 +11,6 @@
|
||||
namespace CaveGame::Core
|
||||
{
|
||||
void CaveGame::Core::World::Update(float elapsed) {
|
||||
/*
|
||||
auto DoRandom = [this](const Vector2 coords, Chunk* chunk) {
|
||||
Tile* tile = nullptr;
|
||||
|
||||
for (int ticc = 0; ticc < RandomTileTickCoefficient; ticc++)
|
||||
{
|
||||
// Select random x,y coordinates between [0, ChunkSize]
|
||||
int x = rng.Int(0, Chunk::ChunkSize-1);
|
||||
int y = rng.Int(0, Chunk::ChunkSize-1);
|
||||
|
||||
int wx = coords.x*Chunk::ChunkSize + x;
|
||||
int wy = coords.y*Chunk::ChunkSize + y;
|
||||
|
||||
TileID at = chunk->GetTile(x, y);
|
||||
|
||||
if (at == TileID::AIR)
|
||||
continue;
|
||||
|
||||
tile = GetByNumeric(at);
|
||||
if ((tile != nullptr) && (tile->DoesRandomTicc()))
|
||||
tile->RandomTicc(this, 0, wx, wy);
|
||||
}
|
||||
};
|
||||
|
||||
auto DoForced = [this](const Vector2 coords, Chunk* chunk) {
|
||||
Tile* tile = nullptr;
|
||||
|
||||
chunk->SwapTileUpdateBuffers();
|
||||
|
||||
for (int x = 0; x < Core::Chunk::ChunkSize; x++) {
|
||||
for (int y = 0; y < Core::Chunk::ChunkSize; y++) {
|
||||
int wx = coords.x*Chunk::ChunkSize + x;
|
||||
int wy = coords.y*Chunk::ChunkSize + y;
|
||||
|
||||
if (!chunk->GetTileUpdateBufferFlag(x, y))
|
||||
continue;
|
||||
|
||||
chunk->SetTileUpdateBufferFlag(x, y, false);
|
||||
|
||||
TileID at = chunk->GetTile(x, y);
|
||||
|
||||
if (at == TileID::AIR)
|
||||
continue;
|
||||
|
||||
tile = GetByNumeric(at);
|
||||
|
||||
if (tile != nullptr) {
|
||||
if (tile->DoesForcedTicc()) {
|
||||
tile->ForcedTicc(this, 0, wx, wy);
|
||||
//chunk->SetTileUpdateFlag(x, y, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
*/
|
||||
|
||||
time_of_day += elapsed;
|
||||
|
||||
time_of_day = std::fmod(time_of_day, 24*60);
|
||||
@@ -105,6 +47,7 @@ namespace CaveGame::Core
|
||||
}
|
||||
|
||||
TileID World::GetTile(int x, int y) const {
|
||||
/*
|
||||
float chunkX = Math::Floor((float)x / Chunk::ChunkSize);
|
||||
float chunkY = Math::Floor((float)y / Chunk::ChunkSize);
|
||||
float tileX = Math::Mod(x, Chunk::ChunkSize);
|
||||
@@ -120,10 +63,19 @@ namespace CaveGame::Core
|
||||
if (HasChunkAtCell(coords))
|
||||
return loaded_chunks.at(coords)->GetTile(tileX, tileY);
|
||||
|
||||
return TileID::VOID;*/
|
||||
|
||||
Vector2 chunkCoords = GetChunkCoordinatesAtCell(x, y);
|
||||
Vector2 tileCoords = GetTileCoordinatesAtCell(x, y);
|
||||
|
||||
if (HasChunkAtCell(chunkCoords))
|
||||
return loaded_chunks.at(chunkCoords)->GetTile(tileCoords.x, tileCoords.y);
|
||||
|
||||
return TileID::VOID;
|
||||
}
|
||||
|
||||
void World::SetTile(int x, int y, TileID t, bool flag_update) {
|
||||
/*
|
||||
float chunkX = Math::Floor((float)x / Chunk::ChunkSize);
|
||||
float chunkY = Math::Floor((float)y / Chunk::ChunkSize);
|
||||
float tileX = Math::Mod(x, Chunk::ChunkSize);
|
||||
@@ -148,6 +100,21 @@ namespace CaveGame::Core
|
||||
SetTileUpdateFlag(x-1, y, true);
|
||||
SetTileUpdateFlag(x+1, y, true);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
Vector2 chunkCoords = GetChunkCoordinatesAtCell(x, y);
|
||||
Vector2 tileCoords = GetTileCoordinatesAtCell(x, y);
|
||||
|
||||
if (HasChunkAtCell(chunkCoords)) {
|
||||
loaded_chunks.at(chunkCoords)->SetTile(tileCoords.x, tileCoords.y, t, flag_update);
|
||||
if (flag_update) {
|
||||
SetTileUpdateFlag(x, y, true);
|
||||
SetTileUpdateFlag(x, y-1, true);
|
||||
SetTileUpdateFlag(x, y+1, true);
|
||||
SetTileUpdateFlag(x-1, y, true);
|
||||
SetTileUpdateFlag(x+1, y, true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -188,6 +155,7 @@ namespace CaveGame::Core
|
||||
std::unordered_map<Vector2, Chunk*> World::GetChunkList() { return loaded_chunks; }
|
||||
|
||||
void World::SetTileUpdateFlag(int x, int y, bool flag) {
|
||||
/*
|
||||
float chunkX = Math::Floor((float)x / Chunk::ChunkSize);
|
||||
float chunkY = Math::Floor((float)y / Chunk::ChunkSize);
|
||||
|
||||
@@ -203,10 +171,19 @@ namespace CaveGame::Core
|
||||
const Vector2 coords = Vector2(chunkX, chunkY);
|
||||
|
||||
if (HasChunkAtCell(coords))
|
||||
loaded_chunks.at(coords)->SetTileUpdateFlag(tileX, tileY, flag);
|
||||
loaded_chunks.at(coords)->SetTileUpdateFlag(tileX, tileY, flag);*/
|
||||
|
||||
|
||||
const Vector2 chunkCoords = GetChunkCoordinatesAtCell(x, y);
|
||||
Vector2 tileCoords = GetTileCoordinatesAtCell(x, y);
|
||||
|
||||
if (HasChunkAtCell(chunkCoords))
|
||||
loaded_chunks.at(chunkCoords)->SetTileUpdateFlag(tileCoords.x, tileCoords.y, flag);
|
||||
|
||||
}
|
||||
|
||||
bool World::GetTileUpdateFlag(int x, int y) const {
|
||||
/*
|
||||
float chunkX = Math::Floor((float)x / Chunk::ChunkSize);
|
||||
float chunkY = Math::Floor((float)y / Chunk::ChunkSize);
|
||||
|
||||
@@ -224,7 +201,14 @@ namespace CaveGame::Core
|
||||
if (HasChunkAtCell(coords))
|
||||
return loaded_chunks.at(coords)->GetTileUpdateFlag(tileX, tileY);
|
||||
|
||||
return false;
|
||||
return false;*/
|
||||
|
||||
Vector2 chunkCoords = GetChunkCoordinatesAtCell(x, y);
|
||||
Vector2 tileCoords = GetTileCoordinatesAtCell(x, y);
|
||||
|
||||
if (HasChunkAtCell(chunkCoords))
|
||||
return loaded_chunks.at(chunkCoords)->GetTileUpdateFlag(tileCoords.x, tileCoords.y);
|
||||
|
||||
}
|
||||
|
||||
Vector2 World::ToUnitDirection(float rotation) {
|
||||
@@ -500,4 +484,68 @@ namespace CaveGame::Core
|
||||
}
|
||||
Logs::Info("Saving successful!");
|
||||
}
|
||||
|
||||
void World::DoRandomTileTick(const Vector2 coords, Chunk *chunk) {
|
||||
Tile* tile = nullptr;
|
||||
|
||||
int max_tries = RandomTileTickCoefficient*5;
|
||||
int max_successes = RandomTileTickCoefficient;
|
||||
|
||||
for (int ticc = 0; ticc < RandomTileTickCoefficient; ticc++)
|
||||
{
|
||||
// Select random x,y coordinates between [0, ChunkSize]
|
||||
int x = rng.Int(0, Chunk::ChunkSize-1);
|
||||
int y = rng.Int(0, Chunk::ChunkSize-1);
|
||||
|
||||
int wx = coords.x*Chunk::ChunkSize + x;
|
||||
int wy = coords.y*Chunk::ChunkSize + y;
|
||||
|
||||
TileID at = chunk->GetTile(x, y);
|
||||
|
||||
if (at == TileID::AIR)
|
||||
continue;
|
||||
|
||||
tile = GetByNumeric(at);
|
||||
|
||||
/*
|
||||
if (tile != nullptr)
|
||||
if (tile->DoesRandomTicc())
|
||||
tile->RandomTicc(this, 0, wx, wy);
|
||||
*/
|
||||
if ((tile != nullptr) && (tile->DoesRandomTicc()))
|
||||
tile->RandomTicc(this, 0, wx, wy);
|
||||
}
|
||||
}
|
||||
|
||||
void World::DoForcedTileTick(const Vector2 coords, Chunk *chunk) {
|
||||
Tile* tile = nullptr;
|
||||
|
||||
chunk->SwapTileUpdateBuffers();
|
||||
|
||||
for (int x = 0; x < Core::Chunk::ChunkSize; x++) {
|
||||
for (int y = 0; y < Core::Chunk::ChunkSize; y++) {
|
||||
int wx = coords.x*Chunk::ChunkSize + x;
|
||||
int wy = coords.y*Chunk::ChunkSize + y;
|
||||
|
||||
if (!chunk->GetTileUpdateBufferFlag(x, y))
|
||||
continue;
|
||||
|
||||
chunk->SetTileUpdateBufferFlag(x, y, false);
|
||||
|
||||
TileID at = chunk->GetTile(x, y);
|
||||
|
||||
if (at == TileID::AIR)
|
||||
continue;
|
||||
|
||||
tile = GetByNumeric(at);
|
||||
|
||||
if (tile != nullptr) {
|
||||
if (tile->DoesForcedTicc()) {
|
||||
tile->ForcedTicc(this, 0, wx, wy);
|
||||
//chunk->SetTileUpdateFlag(x, y, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user