MVC架构和FSM有限状态机的初步认识与学习

UnityMVC框架编程核心思想

MVC 编程模式
MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:

Model(模型)表示应用程序核心(比如数据库记录列表)。

View(视图)显示数据(数据库记录)。

Controller(控制器)处理输入(写入数据库记录)。

MVC 模式同时提供了对 HTML、CSS 和 JavaScript 的完全控制。
Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。  通常模型对象负责在数据库中存取数据。
View(视图)是应用程序中处理数据显示的部分。  通常视图是依据模型数据创建的。
Controller(控制器)是应用程序中处理用户交互的部分。  通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
MVC 分层有助于管理复杂的应用程序,因为您可以在一个时间内专门关注一个方面。例如,您可以在不依赖业务逻辑的情况下专注于视图设计。同时也让应用程序的测试更加容易。

MVC 分层同时也简化了分组开发。不同的开发人员可同时开发视图、控制器逻辑和业务逻辑。
视图

视图是用户看到并与之交互的界面。对老式的Web应用程序来说,视图就是由HTML元素组成的界面,在新式的Web应用程序中,HTML依旧在视图中扮演着重要的角色,但一些新的技术已层出不穷,它们包括Adobe Flash和像XHTMLXML/XSL,WML等一些标识语言和Web services.
MVC好处是它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,不管这些数据是联机存储的还是一个雇员列表,作为视图来讲,它只是作为一种输出数据并允许用户操纵的方式。

模型

模型表示企业数据和业务规则。在MVC的三个部件中,模型拥有最多的处理任务。例如它可能用像EJBs和ColdFusion Components这样的构件对象来处理数据库,被模型返回的数据是中立的,就是说模型与数据格式无关,这样一个模型能为多个视图提供数据,由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。

控制器

控制器接受用户的输入并调用模型和视图去完成用户的需求,所以当单击Web页面中的超链接和发送HTML表单时,控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,然后再确定用哪个视图来显示返回的数据

 

https://blog.****.net/muyi_amen/article/details/54341065

MVC架构简介

MVC是一种架构设计模式,是一种设计理念。是为了达到分层设计的目的,从而使代码解耦,便于维护和代码的复用。MVC是3个单词的缩写,全称:Model-View-Controller(模型-视图-控制器)。

举一个例子,MVC就好比我们的鞋柜。当没有鞋柜的时候,鞋子是这样摆放的:

MVC架构和FSM有限状态机的初步认识与学习

有了鞋柜之后,我们的鞋子是这样摆放的:

MVC架构和FSM有限状态机的初步认识与学习

一眼就能看出,有了鞋柜之后,鞋子的摆放明显的整齐和有序很多,这样也很方便我们找到自己想穿的鞋子,不用将大量的时间花在寻找鞋子上。如果把我们的成千上万行代码和各种复杂的业务逻辑看作是各式各样的鞋子,那我们的MVC就是鞋柜。MVC让你的代码结构更加清晰明了。

没有使用MVC的时候,我们的代码结构如下:

MVC架构和FSM有限状态机的初步认识与学习

上图那一坨“剪不断、理还乱”的乱麻就是你没有使用分层设计的代码结构。如果这时让你找你代码中的某一段逻辑估计是很费劲的,更别说将代码中的某一段代码进行复用或者替换了。

使用MVC分层设计之后,我们的代码结构如下:

MVC架构和FSM有限状态机的初步认识与学习

上面的图示可能有点夸张,但是这样可能更好的理解。MVC其实就是提供一种规则,让你把相同类型的代码放在一起,这样就形成了层次,从而达到分层解耦、复用、便于测试和维护的目的。

以上说了一堆,其实就是想让大家理解MVC是什么,有什么作用。接下来,我们结合我们实际开发中的代码类型来解释一下MVC。

1、Model

模型层,可以简单理解就是数据层,用于提供数据。在项目中,(简单理解)一般把数据访问和操作,比如将对象关系映射这样的代码作为Model层,也就是对数据库的操作这一些列的代码作为Model层。比如代码中我们会写DAO和DTO类型的代码,那这个DAO和DTO我们可以理解为是属于Model层的代码。

2、View

视图层,就是UI界面,用于跟用户进行交互。一般所有的JSP、Html等页面就是View层。

