Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework setTileData #881

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/engine/GameObjects/MapNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../map/MapLayers.hxx"
#include "GameStates.hxx"
#include "Settings.hxx"
#include "Engine.hxx"

MapNode::MapNode(Point isoCoordinates, const std::string &terrainID, const std::string &tileID)
: m_isoCoordinates(std::move(isoCoordinates)), m_sprite{std::make_unique<Sprite>(m_isoCoordinates)},
Expand Down Expand Up @@ -52,6 +53,26 @@ void MapNode::setTileID(const std::string &tileID, const Point &origCornerPoint)
TileData *tileData = TileManager::instance().getTileData(tileID);
if (tileData && !tileID.empty())
{
std::vector<Point> targetCoordinates = TileManager::instance().getTargetCoordsOfTileID(origCornerPoint, tileID);
if (targetCoordinates.size() > 1 && m_isoCoordinates == origCornerPoint)
{ // multibuilding placed on this node
for (auto coord : targetCoordinates)
{
if (coord == origCornerPoint)
{
LOG(LOG_INFO) << "i'm the origin coordinate";

}
else
{
Engine::instance().map->getMapNode(coord).setTileID(tileID, origCornerPoint);
Engine::instance().map->getMapNode(coord).setRenderFlag(Layer::BUILDINGS,false);

LOG(LOG_INFO) << "i'm a multinode";
}
}

}
const Layer layer = TileManager::instance().getTileLayer(tileID);
switch (layer)
{
Expand Down Expand Up @@ -442,6 +463,7 @@ void MapNode::demolishLayer(const Layer &layer)
TileOrientation::TILE_DEFAULT_ORIENTATION; // We need to reset TileOrientation, in case it's set (demolishing autotiles)
m_mapNodeData[layer].origCornerPoint = this->getCoordinates();
m_mapNodeData[Layer::ZONE].shouldRender = true;
//LOG(LOG_INFO) << "reset render to true";
m_sprite->clearSprite(layer);
}

Expand Down
5 changes: 4 additions & 1 deletion src/engine/GameObjects/MapNode.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ public:

bool isLayerOccupied(const Layer &layer) const { return m_mapNodeData[layer].tileData != nullptr; }

void setRenderFlag(Layer layer, bool shouldRender) { m_mapNodeData[layer].shouldRender = shouldRender; }
void setRenderFlag(Layer layer, bool shouldRender) {
//LOG(LOG_INFO) << "reset render to true";
m_mapNodeData[layer].shouldRender = shouldRender;
}

/** @brief Set elevation bit mask.
*/
Expand Down
46 changes: 37 additions & 9 deletions src/engine/Map.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void Map::getNodeInformation(const Point &isoCoordinates) const
LOG(LOG_INFO) << "[Layer: TERRAIN] ID: " << mapNode.getMapNodeDataForLayer(Layer::TERRAIN).tileID;
LOG(LOG_INFO) << "[Layer: WATER] ID: " << mapNode.getMapNodeDataForLayer(Layer::WATER).tileID;
LOG(LOG_INFO) << "[Layer: BUILDINGS] ID: " << mapNode.getMapNodeDataForLayer(Layer::BUILDINGS).tileID;
LOG(LOG_INFO) << "Z-Index: " << mapNode.getCoordinates().z;
LOG(LOG_INFO) << "Category: " << tileData->category;
LOG(LOG_INFO) << "FileName: " << tileData->tiles.fileName;
LOG(LOG_INFO) << "PickRandomTile: " << tileData->tiles.pickRandomTile;
Expand Down Expand Up @@ -360,9 +361,16 @@ void Map::renderMap() const
MICROPROFILE_SCOPEI("Map", "Render Map", MP_YELLOW);
#endif

for (int i = 0; i < m_visibleNodesCount; ++i)
//for (int i = m_visibleNodesCount-1; i > 0; --i)
// //for (int i = 0; i < m_visibleNodesCount; ++i)
//{
// //LOG(LOG_INFO) << "Rendering " << pMapNodesVisible[i].get << "," << y;
// pMapNodesVisible[i]->render();
//}

for (auto node : mapNodesInDrawingOrder)
{
pMapNodesVisible[i]->render();
node->render();
}
}

Expand All @@ -373,13 +381,26 @@ void Map::refresh()
#endif

calculateVisibleMap();
sortMapByZIndex();

for (int i = 0; i < m_visibleNodesCount; ++i)
//for (int i = m_visibleNodesCount - 1; i > 0; --i)
// //for (int i = 0; i < m_visibleNodesCount; ++i)
//{
// pMapNodesVisible[i]->refresh();
//}
for (auto node : mapNodesInDrawingOrder)
{
pMapNodesVisible[i]->refresh();
node->getSprite()->refresh();
}
}

