Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

03 fsm how_toimplementai_state_20161006_jintaeks

118 views

Published on

FSM implementation from the scratch.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

03 fsm how_toimplementai_state_20161006_jintaeks

  1. 1. Finite State Machine Mobile Game Programming jintaeks@gmail.com Division of Digital Contents, Dongseo Univ. October 6, 2016
  2. 2. Action states 2  states of the monster – Idle – Moving – Attack – Hurt – Dead
  3. 3. Simplification  Step1: How to model the problem to find a solution.  Step2: How to implement the model. 3
  4. 4. Step1: Model 4
  5. 5. Model: Object State Diagram  The State Diagram is a well known data structure in computer science.  In compiler theory, we learn State Diagram which is used in automata.  In database, there is an entity-relation model which is similar to the state diagram.  In game programming, there is a state machine which is based on the state diagram.  We apply this data structure to an object in game. – It is called ‘Object State Diagram’. 5
  6. 6. State DiagramState  A State is represented as a circle.  Start state has a incoming arrow.  Final state has a double circle. 6
  7. 7. State DiagramState Action  A State can have action(s).  An action is described in text after slash symbol.  An action describes the dynamic behavior of an object in that state.  Example: 7 State/Action
  8. 8. State DiagramTransition  A transition is represented as a directed solid arrow.  A transition means the state change between two different states. 8  A state transition between A and B.  It can be read, ‘State A goes to B’.
  9. 9. State DiagramTransition Input/Action  A transition can have input(s) and action(s).  An input describes a condition which trigger the state change.  An action describes the dynamic behavior of an object when it goes to other state.  Example: 9
  10. 10. Monster State Diagram  For simplicity, we only select 3 states. 10
  11. 11. Final Monster State Diagram 11
  12. 12. Question  In Moving state, I want to add more detailed motions for the monster. – Crawl – Walk – Run  How to add this feature? 12
  13. 13. (Answer to Question)  We can build the ‘Moving State Diagram’.  Add this sub state diagram into inner state diagram of Moving State. 13
  14. 14. Step2: Implementation  Step1: How to model the problem to find solution.  Step2: How to implement the model. 14
  15. 15. Simple Game Client 15
  16. 16. 16
  17. 17. Adding States 17
  18. 18. Adding Properties 18 m_fIdleTimer m_dwIdleMotion m_dwDistanceToTarget m_fSpellGauge m_dwAttackMotion m_dwTargetId m_dwBboxId m_fPrepareAttackTimer
  19. 19. 19
  20. 20. Adding State Transitions 20
  21. 21. 21
  22. 22. State transition: Idle state 22
  23. 23. State transition: Moving state 23
  24. 24. State transition: Attack state 24
  25. 25. Test routine 25
  26. 26. 26
  27. 27. Demo  The Simple Console application project using Microsoft Visual Studio 2010. 27 User press a ‘1' key. User press a 'Esc' key.  Question: There is an output sequence of 1 and 0 after first 1 is printed. Why?
  28. 28. (Answer to question)  In Moving state, the condition of first if-statement is always true. Because the string expression “target is out in range” always returns a non-zero value.  In C++, a non-zero value evaluated to true for a boolean expression. 28
  29. 29. (Implementation Optimization)  Our C++ implementation can be structured to more elegant form.  We can use well-known design patterns.  Use the pointer to member functions! – I will show it in the next lecture. 29
  30. 30. More Complex Example  Most programmer are aware of this concept.  But they cannot use it freely, why? 30
  31. 31. Implementing Simple Desktop Calculator. ◇Input ◇Output • 1 • + • 2 • + • 13 • = • + • 3 • - • … 31 1 2 1 16 3 3 16 13 1 19
  32. 32. How to Find States? Let’s focus on number. 1 + 2 + 13 = + 3 - 32 first operand second operandsecond operand
  33. 33. Can you find a hidden state? 1 + 2 + 13 = + 3 - 33 The result can be used as first operand, but also it can be discarded.
  34. 34. 3 States for Calculator problem 34
  35. 35. Homework: Complete the Calculator State Model. 35
  36. 36. Q&A 36

×