3、Controller

控制层,Controller层的功能就是将Model和View层进行关联。比如View主要是显示数据的,但是数据又需要Model去访问,这样的话,View会先告诉Controller,然后Controller再告诉Model,Model请求完数据之后,再告诉View。这样View就可以显示数据了。如下图:

MVC架构和FSM有限状态机的初步认识与学习

关于Spring MVC和Struts,与MVC的关系:

大家还记得在上面我举过的一个例子,MVC好比鞋柜。那Spring和Struts2只是不同牌子的鞋柜而已。并且Spring MVC和Struts2只是一个协助程序员更好实现MVC分层架构的框架而已。就是说,我们实现MVC不一定非要使用Spring或者struts2,自己按照MVC的理解,自己完成自己代码的分层也行。就好比自己在家用木棍自己制作一个鞋柜也照样可以把鞋子摆放整齐,当然,这样做的话首先要你有这样的一个木工技术。我们没有必要为了一个鞋柜,还要自己去学习木工技术,所以最好最快的方式就是去超市买一个鞋柜。

我们直接使用Spring mvc或者struts2来实现MVC,就是我们直接使用别人做好的东西,直接用。快捷、省时、省事、而且质量好。

 

最后:

其实现在除了MVC架构之外,还有MVP、MVVM等。

在实际项目中MVC更好的实现应该还多一个service层,用来处理业务逻辑。如下:

MVC架构和FSM有限状态机的初步认识与学习

其中多出来的Service层,主要是用来处理复杂的业务逻辑,这样结构层次更加鲜明和简介。

 

FSM(有限状态机)

FSM官方WIki C# - FSMSystem.cs

http://wiki.unity3d.com/index.php/Finite_State_Machine

 

https://blog.****.net/silangquan/article/details/51155805

一个通用的有限状态机(FSM)框架

吃饭,睡觉,打豆豆

现在要实现一个游戏中的一个NPC的AI, 他只做三件事,吃饭,睡觉,打豆豆,最直接,最简答想到的代码应该是这样。

  1. void Update()

  2. {

  3. if(Hungry)

  4. {

  5. Eat();

  6. return;

  7. }

  8.  
  9. if(Sleepy)

  10. {

  11. Sleep();

  12. return;

  13. }

  14.  
  15. if(Bored)

  16. {

  17. KickDD();

  18. return;

  19. }

  20.  
  21. }

这样的代码是Work,但是,当NPC的状态和行为不断复杂化的时候,慢慢的,你就会添加各种条件变量,慢慢的,你就会用上了各种switch case,慢慢的,判断层级越来越多,慢慢的....

这个时候你就需要一个状态机了。

FSM简介

FSM定义:

一个有限状态机是一个设备,或者是一个设备模型,具有有限数量的状态,它可以在任何给定的时间根据输入进行操作,使得一个状态变换到另一个状态,或者是使一个输入或者一种行为的发生。一个有限状态机在任何瞬间只能处在一种状态。

它的优点:

1.编程快速简单,2.易于调试,3.很少的计算开销,4.直觉性,5.灵活性。

 

 

简单的框架

MVC架构和FSM有限状态机的初步认识与学习

 

主要的两个类,FSMState表示状态,FSMSystem里面维护了一个状态的列表,最后需要一个StateController作为状态的控制器。

代码清单

