1
edit
Changes
State
,→Code Samples
public:
enum ID_STATE {STATE_ATTACK, STATE_MOVE, STATE_STOP,
STATE_HOLDPOSITION, STATE_SLAUGHTER };
IState(ID_STATE state) : _ID(state) {}
ID_STATE GetID() {return _ID;}
cUnit* _pUnit;
public:
cState_Attack(cUnit* target, cUnit* pUnit) : IState(STATE_ATTACK), _pTarget(target), _pUnit(pUnit) {}
void Operate();
};
cUnit* _pUnit;
public:
cState_Slaughter(cUnit* target, cUnit* pUnit) : IState(STATE_SLAUGHTER), _pTarget(target), _pUnit(pUnit) {}
void Operate();
};
cDarkElf::cDarkElf() {_pState = new cState_Stop;}
cDarkElf::~cDarkElf() {delete _pState;}
void cDarkElf::ChangeState(IState* pState) {
delete _pState;
_pState = pState;
}
void cDarkElf::Attack(cUnit* Target) {
ChangeState(new cState_Attack(Target, this));
}
void cDarkElf::Move(int x, int y) {
ChangeState(new cState_Move(x, y, this));
}
void cDarkElf::Stop() {ChangeState(new cState_Stop);}
void cDarkElf::HoldPosition() {
ChangeState(new cState_HoldPosition);
}
void cDarkElf::Patrol(int x1, int y1, int x2, int y2) {
ChangeState(new cState_Patrol(x1, y1, x2, y2));
}
void cDarkElf::Update() {_pState->Operate();}
</pre>