void Map::sortMapByZIndex()
{
LOG(LOG_INFO) << "sorting " << mapNodesInDrawingOrder.size() << " nodes";
std::sort(mapNodesInDrawingOrder.begin(), mapNodesInDrawingOrder.end(),
[](MapNode *lhs, MapNode *rhs) { return lhs->getCoordinates().z > rhs->getCoordinates().z; });
}

//TODO: move it out from the map
SDL_Color Map::getColorOfPixelInSurface(SDL_Surface *surface, int x, int y) const
{
Expand Down Expand Up @@ -432,7 +453,8 @@ Point Map::findNodeInMap(const SDL_Point &screenCoordinates, const Layer &layer)
const int yMiddlePoint = isoY - diff;

// Move y up and down 2 neighbors.
for (int y = std::max(yMiddlePoint - neighborReach, 0); (y <= yMiddlePoint + neighborReach) && (y < mapSize); ++y)
for (int y = std::max(yMiddlePoint + neighborReach, 0); (y >= yMiddlePoint - neighborReach) && (y < mapSize); --y)
//for (int y = std::max(yMiddlePoint - neighborReach, 0); (y <= yMiddlePoint + neighborReach) && (y < mapSize); ++y)
{
//get all coordinates for node at x,y
Point coordinate = getMapNode(Point(x, y)).getCoordinates();
Expand Down Expand Up @@ -710,13 +732,15 @@ void Map::calculateVisibleMap(void)
// ZOrder starts from topmost node to the right. (0,127) =1,(1,127) =2, ...
for (int y = m_columns - 1; y >= 0; y--)
{
for (int x = 0; x < m_rows; x++)
//for (int y = 0; y <= m_columns - 1; y++)
//for (int x = 0; x < m_rows; x++)
for (int x = m_rows - 1; x > 0; x--)
{
const int xVal = x + y;
const int yVal = y - x;

if ((xVal >= left) && (xVal <= right) && (yVal <= top) && (yVal >= bottom))
{
//LOG(LOG_INFO) << "Rendering " << x << "," << y;
pMapNodesVisible[m_visibleNodesCount++] = mapNodes[nodeIdx(x, y)].getSprite();
}
}
Expand Down Expand Up @@ -758,9 +782,13 @@ void Map::setTileID(const std::string &tileID, Point coordinate)
demolishNode(targetCoordinates, 0, Layer::BUILDINGS);
}

MapNode &currentNode = getMapNode(coordinate);
currentNode.setTileID(tileID, coordinate);


for (auto coord : targetCoordinates)
{ // now we can place our building

break; // don't doanything
MapNode &currentMapNode = mapNodes[nodeIdx(coord.x, coord.y)];

if (coord != coordinate && targetCoordinates.size() > 1)
Expand All @@ -774,7 +802,7 @@ void Map::setTileID(const std::string &tileID, Point coordinate)

if (!targetCoordinates.size() == 1)
{ // if it's not a >1x1 building, place tileID on the current coordinate (e.g. ground decoration beneath a > 1x1 building)
currentMapNode.setTileID(tileID, coord);
//currentMapNode.setTileID(tileID, coord);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removed the setTileID here ?

}
else
{ // set the tileID for the mapNode of the origin coordinates only on the origin coordinate
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Map.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private:
int m_rows;
std::default_random_engine randomEngine;
TerrainGenerator m_terrainGen;

void sortMapByZIndex();
static const size_t m_saveGameVersion;

// Signals
Expand Down
14 changes: 8 additions & 6 deletions src/engine/map/TerrainGenerator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifdef NOISE_IN_SUBDIR
#include <noise/noise.h>
#else
#include <noise.h>
#include <noise/noise.h>
#endif

using json = nlohmann::json;
Expand Down Expand Up @@ -150,15 +150,17 @@ void TerrainGenerator::generateTerrain(std::vector<MapNode> &mapNodes, std::vect

int z = 0;
// set the z-Index for the mapNodes. It is not used, but it's better to have the correct z-index set
for (int y = mapSize - 1; y >= 0; y--)
{
for (int x = 0; x < mapSize; x++)
{
//for (int y = mapSize - 1; y >= 0; y--)
//for (int y = mapSize - 1; y >= 0; y--)
// for (int x = mapSize - 1; x >= 0; x--)
for (int y = 0; y < mapSize; y++)
for (int x = mapSize - 1; x >= 0; x--)
//for (int x = 0; x < mapSize; x++)
{
z++;
mapNodes[x * mapSize + y].setZIndex(z);
mapNodesInDrawingOrder.push_back(&mapNodes[x * mapSize + y]);
}
}
}

void TerrainGenerator::loadTerrainDataFromJSON()
Expand Down