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

Merge branch 'data' into dev

This commit is contained in:
Nimac0 2025-01-28 22:43:18 +01:00
commit 1b795c3732
9 changed files with 80 additions and 87 deletions

View File

@ -24,7 +24,6 @@ public:
AssetManager(Manager* manager); AssetManager(Manager* manager);
~AssetManager(); ~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); void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture);
Vector2D calculateSpawnPosition(); Vector2D calculateSpawnPosition();

38
include/DataComponent.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include <map>
#include <any>
#include <string>
#include <optional>
#include "Component.h"
class DataComponent : public Component
{
public:
DataComponent() {};
~DataComponent() {};
/**
* @brief Set a key-value pair of any type in the data map
* @details e.g. setEntry("speed", 180); in this case the key is "speed" and the value is set to an integer of 180
* @param key The name to store the value under
* @param value The value to store of type T
*/
template <typename T>
void setEntry(const std::string& key, const T& value) { dataMap.insert_or_assign(key, value); }
/**
* @brief Get a value of type T from the data map
* @details e.g. getEntry<int>("speed"); in this case the key is "speed" and the value is returned as an integer
* @param key The name to retrieve the value from
* @return An optional of type T containing the value if it exists and matches in typeid, otherwise std::nullopt
*/
template<typename T>
std::optional<T> getEntry(std::string key) const {
if (!this->dataMap.contains(key)) return std::nullopt;
const std::any& value = this->dataMap.at(key);
if (value.type() != typeid(T)) { return std::nullopt; }
return std::any_cast<T>(value);
}
private:
std::map<std::string, std::any> dataMap;
};

View File

