Skip to content

Commit

Permalink
remove building action from the map
Browse files Browse the repository at this point in the history
  • Loading branch information
stwe committed Jan 3, 2023
1 parent e386da0 commit 9c8e254
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 45 deletions.
55 changes: 43 additions & 12 deletions src/renderer/TerrainRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ void mdcii::renderer::TerrainRenderer::Render(const layer::TerrainLayer& t_terra
}

//-------------------------------------------------
// Add / remove building
// Remove / add building - Gpu
//-------------------------------------------------

void mdcii::renderer::TerrainRenderer::DeleteBuilding(world::Island& t_island, const layer::Tile& t_tile)
void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Island& t_island, const layer::Tile& t_tile)
{
MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuilding()] No building to delete.")
MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuildingFromGpu()] No building to delete.")

Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuilding()] Delete building with Id {} from the world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0);
Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromGpu()] Delete building with Id {} from the world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0);

magic_enum::enum_for_each<world::Zoom>([this, &t_island, &t_tile](const world::Zoom t_zoom) {
magic_enum::enum_for_each<world::Rotation>([this, &t_zoom, &t_island, &t_tile](const world::Rotation t_rotation) {
Expand Down Expand Up @@ -211,19 +211,19 @@ void mdcii::renderer::TerrainRenderer::DeleteBuilding(world::Island& t_island, c
});
}

void mdcii::renderer::TerrainRenderer::DeleteBuilding(world::Terrain& t_terrain)
void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Terrain& t_terrain)
{
MDCII_ASSERT(!t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::DeleteBuilding()] No Tile objects available.")
MDCII_ASSERT(!t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::DeleteBuildingFromGpu()] No Tile objects available.")
for (const auto& tile : t_terrain.tilesToAdd.tiles)
{
DeleteBuilding(*t_terrain.tilesToAdd.island, *tile);
DeleteBuildingFromGpu(*t_terrain.tilesToAdd.island, *tile);
}

// clear vector
std::vector<std::unique_ptr<layer::Tile>>().swap(t_terrain.tilesToAdd.tiles);
}

void mdcii::renderer::TerrainRenderer::AddBuilding(
void mdcii::renderer::TerrainRenderer::AddBuildingToGpu(
const layer::Tile& t_selectedBuildingTile,
const glm::ivec2& t_startWorldPosition,
world::Terrain& t_terrain
Expand All @@ -235,7 +235,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding(
return;
}

MDCII_ASSERT(t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::AddBuilding()] Invalid number of tiles.")
MDCII_ASSERT(t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::AddBuildingToGpu()] Invalid number of tiles.")

const auto& terrainLayer{ t_terrain.tilesToAdd.island->terrainLayer };
const auto& mixedLayer{ t_terrain.tilesToAdd.island->mixedLayer };
Expand All @@ -253,7 +253,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding(

// calc final world position
const auto buildingWorldPosition{ glm::ivec2(t_startWorldPosition.x + rp.x, t_startWorldPosition.y + rp.y) };
MDCII_ASSERT(t_terrain.currentIslandUnderMouse->IsWorldPositionInAabb(buildingWorldPosition), "[TerrainRenderer::AddBuilding()] Invalid world position.")
MDCII_ASSERT(t_terrain.currentIslandUnderMouse->IsWorldPositionInAabb(buildingWorldPosition), "[TerrainRenderer::AddBuildingToGpu()] Invalid world position.")

// get position on island from world position
const auto buildingIslandPosition{ t_terrain.currentIslandUnderMouse->GetIslandPositionFromWorldPosition(buildingWorldPosition) };
Expand Down Expand Up @@ -315,7 +315,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding(
}
}

MDCII_ASSERT(t_terrain.tilesToAdd.tiles.size() == building.size.w * building.size.h, "[TerrainRenderer::AddBuilding()] Invalid number of created tiles.")
MDCII_ASSERT(t_terrain.tilesToAdd.tiles.size() == building.size.w * building.size.h, "[TerrainRenderer::AddBuildingToGpu()] Invalid number of created tiles.")

std::vector<int32_t> connected;
for (const auto& tile : t_terrain.tilesToAdd.tiles)
Expand All @@ -328,7 +328,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding(
tile->connectedTiles = connected;
}

Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuilding()] Added building with Id {} to the world on ({}, {}).", building.id, t_startWorldPosition.x, t_startWorldPosition.y);
Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToGpu()] Added building with Id {} to the world on ({}, {}).", building.id, t_startWorldPosition.x, t_startWorldPosition.y);
}

