首页 > 编程知识 正文

状态模式,c++状态模式

时间:2023-05-03 17:10:41 阅读:190443 作者:2548

有限状态机(英文: finite-state machine,简称: FSM )有限状态机是有限状态机)英文: finite-state automation,简称: FSA ),也称为状态机,用于描述有限状态及其之间的过渡或

简单来说,有三个特征。

状态总数是有限的。 无论何时,都只是处于一种状态。 在某些条件下,从一个状态转换到另一个状态。状态机一般有两种实现方式:

使用swith case实现非常简单,在此不再赘述。 状态模式实现(以本文为中心)使用状态模式定义:允许当对象的内在状态改变时改变其行为,并且对象看起来改变了其类。

状态模式主要解决了两个问题。

如果对象的内部状态发生更改,则还必须更改其行为。 特定于状态的行为必须独立定义。 也就是说,即使添加了新状态,也不应该影响现有状态的操作状态模式和策略模式非常相似,状态模式和策略模式之间存在差异

类图:

例:如果有游戏的话,作为女性英雄的他有站立、蹲下、跳跃、欠切等各种各样的状态。 通常的实现方式可以通过大量的if else进行判断,也可以通过switch case进行实现,但这样做会暴露缺点,存在大量的错误,代码不会懈怠维护和扩展。

在这种情况下,可以通过状态机(状态机)来实现。 流程图如下。

C#实现(状态模式方式) :大神-来自浅墨

请参阅-------------------------请参阅using system.colol publicclasstestheroine : mono behaviour { private heroine _ heroine; void Start () { _heroine=new Heroine ); }void Update () { _heroine.Update ); (英雄类(-------------) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )0------0 ) public class heroine { heroinebasestate _ state; public Heroine () _state=newstandingstate ) this; } publicvoidsetheroinestate (heroinebasestatenewstate ) { _state=newState; } publicvoidhandleinput { } public void update { } { _ state.handle input }; (状态接口0 -----------------() ) ) ) ) ) ) ) )0------0 ) publicinterfaceheroinebasestate { void update (; void HandleInput (; (请参阅站立状态(---------------------- -请参阅using System.Collections; publicclassstandingstate : heroinebasestate { private heroine _ heroine; publicstandingstate(he

roine heroine) { _heroine = heroine; Debug.Log("------------------------Heroine in StandingState~!(进入站立状态!)"); } public void Update() { } public void HandleInput() { if (Input.GetKeyDown(KeyCode.UpArrow)) { Debug.Log("get KeyCode.UpArrow!"); _heroine.SetHeroineState(new JumpingState(_heroine)); } if (Input.GetKeyDown(KeyCode.DownArrow)) { Debug.Log("get KeyCode.DownArrow!"); _heroine.SetHeroineState(new DuckingState(_heroine)); } }} 跳跃状态 //-------------------------------------------------------------------------------------//JumpState.cs//-------------------------------------------------------------------------------------using UnityEngine;using System.Collections;public class JumpingState : HeroineBaseState{ private Heroine _heroine; public JumpingState(Heroine heroine) { _heroine = heroine; Debug.Log("------------------------Heroine in JumpingState~!(进入跳跃状态!)"); } public void Update() { } public void HandleInput() { if (Input.GetKeyDown(KeyCode.UpArrow)) { Debug.Log("get GetKeyDown.UpArrow! but already in Jumping! return!(已经在跳跃状态中!)"); return; } if (Input.GetKeyDown(KeyCode.DownArrow)) { Debug.Log("get KeyCode.DownArrow!"); _heroine.SetHeroineState(new DrivingState(_heroine)); } }} 下蹲躲避状态 //-------------------------------------------------------------------------------------//DuckingState.cs//-------------------------------------------------------------------------------------using UnityEngine;using System.Collections;public class DuckingState : HeroineBaseState{ private Heroine _heroine; public DuckingState(Heroine heroine) { _heroine = heroine; Debug.Log("------------------------Heroine in DuckingState~!(进入下蹲躲避状态!)"); } public void Update() { } public void HandleInput() { if (Input.GetKeyDown(KeyCode.DownArrow)) { Debug.Log("已经在下蹲躲避状态中!"); return; } if (Input.GetKeyUp(KeyCode.UpArrow)) { Debug.Log("get GetKeyUp.UpArrow!"); _heroine.SetHeroineState(new StandingState(_heroine)); } }} 下斩状态 //-------------------------------------------------------------------------------------//DrivingState.cs//-------------------------------------------------------------------------------------using UnityEngine;using System.Collections;public class DrivingState : HeroineBaseState{ private Heroine _heroine; public DrivingState(Heroine heroine) { _heroine = heroine; Debug.Log("------------------------Heroine in DrivingState~!(进入下斩状态!)"); } public void Update() { } public void HandleInput() { if (Input.GetKeyDown(KeyCode.UpArrow)) { Debug.Log("get KeyCode.UpArrow!"); _heroine.SetHeroineState(new StandingState(_heroine)); } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。