0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 10:13:42 +00:00

ref(Healthcomp, Stateffects)

This commit is contained in:
Nimac0 2024-05-02 23:50:28 +02:00
parent 737fd70851
commit 1f27d24de3
10 changed files with 47 additions and 19 deletions

View File

@ -24,7 +24,3 @@ constexpr int MAP_SIZE_Y = 20;
constexpr int SPAWN_ATTEMPTS = 20; constexpr int SPAWN_ATTEMPTS = 20;
constexpr int BUFF_DURATION = 240;
constexpr int BUFF_VALUE = 1;

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <string>
#include "Direction.h" #include "Direction.h"
#include "Component.h" #include "Component.h"
@ -9,7 +10,7 @@ class HealthComponent : public Component
{ {
public: public:
HealthComponent(int health, Direction side) : health(health), side(side) {} HealthComponent(int health, Direction side, std::string healthTexture) : health(health), side(side), healthTexture(healthTexture) {}
~HealthComponent() {} ~HealthComponent() {}
void modifyHealth(int health = -1); void modifyHealth(int health = -1);
@ -26,4 +27,5 @@ private:
int health; int health;
Direction side; Direction side;
std::string healthTexture;
}; };

View File

@ -24,7 +24,6 @@ public:
//for attack cooldown in between shots //for attack cooldown in between shots
uint32_t lastFireTime = 0; uint32_t lastFireTime = 0;
uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
KeyboardController() = default; KeyboardController() = default;
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity); KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
@ -33,7 +32,10 @@ public:
void init() override; void init() override;
void update() override; void update() override;
uint32_t getFireCooldown() { return this->fireCooldown + this->fireCooldownMod > 0 ? this->fireCooldown + this->fireCooldownMod : 0; };
void modifyAtkSpeed(int8_t modifier); void modifyAtkSpeed(int8_t modifier);
void resetAtkSpeedMod();
private: private:
//for creation of projectiles //for creation of projectiles
@ -41,4 +43,7 @@ private:
Vector2D fireVelocity; //decide source of projectile and flying direction Vector2D fireVelocity; //decide source of projectile and flying direction
// SoundManager* soundEffect = Game::assets->getSound; // SoundManager* soundEffect = Game::assets->getSound;
//SoundManager* soundEffect = new SoundManager(); //SoundManager* soundEffect = new SoundManager();
uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
int32_t fireCooldownMod = 0;
}; };

View File

@ -18,9 +18,10 @@ public:
void init() override; void init() override;
void update() override; void update() override;
void modifyStatDur(Stats stat, int duration); void modifyStatDur(Stats stat, int duration, int value);
void modifyStatValue(Stats stat, int modifier); void modifyStatValue(Stats stat, int modifier);
void resetStatValue(Stats stat);
private: private:
std::array<int, MAX_STATS> buffs = { 0 }; std::array<int, MAX_STATS> buffs = { 0 };

View File

@ -14,7 +14,8 @@ public:
int width = 32; int width = 32;
int scale = 1; int scale = 1;
int speed = 3; int getSpeed() { return speed + speedMod; };
void resetSpeedMod() { speedMod = 0; };
TransformComponent(); TransformComponent();
explicit TransformComponent(int scale); explicit TransformComponent(int scale);
@ -26,4 +27,8 @@ public:
void update() override; void update() override;
void setPositionAfterCollision(Vector2D& positionChange); void setPositionAfterCollision(Vector2D& positionChange);
void modifySpeed(int8_t modifier); void modifySpeed(int8_t modifier);
private:
int speed = 3;
int speedMod = 0;
}; };

View File

@ -163,7 +163,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag) player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
player1.addComponent<HealthComponent>(5, Direction::LEFT); player1.addComponent<HealthComponent>(5, Direction::LEFT, "assets/heart.png");
player1.addComponent<StatEffectsComponent>(); player1.addComponent<StatEffectsComponent>();
player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order player1.addGroup((size_t) Entity::GroupLabel::PLAYERS); //tell programm what group it belongs to for rendering order
@ -173,7 +173,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
player2.addComponent<SpriteComponent>(player2Sprite, true); player2.addComponent<SpriteComponent>(player2Sprite, true);
player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
player2.addComponent<ColliderComponent>("enemy", 0.8f); player2.addComponent<ColliderComponent>("enemy", 0.8f);
player2.addComponent<HealthComponent>(5, Direction::RIGHT); player2.addComponent<HealthComponent>(5, Direction::RIGHT, "assets/heart.png");
player2.addComponent<StatEffectsComponent>(); player2.addComponent<StatEffectsComponent>();
player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
} }

View File

@ -58,7 +58,7 @@ void HealthComponent::createHeartComponents(int x)
{ {
auto& heart(this->entity->getManager().addEntity()); auto& heart(this->entity->getManager().addEntity());
heart.addComponent<TransformComponent>(x,5,2); heart.addComponent<TransformComponent>(x,5,2);
heart.addComponent<SpriteComponent>("assets/heart.png"); heart.addComponent<SpriteComponent>(this->healthTexture.data());
heart.addGroup((size_t)Entity::GroupLabel::HEARTS); heart.addGroup((size_t)Entity::GroupLabel::HEARTS);
heart.setTeam(this->entity->getTeam()); heart.setTeam(this->entity->getTeam());
} }

View File

@ -54,7 +54,7 @@ void KeyboardController::update()
Uint32 currentTicks = SDL_GetTicks(); Uint32 currentTicks = SDL_GetTicks();
if (currentTicks - lastFireTime >= fireCooldown) { if (currentTicks - lastFireTime >= this->getFireCooldown()) {
player = &entity->getComponent<TransformComponent>(); player = &entity->getComponent<TransformComponent>();
@ -78,5 +78,10 @@ void KeyboardController::update()
void KeyboardController::modifyAtkSpeed(int8_t modifier) void KeyboardController::modifyAtkSpeed(int8_t modifier)
{ {
this->fireCooldown -= modifier * 400; this->fireCooldownMod -= modifier * 400;
}
void KeyboardController::resetAtkSpeedMod()
{
this->fireCooldownMod = 0;
} }

View File

@ -15,15 +15,15 @@ void StatEffectsComponent::update()
if (this->buffs.at(i) == 0) continue; if (this->buffs.at(i) == 0) continue;
if (this->buffs.at(i) - 1 == 0) if (this->buffs.at(i) - 1 == 0)
{ {
this->modifyStatValue((Stats)i, BUFF_VALUE * -1); this->resetStatValue((Stats)i);
} }
this->buffs.at(i) -= 1; this->buffs.at(i) -= 1;
} }
} }
void StatEffectsComponent::modifyStatDur(Stats stat, int duration) void StatEffectsComponent::modifyStatDur(Stats stat, int duration, int value)
{ {
if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, BUFF_VALUE); if(this->buffs.at((uint8_t)stat) == 0) this->modifyStatValue(stat, value);
this->buffs.at((uint8_t)stat) += duration; this->buffs.at((uint8_t)stat) += duration;
} }
@ -39,4 +39,18 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier
break; break;
default: 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

@ -56,8 +56,8 @@ void TransformComponent::update()
float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45° float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45°
Vector2D positionChange( Vector2D positionChange(
direction.x * speed * multiplier, direction.x * this->getSpeed() * multiplier,
direction.y * speed * multiplier direction.y * this->getSpeed() * multiplier
); );
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){ if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
@ -69,7 +69,7 @@ void TransformComponent::update()
void TransformComponent::modifySpeed(int8_t modifier) void TransformComponent::modifySpeed(int8_t modifier)
{ {
this->speed += modifier; this->speedMod += modifier;
} }
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange) void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)