Syntaxe fixes

This commit is contained in:
Ulysse Cura 2024-11-29 10:41:50 +01:00
parent 6b948eaa43
commit 49bbaaca99
3 changed files with 48 additions and 15 deletions

View File

@ -13,21 +13,23 @@
#define ECS_HPP #define ECS_HPP
#include <algorithm> #include <algorithm>
#include <array> #include <array> // array
#include <bitset> #include <bitset> // bitset
#include <stdexcept> #include <stdexcept> // runtime_error
#include <iostream> #include <memory> // unique_ptr, make_unique
#include <memory> #include <vector> // vector
#include <vector> #include "../ChannelManager.hpp" // Class ChannelManager
#include "../ChannelManager.hpp"
using std::bitset, std::array, std::vector, std::unique_ptr, std::make_unique, std::move, std::forward, std::runtime_error, std::sort, std::remove_if; using std::bitset, std::array, std::vector, std::unique_ptr, std::make_unique, std::move, std::forward, std::runtime_error, std::sort, std::remove_if;
// Prédefinition des class Component et Entity
class Component; class Component;
class Entity; class Entity;
// Type pour l'ID des Components
using ComponentID = std::size_t; using ComponentID = std::size_t;
// Donne un ID différent en fonction du Component donné
inline ComponentID getComponentTypeID() inline ComponentID getComponentTypeID()
{ {
static ComponentID lastID = 0; static ComponentID lastID = 0;
@ -40,11 +42,14 @@ template<typename T> inline ComponentID getComponentTypeID() noexcept
return typeID; return typeID;
} }
// Nombre maximum de Components dans une Entity
constexpr std::size_t maxComponents = 32; constexpr std::size_t maxComponents = 32;
// Definition de types pour la contenance des Components
using ComponentBitSet = bitset<maxComponents>; using ComponentBitSet = bitset<maxComponents>;
using ComponentArray = array<Component*, maxComponents>; using ComponentArray = array<Component*, maxComponents>;
// Definition de la class Components
struct Component { struct Component {
Entity *entity; Entity *entity;
@ -55,18 +60,22 @@ struct Component {
virtual ~Component() {} virtual ~Component() {}
}; };
// Definition de la class Entity
class Entity { class Entity {
public: public:
// Met à jours les components
void update() void update()
{ {
for(auto &c : m_components) c->update(); for(auto &c : m_components) c->update();
} }
// Dessine les components
void draw() void draw()
{ {
for(auto &c : m_components) c->draw(); for(auto &c : m_components) c->draw();
} }
// Ajouter un composant à l'entité
bool isActive() const { return m_active; } bool isActive() const { return m_active; }
void destroy() { m_active = false; } void destroy() { m_active = false; }
@ -96,6 +105,7 @@ class Entity {
return *c; return *c;
} }
// Vérifier si l'entité à un composant donné
template <typename T> template <typename T>
bool hasComponent() const bool hasComponent() const
{ {
@ -117,6 +127,7 @@ class Entity {
return false; return false;
} }
// Prendre un argument en tant qu'objet
template <typename T> template <typename T>
T& getComponent() const T& getComponent() const
{ {
@ -139,6 +150,7 @@ class Entity {
throw runtime_error("Composant non trouvé.\n"); throw runtime_error("Composant non trouvé.\n");
} }
// Priorité de dessin
int draw_priority; int draw_priority;
private: private:
@ -154,7 +166,10 @@ class Manager {
public: public:
void update() void update()
{ {
for(auto &e : m_entities) e->update(); for(auto &e : m_entities)
{
e->update();
}
} }
void draw() void draw()
@ -163,9 +178,12 @@ class Manager {
for(int i {0}; i < static_cast<int>(m_entities.size()); i++) drawOrder[i] = i; for(int i {0}; i < static_cast<int>(m_entities.size()); i++) drawOrder[i] = i;
sort(drawOrder.begin(), drawOrder.end(), [this](const int &a, const int &b) { sort(drawOrder.begin(), drawOrder.end(),
return m_entities.at(a)->draw_priority < m_entities.at(b)->draw_priority; [this](const int &a, const int &b)
}); {
return m_entities.at(a)->draw_priority < m_entities.at(b)->draw_priority;
}
);
for(int i {0}; i < static_cast<int>(m_entities.size()); i++) for(int i {0}; i < static_cast<int>(m_entities.size()); i++)
{ {
@ -190,7 +208,7 @@ class Manager {
return *e; return *e;
} }
std::size_t getNumberOfEntity() const size_t getNumberOfEntities() const
{ {
return m_entities.size(); return m_entities.size();
} }

View File

@ -14,7 +14,6 @@
#include "Camera.hpp" #include "Camera.hpp"
#include "Game.hpp" #include "Game.hpp"
#include "MapManager.hpp" #include "MapManager.hpp"
#include "externLibs/nlohmann/json.hpp"
#include "TextureManager.hpp" #include "TextureManager.hpp"
#include "TileMap.hpp" #include "TileMap.hpp"
#include "Vector2D.hpp" #include "Vector2D.hpp"
@ -27,8 +26,7 @@ using json = nlohmann::json;
void TileMap::LoadTileMap(const string &path) void TileMap::LoadTileMap(const string &path)
{ {
// Load the map data from memory // Load the map data from memory
json *mapData; json *mapData = Game::mapManager.LoadMap(path);
mapData = Game::mapManager.LoadMap(path);
// Get tilemap width and height same for world // Get tilemap width and height same for world
tileMapWidth = mapData->at("TileMapWidth").get<int>(); tileMapWidth = mapData->at("TileMapWidth").get<int>();
@ -81,6 +79,8 @@ void TileMap::LoadTileMap(const string &path)
} }
} }
LoadEntities(mapData);
if(mapData->contains("PlayerInitPos")) if(mapData->contains("PlayerInitPos"))
{ {
m_playerInitPos.x = mapData->at("PlayerInitPos").at("x").get<float>() * TILE_SIZE * TILEMAP_SCALE; m_playerInitPos.x = mapData->at("PlayerInitPos").at("x").get<float>() * TILE_SIZE * TILEMAP_SCALE;
@ -98,6 +98,18 @@ void TileMap::LoadTileMap(const string &path)
m_nextMaps.emplace(it->at("Number").get<int>(), nextMap); m_nextMaps.emplace(it->at("Number").get<int>(), nextMap);
} }
// Texture renderer creating
if(Game::textureRenderer != NULL) SDL_DestroyTexture(Game::textureRenderer);
Game::textureRenderer = SDL_CreateTexture(Game::renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, worldWidth, worldHeight);
if(Game::textureRenderer == NULL)
{
cerr << "Erreur SDL_CreateTexture : " << SDL_GetError() << '\n';
throw runtime_error("Impossible de creer la texture de rendu.\n");
}
}
void TileMap::LoadEntities(json *mapData)
{
// Erase lasts entities and load news if there are some // Erase lasts entities and load news if there are some
std::size_t numberOfEntity {Game::entityManager.getNumberOfEntity()}; std::size_t numberOfEntity {Game::entityManager.getNumberOfEntity()};
for(std::size_t i {1}; i < numberOfEntity; i++) Game::entityManager.destroy(i); for(std::size_t i {1}; i < numberOfEntity; i++) Game::entityManager.destroy(i);

View File

@ -17,12 +17,14 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "externLibs/nlohmann/json.hpp"
#include "Vector2D.hpp" #include "Vector2D.hpp"
#define TILE_SIZE 16 #define TILE_SIZE 16
#define TILEMAP_SCALE 2 #define TILEMAP_SCALE 2
using std::string, std::vector, std::unordered_map; using std::string, std::vector, std::unordered_map;
using json = nlohmann::json;
using TileID = int; using TileID = int;
@ -36,6 +38,7 @@ class TileMap {
TileMap() = default; TileMap() = default;
~TileMap(); ~TileMap();
void LoadEntities(json *);
void LoadTileMap(const string &); void LoadTileMap(const string &);
void LoadNextMap(int); void LoadNextMap(int);
void LoadTileset(const string &); void LoadTileset(const string &);