引言
我们在游戏开发中常常遇到不同游戏状态之间的切换,如果我们刚刚入门游戏编程的话,很自然的会用一堆条件判断语句暴力解决。如果我们的游戏状态不多,也不复杂,这样当然可以完成任务。但是如果我们的游戏状态复杂,并且后续会不断扩展新的状态,这时你会发现自己掉入了一堆像毛线团一样的代码泥淖中。每增加一种新的状态,或者对原来的状态进行修改,你都有可能对所有的状态处理代码进行修改,一不留神就会造成很多bug,让代码的后续维护工作苦不堪言。
当然我们可以对游戏的不同状态进行划分,然后用switch...case进行解决。每个状态就是一个分支,针对每一种状态的处理都在同一个分支下进行,这样可以避免我们陷入繁杂的条件语句判断中。但是当我们修改或新增加状态时有可能对多处进行修改。真正好的方案是把各个状态相关的数据和方法进行封装。这就引出了我们要说的状态模式。
定义
允许对象在内部状态改变时改变自身的行为,对象看起来好像在修改自身类。
类图
说明
Context(状态拥有者)
- 是一个具有“状态”属性的类,可以制定相关的接口,让外界能够得知状态的改变或通过操作让状态改变。
- 有状态属性的类,例如:游戏角色有潜行、攻击、施法等状态;好友上线、脱机、忙碌等状态;GoF使用TCP联网为例,有已连接、等待连接、断线等状态。这些类中会有一个ConcreteState[X]子类的对象为其成员,用来代表当前的状态。
State(状态接口类)
- 制定状态的接口,负责规范Context(状态拥有者)在特定状态下要表现的行为。
ConcreteState(具体状态类)
- 继承自State(状态接口类)。
- 实现Context(状态拥有者)在特定状态下该有的行为。例如,实现角色在潜行状态时该有的行动变缓、3D模型变半透明、不能被敌方角色察觉等行为。
实例讲解
假设我们正在制作一个跑酷游戏,针对游戏角色有3中操作:
1.按下W键玩家会跳跃 2.按下A,D键玩家向左或向右 3.在跳跃时按下S键会俯冲
针对不同的操作,我们可以把玩家所处状态分成4中状态:
1.站立状态 2.跳跃状态 3.走动状态 4.俯冲状态
下面时使用CocosCreator模拟实现4种状态,以及根据不同操作进行对应的状态切换的代码
export interface HeroState {
enter(hero: Hero, input?: KeyCode);
handleInput(hero: Hero, input: KeyCode);
update();
}
// 站立状态
export class StandingState implements HeroState {
enter(hero: Hero, input?: KeyCode) {
hero.label.string = "站立";
}
handleInput(hero: Hero, input: KeyCode) {
if (input == KeyCode.KEY_W) { // 按下W键进入跳跃状态
hero.curState = hero.jumpingState;
hero.curState.enter(hero);
} else if (input == KeyCode.KEY_A || input == KeyCode.KEY_D) { // 按下A或D键进入走动状态,分别代表向左向右
hero.curState = hero.runningState;
hero.curState.enter(hero, input);
}
}
update() {
}
}
// 行走状态
export class RunningState implements HeroState {
enter(hero: Hero, input?: KeyCode) {
// 模拟向左,向右行走
if (input == KeyCode.KEY_A) {
hero.label.string = "向左走";
let pos = hero.node.position;
tween(hero.node).to(0.5, {position: new Vec3(pos.x - 10, pos.y, pos.z)})
.start()
} else if (input == KeyCode.KEY_B) {
let pos = hero.node.position;
tween(hero.node).to(0.5, {position: new Vec3(pos.x + 10, pos.y, pos.z)})
.start()
hero.label.string = "向右走";
}
}
handleInput(hero: Hero, input: KeyCode) {
if (input == KeyCode.KEY_W) { // 行走状态时可以按下W键进入跳跃状态
hero.curState = hero.jumpingState;
hero.curState.enter(hero);
} else if (input == KeyCode.KEY_A || input == KeyCode.KEY_D) { // 行走状态的延续处理
hero.curState.enter(hero, input);
}
}
update() {
}
}
// 跳跃状态
export class JumpState implements HeroState {
enter(hero: Hero, input?: KeyCode) {
// 模拟跳跃动作,向上移动50个像素
hero.label.string = "跳跃";
let pos = hero.node.position;
tween(hero.node).to(0.5, {position: new Vec3(pos.x, pos.y + 50, pos.z)})
.to(0.5, {position: new Vec3(pos.x, pos.y - 50, pos.z)})
.call(() => {
hero.curState = hero.standingState;
hero.curState.enter(hero);
})
.start()
}
handleInput(hero: Hero, input: KeyCode) {
if (input == KeyCode.KEY_S) { // // 跳跃状态时按下S键进入俯冲状态
hero.curState = hero.divingState;
hero.curState.enter(hero);
}
}
update() {
}
}
// 俯冲攻击
export class DivingState implements HeroState {
enter(hero: Hero, input?: KeyCode) {
// 模拟俯冲动作,向前下方移动
hero.label.string = "俯冲";
let pos = hero.node.position;
tween(hero.node).to(1.0, {position: new Vec3(pos.x + 20, -320, pos.z)})
.call(() => {
// 俯冲动作完成我们要回到站立状态
hero.curState = hero.standingState;
hero.curState.enter(hero);
})
.start()
}
handleInput(hero: Hero, input: KeyCode) {
}
update() {
}
}
在Hero对象初始化时,我们实例化所有的状态对象
@ccclass('Hero')
export class Hero extends Component {
public curState: HeroState;
public label: Label;
public standingState: HeroState; // 站立状态
public runningState: HeroState; // 走动状态
public jumpingState: HeroState; // 跳跃状态
public divingState: HeroState; // 俯冲状态
onLoad () {
this.label = this.node.getChildByName("label").getComponent(Label);
systemEvent.on(SystemEvent.EventType.KEY_DOWN, this.handleInput, this);
// 状态初始化
this.standingState = new StandingState();
this.runningState = new RunningState();
this.jumpingState = new JumpState();
this.divingState = new DivingState();
this.curState = this.standingState;
}
onDestroy () {
systemEvent.off(SystemEvent.EventType.KEY_DOWN, this.handleInput, this);
}
handleInput (event: EventKeyboard) {
// 把键盘事件交给当前所指向的状态实例进行处理
this.curState.handleInput(this, event.keyCode)
}
}
这种方式适合状态不多的情况,如果游戏中有多个玩家,那么这种方式就不合适。此时我们可以在状态切换时动态的创建状态对象,在当前状态赋值新的状态对象之前移除老的状态对象即可。
参考:State · Design Patterns Revisited · Game Programming Patterns