From 9e346a719df5da7840170a3550f77c9bf5d87d80 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Fri, 15 Nov 2024 22:25:43 +0100 Subject: [PATCH] Reimplemented collision still missing documentation --- include/Map.h | 9 +++++++-- src/Map.cpp | 47 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/include/Map.h b/include/Map.h index 92b8179..ab784bb 100644 --- a/include/Map.h +++ b/include/Map.h @@ -1,8 +1,11 @@ #pragma once +#include +#include #include #include #include +#include class GameInternal; class Map @@ -32,5 +35,7 @@ public: */ static void loadMapTmx(const char* path); private: - static void addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath); -}; + static void addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath, bool hasCollision); + template + static std::optional getLayerProperty(const std::vector& properties, std::string propertyName) { return std::nullopt; }; +}; \ No newline at end of file diff --git a/src/Map.cpp b/src/Map.cpp index 1653c72..0fb735b 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include #include +#include "ColliderComponent.h" #include "Constants.h" #include "GameInternal.h" #include "SpriteComponent.h" @@ -81,6 +83,31 @@ void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std: tile.addGroup((size_t)Entity::GroupLabel::MAPTILES); } + +template<> std::optional Map::getLayerProperty(const std::vector& properties, std::string propertyName) { + auto zIndexIterator = std::ranges::find_if(properties, [propertyName](const tmx::Property& property) { + return property.getName().compare(propertyName) == 0; + }); + + if (zIndexIterator != properties.end() && zIndexIterator->getType() == tmx::Property::Type::Boolean) { + return zIndexIterator->getBoolValue(); + } + + return std::nullopt; +} + +template<> std::optional Map::getLayerProperty(const std::vector& properties, std::string propertyName) { + auto zIndexIterator = std::ranges::find_if(properties, [propertyName](const tmx::Property& property) { + return property.getName().compare(propertyName) == 0; + }); + + if (zIndexIterator != properties.end() && zIndexIterator->getType() == tmx::Property::Type::Int) { + return zIndexIterator->getIntValue(); + } + + return std::nullopt; +} + void Map::loadMapTmx(const char* path) { tmx::Map map; @@ -105,16 +132,11 @@ void Map::loadMapTmx(const char* path) if (layer->getType() == tmx::Layer::Type::Tile) { auto& tileLayer = layer->getLayerAs(); - int zIndex = 0; - const std::vector& properties = layer->getProperties(); - auto zIndexIterator = std::find_if(properties.begin(), properties.end(), [](const tmx::Property& property) { - return property.getName() == "zIndex"; - }); + int zIndex = getLayerProperty(properties, "zIndex").value_or(0); + bool collision = getLayerProperty(properties, "collision").value_or(false); - if (zIndexIterator != properties.end() && std::is_nothrow_convertiblegetType()), int>::value) { - zIndex = zIndexIterator->getIntValue(); - } + printf("z-index: %d, collision: %d\n", zIndex, collision); const auto& tiles = tileLayer.getTiles(); @@ -151,7 +173,7 @@ void Map::loadMapTmx(const char* path) const float tilePosX = static_cast(x) * mapTileSize.x; const float tilePosY = (static_cast(y) * mapTileSize.y); - Map::addTile(tilePosX, tilePosY, mapTileSize, u, v, zIndex, texturePaths.at(i).c_str()); + Map::addTile(tilePosX, tilePosY, mapTileSize, u, v, zIndex, texturePaths.at(i).c_str(), collision); } } if (layer->getType() == tmx::Layer::Type::Object) { @@ -162,10 +184,15 @@ void Map::loadMapTmx(const char* path) } } -void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath) +void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath, bool hasCollision) { auto& tile(VEGO_Game().manager.addEntity()); tile.addComponent(x, y, mapTileSize.x, mapTileSize.y, 1); tile.addComponent(texturePath, v, u, zIndex); // why does uv need to be reversed? + + if (hasCollision) { + tile.addComponent("hello I am a collider of a tile!"); + tile.addGroup((size_t)Entity::GroupLabel::MAPTILES); + } } \ No newline at end of file