FSMState.cs

  1. using UnityEngine;

  2. using System;

  3. using System.Collections.Generic;

  4. using System.Collections;

  5.  
  6. public enum Transition

  7. {

  8. NullTransition = 0, // Use this transition to represent a non-existing transition in your system

  9. SawPlayer,

  10. LostPlayer,

  11. NoHealth,

  12. ReadytoAim,

  13. ReadytoShot,

  14. ReadytoIdle,

  15. ReadytoAttack,

  16. ReadytoChasing

  17. }

  18.  
  19. public enum StateID

  20. {

  21. NullStateID = 0, // Use this ID to represent a non-existing State in your system

  22. Idle,

  23. Chasing, // jump

  24. Attack,

  25. Shooting,

  26. Aiming,

  27. BacktoIdle,//jump

  28. Dead,

  29. }

  30.  
  31.  
  32. public abstract class FSMState{

  33. protected Dictionary<Transition, StateID> map = new Dictionary<Transition, StateID>();

  34. protected StateID stateID;

  35. public StateID ID { get { return stateID; } }

  36.  
  37. public void AddTransition(Transition trans, StateID id)

  38. {

  39. // Check if anyone of the args is invalid

  40. if (trans == Transition.NullTransition)

  41. {

  42. Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");

  43. return;

  44. }

  45.  
  46. if (id == StateID.NullStateID)

  47. {

  48. Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID");

  49. return;

  50. }

  51.  
  52. // Since this is a Deterministic FSM,

  53. // check if the current transition was already inside the map

  54. if (map.ContainsKey(trans))

  55. {

  56. Debug.LogError("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() +

  57. "Impossible to assign to another state");

  58. return;

  59. }

  60.  
  61. map.Add(trans, id);

  62. }

  63.  
  64. /// <summary>

  65. /// This method deletes a pair transition-state from this state's map.

  66. /// If the transition was not inside the state's map, an ERROR message is printed.

  67. /// </summary>

  68. public void DeleteTransition(Transition trans)

  69. {

  70. // Check for NullTransition

  71. if (trans == Transition.NullTransition)

  72. {

  73. Debug.LogError("FSMState ERROR: NullTransition is not allowed");

  74. return;

  75. }

  76.  
  77. // Check if the pair is inside the map before deleting

  78. if (map.ContainsKey(trans))

  79. {

  80. map.Remove(trans);

  81. return;

  82. }

  83. Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + stateID.ToString() +

  84. " was not on the state's transition list");

  85. }

  86.  
  87. /// <summary>

  88. /// This method returns the new state the FSM should be if

  89. /// this state receives a transition and

  90. /// </summary>

  91. public StateID GetOutputState(Transition trans)

  92. {

  93. // Check if the map has this transition

  94. if (map.ContainsKey(trans))

  95. {

  96. return map[trans];

  97. }

  98. return StateID.NullStateID;

  99. }

  100.  
  101. /// <summary>

  102. /// This method is used to set up the State condition before entering it.

  103. /// It is called automatically by the FSMSystem class before assigning it

  104. /// to the current state.

  105. /// </summary>

  106. public virtual void DoBeforeEntering() { }

  107.  
  108. /// <summary>

  109. /// This method is used to make anything necessary, as reseting variables

  110. /// before the FSMSystem changes to another one. It is called automatically

  111. /// by the FSMSystem before changing to a new state.

  112. /// </summary>

  113. public virtual void DoBeforeLeaving() { }

  114.  
  115. /// <summary>

  116. /// This method decides if the state should transition to another on its list

  117. /// NPC is a reference to the object that is controlled by this class

  118. /// </summary>

  119. public abstract void Reason(GameObject player, GameObject npc);

  120.  
  121. /// <summary>

  122. /// This method controls the behavior of the NPC in the game World.

  123. /// Every action, movement or communication the NPC does should be placed here

  124. /// NPC is a reference to the object that is controlled by this class

  125. /// </summary>

  126. public abstract void Act(GameObject player, GameObject npc);

  127. }

FSMSystem.cs

  1. using UnityEngine;

  2. using System.Collections;

  3. using System.Collections.Generic;

  4.  
  5.  
  6. public class FSMSystem{

  7. private List<FSMState> states;

  8. // The only way one can change the state of the FSM is by performing a transition

  9. // Don't change the CurrentState directly

  10. private StateID currentStateID;

  11. public StateID CurrentStateID { get { return currentStateID; } }

  12. private FSMState currentState;

  13. public FSMState CurrentState { get { return currentState; } }

  14. public StateID defaultState {set{defaultState = value;} get {return defaultState;}}

  15.  
  16. public void resetToDefaultState()

  17. {

  18. currentState = states[0];

  19. currentStateID = states[0].ID;

  20. /*for(int i =0; i< states.Count; i++)

  21. {

  22. if(states[i].ID == defaultState)

  23. {

  24. currentState = states[i];

  25. currentStateID = states[i].ID;

  26. }

  27. }*/

  28. }

  29.  
  30. public FSMSystem()

  31. {

  32. states = new List<FSMState>();

  33. }

  34.  
  35. /// <summary>

  36. /// This method places new states inside the FSM,

  37. /// or prints an ERROR message if the state was already inside the List.

  38. /// First state added is also the initial state.

  39. /// </summary>

  40. public void AddState(FSMState s)

  41. {

  42. // Check for Null reference before deleting

  43. if (s == null)

  44. {

  45. Debug.LogError("FSM ERROR: Null reference is not allowed");

  46. }

  47.  
  48. // First State inserted is also the Initial state,

  49. // the state the machine is in when the simulation begins

  50. if (states.Count == 0)

  51. {

  52. states.Add(s);

  53. currentState = s;

  54. currentStateID = s.ID;

  55. return;

  56. }

  57.  
  58. // Add the state to the List if it's not inside it

  59. foreach (FSMState state in states)

  60. {

  61. if (state.ID == s.ID)

  62. {

  63. Debug.LogError("FSM ERROR: Impossible to add state " + s.ID.ToString() +

  64. " because state has already been added");

  65. return;

  66. }

  67. }

  68. states.Add(s);

  69. }

  70.  
  71. /// <summary>

  72. /// This method delete a state from the FSM List if it exists,

  73. /// or prints an ERROR message if the state was not on the List.

  74. /// </summary>

  75. public void DeleteState(StateID id)

  76. {

  77. // Check for NullState before deleting

  78. if (id == StateID.NullStateID)

  79. {

  80. Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state");

  81. return;

  82. }

  83.  
  84. // Search the List and delete the state if it's inside it

  85. foreach (FSMState state in states)

  86. {

  87. if (state.ID == id)

  88. {

  89. states.Remove(state);

  90. return;

  91. }

  92. }

  93. Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() +

  94. ". It was not on the list of states");

  95. }

  96.  
  97. /// <summary>

  98. /// This method tries to change the state the FSM is in based on

  99. /// the current state and the transition passed. If current state

  100. /// doesn't have a target state for the transition passed,

  101. /// an ERROR message is printed.

  102. /// </summary>

  103. public void PerformTransition(Transition trans)

  104. {

  105. // Check for NullTransition before changing the current state

  106. if (trans == Transition.NullTransition)

  107. {

  108. Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");

  109. return;

  110. }

  111.  
  112. // Check if the currentState has the transition passed as argument

  113. StateID id = currentState.GetOutputState(trans);

  114. if (id == StateID.NullStateID)

  115. {

  116. Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " +

  117. " for transition " + trans.ToString());

  118. return;

  119. }

  120.  
  121. // Update the currentStateID and currentState

  122. currentStateID = id;

  123. foreach (FSMState state in states)

  124. {

  125. if (state.ID == currentStateID)

  126. {

  127. // Do the post processing of the state before setting the new one

  128. currentState.DoBeforeLeaving();

  129.  
  130. currentState = state;

  131.  
  132. // Reset the state to its desired condition before it can reason or act

  133. currentState.DoBeforeEntering();

  134. break;

  135. }

  136. }

  137.  
  138. } // PerformTransition()

  139. }

 

角色控制状态机

角色的控制器通常也是通过状态机来实现。

首先要定义出角色的各种状态已经状态间的转换条件,就像这样:

 

MVC架构和FSM有限状态机的初步认识与学习

 

接下来就是用代码定义各种状态的执行逻辑,跳转条件等。有些复杂的游戏还有通过分层的概念来处理角色的。

下面是最简单的两个状态,Idle和Move。

IdleState.cs

  1. using UnityEngine;

  2. using System.Collections;

  3.  
  4. namespace CharacterFSM

  5. {

  6. public class IdleState : CharacterState

  7. {

  8. float horizontalMove;

  9. float verticalMove;

  10.  
  11. public IdleState(Character _host)

  12. {

  13. host = _host;

  14. stateID = CharacterStateID.Idle;

  15. }

  16.  
  17. public override void HandleInput(MovementInput movementInput)

  18. {

  19. horizontalMove = movementInput.moveStrafe;

  20. verticalMove = movementInput.moveForward;

  21. }

  22.  
  23. public override void Act()

  24. {

  25.  
  26. }

  27. public override void Reason()

  28. {

  29. if (horizontalMove * horizontalMove + verticalMove * verticalMove < 0.1f)

  30. {

  31. return;

  32. }

  33. else

  34. {

  35. host.stateController.SetTransition(CharacterStateTransition.ToMove);

  36. }

  37. }

  38.  
  39. public override void DoBeforeEntering()

  40. {

  41. host.animator.SetBool("Static_b", true);

  42. host.animator.SetFloat("Speed_f", 0);

  43. }

  44. }

  45.  
  46. }

