mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
unmanaged scenes first impl
This commit is contained in:
parent
7c50c8d1fb
commit
0bfe720a4a
@ -24,8 +24,8 @@ public:
|
||||
AssetManager(Manager* manager);
|
||||
~AssetManager();
|
||||
|
||||
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner);
|
||||
void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture);
|
||||
Entity* createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner);
|
||||
Entity* createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture);
|
||||
|
||||
Vector2D calculateSpawnPosition();
|
||||
PowerupType calculateType();
|
||||
|
||||
@ -24,6 +24,7 @@ public:
|
||||
void init() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
void removeCollision();
|
||||
void addCollision();
|
||||
|
||||
void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider);
|
||||
};
|
||||
136
include/Entity.h
136
include/Entity.h
@ -5,6 +5,8 @@
|
||||
#include <bitset>
|
||||
#include <vector>
|
||||
|
||||
#include "VEGO.h"
|
||||
|
||||
#include "ColliderComponent.h"
|
||||
#include "ECS.h"
|
||||
#include "Constants.h"
|
||||
@ -35,85 +37,83 @@ class Entity
|
||||
{
|
||||
public:
|
||||
|
||||
/*!
|
||||
* \brief Used for rendering order (last is highest) or retrieving entities of group
|
||||
* \todo Label used in singular entity shouldn't use plural
|
||||
* \todo HEARTS are rendered above POWERUPS, missleading order
|
||||
* \todo PROJECTILE are rendered above POWERUPS, missleading order
|
||||
* \todo Generalize HEARTS as UI or similar
|
||||
*/
|
||||
enum class GroupLabel
|
||||
{
|
||||
MAPTILES, //!< Entity using TileComponent
|
||||
PLAYERS, //!< Primary entity in player controll
|
||||
ENEMIES, //!< \deprecated All players now grouped as Entity::PLAYERS
|
||||
COLLIDERS, //!< Fixed collider entity, e.g. a wall
|
||||
PROJECTILE, //!< \todo Document
|
||||
HEARTS, //!< \todo Document
|
||||
POWERUPS //!< \todo Document
|
||||
};
|
||||
/*!
|
||||
* \brief Used for rendering order (last is highest) or retrieving entities of group
|
||||
* \todo Label used in singular entity shouldn't use plural
|
||||
* \todo HEARTS are rendered above POWERUPS, missleading order
|
||||
* \todo PROJECTILE are rendered above POWERUPS, missleading order
|
||||
* \todo Generalize HEARTS as UI or similar
|
||||
*/
|
||||
enum class GroupLabel
|
||||
{
|
||||
MAPTILES, //!< Entity using TileComponent
|
||||
PLAYERS, //!< Primary entity in player controll
|
||||
ENEMIES, //!< \deprecated All players now grouped as Entity::PLAYERS
|
||||
COLLIDERS, //!< Fixed collider entity, e.g. a wall
|
||||
PROJECTILE, //!< \todo Document
|
||||
HEARTS, //!< \todo Document
|
||||
POWERUPS //!< \todo Document
|
||||
};
|
||||
|
||||
/*!
|
||||
* \todo Document
|
||||
*/
|
||||
explicit Entity(Manager& mManager) :
|
||||
manager(mManager) { };
|
||||
/*!
|
||||
* \todo Document
|
||||
*/
|
||||
explicit Entity(Manager& mManager) :
|
||||
manager(mManager) { };
|
||||
|
||||
void update(uint_fast16_t diffTime) const; //!< Call each frame to update all components
|
||||
void update(uint_fast16_t diffTime) const; //!< Call each frame to update all components
|
||||
|
||||
bool isActive() const { return this->active; } //!< \sa destroy()
|
||||
//! Mark for destruction for Manager::refresh() and disables collision
|
||||
//! \sa ColliderComponent
|
||||
void destroy() {
|
||||
this->active = false;
|
||||
if (this->hasComponent<ColliderComponent>()) {
|
||||
this->getComponent<ColliderComponent>().removeCollision();
|
||||
}
|
||||
}
|
||||
bool isActive() const { return this->active; } //!< \sa destroy()
|
||||
//! Mark for destruction for Manager::refresh() and disables collision
|
||||
//! \sa ColliderComponent
|
||||
void destroy();
|
||||
//! Reactivate an entity and reenables collision
|
||||
//! \sa ColliderComponent
|
||||
void reactivate();
|
||||
|
||||
bool hasGroup(Group mGroup); //!< \sa GroupLabel
|
||||
void addGroup(Group mGroup); //!< \sa GroupLabel
|
||||
void delGroup(Group mGroup); //!< \sa GroupLabel
|
||||
//! \returns bitset with true on position GroupLabel if the entity belongs to group
|
||||
//! \sa GroupLabel
|
||||
std::bitset<MAX_GROUPS> getGroupBitSet();
|
||||
bool hasGroup(Group mGroup); //!< \sa GroupLabel
|
||||
void addGroup(Group mGroup); //!< \sa GroupLabel
|
||||
void delGroup(Group mGroup); //!< \sa GroupLabel
|
||||
//! \returns bitset with true on position GroupLabel if the entity belongs to group
|
||||
//! \sa GroupLabel
|
||||
std::bitset<MAX_GROUPS> getGroupBitSet();
|
||||
|
||||
//! \sa Manager
|
||||
Manager& getManager() { return manager; };
|
||||
//! \sa Manager
|
||||
Manager& getManager() { return manager; };
|
||||
|
||||
template <typename T> bool hasComponent() const //! \sa Component
|
||||
{
|
||||
return componentBitSet[getComponentTypeID<T>()];
|
||||
}
|
||||
template <typename T> bool hasComponent() const //! \sa Component
|
||||
{
|
||||
return componentBitSet[getComponentTypeID<T>()];
|
||||
}
|
||||
|
||||
//! \brief Adds specified type as component and calls Component::init()
|
||||
//! \param mArgs Constructor arguments of component
|
||||
template <typename T, typename...TArgs> T& addComponent(TArgs&&...mArgs)
|
||||
{
|
||||
T* c(new T(std::forward<TArgs>(mArgs)...));
|
||||
c->entity = this;
|
||||
std::unique_ptr<Component> uPtr{ c };
|
||||
this->components.emplace_back(std::move(uPtr));
|
||||
//! \brief Adds specified type as component and calls Component::init()
|
||||
//! \param mArgs Constructor arguments of component
|
||||
template <typename T, typename...TArgs> T& addComponent(TArgs&&...mArgs)
|
||||
{
|
||||
T* c(new T(std::forward<TArgs>(mArgs)...));
|
||||
c->entity = this;
|
||||
std::unique_ptr<Component> uPtr{ c };
|
||||
this->components.emplace_back(std::move(uPtr));
|
||||
|
||||
componentArray[getComponentTypeID<T>()] = c;
|
||||
componentBitSet[getComponentTypeID<T>()] = true;
|
||||
componentArray[getComponentTypeID<T>()] = c;
|
||||
componentBitSet[getComponentTypeID<T>()] = true;
|
||||
|
||||
c->init();
|
||||
return *c;
|
||||
};
|
||||
c->init();
|
||||
return *c;
|
||||
};
|
||||
|
||||
template <typename T> T& getComponent() const //! \returns Component of type T
|
||||
{
|
||||
auto ptr(componentArray[getComponentTypeID<T>()]);
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
template <typename T> T& getComponent() const //! \returns Component of type T
|
||||
{
|
||||
auto ptr(componentArray[getComponentTypeID<T>()]);
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
Manager& manager;
|
||||
bool active = true;
|
||||
std::vector<std::unique_ptr<Component>> components;
|
||||
Manager& manager;
|
||||
bool active = true;
|
||||
std::vector<std::unique_ptr<Component>> components;
|
||||
|
||||
ComponentArray componentArray = {};
|
||||
ComponentBitSet componentBitSet;
|
||||
GroupBitSet groupBitSet;
|
||||
ComponentArray componentArray = {};
|
||||
ComponentBitSet componentBitSet;
|
||||
GroupBitSet groupBitSet;
|
||||
};
|
||||
@ -7,9 +7,10 @@
|
||||
#include <vector>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "Entity.h"
|
||||
|
||||
class GameInternal;
|
||||
class Entity;
|
||||
|
||||
/*!
|
||||
*
|
||||
* \brief Is responsible for managing all entities
|
||||
@ -22,24 +23,30 @@ class GameInternal;
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
Manager(GameInternal* game) : game(game) {};
|
||||
Manager(GameInternal* game) : game(game) {};
|
||||
|
||||
void update(uint_fast16_t diffTime); //!< \sa Entity::update()
|
||||
//! Disables all functionality of entities marked for destruction
|
||||
//! \sa Entity::destroy()
|
||||
void refresh();
|
||||
void update(uint_fast16_t diffTime); //!< \sa Entity::update()
|
||||
//! Disables all functionality of entities marked for destruction
|
||||
//! \sa Entity::destroy()
|
||||
void refresh();
|
||||
|
||||
void addToGroup(Entity* mEntity, Group mGroup); //!< \todo `friend` to Entity
|
||||
std::vector<Entity*>& getGroup(Group mGroup); //!< \returns std::vector containing all entities in group Entity::GroupLabel
|
||||
void addToGroup(Entity* mEntity, Group mGroup); //!< \todo `friend` to Entity
|
||||
std::vector<Entity*>& getGroup(Group mGroup); //!< \returns std::vector containing all entities in group Entity::GroupLabel
|
||||
|
||||
std::vector<Entity*> getAll(); //!< \returns std::vector containing all entities
|
||||
std::vector<Entity*> getAll(); //!< \returns std::vector containing all entities
|
||||
|
||||
Entity& addEntity(); //!< Creates and returns a new, empty entity
|
||||
Entity& addEntity(); //!< Creates and returns a new, empty entity
|
||||
|
||||
GameInternal* getGame() { return this->game; };
|
||||
GameInternal* getGame() { return this->game; };
|
||||
|
||||
std::shared_ptr<Entity> getShared(Entity* entity);
|
||||
std::weak_ptr<Entity> getWeak(Entity* entity);
|
||||
|
||||
// this function is ugly af and should not be merged
|
||||
void addExistingEntity(std::shared_ptr<Entity> entity);
|
||||
|
||||
private:
|
||||
GameInternal* game;
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
std::array<std::vector<Entity*>, MAX_GROUPS> entitiesByGroup;
|
||||
GameInternal* game;
|
||||
std::vector<std::shared_ptr<Entity>> entities;
|
||||
std::array<std::vector<Entity*>, MAX_GROUPS> entitiesByGroup;
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@ -22,6 +23,7 @@ public:
|
||||
*/
|
||||
Map(const char* path);
|
||||
void generateTiles(); //!< Generates the map based on the loaded definition
|
||||
void removeTiles(); //!< Removes all tiles previously generated
|
||||
private:
|
||||
// struct required for initialisation
|
||||
struct MapData {
|
||||
@ -42,10 +44,13 @@ private:
|
||||
|
||||
tmx::Map map;
|
||||
Map::MapData mapData;
|
||||
std::vector<std::function<void()>> tileConstructors;
|
||||
std::vector<std::function<Entity*()>> tileConstructors;
|
||||
|
||||
std::vector<Entity*> tiles;
|
||||
|
||||
void loadTileLayer(const tmx::TileLayer& layer);
|
||||
static void addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, std::string texturePath, bool hasCollision);
|
||||
// created tile is returned so it can be removed later
|
||||
static Entity* addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, std::string texturePath, bool hasCollision);
|
||||
|
||||
template<typename T>
|
||||
static std::optional<T> getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName) { return std::nullopt; };
|
||||
|
||||
63
include/Scene.h
Normal file
63
include/Scene.h
Normal file
@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Map.h"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
/**
|
||||
* @brief Adds an Entity limited to the scene
|
||||
* @details Adds an Entity, that is limited to the scene only. The entity will be destroyed with the seen, and should not be accessed afterward. For Entities that need to be reset to a specific position when loading a scene, use addInitFunc. Entities will respawn with the same parameters each time the scene is initialized.
|
||||
* @param[in] entityDefinition Function to define the entity. The function will only be called when the scene will be initialized
|
||||
*/
|
||||
void addEntity(std::function<Entity*()> entityDefinition);
|
||||
/**
|
||||
* @brief Adds an Entity limited to this cycle of the scene
|
||||
* @details Adds an Entity, that is limited to the scene only. The entity will be destroyed with the seen, and should not be accessed afterward. For Entities that need to be reset to a specific position when loading a scene, use addInitFunc. Entities will only spawn once.
|
||||
* @param[in] entityDefinition Function to define the entity. The function will only be called when the scene will be initialized
|
||||
*/
|
||||
void addTemporaryEntity(std::function<Entity*()> entityDefinition);
|
||||
/**
|
||||
* @brief Adds a function to be executed on init of the scene.
|
||||
*
|
||||
* @param[in] initFunc The initialize function
|
||||
*/
|
||||
void addInitFunc(std::function<void()> initFunc);
|
||||
/**
|
||||
* @brief Adds a function to be executed on update of the scene.
|
||||
*
|
||||
* @param[in] updateFunc The update function
|
||||
*/
|
||||
void addUpdateFunc(std::function<void(uint_fast16_t diffTime)> updateFunc);
|
||||
/**
|
||||
* @brief Adds a function to be executed on reset of the scene.
|
||||
*
|
||||
* @param[in] resetFunc The reset function
|
||||
*/
|
||||
void addResetFunc(std::function<void()> resetFunc);
|
||||
|
||||
void addMap(Map* map);
|
||||
void addMap(const char* path);
|
||||
|
||||
void init();
|
||||
void update(uint_fast16_t diffTime);
|
||||
void reset();
|
||||
|
||||
bool isActive() { return this->active; };
|
||||
private:
|
||||
std::vector<std::shared_ptr<Entity>> entities;
|
||||
std::vector<std::weak_ptr<Entity>> tempEntities;
|
||||
std::vector<std::function<Entity*()>> entityFuncs;
|
||||
std::vector<std::function<Entity*()>> tempEntityFuncs;
|
||||
std::vector<std::function<void()>> initFuncs;
|
||||
std::vector<std::function<void(uint_fast16_t diffTime)>> updateFuncs;
|
||||
std::vector<std::function<void()>> resetFuncs;
|
||||
|
||||
std::unique_ptr<Map> map_unique; // only used if map is constructed by scene
|
||||
Map* map = nullptr;
|
||||
|
||||
bool active = false;
|
||||
};
|
||||
@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "GameInternal.h"
|
||||
|
||||
namespace vego {
|
||||
extern GameInternal* game;
|
||||
extern GameInternal* game;
|
||||
}
|
||||
|
||||
inline GameInternal& VEGO_Game() {
|
||||
return *vego::game;
|
||||
return *vego::game;
|
||||
};
|
||||
@ -41,7 +41,7 @@ Mix_Music* AssetManager::getMusic(std::string id)
|
||||
return music.at(id);
|
||||
}
|
||||
|
||||
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner) {
|
||||
Entity* AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner) {
|
||||
|
||||
auto& projectile(man->addEntity());
|
||||
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
|
||||
@ -49,9 +49,11 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
|
||||
projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner);
|
||||
projectile.addComponent<ColliderComponent>("projectile", 0.6f);
|
||||
projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE);
|
||||
|
||||
return &projectile;
|
||||
}
|
||||
|
||||
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture) {
|
||||
Entity* AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture) {
|
||||
|
||||
auto& powerups(man->addEntity());
|
||||
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
|
||||
@ -66,6 +68,8 @@ void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pic
|
||||
powerups.addComponent<ColliderComponent>("powerup", 0.6f);
|
||||
powerups.addComponent<PowerupComponent>(pickupFunc);
|
||||
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
|
||||
|
||||
return &powerups;
|
||||
}
|
||||
|
||||
Vector2D AssetManager::calculateSpawnPosition()
|
||||
|
||||
@ -49,6 +49,11 @@ void ColliderComponent::removeCollision()
|
||||
this->hasCollision = false;
|
||||
}
|
||||
|
||||
void ColliderComponent::addCollision()
|
||||
{
|
||||
this->hasCollision = true;
|
||||
}
|
||||
|
||||
void ColliderComponent::handleCollision(Vector2D& entityPos, SDL_Rect& entityCollider, SDL_Rect& componentCollider)
|
||||
{
|
||||
// collision to right of character
|
||||
|
||||
@ -2,6 +2,9 @@
|
||||
|
||||
#include "Manager.h"
|
||||
#include "Component.h"
|
||||
|
||||
#include "VEGO.h"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
void Entity::update(uint_fast16_t diffTime) const
|
||||
@ -29,3 +32,23 @@ std::bitset<MAX_GROUPS> Entity::getGroupBitSet()
|
||||
{
|
||||
return groupBitSet;
|
||||
}
|
||||
|
||||
void Entity::destroy() {
|
||||
this->active = false;
|
||||
if (this->hasComponent<ColliderComponent>()) {
|
||||
this->getComponent<ColliderComponent>().removeCollision();
|
||||
}
|
||||
if (this->hasComponent<SpriteComponent>()) {
|
||||
VEGO_Game().renderManager.remove(&this->getComponent<SpriteComponent>());
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::reactivate() {
|
||||
this->active = true;
|
||||
if (this->hasComponent<ColliderComponent>()) {
|
||||
this->getComponent<ColliderComponent>().addCollision();
|
||||
}
|
||||
if (this->hasComponent<SpriteComponent>()) {
|
||||
VEGO_Game().renderManager.add(&this->getComponent<SpriteComponent>());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
#include "Manager.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
#include "Constants.h"
|
||||
@ -8,53 +12,78 @@
|
||||
|
||||
void Manager::refresh()
|
||||
{
|
||||
for (auto i(0u); i < MAX_GROUPS; i++)
|
||||
{
|
||||
auto& v(entitiesByGroup[i]);
|
||||
v.erase(
|
||||
std::remove_if(std::begin(v), std::end(v),
|
||||
[i](Entity* mEntity)
|
||||
{
|
||||
return !mEntity->isActive() || !mEntity->hasGroup(i);
|
||||
}), std::end(v));
|
||||
}
|
||||
for (auto i(0u); i < MAX_GROUPS; i++)
|
||||
{
|
||||
auto& v(entitiesByGroup[i]);
|
||||
v.erase(
|
||||
std::remove_if(std::begin(v), std::end(v),
|
||||
[i](Entity* mEntity)
|
||||
{
|
||||
return !mEntity->isActive() || !mEntity->hasGroup(i);
|
||||
}), std::end(v));
|
||||
}
|
||||
|
||||
entities.erase(std::remove_if(std::begin(entities), std::end(entities),
|
||||
[](const std::unique_ptr<Entity>& mEntity)
|
||||
{
|
||||
return !mEntity->isActive();
|
||||
}),
|
||||
std::end(entities));
|
||||
entities.erase(std::remove_if(std::begin(entities), std::end(entities),
|
||||
[](const std::shared_ptr<Entity>& mEntity)
|
||||
{
|
||||
return !mEntity->isActive();
|
||||
}),
|
||||
std::end(entities));
|
||||
}
|
||||
|
||||
void Manager::update(uint_fast16_t diffTime)
|
||||
{
|
||||
for (auto& e : entities) e->update(diffTime);
|
||||
for (auto& e : entities) e->update(diffTime);
|
||||
}
|
||||
|
||||
void Manager::addToGroup(Entity* mEntity, Group mGroup)
|
||||
{
|
||||
entitiesByGroup.at(mGroup).emplace_back(mEntity);
|
||||
entitiesByGroup.at(mGroup).emplace_back(mEntity);
|
||||
}
|
||||
|
||||
std::vector<Entity*>& Manager::getGroup(Group mGroup)
|
||||
{
|
||||
return entitiesByGroup.at(mGroup);
|
||||
return entitiesByGroup.at(mGroup);
|
||||
}
|
||||
|
||||
std::vector<Entity*> Manager::getAll()
|
||||
{
|
||||
std::vector<Entity*> entity_vec;
|
||||
for (auto& entity_ptr : entities) {
|
||||
entity_vec.emplace_back(entity_ptr.get());
|
||||
}
|
||||
return entity_vec;
|
||||
std::vector<Entity*> entity_vec;
|
||||
for (auto& entity_ptr : entities) {
|
||||
entity_vec.emplace_back(entity_ptr.get());
|
||||
}
|
||||
return entity_vec;
|
||||
}
|
||||
|
||||
Entity& Manager::addEntity()
|
||||
{
|
||||
Entity* e = new Entity(*this);
|
||||
std::unique_ptr<Entity> uPtr{ e };
|
||||
entities.emplace_back(std::move(uPtr));
|
||||
return *e;
|
||||
Entity* e = new Entity(*this);
|
||||
entities.emplace_back(e);
|
||||
return *e;
|
||||
}
|
||||
|
||||
void Manager::addExistingEntity(std::shared_ptr<Entity> entity)
|
||||
{
|
||||
entities.emplace_back(entity);
|
||||
std::ranges::for_each(std::views::iota(0ul, MAX_GROUPS), [&entity, this](auto i) {
|
||||
if (entity->hasGroup(i)) {
|
||||
this->entitiesByGroup.at(i).emplace_back(entity.get());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::shared_ptr<Entity> Manager::getShared(Entity* entity)
|
||||
{
|
||||
auto entityPtr = std::find_if(std::begin(entities), std::end(entities), [&entity](const auto& entityPtr) {
|
||||
return entity == entityPtr.get();
|
||||
});
|
||||
return std::shared_ptr<Entity>(*entityPtr); // careful, error if find_if returns end
|
||||
}
|
||||
|
||||
std::weak_ptr<Entity> Manager::getWeak(Entity* entity)
|
||||
{
|
||||
auto entityPtr = std::find_if(std::begin(entities), std::end(entities), [&entity](const auto& entityPtr) {
|
||||
return entity == entityPtr.get();
|
||||
});
|
||||
return *entityPtr; // careful, error if find_if returns end
|
||||
}
|
||||
20
src/Map.cpp
20
src/Map.cpp
@ -20,6 +20,7 @@
|
||||
#include <tmxlite/Types.hpp>
|
||||
|
||||
#include "ColliderComponent.h"
|
||||
#include "Entity.h"
|
||||
#include "GameInternal.h"
|
||||
#include "SpriteComponent.h"
|
||||
#include "TextureManager.h"
|
||||
@ -148,9 +149,9 @@ void Map::loadTileLayer(const tmx::TileLayer& layer)
|
||||
const float tilePosY = (static_cast<float>(y) * this->mapData.mapTileSize->y);
|
||||
|
||||
// return tile data as a function to spawn said tile
|
||||
return std::function<void()>(
|
||||
return std::function<Entity*()>(
|
||||
[tilePosX, tilePosY, capture0 = *this->mapData.mapTileSize, u, v, zIndex, capture1 = data.texturePath, collision] {
|
||||
Map::addTile(tilePosX, tilePosY, capture0, u, v, zIndex, capture1, collision);
|
||||
return Map::addTile(tilePosX, tilePosY, capture0, u, v, zIndex, capture1, collision);
|
||||
}
|
||||
);
|
||||
});
|
||||
@ -162,7 +163,7 @@ void Map::loadTileLayer(const tmx::TileLayer& layer)
|
||||
this->tileConstructors.insert(this->tileConstructors.end(), tileConstructorRange.begin(), tileConstructorRange.end());
|
||||
}
|
||||
|
||||
void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, std::string texturePath, bool hasCollision)
|
||||
Entity* Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, std::string texturePath, bool hasCollision)
|
||||
{
|
||||
auto& tile(VEGO_Game().manager.addEntity());
|
||||
|
||||
@ -175,11 +176,20 @@ void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int
|
||||
tile.addComponent<ColliderComponent>("hello I am a collider of a tile!");
|
||||
tile.addGroup((size_t)Entity::GroupLabel::MAPTILES);
|
||||
}
|
||||
|
||||
return &tile;
|
||||
}
|
||||
|
||||
void Map::generateTiles()
|
||||
{
|
||||
std::ranges::for_each(this->tileConstructors, [](auto& function) {
|
||||
function();
|
||||
this->tiles = std::views::transform(this->tileConstructors, [](auto& function) {
|
||||
return function();
|
||||
}) | std::ranges::to<std::vector<Entity*>>();
|
||||
}
|
||||
|
||||
void Map::removeTiles()
|
||||
{
|
||||
std::ranges::for_each(this->tiles, [](const auto& tile) {
|
||||
tile->destroy();
|
||||
});
|
||||
}
|
||||
102
src/Scene.cpp
Normal file
102
src/Scene.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "Scene.h"
|
||||
#include "Entity.h"
|
||||
#include "Map.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
#include <VEGO.h>
|
||||
|
||||
void Scene::addEntity(std::function<Entity*()> entityDefinition)
|
||||
{
|
||||
this->entityFuncs.push_back(entityDefinition);
|
||||
|
||||
if (this->active) {
|
||||
this->entities.emplace_back(VEGO_Game().manager.getWeak(entityDefinition()));
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::addTemporaryEntity(std::function<Entity*()> entityDefinition)
|
||||
{
|
||||
if (this->active) {
|
||||
this->tempEntities.push_back(VEGO_Game().manager.getWeak(entityDefinition()));
|
||||
return;
|
||||
}
|
||||
|
||||
this->tempEntityFuncs.push_back(entityDefinition);
|
||||
}
|
||||
|
||||
void Scene::addInitFunc(std::function<void()> initFunc)
|
||||
{
|
||||
this->initFuncs.push_back(initFunc);
|
||||
}
|
||||
|
||||
void Scene::addUpdateFunc(std::function<void(uint_fast16_t diffTime)> updateFunc)
|
||||
{
|
||||
this->updateFuncs.push_back(updateFunc);
|
||||
}
|
||||
|
||||
void Scene::addResetFunc(std::function<void()> resetFunc)
|
||||
{
|
||||
this->resetFuncs.push_back(resetFunc);
|
||||
}
|
||||
|
||||
void Scene::init()
|
||||
{
|
||||
this->map->generateTiles();
|
||||
|
||||
std::ranges::for_each(this->initFuncs, [](const auto& func) {
|
||||
func();
|
||||
});
|
||||
std::ranges::for_each(this->entities, [](auto entity) {
|
||||
entity->reactivate();
|
||||
VEGO_Game().manager.addExistingEntity(entity);
|
||||
});
|
||||
std::ranges::for_each(this->entityFuncs, [this](const auto& func) {
|
||||
this->entities.emplace_back(VEGO_Game().manager.getShared(func()));
|
||||
});
|
||||
std::ranges::for_each(this->tempEntityFuncs, [this](const auto& func) {
|
||||
this->tempEntities.emplace_back(VEGO_Game().manager.getWeak(func()));
|
||||
});
|
||||
|
||||
this->active = true;
|
||||
}
|
||||
|
||||
void Scene::update(uint_fast16_t diffTime)
|
||||
{
|
||||
std::ranges::for_each(this->updateFuncs, [diffTime](const auto& func) {
|
||||
func(diffTime);
|
||||
});
|
||||
}
|
||||
|
||||
void Scene::reset()
|
||||
{
|
||||
this->active = false;
|
||||
|
||||
std::ranges::for_each(this->entities, [](const auto& entity) {
|
||||
entity->destroy();
|
||||
});
|
||||
std::ranges::for_each(this->tempEntities, [](const auto& entity) {
|
||||
if (!entity.expired())
|
||||
entity.lock()->destroy();
|
||||
});
|
||||
this->tempEntities.clear();
|
||||
std::ranges::for_each(this->resetFuncs, [](const auto& func) {
|
||||
func();
|
||||
});
|
||||
|
||||
if (this->map) {
|
||||
this->map->removeTiles();
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::addMap(Map* map)
|
||||
{
|
||||
this->map = map;
|
||||
}
|
||||
|
||||
void Scene::addMap(const char* path)
|
||||
{
|
||||
this->map_unique = std::unique_ptr<Map>(new Map(path));
|
||||
this->addMap(this->map_unique.get());
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user