@ -102,7 +102,7 @@ public:
return *c; return *c;
}; };
template <typename T> T& getComponent() const //! \returns Component of type T template <typename T> T& getComponent() const //!< \todo: rewrite to use optionals
{ {
auto ptr(componentArray[getComponentTypeID<T>()]); auto ptr(componentArray[getComponentTypeID<T>()]);
return *static_cast<T*>(ptr); return *static_cast<T*>(ptr);

View File

@ -6,6 +6,10 @@
class PowerupComponent : public Component class PowerupComponent : public Component
{ {
public: public:
/**
* @brief Construct a new Powerup Component object
* @param func The function to be called when the powerup is picked up
*/
PowerupComponent(std::function<void (Entity*)> func); PowerupComponent(std::function<void (Entity*)> func);
~PowerupComponent() {}; ~PowerupComponent() {};

View File

@ -3,11 +3,15 @@
#include "Constants.h" #include "Constants.h"
#include <cstdint> #include <cstdint>
#include <array> #include <array>
#include <functional>
enum class Stats /**
{ * @brief Struct to hold the duration, reset function and start time of a stat effect
MOVEMENT_SPEED, */
ATTACK_SPEED struct StatEffect {
uint32_t duration; //!< Duration of the effect in milliseconds
std::function<void()> resetFunction; //!< Function to reset the effect, will be called on expiry of duration
uint32_t startTime;
}; };
class StatEffectsComponent : public Component{ class StatEffectsComponent : public Component{
@ -17,12 +21,13 @@ public:
void init() override; void init() override;
void update(uint_fast16_t diffTime) override; void update(uint_fast16_t diffTime) override;
/**
void modifyStatDur(Stats stat, int duration, int value); * @brief Add a stat effect to the entity
* @param duration The duration of the effect in milliseconds
void modifyStatValue(Stats stat, int modifier); * @param resetFunction The function to reset the effect, will be called on expiry of duration
void resetStatValue(Stats stat); */
void addEffect(uint32_t duration, std::function<void()> resetFunction);
private: private:
std::array<int, MAX_STATS> buffs = { 0 }; std::vector<StatEffect> effects = {};
}; };

View File

@ -3,6 +3,7 @@
#include "Component.h" #include "Component.h"
#include "Vector2D.h" #include "Vector2D.h"
#include "Constants.h" #include "Constants.h"
#include "DataComponent.h"
class TransformComponent : public Component class TransformComponent : public Component
{ {
@ -14,22 +15,14 @@ public:
int width = 32; int width = 32;
int scale = 1; int scale = 1;
int getSpeed() { return speed + speedMod; }; explicit TransformComponent(int scale = 1);
void resetSpeedMod() { speedMod = 0; }; TransformComponent(float x, float y, int scale = 1);
TransformComponent(float x, float y, int w, int h, int scale = 1);
TransformComponent();
explicit TransformComponent(int scale);
TransformComponent(float x, float y);
TransformComponent(float x, float y, int scale);
TransformComponent(float x, float y, int w, int h, int scale);
void init() override; void init() override;
/*! TODO: document usage of collision handler */ /*! TODO: document usage of collision handler */
void update(uint_fast16_t diffTime) override; void update(uint_fast16_t diffTime) override;
void setPositionAfterCollision(Vector2D& positionChange); void setPositionAfterCollision(Vector2D& positionChange);
void modifySpeed(int8_t modifier); int getSpeed();
private:
int speed = 180;
int speedMod = 0;
}; };

View File

@ -42,16 +42,6 @@ Mix_Music* AssetManager::getMusic(std::string id)
return music.at(id); return music.at(id);
} }
void 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
projectile.addComponent<SpriteComponent>(textureEnum, 4);
projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner);
projectile.addComponent<ColliderComponent>("projectile", 0.6f);
projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE);
}
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture) { void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture) {
auto& powerups(man->addEntity()); auto& powerups(man->addEntity());

View File

@ -1,7 +1,6 @@
#include "StatEffectsComponent.h" #include "StatEffectsComponent.h"
#include "Entity.h" #include "Entity.h"
#include "TransformComponent.h" #include "TransformComponent.h"
// #include "KeyboardController.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -10,45 +9,18 @@ void StatEffectsComponent::init()
void StatEffectsComponent::update(uint_fast16_t diffTime) void StatEffectsComponent::update(uint_fast16_t diffTime)
{ {
for (int i = 0; i < MAX_STATS; i++) for (auto it = effects.begin(); it != effects.end(); ) {
{ it->duration -= diffTime;
this->buffs.at(i) -= diffTime;
if (this->buffs.at(i) <= 0) { if (it->duration <= 0) {
this->resetStatValue((Stats)i); it->resetFunction();
it = effects.erase(it);
continue;
} }
} it++;
}
} }
void StatEffectsComponent::modifyStatDur(Stats stat, int duration, int value) void StatEffectsComponent::addEffect(uint32_t duration, std::function<void()> resetFunction) {
{ effects.push_back({duration, resetFunction});
if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, value);
this->buffs.at((uint8_t)stat) += duration;
}
void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier is basically there so the modifyfuncs in the components know if stats should be increased or decreased
{
switch (stat)
{
case Stats::MOVEMENT_SPEED:
this->entity->getComponent<TransformComponent>().modifySpeed(modifier);
break;
case Stats::ATTACK_SPEED:
// this->entity->getComponent<KeyboardController>().modifyAtkSpeed(modifier);
break;
default: break;
}
}
void StatEffectsComponent::resetStatValue(Stats stat)
{
switch (stat)
{
case Stats::MOVEMENT_SPEED:
this->entity->getComponent<TransformComponent>().resetSpeedMod();
break;
case Stats::ATTACK_SPEED:
// this->entity->getComponent<KeyboardController>().resetAtkSpeedMod();
break;
default: break;
}
} }

View File

@ -9,26 +9,16 @@
#include <cstdio> #include <cstdio>
#include <initializer_list> #include <initializer_list>
#include <iostream> #include <iostream>
#include <optional>
#include "SoundManager.h" #include "SoundManager.h"
TransformComponent::TransformComponent()
{
position.zero();
}
TransformComponent::TransformComponent(int scale) TransformComponent::TransformComponent(int scale)
{ {
position.zero(); position.zero();
this->scale = scale; this->scale = scale;
} }
TransformComponent::TransformComponent(float x, float y)
{
this->position.x = x;
this->position.y = y;
}
TransformComponent::TransformComponent(float x, float y, int scale) TransformComponent::TransformComponent(float x, float y, int scale)
{ {
this->position.x = x; this->position.x = x;
@ -65,9 +55,11 @@ void TransformComponent::update(uint_fast16_t diffTime)
position += positionChange; position += positionChange;
} }
void TransformComponent::modifySpeed(int8_t modifier) int TransformComponent::getSpeed()
{ {
this->speedMod += modifier; return (this->entity->hasComponent<DataComponent>()
? this->entity->getComponent<DataComponent>().getEntry<int>("speed").value_or(0)
: 0);
} }
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange) void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)