Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TileManager Class Reference

This class holds all the information about tileIDs gathered from TileData.json. More...

#include <TileManager.hxx>

+ Inheritance diagram for TileManager:
+ Collaboration diagram for TileManager:

Public Member Functions

 TileManager (TileManager const &)=delete
 
TileManageroperator= (TileManager const &)=delete
 
SDL_Texture * getTexture (const std::string &tileID) const
 Get the Texture for the tileID. More...
 
TileDatagetTileData (const std::string &id) noexcept
 Get the TileData struct for this tileID and all information associated with it. More...
 
Layer getTileLayer (const std::string &tileID) const
 Get the Layer that is associated with a tileID. The Tile will be placed on this layer. More...
 
size_t calculateSlopeOrientation (unsigned char bitMaskElevation)
 Calculates the slope orientation for elevated tiles to pick the correct slope Sprite. More...
 
TileOrientation calculateTileOrientation (unsigned char bitMaskElevation)
 Calculates the TileOrientation for elevated tiles to pick the correct Sprite. More...
 
const std::unordered_map< std::string, TileData > & getAllTileData () const
 
std::vector< std::stringgetAllTileIDsForZone (ZoneType zone, ZoneDensity zoneDensity, TileSize tileSize={1, 1})
 Get the All Tile IDs for a zone with a certain tileSize. More...
 
std::optional< std::stringgetRandomTileIDForZoneWithRandomSize (ZoneType zone, ZoneDensity zoneDensity, TileSize maxTileSize={1, 1})
 Pick a single random tileID for a zone with a random tilesize within the supplied max Size. More...
 
std::vector< PointgetTargetCoordsOfTileID (const Point &targetCoordinates, const std::string &tileID)
 Return a vector of Points on a target node (origin corner) that would be occupied by a given tileID if the placement is valid. More...
 
bool isTileIDAutoTile (const std::string &tileID)
 check if given TileID can autotile (meaning there are textures that look differently according to the position of tiles to each other). More...
 
void init ()
 Parse the tileData JSON and set up the tileManager. More...
 

Public Attributes

friend Singleton< TileManager >
 

Private Member Functions

 TileManager ()
 
 ~TileManager ()=default
 
void addJSONObjectToTileData (const nlohmann::json &tileDataJSON, size_t idx, const std::string &id)
 

Private Attributes

std::unordered_map< std::string, TileDatam_tileData
 
std::unordered_set< TileSizem_tileSizeCombinations
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< TileManager >
static TileManagerinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< TileManager >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

This class holds all the information about tileIDs gathered from TileData.json.

Definition at line 71 of file TileManager.hxx.

Constructor & Destructor Documentation

◆ TileManager() [1/2]

TileManager::TileManager ( TileManager const &  )
delete

◆ TileManager() [2/2]

TileManager::TileManager ( )
private

Definition at line 15 of file TileManager.cxx.

15 { init(); }
+ Here is the call graph for this function:

◆ ~TileManager()

TileManager::~TileManager ( )
privatedefault

Member Function Documentation

◆ addJSONObjectToTileData()

void TileManager::addJSONObjectToTileData ( const nlohmann::json tileDataJSON,
size_t  idx,
const std::string id 
)
private

Definition at line 372 of file TileManager.cxx.

