Posted on 2008-11-19 00:57
Fox 閱讀(1904)
評論(3) 編輯 收藏 引用 所屬分類:
T技術碎語
State模式對應到C++的多態特性。
State模式適用較廣,這兒給出比較常見易懂的三種情況:
1. 當怪物在面對不同職業和特性的玩家時對應不同的AI處理和技能釋放:
CSkill* pAttackSkill;
if( pPlayer->MagicImmune() ) pAttackSkill = SomePhysicalAttackSkill;
else if( pPlayer->PhysicalImmune() ) pAttackSkill = SomeMagicAttackSkill;
...
pAttackSkill->Begin();
...
或者使用分支結構:
CSkill* pAttackSkill;
switch( pPlayer->GetOccupation() )
{
case WARRIOR: pAttackSkill = SomeLongRangeSkill; break;
case MAGICIAN: pAttackSkill = SomeForceSkill; break;
case NIMROD: pAttackSkill = SomeMagicSkill; break;
...
}
pAttackSkill->Begin();
...
閱讀全文