From 737fd70851a1508274f2e91078c0551f1d5e8889 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Thu, 2 May 2024 21:50:22 +0200 Subject: [PATCH] ref(addTile): adding tiles for the tilemap is now on the dev side --- include/Game.h | 1 - include/Map.h | 6 +++++- include/TextureDict.h | 24 ------------------------ include/TileComponent.h | 12 +++++++++--- src/AssetManager.cpp | 1 - src/Game.cpp | 12 ------------ src/Map.cpp | 28 +++++++++++++++++++--------- src/TileComponent.cpp | 11 +++++++---- 8 files changed, 40 insertions(+), 55 deletions(-) delete mode 100644 include/TextureDict.h diff --git a/include/Game.h b/include/Game.h index 7ff50d6..1edbada 100644 --- a/include/Game.h +++ b/include/Game.h @@ -40,7 +40,6 @@ public: void clean(); bool running() const; - void addTile(unsigned long id, int x, int y); /* static */ SDL_Renderer* renderer = nullptr; /* static */ SDL_Event event; /* static */ CollisionHandler* collisionHandler; diff --git a/include/Map.h b/include/Map.h index 8197d40..09f46b6 100644 --- a/include/Map.h +++ b/include/Map.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + class Game; class Map { @@ -18,5 +21,6 @@ public: * \return Boolean for success * */ - static bool loadMap(const char* path, int sizeX, int sizeY, Game* game /* backreference */); + static void loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map>* textureDict /* backreference */); + static void addTile(unsigned long id, int x, int y, Game* game, const std::map>* textureDict); }; diff --git a/include/TextureDict.h b/include/TextureDict.h deleted file mode 100644 index 0a4b6ed..0000000 --- a/include/TextureDict.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include "AssetManager.h" - -class TextureDict -{ -public: - const std::map tileDictionary = { - {1, "assets/water.png"}, - {2, "assets/dirt.png"}, - {3, "assets/grass.png"}, - {7, "assets/grass_water_left.png"}, - {9, "assets/grass_water_right.png"} - }; - - - std::map powerupDictionary = { - {PowerupType::HEART, "assets/heart_powerup.png"}, - {PowerupType::WALKINGSPEED, "assets/movement_speed_powerup.png"}, - {PowerupType::SHOOTINGSPEED, "assets/atk_speed_powerup.png"} - }; -}; diff --git a/include/TileComponent.h b/include/TileComponent.h index c0a9e1b..8844daa 100644 --- a/include/TileComponent.h +++ b/include/TileComponent.h @@ -1,9 +1,10 @@ #pragma once #include +#include +#include #include "Component.h" -#include "TextureDict.h" class SpriteComponent; class TransformComponent; @@ -13,15 +14,20 @@ class TileComponent : public Component public: TransformComponent* transform; SpriteComponent* sprite; - TextureDict textureDict; SDL_Rect tileRect; int tileID; const char* path; TileComponent() = default; - TileComponent(int x, int y, int w, int h, int id); + TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict); ~TileComponent() = default; void init() override; + + bool hasCollision(){return this->collision;} + std::string getName(){return this->tileName;} +private: + bool collision; + std::string tileName; }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index f759b19..77915ae 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -49,7 +49,6 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, } void AssetManager::createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath) { - TextureDict textureDict; auto& powerups(man->addEntity()); powerups.addComponent(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects diff --git a/src/Game.cpp b/src/Game.cpp index e7ac1d4..22bf7bf 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -143,10 +143,6 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo if (this->isRunning == false) return; map = new Map(); - if (!map->loadMap("assets/SDL_map_test.txt", 25, 20, this)) { - std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; - SDL_ClearError(); - }; //adding textures to the library in AssetManager @@ -330,14 +326,6 @@ void Game::clean() std::cout << "Game Cleaned!" << std::endl; } -void Game::addTile(unsigned long id, int x, int y) // tile entity -{ - auto& tile(manager.addEntity()); - tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id); - if (id == 1) tile.addComponent("water"); - tile.addGroup((size_t)Entity::GroupLabel::MAPTILES); -} - bool Game::running() const { return isRunning; diff --git a/src/Map.cpp b/src/Map.cpp index b41b793..125f029 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -3,13 +3,14 @@ #include #include #include -#include +#include #include "Constants.h" #include "Game.h" #include "SDL_error.h" +#include "TileComponent.h" -bool Map::loadMap(const char* path, int sizeX, int sizeY, Game* game /* backreference */) +void Map::loadMap(const char* path, int sizeX, int sizeY, Game* game, const std::map>* textureDict /* backreference */) { std::string tileIDstr; char singleChar = 0; @@ -18,23 +19,24 @@ bool Map::loadMap(const char* path, int sizeX, int sizeY, Game* game /* backrefe if (!mapFile.is_open()) { SDL_SetError("Error loading map: Couldn't open map file!"); - return false; + std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; + SDL_ClearError(); } int x = 0, y = 0; // needed outside for-loop for error handling - bool success = true; for (; !mapFile.eof(); mapFile.get(singleChar)) { if (singleChar == ',' || singleChar == '\n') { if (tileIDstr.empty()) continue; - game->addTile(std::stoi(tileIDstr), x * TILE_SIZE, y * TILE_SIZE); + Map::addTile(std::stoi(tileIDstr), x * TILE_SIZE, y * TILE_SIZE, game, textureDict); tileIDstr.clear(); x++; if (singleChar == '\n') { if (x != sizeX) { SDL_SetError("Error loading map: specified x size doesn't match map file!"); - success = false; + std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; + SDL_ClearError(); } x = 0; y++; @@ -47,10 +49,18 @@ bool Map::loadMap(const char* path, int sizeX, int sizeY, Game* game /* backrefe } if (y != sizeY) { SDL_SetError("Error loading map: specified y size doesn't match map file!"); - success = false; + std::cout << "ERROR: Map couldnt be loaded! " << SDL_GetError() << std::endl; + SDL_ClearError(); } mapFile.close(); - - return success; } + +void Map::addTile(unsigned long id, int x, int y, Game* game, const std::map>* textureDict) // tile entity +{ + auto& tile(game->manager.addEntity()); + tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id, textureDict); + + if(tile.getComponent().hasCollision()) tile.addComponent(tile.getComponent().getName().data()); + tile.addGroup((size_t)Entity::GroupLabel::MAPTILES); +} \ No newline at end of file diff --git a/src/TileComponent.cpp b/src/TileComponent.cpp index c6e5cec..eeebb92 100644 --- a/src/TileComponent.cpp +++ b/src/TileComponent.cpp @@ -7,7 +7,7 @@ #include "SpriteComponent.h" #include "TileComponent.h" -TileComponent::TileComponent(int x, int y, int w, int h, int id) +TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict) { this->tileRect.x = x; this->tileRect.y = y; @@ -15,12 +15,15 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id) this->tileRect.h = h; tileID = id; - auto it = textureDict.tileDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) - if (it == textureDict.tileDictionary.end()) { + auto it = textureDict->find(tileID); //every id has its own distinct texture (in texturedict.h) + if (it == textureDict->end()) { std::cout << "it end" << std::endl; return; } - this->path = it->second.data(); + + this->collision = it->second.second; + this->tileName = it->second.first; + this->path = it->second.first.data(); } void TileComponent::init()