373 {
374  m_tileData[id].author = tileDataJSON[idx].value("author", "");
375  m_tileData[id].title = tileDataJSON[idx].value("title", "");
376  m_tileData[id].description = tileDataJSON[idx].value("description", "");
377  m_tileData[id].category = tileDataJSON[idx].value("category", "");
378  m_tileData[id].subCategory = tileDataJSON[idx].value("subCategory", "");
379  m_tileData[id].price = tileDataJSON[idx].value("price", 0);
380  m_tileData[id].power = tileDataJSON[idx].value("power", 0);
381  m_tileData[id].water = tileDataJSON[idx].value("water", 0);
382  m_tileData[id].upkeepCost = tileDataJSON[idx].value("upkeepCost", 0);
383  m_tileData[id].isOverPlacable = tileDataJSON[idx].value("isOverPlacable", false);
384  m_tileData[id].placeOnWater = tileDataJSON[idx].value("placeOnWater", false);
385  m_tileData[id].inhabitants = tileDataJSON[idx].value("inhabitants", 0);
386  m_tileData[id].happiness = tileDataJSON[idx].value("happiness", 0);
387  m_tileData[id].fireHazardLevel = tileDataJSON[idx].value("fireHazardLevel", 0);
388  m_tileData[id].educationLevel = tileDataJSON[idx].value("educationLevel", 0);
389  m_tileData[id].crimeLevel = tileDataJSON[idx].value("crimeLevel", 0);
390  m_tileData[id].pollutionLevel = tileDataJSON[idx].value("pollutionLevel", 0);
391 
392  std::string tileTypeStr = tileDataJSON[idx].value("tileType", "default");
393 
394  if (TileType::_is_valid_nocase(tileTypeStr.c_str()))
395  {
396  m_tileData[id].tileType = TileType::_from_string_nocase(tileTypeStr.c_str());
397  }
398  else
399  {
400  throw ConfigurationError(TRACE_INFO "In TileData.json in field with ID " + id +
401  " the field tileType uses the unsupported value " + tileTypeStr);
402  }
403 
404  if (tileDataJSON[idx].find("zoneDensity") != tileDataJSON[idx].end())
405  {
406  for (auto zoneDensity : tileDataJSON[idx].at("zoneDensity").items())
407  {
408  if (ZoneDensity::_is_valid_nocase(zoneDensity.value().get<std::string>().c_str()))
409  {
410  m_tileData[id].zoneDensity.push_back(ZoneDensity::_from_string_nocase(zoneDensity.value().get<std::string>().c_str()));
411  }
412  else
413  {
414  throw ConfigurationError(TRACE_INFO "In TileData.json in field with ID " + id +
415  " the field zoneDensity uses the unsupported value " + zoneDensity.value().get<std::string>());
416  }
417  }
418  }
419 
420  if (tileDataJSON[idx].find("zoneType") != tileDataJSON[idx].end())
421  {
422  for (auto zoneType : tileDataJSON[idx].at("zoneType").items())
423  {
424  if (ZoneType::_is_valid_nocase(zoneType.value().get<std::string>().c_str()))
425  {
426  m_tileData[id].zoneTypes.push_back(ZoneType::_from_string_nocase(zoneType.value().get<std::string>().c_str()));
427  }
428  else
429  {
430  throw ConfigurationError(TRACE_INFO "In TileData.json in field with ID " + id +
431  " the field zone uses the unsupported value " + zoneType.value().get<std::string>());
432  }
433  }
434  }
435 
436  if (tileDataJSON[idx].find("style") != tileDataJSON[idx].end())
437  {
438  for (auto style : tileDataJSON[idx].at("style").items())
439  {
440  if (Style::_is_valid_nocase(style.value().get<std::string>().c_str()))
441  {
442  m_tileData[id].style.push_back(Style::_from_string_nocase(style.value().get<std::string>().c_str()));
443  }
444  else
445  {
446  throw ConfigurationError(TRACE_INFO "In TileData.json in field with ID " + id +
447  " the field style uses the unsupported value " + style.value().get<std::string>());
448  }
449  }
450  }
451 
452  if (tileDataJSON[idx].find("biomes") != tileDataJSON[idx].end())
453  {
454  for (auto biome : tileDataJSON[idx].at("biomes").items())
455  {
456  m_tileData[id].biomes.push_back(biome.value().get<std::string>());
457  }
458  }
459 
460  if (tileDataJSON[idx].find("groundDecoration") != tileDataJSON[idx].end())
461  {
462  for (auto groundDecoration : tileDataJSON[idx].at("groundDecoration").items())
463  {
464  m_tileData[id].groundDecoration.push_back(groundDecoration.value().get<std::string>());
465  }
466  }
467 
468  if (tileDataJSON[idx].find("RequiredTiles") != tileDataJSON[idx].end())
469  {
470  m_tileData[id].RequiredTiles.width = tileDataJSON[idx]["RequiredTiles"].value("width", 1);
471  m_tileData[id].RequiredTiles.height = tileDataJSON[idx]["RequiredTiles"].value("height", 1);
472  }
473  else
474  {
475  m_tileData[id].RequiredTiles.width = 1;
476  m_tileData[id].RequiredTiles.height = 1;
477  }
478 
479  m_tileSizeCombinations.insert(m_tileData[id].RequiredTiles);
480 
481  m_tileData[id].tiles.fileName = tileDataJSON[idx]["tiles"].value("fileName", "");
482  m_tileData[id].tiles.clippingHeight = tileDataJSON[idx]["tiles"].value("clip_height", 0);
483  m_tileData[id].tiles.clippingWidth = tileDataJSON[idx]["tiles"].value("clip_width", 0);
484 
485  // offset value can be negative in the json, for the tiledata editor, but never in Cytopia
486  int offset = tileDataJSON[idx]["tiles"].value("offset", 0);
487  if (offset < 0)
488  {
489  offset = 0;
490  }
491  m_tileData[id].tiles.offset = offset;
492  m_tileData[id].tiles.count = tileDataJSON[idx]["tiles"].value("count", 1);
493  m_tileData[id].tiles.pickRandomTile = tileDataJSON[idx]["tiles"].value("pickRandomTile", true);
494 
495  if (!m_tileData[id].tiles.fileName.empty())
496  {
497  ResourcesManager::instance().loadTexture(id, m_tileData[id].tiles.fileName);
498  }
499 
500  if (tileDataJSON[idx].find("shoreLine") != tileDataJSON[idx].end())
501  {
502  m_tileData[id].shoreTiles.fileName = tileDataJSON[idx]["shoreLine"].value("fileName", "");
503  m_tileData[id].shoreTiles.count = tileDataJSON[idx]["shoreLine"].value("count", 1);
504  m_tileData[id].shoreTiles.clippingWidth = tileDataJSON[idx]["shoreLine"].value("clip_width", 0);
505  m_tileData[id].shoreTiles.clippingHeight = tileDataJSON[idx]["shoreLine"].value("clip_height", 0);
506 
507  // offset value can be negative in the json, for the tiledata editor, but never in Cytopia
508  int offset = tileDataJSON[idx]["shoreLine"].value("offset", 0);
509  if (offset < 0)
510  {
511  offset = 0;
512  }
513  m_tileData[id].shoreTiles.offset = offset;
514 
515  if (!m_tileData[id].shoreTiles.fileName.empty())
516  {
517  ResourcesManager::instance().loadTexture(id + "_shore", m_tileData[id].shoreTiles.fileName);
518  }
519  }
520 
521  if (tileDataJSON[idx].find("slopeTiles") != tileDataJSON[idx].end())
522  {
523 
524  m_tileData[id].slopeTiles.fileName = tileDataJSON[idx]["slopeTiles"].value("fileName", "");
525  m_tileData[id].slopeTiles.count = tileDataJSON[idx]["slopeTiles"].value("count", 1);
526  m_tileData[id].slopeTiles.clippingWidth = tileDataJSON[idx]["slopeTiles"].value("clip_width", 0);
527  m_tileData[id].slopeTiles.clippingHeight = tileDataJSON[idx]["slopeTiles"].value("clip_height", 0);
528 
529  // offset value can be negative in the json, for the tiledata editor, but never in Cytopia
530  int offset = tileDataJSON[idx]["slopeTiles"].value("offset", 0);
531  if (offset < 0)
532  {
533  offset = 0;
534  }
535  m_tileData[id].slopeTiles.offset = offset;
536 
537  if (!m_tileData[id].slopeTiles.fileName.empty())
538  {
539  ResourcesManager::instance().loadTexture(id, m_tileData[id].slopeTiles.fileName);
540  }
541  }
542 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ calculateSlopeOrientation()

size_t TileManager::calculateSlopeOrientation ( unsigned char  bitMaskElevation)

Calculates the slope orientation for elevated tiles to pick the correct slope Sprite.

Parameters
bitMaskElevationa bitmask of neighboring tiles and their elevation
Returns
Elevation bitmask

Definition at line 147 of file TileManager.cxx.

148 {
149  // initialize with DEFAULT_ORIENTATION which elevationMask.none()
150  size_t orientation = TileSlopes::DEFAULT_ORIENTATION;
151  const std::bitset<8> elevationMask(bitMaskElevation);
152 
153  // Bits:
154  // 0 = 2^0 = 1 = TOP
155  // 1 = 2^1 = 2 = BOTTOM
156  // 2 = 2^2 = 4 = LEFT
157  // 3 = 2^3 = 8 = RIGHT
158  // 4 = 2^4 = 16 = TOP LEFT
159  // 5 = 2^5 = 32 = TOP RIGHT
160  // 6 = 2^6 = 64 = BOTTOM LEFT
161  // 7 = 2^7 = 128 = BOTTOM RIGHT
162 
163  // check for all combinations
164  if (elevationMask.test(3) && elevationMask.test(6))
165  { // BOTTOM_RIGHT
166  orientation = TileSlopes::S_AND_E;
167  }
168  else if (elevationMask.test(2) && elevationMask.test(5))
169  { // BOTTOM_RIGHT
170  orientation = TileSlopes::N_AND_W;
171  }
172  else if (elevationMask.test(3) && elevationMask.test(4))
173  { // BOTTOM_RIGHT
174  orientation = TileSlopes::N_AND_E;
175  }
176  else if (elevationMask.test(2) && elevationMask.test(7))
177  { // BOTTOM_RIGHT
178  orientation = TileSlopes::S_AND_W;
179  }
180 
181  else if (elevationMask.test(0) && elevationMask.test(6))
182  { // BOTTOM_RIGHT
183  orientation = TileSlopes::N_AND_W;
184  }
185  else if (elevationMask.test(1) && elevationMask.test(5))
186  { // BOTTOM_RIGHT
187  orientation = TileSlopes::S_AND_E;
188  }
189  else if (elevationMask.test(0) && elevationMask.test(7))
190  { // BOTTOM_RIGHT
191  orientation = TileSlopes::N_AND_E;
192  }
193  else if (elevationMask.test(1) && elevationMask.test(4))
194  { // BOTTOM_RIGHT
195  orientation = TileSlopes::S_AND_W;
196  }
197 
198  // diagonal combinations
199  else if (elevationMask.test(0) && elevationMask.test(2))
200  { // TOP && RIGHT
201  orientation = TileSlopes::N_AND_W;
202  }
203  else if (elevationMask.test(0) && elevationMask.test(3))
204  { // TOP && LEFT
205  orientation = TileSlopes::N_AND_E;
206  }
207  else if (elevationMask.test(1) && elevationMask.test(2))
208  { // BOTTOM && RIGHT
209  orientation = TileSlopes::S_AND_W;
210  }
211  else if (elevationMask.test(1) && elevationMask.test(3))
212  { // BOTTOM && LEFT
213  orientation = TileSlopes::S_AND_E;
214  }
215 
216  // default directions
217  else if (elevationMask.test(0))
218  { // TOP
219  orientation = TileSlopes::N;
220  }
221  else if (elevationMask.test(1))
222  { // BOTTOM
223  orientation = TileSlopes::S;
224  }
225  else if (elevationMask.test(2))
226  { // LEFT
227  orientation = TileSlopes::W;
228  }
229  else if (elevationMask.test(3))
230  { // RIGHT
231  orientation = TileSlopes::E;
232  }
233  else if ((elevationMask.test(4) && elevationMask.test(7)) || (elevationMask.test(5) && elevationMask.test(6)))
234  { // BOTTOM_RIGHT
235  orientation = TileSlopes::BETWEEN;
236  }
237  else if (elevationMask.test(4))
238  { // TOP_LEFT
239  orientation = TileSlopes::NW;
240  }
241  else if (elevationMask.test(5))
242  { // TOP_RIGHT
243  orientation = TileSlopes::NE;
244  }
245  else if (elevationMask.test(6))
246  { // BOTTOM_LEFT
247  orientation = TileSlopes::SW;
248  }
249  else if (elevationMask.test(7))
250  { // BOTTOM_RIGHT
251  orientation = TileSlopes::SE;
252  }
253 
254  return orientation;
255 }
+ Here is the caller graph for this function:

◆ calculateTileOrientation()

TileOrientation TileManager::calculateTileOrientation ( unsigned char  bitMaskElevation)

Calculates the TileOrientation for elevated tiles to pick the correct Sprite.

Parameters
bitMaskElevationa bitmask of neighboring tiles and their elevation
Returns
The orientation the tile should have to it's elevated neighbors.

Definition at line 257 of file TileManager.cxx.

258 {
259  TileOrientation orientation;
260  const std::bitset<8> elevationMask(bitMaskElevation);
261 
262  // Bits:
263  // 0 = 2^0 = 1 = TOP
264  // 1 = 2^1 = 2 = BOTTOM
265  // 2 = 2^2 = 4 = LEFT
266  // 3 = 2^3 = 8 = RIGHT
267  // 4 = 2^4 = 16 = TOP LEFT
268  // 5 = 2^5 = 32 = TOP RIGHT
269  // 6 = 2^6 = 64 = BOTTOM LEFT
270  // 7 = 2^7 = 128 = BOTTOM RIGHT
271 
272  // check for all combinations
273  if (elevationMask.none())
274  { // NONE
276  }
277  // special cases
278  else if (elevationMask.test(0) && elevationMask.test(1) && elevationMask.test(2) && elevationMask.test(3))
279  { // BOTTOM_RIGHT
281  }
282  else if (elevationMask.test(0) && elevationMask.test(2) && elevationMask.test(3))
283  { // BOTTOM_RIGHT
285  }
286  else if (elevationMask.test(0) && elevationMask.test(1) && elevationMask.test(3))
287  { // BOTTOM_RIGHT
289  }
290  else if (elevationMask.test(0) && elevationMask.test(1) && elevationMask.test(2))
291  { // BOTTOM_RIGHT
293  }
294  else if (elevationMask.test(1) && elevationMask.test(2) && elevationMask.test(3))
295  { // BOTTOM_RIGHT
297  }
298  else if (elevationMask.test(2) && elevationMask.test(3))
299  { // BOTTOM_RIGHT
300  orientation = TileOrientation::TILE_E_AND_W;
301  }
302  else if (elevationMask.test(0) && elevationMask.test(1))
303  { // BOTTOM_RIGHT
304  orientation = TileOrientation::TILE_N_AND_S;
305  }
306 
307  // diagonal combinations
308  else if (elevationMask.test(0) && elevationMask.test(2))
309  { // TOP && RIGHT
310  orientation = TileOrientation::TILE_N_AND_W;
311  }
312  else if (elevationMask.test(0) && elevationMask.test(3))
313  { // TOP && LEFT
314  orientation = TileOrientation::TILE_N_AND_E;
315  }
316  else if (elevationMask.test(1) && elevationMask.test(2))
317  { // BOTTOM && RIGHT
318  orientation = TileOrientation::TILE_S_AND_W;
319  }
320  else if (elevationMask.test(1) && elevationMask.test(3))
321  { // BOTTOM && LEFT
322  orientation = TileOrientation::TILE_S_AND_E;
323  }
324 
325  // default directions
326  else if (elevationMask.test(0))
327  { // TOP
328  orientation = TileOrientation::TILE_N;
329  }
330  else if (elevationMask.test(1))
331  { // BOTTOM
332  orientation = TileOrientation::TILE_S;
333  }
334  else if (elevationMask.test(2))
335  { // LEFT
336  orientation = TileOrientation::TILE_W;
337  }
338  else if (elevationMask.test(3))
339  { // RIGHT
340  orientation = TileOrientation::TILE_E;
341  }
342 
343  else
344  {
345  orientation = TILE_DEFAULT_ORIENTATION;
346  }
347  return orientation;
348 }
+ Here is the caller graph for this function:

◆ getAllTileData()

const std::unordered_map<std::string, TileData>& TileManager::getAllTileData ( ) const
inline

Definition at line 110 of file TileManager.hxx.

110 { return m_tileData; };

◆ getAllTileIDsForZone()

std::vector< std::string > TileManager::getAllTileIDsForZone ( ZoneType  zone,
ZoneDensity  zoneDensity,
TileSize  tileSize = {1, 1} 
)

Get the All Tile IDs for a zone with a certain tileSize.

Parameters
zoneThe Zone we want tileIDs for
zoneDensity
tileSizeGet buildings with a certain tile size
Returns
All tileIDs that match the zone and tileSize

Definition at line 29 of file TileManager.cxx.

30 {
31  std::vector<std::string> results;
32  for (auto &tileData : m_tileData)
33  {
34  if (std::find(tileData.second.zoneTypes.begin(), tileData.second.zoneTypes.end(), +zone) != tileData.second.zoneTypes.end() &&
35  (zone == +ZoneType::AGRICULTURAL || std::find(tileData.second.zoneDensity.begin(), tileData.second.zoneDensity.end(),
36  +zoneDensity) != tileData.second.zoneDensity.end()) &&
37  tileData.second.RequiredTiles.height == tileSize.height && tileData.second.RequiredTiles.width == tileSize.width &&
38  tileData.second.tileType != +TileType::ZONE)
39  {
40  results.push_back(tileData.first);
41  }
42  }
43  return results;
44 }
+ Here is the caller graph for this function:

◆ getRandomTileIDForZoneWithRandomSize()

std::optional< std::string > TileManager::getRandomTileIDForZoneWithRandomSize ( ZoneType  zone,
ZoneDensity  zoneDensity,
TileSize  maxTileSize = {1, 1} 
)

Pick a single random tileID for a zone with a random tilesize within the supplied max Size.

Parameters
zoneThe Zone we want tileIDs for
zoneDensity
maxTileSizemaximum tileSize we want
Returns
A random tileID matching the supplied parameters

Definition at line 72 of file TileManager.cxx.

75 {
76  std::vector<TileSize> elligibleTileSizes;
77 
78  // filter out the tilesizes that are below the maximum from all available tilesizes
79  for (auto tileSize : m_tileSizeCombinations)
80  {
81  // for now only pick square buildings. non square buildings don't work yet.
82  if (tileSize.height <= maxTileSize.height && tileSize.width <= maxTileSize.width && tileSize.height == tileSize.width)
83  {
84  elligibleTileSizes.push_back(tileSize);
85  }
86  }
87 
88  // pick a random tilesize from the new set of elligible tilesizes
89  auto &randomizer = Randomizer::instance();
90  TileSize randomTileSize = *randomizer.choose(elligibleTileSizes.begin(), elligibleTileSizes.end());
91 
92  // get all tile IDs for the according zone and tilesize
93  const auto &tileIDsForThisZone = getAllTileIDsForZone(zone, zoneDensity, randomTileSize);
94 
95  if (tileIDsForThisZone.empty())
96  {
97  return std::nullopt;
98  }
99 
100  // return a random tileID
101  return *randomizer.choose(tileIDsForThisZone.begin(), tileIDsForThisZone.end());
102 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTargetCoordsOfTileID()

std::vector< Point > TileManager::getTargetCoordsOfTileID ( const Point targetCoordinates,
const std::string tileID 
)

Return a vector of Points on a target node (origin corner) that would be occupied by a given tileID if the placement is valid.

Parameters
targetCoordinatesthe origin node where the tile should be placed
tileIDthe tileID to place
Returns
vector of points that will be occupied by this tileID, empty if placement is not allowed

Definition at line 46 of file TileManager.cxx.

47 {
48  std::vector<Point> occupiedCoords;
49  TileData *tileData = getTileData(tileID);
50 
51  if (!tileData)
52  {
53  return occupiedCoords;
54  }
55 
56  for (int i = 0; i < tileData->RequiredTiles.width; i++)
57  {
58  for (int j = 0; j < tileData->RequiredTiles.height; j++)
59  {
60  Point coords = {targetCoordinates.x - i, targetCoordinates.y + j};
61  if (!coords.isWithinMapBoundaries())
62  { // boundary check
63  occupiedCoords.clear();
64  return occupiedCoords;
65  }
66  occupiedCoords.emplace_back(coords);
67  }
68  }
69  return occupiedCoords;
70 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTexture()

SDL_Texture * TileManager::getTexture ( const std::string tileID) const

Get the Texture for the tileID.

Parameters
tileIDTileID
Returns
An SDL Texture we can render

Definition at line 17 of file TileManager.cxx.

18 {
20 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTileData()

TileData * TileManager::getTileData ( const std::string id)
noexcept

Get the TileData struct for this tileID and all information associated with it.

Parameters
idTileID
Returns
A pointer to the TileData Struct

Definition at line 22 of file TileManager.cxx.

23 {
24  if (m_tileData.count(id))
25  return &m_tileData[id];
26  return nullptr;
27 }
+ Here is the caller graph for this function:

◆ getTileLayer()

Layer TileManager::getTileLayer ( const std::string tileID) const

Get the Layer that is associated with a tileID. The Tile will be placed on this layer.

Parameters
tileIDthe tileID to get the Layer for
Returns
The layer this tileID has to be placed on

Definition at line 104 of file TileManager.cxx.

105 {
106  Layer layer = Layer::TERRAIN;
107  TileData *tileData = TileManager::instance().getTileData(tileID);
108  if (tileData)
109  {
110  switch (tileData->tileType)
111  {
112  case TileType::TERRAIN:
113  layer = Layer::TERRAIN;
114  break;
115  case TileType::BLUEPRINT:
116  layer = Layer::BLUEPRINT;
117  break;
118  case TileType::WATER:
119  layer = Layer::WATER;
120  break;
122  layer = Layer::UNDERGROUND;
123  break;
124  case TileType::GROUNDDECORATION:
125  layer = Layer::GROUND_DECORATION;
126  break;
127  case TileType::ZONE:
128  layer = Layer::ZONE;
129  break;
130  case TileType::ROAD:
131  layer = Layer::ROAD;
132  break;
133  case TileType::POWERLINE:
134  layer = Layer::POWERLINES;
135  break;
136  case TileType::FLORA:
137  layer = Layer::FLORA;
138  break;
139  default:
140  layer = Layer::BUILDINGS;
141  break;
142  }
143  }
144  return layer;
145 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init()

void TileManager::init ( )

Parse the tileData JSON and set up the tileManager.

Exceptions
ConfigurationErrorwhen loading tile data file results in an error

Definition at line 350 of file TileManager.cxx.

351 {
352  std::string jsonFile = fs::readFileAsString(Settings::instance().tileDataJSONFile.get());
353  const json tileDataJSON = json::parse(jsonFile, nullptr, false);
354 
355  // check if json file can be parsed
356  if (tileDataJSON.is_discarded())
357  throw ConfigurationError(TRACE_INFO "Error parsing JSON File " + Settings::instance().tileDataJSONFile.get());
358 
359  std::string key;
360 
361  size_t idx = 0;
362 
363  for (const auto &element : tileDataJSON.items())
364  {
365  std::string id;
366  id = element.value().value("id", "");
367  addJSONObjectToTileData(tileDataJSON, idx, id);
368  idx++;
369  }
370 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isTileIDAutoTile()

bool TileManager::isTileIDAutoTile ( const std::string tileID)

check if given TileID can autotile (meaning there are textures that look differently according to the position of tiles to each other).

Parameters
tileID
Returns
bool whether Tile Data item can be autotiled.

Definition at line 544 of file TileManager.cxx.

545 {
546  TileData *tileData = getTileData(tileID);
547  if (tileData)
548  switch (tileData->tileType)
549  {
550  case +TileType::ROAD:
551  case +TileType::AUTOTILE:
552  case +TileType::UNDERGROUND:
553  case +TileType::POWERLINE:
554  return true;
555  default:
556  return false;
557  }
558 
559  return false;
560 }
+ Here is the call graph for this function:

◆ operator=()

TileManager& TileManager::operator= ( TileManager const &  )
delete

Member Data Documentation

◆ m_tileData

std::unordered_map<std::string, TileData> TileManager::m_tileData
private

Definition at line 153 of file TileManager.hxx.

◆ m_tileSizeCombinations

std::unordered_set<TileSize> TileManager::m_tileSizeCombinations
private

Definition at line 154 of file TileManager.hxx.

◆ Singleton< TileManager >

Definition at line 74 of file TileManager.hxx.


The documentation for this class was generated from the following files:
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
TileSize::height
unsigned int height
Definition: tileData.hxx:89
WATER
@ WATER
6- Water tiles
Definition: enums.hxx:17
TileData::tileType
TileType tileType
Definition: tileData.hxx:149
TILE_S
@ TILE_S
Definition: TileManager.hxx:51
TileManager::getAllTileIDsForZone
std::vector< std::string > getAllTileIDsForZone(ZoneType zone, ZoneDensity zoneDensity, TileSize tileSize={1, 1})
Get the All Tile IDs for a zone with a certain tileSize.
Definition: TileManager.cxx:29
TILE_N_AND_W
@ TILE_N_AND_W
Definition: TileManager.hxx:49
TILE_ALL_DIRECTIONS
@ TILE_ALL_DIRECTIONS
Definition: TileManager.hxx:58
ResourcesManager::getTileTexture
SDL_Texture * getTileTexture(const std::string &id)
Definition: ResourcesManager.cxx:59
TILE_N
@ TILE_N
Definition: TileManager.hxx:45
POWERLINES
@ POWERLINES
9- Powerlines
Definition: enums.hxx:20
TILE_S_AND_E_AND_W
@ TILE_S_AND_E_AND_W
Definition: TileManager.hxx:56
BETWEEN
@ BETWEEN
Definition: TileManager.hxx:37
SW
@ SW
Definition: TileManager.hxx:31
TileSize
How many tiles are occupied by a building.
Definition: tileData.hxx:86
W
@ W
Definition: TileManager.hxx:26
TILE_E
@ TILE_E
Definition: TileManager.hxx:44
TILE_N_AND_E
@ TILE_N_AND_E
Definition: TileManager.hxx:46
TILE_E_AND_W
@ TILE_E_AND_W
Definition: TileManager.hxx:48
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
readFileAsString
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
Definition: Filesystem.cxx:12
S_AND_W
@ S_AND_W
Definition: TileManager.hxx:35
TileManager::m_tileData
std::unordered_map< std::string, TileData > m_tileData
Definition: TileManager.hxx:153
ZONE
@ ZONE
4- Optional layer, zones(Industrial/Residential/Commercial).
Definition: enums.hxx:15
BLUEPRINT
@ BLUEPRINT
1- Optional layer - Map Blueprint
Definition: enums.hxx:12
N_AND_W
@ N_AND_W
Definition: TileManager.hxx:33
BUILDINGS
@ BUILDINGS
8- Buildings, Streets and everything that goes on the terrain
Definition: enums.hxx:19
TILE_S_AND_E
@ TILE_S_AND_E
Definition: TileManager.hxx:52
TILE_N_AND_S_AND_W
@ TILE_N_AND_S_AND_W
Definition: TileManager.hxx:57
Point::isWithinMapBoundaries
bool isWithinMapBoundaries() const
Definition: Point.hxx:37
DEFAULT_ORIENTATION
@ DEFAULT_ORIENTATION
Definition: TileManager.hxx:38
NE
@ NE
Definition: TileManager.hxx:30
TileManager::m_tileSizeCombinations
std::unordered_set< TileSize > m_tileSizeCombinations
Definition: TileManager.hxx:154
E
@ E
Definition: TileManager.hxx:27
TileManager::init
void init()
Parse the tileData JSON and set up the tileManager.
Definition: TileManager.cxx:350
TileData::RequiredTiles
TileSize RequiredTiles
How many tiles this building uses.
Definition: tileData.hxx:173
FLORA
@ FLORA
10- Trees and other flora/Fauna tiles
Definition: enums.hxx:21
TileManager::getTileData
TileData * getTileData(const std::string &id) noexcept
Get the TileData struct for this tileID and all information associated with it.
Definition: TileManager.cxx:22
N_AND_E
@ N_AND_E
Definition: TileManager.hxx:34
TERRAIN
@ TERRAIN
3- Terrain tiles, decorations, ... - must always be a "full" tile
Definition: enums.hxx:14
TileOrientation
TileOrientation
Definition: TileManager.hxx:41
TILE_S_AND_W
@ TILE_S_AND_W
Definition: TileManager.hxx:55
TILE_W
@ TILE_W
Definition: TileManager.hxx:47
ROAD
@ ROAD
5- Optional layer, roads.
Definition: enums.hxx:16
SE
@ SE
Definition: TileManager.hxx:32
TILE_N_AND_S
@ TILE_N_AND_S
Definition: TileManager.hxx:53
ResourcesManager::loadTexture
void loadTexture(const std::string &id, const std::string &fileName)
Definition: ResourcesManager.cxx:20
Point
Definition: Point.hxx:7
GROUND_DECORATION
@ GROUND_DECORATION
13- Decoration to place beneath buildings. Like concrete or grass
Definition: enums.hxx:24
NW
@ NW
Definition: TileManager.hxx:29
Singleton< ResourcesManager >::instance
static ResourcesManager & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
S
@ S
Definition: TileManager.hxx:28
N
@ N
Definition: TileManager.hxx:25
TileManager::addJSONObjectToTileData
void addJSONObjectToTileData(const nlohmann::json &tileDataJSON, size_t idx, const std::string &id)
Definition: TileManager.cxx:372
Layer
Layer
All Layers we have.
Definition: enums.hxx:9
S_AND_E
@ S_AND_E
Definition: TileManager.hxx:36
json
nlohmann::json json
Definition: Settings.hxx:12
string
std::string string
Definition: AudioConfig.hxx:14
UNDERGROUND
@ UNDERGROUND
2- Optional layer - Pipes, Subway-pipes and so onn
Definition: enums.hxx:13
TILE_DEFAULT_ORIENTATION
@ TILE_DEFAULT_ORIENTATION
Definition: TileManager.hxx:43
TILE_N_AND_E_AND_S
@ TILE_N_AND_E_AND_S
Definition: TileManager.hxx:54
TILE_N_AND_E_AND_W
@ TILE_N_AND_E_AND_W
Definition: TileManager.hxx:50
TileData
Holds all releavted information to this specific tile.
Definition: tileData.hxx:135
TileSize::width
unsigned int width
Definition: tileData.hxx:88