0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

ref(addTile): adding tiles for the tilemap is now on the dev side

This commit is contained in:
Nimac0 2024-05-02 21:50:22 +02:00
parent e3852379e4
commit 737fd70851
8 changed files with 40 additions and 55 deletions

View File

@ -40,7 +40,6 @@ public:
void clean(); void clean();
bool running() const; bool running() const;
void addTile(unsigned long id, int x, int y);
/* static */ SDL_Renderer* renderer = nullptr; /* static */ SDL_Renderer* renderer = nullptr;
/* static */ SDL_Event event; /* static */ SDL_Event event;
/* static */ CollisionHandler* collisionHandler; /* static */ CollisionHandler* collisionHandler;

View File

@ -1,5 +1,8 @@
#pragma once #pragma once
#include <map>
#include <string>
class Game; class Game;
class Map class Map
{ {
@ -18,5 +21,6 @@ public:
* \return Boolean for success * \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<int, std::pair<std::string, bool>>* textureDict /* backreference */);
static void addTile(unsigned long id, int x, int y, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict);
}; };

View File

@ -1,24 +0,0 @@
#pragma once
#include <map>
#include <string>
#include "AssetManager.h"
class TextureDict
{
public:
const std::map<int, std::string> 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<PowerupType, std::string> powerupDictionary = {
{PowerupType::HEART, "assets/heart_powerup.png"},
{PowerupType::WALKINGSPEED, "assets/movement_speed_powerup.png"},
{PowerupType::SHOOTINGSPEED, "assets/atk_speed_powerup.png"}
};
};

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <SDL.h> #include <SDL.h>
#include <string>
#include <map>
#include "Component.h" #include "Component.h"
#include "TextureDict.h"
class SpriteComponent; class SpriteComponent;
class TransformComponent; class TransformComponent;
@ -13,15 +14,20 @@ class TileComponent : public Component
public: public:
TransformComponent* transform; TransformComponent* transform;
SpriteComponent* sprite; SpriteComponent* sprite;
TextureDict textureDict;
SDL_Rect tileRect; SDL_Rect tileRect;
int tileID; int tileID;
const char* path; const char* path;
TileComponent() = default; 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<int, std::pair<std::string, bool>>* textureDict);
~TileComponent() = default; ~TileComponent() = default;
void init() override; void init() override;
bool hasCollision(){return this->collision;}
std::string getName(){return this->tileName;}
private:
bool collision;
std::string tileName;
}; };

View File

@ -49,7 +49,6 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
} }
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) { void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) {
TextureDict textureDict;
auto& powerups(man->addEntity()); auto& powerups(man->addEntity());
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects

View File

@ -143,10 +143,6 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
if (this->isRunning == false) return; if (this->isRunning == false) return;
map = new Map(); 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 //adding textures to the library in AssetManager
@ -330,14 +326,6 @@ void Game::clean()
std::cout << "Game Cleaned!" << std::endl; std::cout << "Game Cleaned!" << std::endl;
} }
void Game::addTile(unsigned long id, int x, int y) // tile entity
{
auto& tile(manager.addEntity());
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id);
if (id == 1) tile.addComponent<ColliderComponent>("water");
tile.addGroup((size_t)Entity::GroupLabel::MAPTILES);
}
bool Game::running() const bool Game::running() const
{ {
return isRunning; return isRunning;

View File

@ -3,13 +3,14 @@
#include <cctype> #include <cctype>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <utility>
#include "Constants.h" #include "Constants.h"
#include "Game.h" #include "Game.h"
#include "SDL_error.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<int, std::pair<std::string, bool>>* textureDict /* backreference */)
{ {
std::string tileIDstr; std::string tileIDstr;
char singleChar = 0; char singleChar = 0;
@ -18,23 +19,24 @@ bool Map::loadMap(const char* path, int sizeX, int sizeY, Game* game /* backrefe
if (!mapFile.is_open()) { if (!mapFile.is_open()) {
SDL_SetError("Error loading map: Couldn't open map file!"); 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 int x = 0, y = 0; // needed outside for-loop for error handling
bool success = true;
for (; !mapFile.eof(); mapFile.get(singleChar)) for (; !mapFile.eof(); mapFile.get(singleChar))
{ {
if (singleChar == ',' || singleChar == '\n') { if (singleChar == ',' || singleChar == '\n') {
if (tileIDstr.empty()) if (tileIDstr.empty())
continue; 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(); tileIDstr.clear();
x++; x++;
if (singleChar == '\n') { if (singleChar == '\n') {
if (x != sizeX) { if (x != sizeX) {
SDL_SetError("Error loading map: specified x size doesn't match map file!"); 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; x = 0;
y++; y++;
@ -47,10 +49,18 @@ bool Map::loadMap(const char* path, int sizeX, int sizeY, Game* game /* backrefe
} }
if (y != sizeY) { if (y != sizeY) {
SDL_SetError("Error loading map: specified y size doesn't match map file!"); 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(); mapFile.close();
return success;
} }
void Map::addTile(unsigned long id, int x, int y, Game* game, const std::map<int, std::pair<std::string, bool>>* textureDict) // tile entity
{
auto& tile(game->manager.addEntity());
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id, textureDict);
if(tile.getComponent<TileComponent>().hasCollision()) tile.addComponent<ColliderComponent>(tile.getComponent<TileComponent>().getName().data());
tile.addGroup((size_t)Entity::GroupLabel::MAPTILES);
}

View File

@ -7,7 +7,7 @@
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "TileComponent.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<int, std::pair<std::string, bool>>* textureDict)
{ {
this->tileRect.x = x; this->tileRect.x = x;
this->tileRect.y = y; this->tileRect.y = y;
@ -15,12 +15,15 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id)
this->tileRect.h = h; this->tileRect.h = h;
tileID = id; tileID = id;
auto it = textureDict.tileDictionary.find(tileID); //every id has its own distinct texture (in texturedict.h) auto it = textureDict->find(tileID); //every id has its own distinct texture (in texturedict.h)
if (it == textureDict.tileDictionary.end()) { if (it == textureDict->end()) {
std::cout << "it end" << std::endl; std::cout << "it end" << std::endl;
return; 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() void TileComponent::init()