void mdcii::renderer::TerrainRenderer::UpdateGpuData(
Expand Down Expand Up @@ -374,6 +374,37 @@ void mdcii::renderer::TerrainRenderer::UpdateGpuData(
ogl::buffer::Vao::Unbind();
}

//-------------------------------------------------
// Remove / add building - Cpu
//-------------------------------------------------

void mdcii::renderer::TerrainRenderer::DeleteBuildingFromCpu(layer::Tile& t_tile) const
{
MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuildingFromCpu()] No building to delete.")

Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromCpu()] Delete building Id {} Cpu data of Tile in world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0);

t_tile.ResetBuildingInfo();
}

void mdcii::renderer::TerrainRenderer::AddBuildingToCpu(world::Terrain& t_terrain)
{
MDCII_ASSERT(!t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::AddBuildingToCpu()] No Tile objects available.")

// reset Tile pointers and replace with new tile
auto& buildingsLayer{ t_terrain.tilesToAdd.island->buildingsLayer };
for (auto& tile : t_terrain.tilesToAdd.tiles)
{
Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToCpu()] Add building Id {} Cpu data of Tile to world ({}, {}).", tile->buildingId, tile->worldXDeg0, tile->worldYDeg0);

buildingsLayer->ResetTilePointersAt(tile->instanceIds);
buildingsLayer->StoreTile(std::move(tile));
}

// clear vector
std::vector<std::unique_ptr<layer::Tile>>().swap(t_terrain.tilesToAdd.tiles);
}

//-------------------------------------------------
// Init
//-------------------------------------------------
Expand Down
21 changes: 17 additions & 4 deletions src/renderer/TerrainRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace mdcii::renderer
void Render(const layer::TerrainLayer& t_terrainLayer, world::Zoom t_zoom, world::Rotation t_rotation) const;

//-------------------------------------------------
// Add / remove building
// Remove / add building - Gpu
//-------------------------------------------------

/**
Expand All @@ -134,14 +134,14 @@ namespace mdcii::renderer
* @param t_island The Island object.
* @param t_tile The Tile object to delete.
*/
void DeleteBuilding(world::Island& t_island, const layer::Tile& t_tile);
void DeleteBuildingFromGpu(world::Island& t_island, const layer::Tile& t_tile);

/**
* Deletes a building from the Gpu.
*
* @param t_terrain The Terrain object.
*/
void DeleteBuilding(world::Terrain& t_terrain);
void DeleteBuildingFromGpu(world::Terrain& t_terrain);

/**
* Adds a building to the Gpu.
Expand All @@ -150,7 +150,7 @@ namespace mdcii::renderer
* @param t_startWorldPosition The starting position in the world.
* @param t_terrain The Terrain object.
*/
void AddBuilding(
void AddBuildingToGpu(
const layer::Tile& t_selectedBuildingTile,
const glm::ivec2& t_startWorldPosition,
world::Terrain& t_terrain
Expand All @@ -177,6 +177,19 @@ namespace mdcii::renderer
int32_t t_buildingId
);

//-------------------------------------------------
// Remove / add building - Cpu
//-------------------------------------------------

/**
* Deletes a building from the Cpu.
*
* @param t_tile Tile object where building information should be deleted/overwritten.
*/
void DeleteBuildingFromCpu(layer::Tile& t_tile) const;

void AddBuildingToCpu(world::Terrain& t_terrain);

protected:

private:
Expand Down
52 changes: 28 additions & 24 deletions src/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ void mdcii::world::World::RenderImGui()
{
if (ImGui::CollapsingHeader("Buildings"))
{
m_demolishTileIndex = -1;

m_worldGui->ShowBuildingsGui();
}
}
Expand All @@ -254,12 +252,32 @@ void mdcii::world::World::RenderImGui()
m_worldGui->selectedBuildingTile.Reset();
}

