mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
ref(Powerups): move powerup adding functionality to gamedev side
This commit is contained in:
parent
cffe1bdc31
commit
e3852379e4
@ -3,6 +3,7 @@
|
||||
#include <SDL_mixer.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
@ -24,7 +25,7 @@ public:
|
||||
~AssetManager();
|
||||
|
||||
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();
|
||||
PowerupType calculateType();
|
||||
|
||||
@ -1,19 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include "Component.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
class PowerupComponent : public Component
|
||||
{
|
||||
public:
|
||||
PowerupComponent(PowerupType type);
|
||||
PowerupComponent(std::function<void (Entity*)> func);
|
||||
~PowerupComponent() {};
|
||||
|
||||
void update() override;
|
||||
void heartEffect(Entity* player);
|
||||
void movementSpeedEffect(Entity* player);
|
||||
void atkSpeedEffect(Entity* player);
|
||||
|
||||
private:
|
||||
void (PowerupComponent::*pickupFunc)(Entity* player);
|
||||
std::function<void (Entity*)> pickupFunc;
|
||||
};
|
||||
@ -48,25 +48,21 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
|
||||
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;
|
||||
|
||||
auto& powerups(man->addEntity());
|
||||
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 {
|
||||
powerups.addComponent<SpriteComponent>(it->second.data());
|
||||
powerups.addComponent<SpriteComponent>(texturePath.c_str());
|
||||
}
|
||||
catch (std::runtime_error e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
powerups.addComponent<ColliderComponent>("powerup", 0.6f);
|
||||
powerups.addComponent<PowerupComponent>(type);
|
||||
powerups.addComponent<PowerupComponent>(pickupFunc);
|
||||
powerups.addGroup((size_t)Entity::GroupLabel::POWERUPS);
|
||||
}
|
||||
|
||||
|
||||
@ -7,22 +7,9 @@
|
||||
#include "Constants.h"
|
||||
#include <cstdint>
|
||||
|
||||
PowerupComponent::PowerupComponent(PowerupType type)
|
||||
PowerupComponent::PowerupComponent(std::function<void (Entity*)> func)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
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;
|
||||
}
|
||||
this->pickupFunc = func;
|
||||
}
|
||||
|
||||
void PowerupComponent::update()
|
||||
@ -35,23 +22,7 @@ void PowerupComponent::update()
|
||||
{},
|
||||
true)) != nullptr)
|
||||
{
|
||||
(this->*pickupFunc)(player);
|
||||
(this->pickupFunc)(player);
|
||||
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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user