游戏buff系统简单实现
Effect.h
#pragma once
class Obj;
enum eEffect
{
E_EFFECT_ADD_HP = 0,
E_EFFECT_DEC_HP = 1,
E_EFFECT_ADD_SPEED = 2,
E_EFFECT_DEC_SPEED = 3,
E_EFFECT_MAX = 4,
};
//效果父类, 修改玩家各种属性
class Effect
{
public:
virtual void Cast(Obj *pObj) = 0; //受影响者
};
class AddHpEffect : public Effect{
public:
AddHpEffect(int hp):m_hp(hp){}
virtual void Cast(Obj *pObj);
private:
int m_hp;
};
class DecHpEffect : public Effect{
public:
DecHpEffect(int hp):m_hp(hp){}
virtual void Cast(Obj *pObj);
private:
int m_hp;
};
class AddSpeedEffect : public Effect{
public:
AddSpeedEffect(int speed):m_speed(speed){}
virtual void Cast(Obj *pObj);
private:
int m_speed;
};
class DecSpeedEffect : public Effect{
public:
DecSpeedEffect(int speed):m_speed(speed){}
virtual void Cast(Obj *pObj);
private:
int m_speed;
};
Effect.cpp
#include "Effect.h"
#include "Obj.h"
void AddHpEffect::Cast(Obj *pObj)
{
pObj->AddHp(m_hp);
}
void DecHpEffect::Cast(Obj *pObj)
{
pObj->DecHp(m_hp);
}
void AddSpeedEffect::Cast(Obj *pObj)
{
pObj->AddSpeed(m_speed);
}
void DecSpeedEffect::Cast(Obj *pObj)
{
pObj->DecSp