PoorMonk

U3D 俄罗斯方块笔记

1 FSMSystem 有限状态机,存储各个UI状态(菜单状态、游戏状态)

 1 public class FSMSystem
 2 {
 3      private List<FSMState> states;
 4 
 5     private StateID currentStateID;
 6     public StateID CurrentStateID { get { return currentStateID; } }
 7     private FSMState currentState;
 8     public FSMState CurrentState { get { return currentState; } }
 9 
10     public void AddState(FSMState s, Ctrl ctrl)
11     {
12         // Check for Null reference before deleting
13         if (s == null)
14         {
15             Debug.LogError("FSM ERROR: Null reference is not allowed");
16         }
17 
18         s.FSM = this;
19         s.CTRL = ctrl;
20         // First State inserted is also the Initial state,
21         //   the state the machine is in when the simulation begins
22         if (states.Count == 0)
23         {
24             states.Add(s);           
25             return;
26         }
27 
28         states.Add(s);
29     }
30 
31     public void PerformTransition(Transition trans)
32     {
33         // Check for NullTransition before changing the current state
34         if (trans == Transition.NullTransition)
35         {
36             Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");
37             return;
38         }
39 
40         // Check if the currentState has the transition passed as argument
41         StateID id = currentState.GetOutputState(trans);
42         if (id == StateID.NullStateID)
43         {
44             Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " +
45                            " for transition " + trans.ToString());
46             return;
47         }
48 
49         // Update the currentStateID and currentState        
50         currentStateID = id;
51         foreach (FSMState state in states)
52         {
53             if (state.ID == currentStateID)
54             {
55                 // Do the post processing of the state before setting the new one
56                 currentState.DoBeforeLeaving();
57 
58                 currentState = state;
59 
60                 // Reset the state to its desired condition before it can reason or act
61                 currentState.DoBeforeEntering();
62                 break;
63             }
64         }
65 
66     } // PerformTransition()
67 }

调用PerformTransition切换状态,在子类中重写DoBeforeEntering()和DoBeforeLeaving(),显示/隐藏UI动画效果。

2 用DoTween实现UI的动画效果。例如:

相机的缩放:

1     public void ZoomIn()
2     {
3         m_camera.DOOrthoSize(13.5f, 0.5f);
4     }
5 
6     public void ZoomOut()
7     {
8         m_camera.DOOrthoSize(20f, 0.5f);
9     }

MenuUI的显示/隐藏

 1     public void ShowMenu()
 2     {
 3         m_title.gameObject.SetActive(true);
 4         m_title.DOAnchorPosY(-95f, 0.5f);
 5         m_menuUI.gameObject.SetActive(true);
 6         m_menuUI.DOAnchorPosY(9f, 0.5f);
 7     }
 8 
 9     public void HideMenu()
10     {
11         m_title.DOAnchorPosY(98f, 0.5f).OnComplete(delegate { m_title.gameObject.SetActive(false); });
12         m_menuUI.DOAnchorPosY(-109f, 0.5f).OnComplete(delegate { m_menuUI.gameObject.SetActive(false); });
13     }

游戏方块:为每个方块额外添加一个旋转点m_rotatePoint,用来控制自身的旋转

transform.RotateAround(m_rotatePoint.position, Vector3.forward, -90);

vector的扩展方法:取整。

1 public static class Vector3Expand {
2 
3     public static Vector2 Round(this Vector3 v)
4     {
5         int x = Mathf.RoundToInt(v.x);
6         int y = Mathf.RoundToInt(v.y);
7         return new Vector2(x, y);
8     }
9 }

调用的时候可以直接用

Vector2 pos = transform.position.Round(); 

MVC(model-view-control)模式

model控制游戏逻辑

view控制UI

control控制model和view之间的联系 

 

完整游戏源码:https://github.com/PoorMonk/TetrisOfMVC.git

posted on 2018-04-15 22:56  PoorMonk  阅读(52)  评论(0)    收藏  举报

导航