角色状态机
以前写游戏。都是用switch,后来发现状态多了,判断条件变得非常烦杂,最后导致代码的阅读和维护非常困难。
这次重新重整理了思路,用状态机实现。Player类里只存相应的玩家数据,然后PlayerStateControl类来做状态间的逻辑判断(如:前一个状态完成后,接下来要转到哪个状态),然后就是每个状态类了。在每个状态里处理这个状态的输入输出,逻辑运算等等。
- import engine.IState;
- public class PlayerState implements IState
- {
- protected var m_player:Player = null;
- protected var m_stateType:int = PLAYER_STATE_NORMAL;
- public static var PLAYER_STATE_NORMAL = 0x10000; //普通状态
- public static var PLAYER_STATE_WAIT:int = (PLAYER_STATE_NORMAL|1); //待机状态
- public static var PLAYER_STATE_WALK:int = (PLAYER_STATE_NORMAL|2); //行走状态
- public static var PLAYER_STATE_FIGHT = 0x100000; //战斗状态
- public static var PLAYER_STATE_WALK_TO_ATTACK_TARGET = (PLAYER_STATE_FIGHT|1); //行走到功击目标的状态
- public static var PLAYER_STATE_REQUEST_USE_SKILL = (PLAYER_STATE_FIGHT|2); //请求施法状态
- public static var PLAYER_STATE_START_FIRE_SKILL = (PLAYER_STATE_FIGHT|3); //开始施放技能
- public static var PLAYER_STATE_FIRE_SKLL_OVER = (PLAYER_STATE_FIGHT|4); //施法结束
- public static var PLAYER_STATE_FIGHT_WAIT = (PLAYER_STATE_FIGHT|5); //战斗待机装态
- public static var PLAYER_STATE_FIGHT_HITED = (PLAYER_STATE_FIGHT|6); //战斗被击
- (还有其它。根据需求来补充)
- public function PlayerState(plr:Player)
- {
- this.m_player = plr;
- }
- public function GetStateType():int
- {
- return this.m_stateType;
- }
- public function OnEnter():void
- {
- }
- public function OnLeave():void
- {
- }
- public function Run():void
- {
- }
- }