mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 05:43:43 +00:00
feat: data and stats
- reimplemented/unhardcoded stateffects - reimplemented/unhardcoded pickupables - implemented datacomponent - some minor cleanup
This commit is contained in:
parent
0179a27aaf
commit
044d957106
26
include/DataComponent.h
Normal file
26
include/DataComponent.h
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <any>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include "Component.h"
|
||||
|
||||
class DataComponent : public Component
|
||||
{
|
||||
public:
|
||||
DataComponent() {};
|
||||
~DataComponent() {};
|
||||
template <typename T>
|
||||
void setEntry(const std::string& key, const T& value) { dataMap.insert_or_assign(key, value); }
|
||||
|
||||
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;
|
||||
};
|
||||
@ -77,7 +77,7 @@ public:
|
||||
return *c;
|
||||
};
|
||||
|
||||
template <typename T> T& getComponent() const
|
||||
template <typename T> T& getComponent() const //!< \todo: rewrite to use optionals
|
||||
{
|
||||
auto ptr(componentArray[getComponentTypeID<T>()]);
|
||||
return *static_cast<T*>(ptr);
|
||||
|
||||
@ -3,11 +3,13 @@
|
||||
#include "Constants.h"
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <functional>
|
||||
|
||||
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<void()> 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<void()> resetFunction);
|
||||
|
||||
private:
|
||||
std::array<int, MAX_STATS> buffs = { 0 };
|
||||
std::vector<StatEffect> effects = {};
|
||||
};
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -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<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
|
||||
projectile.addComponent<SpriteComponent>(texturePath, 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, std::string texturePath) {
|
||||
|
||||
auto& powerups(man->addEntity());
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#include "StatEffectsComponent.h"
|
||||
#include "Entity.h"
|
||||
#include "TransformComponent.h"
|
||||
// #include "KeyboardController.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
@ -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<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;
|
||||
}
|
||||
void StatEffectsComponent::addEffect(uint32_t duration, std::function<void()> resetFunction) {
|
||||
uint32_t startTime = SDL_GetTicks();
|
||||
effects.push_back({duration, resetFunction, startTime});
|
||||
}
|
||||
@ -9,26 +9,16 @@
|
||||
#include <cstdio>
|
||||
#include <initializer_list>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
#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<DataComponent>()
|
||||
? this->entity->getComponent<DataComponent>().getEntry<int>("speed").value_or(0)
|
||||
: 0);
|
||||
}
|
||||
|
||||
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user