diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-02 19:34:48 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-02 19:34:48 -0500 |
commit | 93b3e4004387047c25b2f5a190aced01c9091934 (patch) | |
tree | 4bc8ff58dd7198bb2bfed77122547c9599b0ef0f /src/map.cpp | |
parent | 9f0e76a47da72573863b22bda72d55e6010def6b (diff) | |
download | tanetane-93b3e4004387047c25b2f5a190aced01c9091934.tar.gz tanetane-93b3e4004387047c25b2f5a190aced01c9091934.tar.bz2 tanetane-93b3e4004387047c25b2f5a190aced01c9091934.zip |
Added collision with map tiles
Diffstat (limited to 'src/map.cpp')
-rw-r--r-- | src/map.cpp | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/src/map.cpp b/src/map.cpp index a60cbba..7d9800d 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -1,6 +1,7 @@ | |||
1 | #include "map.h" | 1 | #include "map.h" |
2 | #include <tmxlite/Map.hpp> | 2 | #include <tmxlite/Map.hpp> |
3 | #include <tmxlite/Layer.hpp> | 3 | #include <tmxlite/Layer.hpp> |
4 | #include <tmxlite/Property.hpp> | ||
4 | #include <tmxlite/TileLayer.hpp> | 5 | #include <tmxlite/TileLayer.hpp> |
5 | #include <tmxlite/Tileset.hpp> | 6 | #include <tmxlite/Tileset.hpp> |
6 | #include "renderer.h" | 7 | #include "renderer.h" |
@@ -21,11 +22,10 @@ Map::Map(std::string_view filename, Renderer& renderer) { | |||
21 | 22 | ||
22 | int firstGID = 0; | 23 | int firstGID = 0; |
23 | // There should only be one tileset. | 24 | // There should only be one tileset. |
24 | for (const tmx::Tileset& tileset : mapfile.getTilesets()) { | 25 | const tmx::Tileset& tileset = mapfile.getTilesets()[0]; |
25 | firstGID = tileset.getFirstGID(); | 26 | firstGID = tileset.getFirstGID(); |
26 | tilesetTextureId_ = renderer.loadImageFromFile(tileset.getImagePath()); | 27 | tilesetTextureId_ = renderer.loadImageFromFile(tileset.getImagePath()); |
27 | tilesetColumns_ = tileset.getColumnCount(); | 28 | tilesetColumns_ = tileset.getColumnCount(); |
28 | } | ||
29 | 29 | ||
30 | for (const auto& layer : mapfile.getLayers()) { | 30 | for (const auto& layer : mapfile.getLayers()) { |
31 | if (layer->getType() == tmx::Layer::Type::Tile) { | 31 | if (layer->getType() == tmx::Layer::Type::Tile) { |
@@ -34,13 +34,33 @@ Map::Map(std::string_view filename, Renderer& renderer) { | |||
34 | std::vector<Tile> tilesToStore; | 34 | std::vector<Tile> tilesToStore; |
35 | 35 | ||
36 | for (const auto& maptile : tileLayer.getTiles()) { | 36 | for (const auto& maptile : tileLayer.getTiles()) { |
37 | tilesToStore.push_back(Tile { | 37 | Tile tile; |
38 | .id = maptile.ID - firstGID, | 38 | tile.id = maptile.ID - firstGID; |
39 | .flipHorizontal = (maptile.flipFlags & tmx::TileLayer::Horizontal) != 0, | 39 | tile.flipHorizontal = (maptile.flipFlags & tmx::TileLayer::Horizontal) != 0; |
40 | .flipVertical = (maptile.flipFlags & tmx::TileLayer::Vertical) != 0 }); | 40 | tile.flipVertical = (maptile.flipFlags & tmx::TileLayer::Vertical) != 0; |
41 | |||
42 | for (const tmx::Property& property : tileset.getTile(maptile.ID)->properties) { | ||
43 | if (property.getName() == "solid" && property.getBoolValue()) { | ||
44 | tile.blocked = true; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | tilesToStore.push_back(std::move(tile)); | ||
41 | } | 49 | } |
42 | 50 | ||
43 | layers_.push_back(std::move(tilesToStore)); | 51 | layers_.push_back(std::move(tilesToStore)); |
44 | } | 52 | } |
45 | } | 53 | } |
46 | } | 54 | } |
55 | |||
56 | bool Map::isBlocked(int x, int y) const { | ||
57 | int i = x + y * mapSize_.w(); | ||
58 | |||
59 | for (const std::vector<Tile>& layer : layers_) { | ||
60 | if (layer.at(i).blocked) { | ||
61 | return true; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | return false; | ||
66 | } | ||