MoveState.cs

  1. using UnityEngine;

  2. using System.Collections;

  3.  
  4. namespace CharacterFSM

  5. {

  6. public class MoveState : CharacterState

  7. {

  8. float stepDelta;

  9. float stepMark;

  10.  
  11. public MoveState(Character _host)

  12. {

  13. stepMark = -1f;

  14. stepDelta = 0.3f;

  15. host = _host;

  16. stateID = CharacterStateID.Move;

  17. }

  18.  
  19. public override void HandleInput(MovementInput movementInput)

  20. {

  21.  
  22.  
  23. float maxSpeed = host.MaxSpeed * Mathf.Sqrt(movementInput.moveStrafe * movementInput.moveStrafe + movementInput.moveForward * movementInput.moveForward);

  24.  
  25. host.CurrentSpeed -= 2 * host.Acceleration * Time.deltaTime;

  26. host.CurrentSpeed = Mathf.Max(maxSpeed, host.CurrentSpeed);

  27.  
  28. Vector2 tmp = new Vector2(movementInput.moveStrafe, movementInput.moveForward).normalized * host.CurrentSpeed;

  29. host.CurrentVelocity = new Vector3(tmp.x, 0, tmp.y);

  30. host.animationController.SetSpeed(host.CurrentSpeed);

  31.  
  32. }

  33.  
  34. public override void Act()

  35. {

  36. }

  37.  
  38. public override void Reason()

  39. {

  40. if(host.CurrentSpeed < 0.01f )

  41. {

  42. host.stateController.SetTransition(CharacterStateTransition.ToIdle);

  43. }

  44. }

  45.  
  46. public override void DoBeforeLeaving()

  47. {

  48.  
  49. }

  50.  
  51. public override void DoBeforeEntering()

  52. {

  53. host.animationController.PerformMove();

  54. }

  55. }

  56. }

还有一个比较重要的类,CharacterStateController

  1. using UnityEngine;

  2. using System.Collections;

  3. using CharacterFSM;

  4.  
  5. public class CharacterStateController {

  6.  
  7. CharacterFSMSystem characterFsm;

  8. Character character;

  9.  
  10. public CharacterStateController(Character _character)

  11. {

  12. character = _character;

  13. }

  14.  
  15. public CharacterStateID GetCurrentStateID()

  16. {

  17. return characterFsm.CurrentStateID;

  18. }

  19.  
  20. public void Init()

  21. {

  22. ConstructFSM();

  23. }

  24.  
  25. // Update is called once per frame

  26. public void Update () {

  27. Debug.Log(GetCurrentStateID());

  28. //Debug.Log(character.movementInput.moveForward + " " +character.movementInput.moveStrafe);

  29. characterFsm.CurrentState.HandleInput(character.movementInput);

  30. characterFsm.CurrentState.Reason();

  31. characterFsm.CurrentState.Act();

  32. }

  33.  
  34.  
  35. public void SetTransition(CharacterStateTransition t)

  36. {

  37. if (characterFsm != null)

  38. {

  39. characterFsm.PerformTransition(t);

  40. }

  41. }

  42.  
  43. void ConstructFSM()

  44. {

  45. IdleState idleState = new IdleState(character);

  46. idleState.AddTransition(CharacterStateTransition.ToMove, CharacterStateID.Move);

  47.  
  48. MoveState moveState = new MoveState(character);

  49. moveState.AddTransition(CharacterStateTransition.ToIdle, CharacterStateID.Idle);

  50.  
  51. characterFsm = new CharacterFSMSystem();

  52. characterFsm.AddState(idleState);

  53. characterFsm.AddState(moveState);

  54.  
  55. }

  56. }


这个类没必要声明称Monobehavior,只需要作为Character的一个成员来处理就可以了。运行的效果是这样的。

 

 

MVC架构和FSM有限状态机的初步认识与学习