if (m_demolishTileIndex >= 0)
if (terrain->currentSelectedIsland)
{
// todo: remove building
if (terrain->currentSelectedIsland->currentSelectedTile && terrain->currentSelectedIsland->currentSelectedTile->HasBuilding())
{
// todo: store parent layer ptr in tile
const auto& buildingsLayer{ terrain->currentSelectedIsland->buildingsLayer };
const auto idx{ terrain->currentSelectedIsland->currentSelectedTile->indices.at(0) };
if (buildingsLayer->tiles.at(idx)->HasBuilding())
{
if (terrain->currentSelectedIsland->currentSelectedTile->connectedTiles.empty())
{
terrainRenderer->DeleteBuildingFromGpu(*terrain->currentSelectedIsland, *terrain->currentSelectedIsland->currentSelectedTile);
terrainRenderer->DeleteBuildingFromCpu(*terrain->currentSelectedIsland->currentSelectedTile);
}
else
{
for (const auto tileIndex : terrain->currentSelectedIsland->currentSelectedTile->connectedTiles)
{
auto& tile{ buildingsLayer->tiles.at(tileIndex) };
terrainRenderer->DeleteBuildingFromGpu(*terrain->currentSelectedIsland, *tile);
terrainRenderer->DeleteBuildingFromCpu(*tile);
}
}
}
}
}

m_demolishTileIndex = -1;
}

if (currentAction == Action::STATUS)
Expand All @@ -280,8 +298,6 @@ void mdcii::world::World::RenderImGui()

if (currentAction == Action::OPTIONS)
{
m_demolishTileIndex = -1;

if (m_worldGui->selectedBuildingTile.HasBuilding())
{
m_worldGui->selectedBuildingTile.Reset();
Expand Down Expand Up @@ -347,19 +363,7 @@ void mdcii::world::World::OnLeftMouseButtonPressed()
!terrain->tilesToAdd.tiles.empty()
)
{
// reset Tile pointers and replace with new Tile
// todo: method AddToCpu
/*
auto& buildingsLayer{ GetLayer(WorldLayerType::BUILDINGS) };
for (auto& tile : m_tilesToAdd)
{
buildingsLayer.ResetTilePointersAt(tile->instanceIds);
buildingsLayer.StoreTile(std::move(tile));
}
*/

// clear vector
std::vector<std::unique_ptr<layer::Tile>>().swap(terrain->tilesToAdd.tiles);
terrainRenderer->AddBuildingToCpu(*terrain);
}

MDCII_ASSERT(terrain->tilesToAdd.tiles.empty(), "[World::OnLeftMouseButtonPressed()] Invalid number of tiles to add.")
Expand All @@ -383,17 +387,17 @@ void mdcii::world::World::OnMouseMoved()
{
if (!terrain->tilesToAdd.tiles.empty())
{
terrainRenderer->DeleteBuilding(*terrain);
terrainRenderer->DeleteBuildingFromGpu(*terrain);
}
if (terrain->tilesToAdd.tiles.empty())
{
terrainRenderer->AddBuilding(m_worldGui->selectedBuildingTile, mousePicker->currentPosition, *terrain);
terrainRenderer->AddBuildingToGpu(m_worldGui->selectedBuildingTile, mousePicker->currentPosition, *terrain);
}
}

if (currentAction == Action::BUILD && !IsPositionInWorld(mousePicker->currentPosition) && !terrain->tilesToAdd.tiles.empty())
{
terrainRenderer->DeleteBuilding(*terrain);
terrainRenderer->DeleteBuildingFromGpu(*terrain);
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/world/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,6 @@ namespace mdcii::world
*/
bool m_runAnimations{ true };

/**
* To demolish a building.
*/
int m_demolishTileIndex{ -1 };

/**
* The mouse button pressed listener handle.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/world/WorldGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <imgui.h>
#include "WorldGui.h"
#include "World.h"
#include "Terrain.h"
#include "Island.h"
#include "MdciiAssert.h"
#include "Game.h"
#include "state/State.h"
Expand Down Expand Up @@ -166,6 +168,11 @@ void mdcii::world::WorldGui::ShowActionsGui() const
if (ImGui::Button(data::Text::GetMenuText(Game::INI.Get<std::string>("locale", "lang"), World::ACTION_NAMES[i].data()).c_str()))
{
std::fill(m_world->actionButtons.begin(), m_world->actionButtons.end(), false);
if (m_world->terrain->currentSelectedIsland && m_world->terrain->currentSelectedIsland->currentSelectedTile)
{
m_world->terrain->currentSelectedIsland->currentSelectedTile = nullptr;
m_world->terrain->currentSelectedIsland = nullptr;
}
m_world->actionButtons[i] = true;
m_world->currentAction = action;

Expand Down

0 comments on commit 9c8e254

Please sign in to comment.