使用:
let player = new Player('brook'); player.changeState(MoveState.instance); player.click(); player.changeState(AttackState.instance); player.click(); player.changeState(TalkState.instance); player.click(); //输出: change to MoveState brook is moving change to AttackState brook is attacking change to TalkState brook is talking这样随着状态的变化,点击左键做不同的事。
对于由谁来驱动状态变化可以根据实际情况来考虑,简单的话直接放角色里面就行,由角色自己决定自己的状态,复杂的话可以考虑用表来驱动状态机,通过表过实现状态的跳转。
关于TypeScript的推荐文章: