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

View File

@ -14,7 +14,6 @@
#include "Camera.hpp"
#include "Game.hpp"
#include "MapManager.hpp"
#include "externLibs/nlohmann/json.hpp"
#include "TextureManager.hpp"
#include "TileMap.hpp"
#include "Vector2D.hpp"
@ -27,8 +26,7 @@ using json = nlohmann::json;
void TileMap::LoadTileMap(const string &path)
{
// Load the map data from memory
json *mapData;
mapData = Game::mapManager.LoadMap(path);
json *mapData = Game::mapManager.LoadMap(path);
// Get tilemap width and height same for world
tileMapWidth = mapData->at("TileMapWidth").get<int>();
@ -81,6 +79,8 @@ void TileMap::LoadTileMap(const string &path)
}
}
LoadEntities(mapData);
if(mapData->contains("PlayerInitPos"))
{
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);
}
// 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
std::size_t numberOfEntity {Game::entityManager.getNumberOfEntity()};
for(std::size_t i {1}; i < numberOfEntity; i++) Game::entityManager.destroy(i);

View File

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