From 044d957106fc7c3e396637162942c9f47d8bfeb4 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Sun, 26 Jan 2025 22:36:50 +0100 Subject: [PATCH 1/2] feat: data and stats - reimplemented/unhardcoded stateffects - reimplemented/unhardcoded pickupables - implemented datacomponent - some minor cleanup --- include/DataComponent.h | 26 ++++++++++++++++ include/Entity.h | 2 +- include/StatEffectsComponent.h | 18 +++++------- include/TransformComponent.h | 20 +++++-------- src/AssetManager.cpp | 10 ------- src/StatEffectsComponent.cpp | 54 ++++++++-------------------------- src/TransformComponent.cpp | 20 ++++--------- 7 files changed, 61 insertions(+), 89 deletions(-) create mode 100644 include/DataComponent.h diff --git a/include/DataComponent.h b/include/DataComponent.h new file mode 100644 index 0000000..8d514bf --- /dev/null +++ b/include/DataComponent.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include +#include +#include "Component.h" + +class DataComponent : public Component +{ +public: + DataComponent() {}; + ~DataComponent() {}; + template + void setEntry(const std::string& key, const T& value) { dataMap.insert_or_assign(key, value); } + + template + std::optional 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(value); + } +private: + std::map dataMap; +}; \ No newline at end of file diff --git a/include/Entity.h b/include/Entity.h index 196a105..b609bf1 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -77,7 +77,7 @@ public: return *c; }; - template T& getComponent() const + template T& getComponent() const //!< \todo: rewrite to use optionals { auto ptr(componentArray[getComponentTypeID()]); return *static_cast(ptr); diff --git a/include/StatEffectsComponent.h b/include/StatEffectsComponent.h index bc93f2b..8c438f9 100644 --- a/include/StatEffectsComponent.h +++ b/include/StatEffectsComponent.h @@ -3,11 +3,13 @@ #include "Constants.h" #include #include +#include -enum class Stats -{ - MOVEMENT_SPEED, - ATTACK_SPEED +// This acts as a manager for the lifetime of a stateffect +struct StatEffect { + uint32_t duration; + std::function resetFunction; + uint32_t startTime; }; class StatEffectsComponent : public Component{ @@ -17,12 +19,8 @@ public: void init() override; void update() override; - - void modifyStatDur(Stats stat, int duration, int value); - - void modifyStatValue(Stats stat, int modifier); - void resetStatValue(Stats stat); + void addEffect(uint32_t duration, std::function resetFunction); private: - std::array buffs = { 0 }; + std::vector effects = {}; }; \ No newline at end of file diff --git a/include/TransformComponent.h b/include/TransformComponent.h index bfe3160..6b68b45 100644 --- a/include/TransformComponent.h +++ b/include/TransformComponent.h @@ -3,6 +3,7 @@ #include "Component.h" #include "Vector2D.h" #include "Constants.h" +#include "DataComponent.h" class TransformComponent : public Component { @@ -14,21 +15,14 @@ public: int width = 32; int scale = 1; - int getSpeed() { return speed + speedMod; }; - void resetSpeedMod() { speedMod = 0; }; - - 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); + explicit TransformComponent(int scale = 1); + TransformComponent(float x, float y, int scale = 1); + TransformComponent(float x, float y, int w, int h, int scale = 1); void init() override; void update() override; - void setPositionAfterCollision(Vector2D& positionChange); - void modifySpeed(int8_t modifier); + + int getSpeed(); -private: - int speed = 3; - int speedMod = 0; + void setPositionAfterCollision(Vector2D& positionChange); }; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 25ce8ad..db3d1b8 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -46,16 +46,6 @@ Mix_Music* AssetManager::getMusic(std::string id) return music.at(id); } -void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner) { - - auto& projectile(man->addEntity()); - projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects - projectile.addComponent(texturePath, 4); - projectile.addComponent(range, speed, velocity, owner); - projectile.addComponent("projectile", 0.6f); - projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE); -} - void AssetManager::createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath) { auto& powerups(man->addEntity()); diff --git a/src/StatEffectsComponent.cpp b/src/StatEffectsComponent.cpp index c66e206..2598176 100644 --- a/src/StatEffectsComponent.cpp +++ b/src/StatEffectsComponent.cpp @@ -1,7 +1,6 @@ #include "StatEffectsComponent.h" #include "Entity.h" #include "TransformComponent.h" -// #include "KeyboardController.h" #include #include @@ -10,47 +9,20 @@ void StatEffectsComponent::init() void StatEffectsComponent::update() { - for (int i = 0; i < MAX_STATS; i++) - { - if (this->buffs.at(i) == 0) continue; - if (this->buffs.at(i) - 1 == 0) - { - this->resetStatValue((Stats)i); + uint32_t currentTime = SDL_GetTicks(); + for (auto it = effects.begin(); it != effects.end(); ) { + uint32_t elapsedTime = currentTime - it->startTime; + + if (elapsedTime >= it->duration) { + it->resetFunction(); + it = effects.erase(it); + continue; } - this->buffs.at(i) -= 1; - } + it++; + } } -void StatEffectsComponent::modifyStatDur(Stats stat, int duration, int value) -{ - 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().modifySpeed(modifier); - break; - case Stats::ATTACK_SPEED: - // this->entity->getComponent().modifyAtkSpeed(modifier); - break; - default: break; - } -} - -void StatEffectsComponent::resetStatValue(Stats stat) -{ - switch (stat) - { - case Stats::MOVEMENT_SPEED: - this->entity->getComponent().resetSpeedMod(); - break; - case Stats::ATTACK_SPEED: - // this->entity->getComponent().resetAtkSpeedMod(); - break; - default: break; - } +void StatEffectsComponent::addEffect(uint32_t duration, std::function resetFunction) { + uint32_t startTime = SDL_GetTicks(); + effects.push_back({duration, resetFunction, startTime}); } \ No newline at end of file diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 3854176..c845166 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -9,26 +9,16 @@ #include #include #include +#include #include "SoundManager.h" -TransformComponent::TransformComponent() -{ - position.zero(); -} - TransformComponent::TransformComponent(int scale) { position.zero(); 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) { this->position.x = x; @@ -65,9 +55,11 @@ void TransformComponent::update() position += positionChange; } -void TransformComponent::modifySpeed(int8_t modifier) -{ - this->speedMod += modifier; +int TransformComponent::getSpeed() +{ + return (this->entity->hasComponent() + ? this->entity->getComponent().getEntry("speed").value_or(0) + : 0); } void TransformComponent::setPositionAfterCollision(Vector2D& positionChange) From eba3cdb6c8429c9de476ed4a3aa9cced0418685e Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Tue, 28 Jan 2025 22:33:07 +0100 Subject: [PATCH 2/2] doc: add docu - datacomponent - stateffectcomponent - powerupcomponent --- include/AssetManager.h | 1 - include/DataComponent.h | 12 ++++++++++++ include/PowerupComponent.h | 4 ++++ include/StatEffectsComponent.h | 13 ++++++++++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 1d3c97a..1ba04c9 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -24,7 +24,6 @@ public: AssetManager(Manager* manager); ~AssetManager(); - void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner); void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); Vector2D calculateSpawnPosition(); diff --git a/include/DataComponent.h b/include/DataComponent.h index 8d514bf..805fd30 100644 --- a/include/DataComponent.h +++ b/include/DataComponent.h @@ -11,9 +11,21 @@ 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 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("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 std::optional getEntry(std::string key) const { if (!this->dataMap.contains(key)) return std::nullopt; diff --git a/include/PowerupComponent.h b/include/PowerupComponent.h index 53f365b..2e58880 100644 --- a/include/PowerupComponent.h +++ b/include/PowerupComponent.h @@ -6,6 +6,10 @@ class PowerupComponent : public Component { public: + /** + * @brief Construct a new Powerup Component object + * @param func The function to be called when the powerup is picked up + */ PowerupComponent(std::function func); ~PowerupComponent() {}; diff --git a/include/StatEffectsComponent.h b/include/StatEffectsComponent.h index 8c438f9..356d417 100644 --- a/include/StatEffectsComponent.h +++ b/include/StatEffectsComponent.h @@ -5,10 +5,12 @@ #include #include -// This acts as a manager for the lifetime of a stateffect +/** + * @brief Struct to hold the duration, reset function and start time of a stat effect + */ struct StatEffect { - uint32_t duration; - std::function resetFunction; + uint32_t duration; //!< Duration of the effect in milliseconds + std::function resetFunction; //!< Function to reset the effect, will be called on expiry of duration uint32_t startTime; }; @@ -19,6 +21,11 @@ public: void init() override; void update() override; + /** + * @brief Add a stat effect to the entity + * @param duration The duration of the effect in milliseconds + * @param resetFunction The function to reset the effect, will be called on expiry of duration + */ void addEffect(uint32_t duration, std::function resetFunction); private: