0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-13 14:53:41 +00:00

ref(Powerups): move powerup adding functionality to gamedev side

This commit is contained in:
Nimac0 2024-04-30 01:20:40 +02:00
parent cffe1bdc31
commit e3852379e4
4 changed files with 11 additions and 46 deletions

View File

@ -3,6 +3,7 @@
#include <SDL_mixer.h> #include <SDL_mixer.h>
#include <map> #include <map>
#include <string> #include <string>
#include <functional>
#include "Entity.h" #include "Entity.h"
@ -24,7 +25,7 @@ public:
~AssetManager(); ~AssetManager();
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel); void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel);
void createPowerup(Vector2D pos, PowerupType type); void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath);
Vector2D calculateSpawnPosition(); Vector2D calculateSpawnPosition();
PowerupType calculateType(); PowerupType calculateType();

View File

@ -1,19 +1,16 @@
#pragma once #pragma once
#include <functional>
#include "Component.h" #include "Component.h"
#include "AssetManager.h"
class PowerupComponent : public Component class PowerupComponent : public Component
{ {
public: public:
PowerupComponent(PowerupType type); PowerupComponent(std::function<void (Entity*)> func);
~PowerupComponent() {}; ~PowerupComponent() {};
void update() override; void update() override;
void heartEffect(Entity* player);
void movementSpeedEffect(Entity* player);
void atkSpeedEffect(Entity* player);
private: private:
void (PowerupComponent::*pickupFunc)(Entity* player); std::function<void (Entity*)> pickupFunc;
}; };

View File

@ -48,25 +48,21 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
projectile.setTeam(teamLabel); projectile.setTeam(teamLabel);
} }
void AssetManager::createPowerup(Vector2D pos, PowerupType type) { void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) {
TextureDict textureDict; TextureDict textureDict;
auto& powerups(man->addEntity()); auto& powerups(man->addEntity());
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
auto it = textureDict.powerupDictionary.find(type);
if (it == textureDict.powerupDictionary.end()) {
std::cout << "it end" << std::endl;
}
try { try {
powerups.addComponent<SpriteComponent>(it->second.data()); powerups.addComponent<SpriteComponent>(texturePath.c_str());
} }
catch (std::runtime_error e) { catch (std::runtime_error e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
} }
powerups.addComponent<ColliderComponent>("powerup", 0.6f); powerups.addComponent<ColliderComponent>("powerup", 0.6f);
powerups.addComponent<PowerupComponent>(type); powerups.addComponent<PowerupComponent>(pickupFunc);
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS); powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
} }

View File

@ -7,22 +7,9 @@
#include "Constants.h" #include "Constants.h"
#include <cstdint> #include <cstdint>
PowerupComponent::PowerupComponent(PowerupType type) PowerupComponent::PowerupComponent(std::function<void (Entity*)> func)
{ {
switch (type) this->pickupFunc = func;
{
case PowerupType::HEART:
this->pickupFunc = (&PowerupComponent::heartEffect);
break;
case PowerupType::WALKINGSPEED:
this->pickupFunc = (&PowerupComponent::movementSpeedEffect);
break;
case PowerupType::SHOOTINGSPEED:
this->pickupFunc = (&PowerupComponent::atkSpeedEffect);
break;
default:
break;
}
} }
void PowerupComponent::update() void PowerupComponent::update()
@ -35,23 +22,7 @@ void PowerupComponent::update()
{}, {},
true)) != nullptr) true)) != nullptr)
{ {
(this->*pickupFunc)(player); (this->pickupFunc)(player);
this->entity->destroy(); this->entity->destroy();
} }
} }
void PowerupComponent::heartEffect(Entity* player)
{
if(player->getComponent<HealthComponent>().getHealth() < 5)
player->getComponent<HealthComponent>().modifyHealth(1);
}
void PowerupComponent::movementSpeedEffect(Entity* player)
{
player->getComponent<StatEffectsComponent>().modifyStatDur(Stats::MOVEMENT_SPEED, BUFF_DURATION);
}
void PowerupComponent::atkSpeedEffect(Entity* player)
{
player->getComponent<StatEffectsComponent>().modifyStatDur(Stats::ATTACK_SPEED, BUFF_DURATION);
}