summary refs log tree commit diff stats
path: root/src/map.cpp
blob: 1a2a250707b63aaa3fb89255453fbf0fe51bcecf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "map.h"
#include <tmxlite/Map.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/Property.hpp>
#include <tmxlite/TileLayer.hpp>
#include <tmxlite/Tileset.hpp>
#include "renderer.h"

Map::Map(std::string_view filename, Renderer& renderer) {
  tmx::Map mapfile;
  if (!mapfile.load(filename.data())) {
    throw std::invalid_argument("Could not find map file: " + std::string(filename));
  }

  const tmx::Vector2u& mapSize = mapfile.getTileCount();
  mapSize_.x() = mapSize.x;
  mapSize_.y() = mapSize.y;

  const tmx::Vector2u& tileSize = mapfile.getTileSize();
  tileSize_.x() = tileSize.x;
  tileSize_.y() = tileSize.y;

  int firstGID = 0;
  // There should only be one tileset.
  const tmx::Tileset& tileset = mapfile.getTilesets()[0];
  firstGID = tileset.getFirstGID();
  tilesetTextureId_ = renderer.loadImageFromFile(tileset.getImagePath());
  tilesetColumns_ = tileset.getColumnCount();

  for (const auto& layer : mapfile.getLayers()) {
    if (layer->getType() == tmx::Layer::Type::Tile) {
      const auto& tileLayer = layer->getLayerAs<tmx::TileLayer>();

      std::vector<Tile> tilesToStore;

      for (const auto& maptile : tileLayer.getTiles()) {
        Tile tile;
        tile.id = maptile.ID - firstGID;
        tile.flipHorizontal = (maptile.flipFlags & tmx::TileLayer::Horizontal) != 0;
        tile.flipVertical = (maptile.flipFlags & tmx::TileLayer::Vertical) != 0;

        for (const tmx::Property& property : tileset.getTile(maptile.ID)->properties) {
          if (property.getName() == "solid" && property.getBoolValue()) {
            tile.blocked = true;
          } else if (property.getName() == "runSound") {
            tile.step = stepTypeFromString(property.getStringValue());
          }
        }

        tilesToStore.push_back(std::move(tile));
      }

      layers_.push_back(std::move(tilesToStore));
    } else if (layer->getType() == tmx::Layer::Type::Object) {
      const auto& objectLayer = layer->getLayerAs<tmx::ObjectGroup>();

      for (const tmx::Object& object : objectLayer.getObjects()) {
        if (object.getType() == "sprite") {
          Prototype p;
          p.name = object.getName();
          p.pos.x() = object.getPosition().x;
          p.pos.y() = object.getPosition().y;

          for (const tmx::Property& property : object.getProperties()) {
            if (property.getName() == "collisionOffsetX") {
              p.collisionOffset.x() = property.getIntValue();
            } else if (property.getName() == "collisionOffsetY") {
              p.collisionOffset.y() = property.getIntValue();
            } else if (property.getName() == "collisionWidth") {
              p.collisionSize.w() = property.getIntValue();
            } else if (property.getName() == "collisionHeight") {
              p.collisionSize.h() = property.getIntValue();
            } else if (property.getName() == "animation") {
              p.animationFilename = property.getStringValue();
            } else if (property.getName() == "interactionScript") {
              p.interactionScript = property.getStringValue();
            }
          }

          prototypes_.push_back(std::move(p));
        }
      }
    }
  }
}

bool Map::isBlocked(int x, int y) const {
  int i = x + y * mapSize_.w();

  for (const std::vector<Tile>& layer : layers_) {
    if (layer.at(i).blocked) {
      return true;
    }
  }

  return false;
}

StepType Map::getStepType(int x, int y) const {
  int i = x + y * mapSize_.w();

  for (const std::vector<Tile>& layer : layers_) {
    if (layer.at(i).step != StepType::none) {
      return layer.at(i).step;
    }
  }

  return StepType::none;
}