Dr Nick Mitchell (Room CM 224)

Slides:



Advertisements
Similar presentations
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Advertisements

Concurrency.
1 Introduction to Computability Theory Lecture15: Reductions Prof. Amos Israeli.
LAB 3 – Review of the Assignment. -- Clarifications Vikram Murali. TA : CSE 140L Prof. CK Cheng.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
Software Requirements
THE OBJECT-ORIENTED DESIGN WORKFLOW Statechart Diagrams.
UML Sequence Diagrams Eileen Kraemer CSE 335 Michigan State University.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 10: Statecharts.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
CORE MECHANICS. WHAT ARE CORE MECHANICS? Core mechanics are the heart of a game; they generate the gameplay and implement the rules. Formal definition:
1 CS101 Introduction to Computing Lecture 19 Programming Languages.
Software Design & Development Software Design & Development Programming Types Event Driven Programming Event driven programming Is a type of programming.
State and Sequence Diagrams Modelling dynamic information So far we have seen: Use Case Diagrams – requirements capture, interface.
CSCI 101 Introduction to Software Development and Design.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 8: Modelling Interactions and Behaviour.
SOFTWARE ENGINEERING BIT-8 APRIL, 16,2008 Introduction to UML.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Laurent Noel.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Laurent Noel.
Event Driven Programming
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Fundamentals of Game Design, 2 nd Edition by Ernest Adams Chapter 10: Core Mechanics.
Art 315 Lecture 5 Dr. J. Parker AB 606. Last time … We wrote our first program. We used a tool called GameMaker. The program we wrote causes a ball to.
By the end of this session you should be able to...
Lecture 05: Theory of Automata:08 Kleene’s Theorem and NFA.
1 Modeling interactions and behavior Lecturer Dr. Mai Fadel.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Software Engineering Prof. Ing. Ivo Vondrak, CSc. Dept. of Computer Science Technical University of Ostrava
CS 4730 Game Architecture CS 4730 – Computer Game Design Credit: Some slide material courtesy Walker White (Cornell)
A Use Case Primer 1. The Benefits of Use Cases  Compared to traditional methods, use cases are easy to write and to read.  Use cases force the developers.
The Unified Modeling Language Part II Omar Meqdadi SE 2730 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Game Maker Terminology
University of Windsor School of Computer Science Topics in Artificial Intelligence Fall 2008 Sept 11, 2008.
Games Development Game Architecture: Entities CO2301 Games Development 1 Week 22.
Computational theory techniques in interactive video games.
OMT Modeling 1. Object Model : presented by the object model and the data dictionary. 2. Dynamic Model: presented by the state diagrams and event flow.
Lecture 9-1 : Intro. to UML (Unified Modeling Language)
1 Technical & Business Writing (ENG-715) Muhammad Bilal Bashir UIIT, Rawalpindi.
Scis.regis.edu ● CS-432: Modern Software Engineering Week 4 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 10: Statecharts.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 CO Games Development 1 Week 3 Game Agents 2 Gareth Bellaby.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
COIT23003 Games Development 8. Elaboration: Behaviour.
Finite State Machines Logical and Artificial Intelligence in Games Lecture 3a.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
Laurea Triennale in Informatica – Corso di Ingegneria del Software I – A.A. 2006/2007 Andrea Polini VII. System Specification (I)
Chapter 14 Part 1: Core Game Mechanics By Nolan Driessen.
Requirements Specification
CompSci 280 S Introduction to Software Development
Collision Theory and Logic
Finite State Machines Dr K R Bond 2009
Collision Theory and Logic
Object-Oriented Software Engineering Using UML, Patterns, and Java,
Unified Modeling Language
Graph Coverage for Specifications CS 4501 / 6501 Software Testing
Lecture 2 Introduction to Programming
Introduction to Events
UML dynamic Modeling (Behavior Diagram)
Chapter 10 Object States and The Statechart Diagram
Graph Coverage for Specifications CS 4501 / 6501 Software Testing
Chapter 14 Part 1: Core Game Mechanics By Nolan Driessen
ECE 352 Digital System Fundamentals
Creating behaviours: MOVEMENT
Presentation transcript:

Dr Nick Mitchell (Room CM 224) CO1301: Games pts 2015 Lecture 5 Finite State Machines Dr Nick Mitchell (Room CM 224) email: npmitchell@uclan.ac.uk

References Rabin, Introduction to Game Development, chapter 5.3 discusses Finite State Machines in the context of AI.

States In the labs, I have informally introduced the idea of using states to model behaviour of objects in the game, or even the game itself. Whilst getting some practical experience of working with states we shall briefly look at some of the theory. States are a commonly used approach in computing. It is possible to express the “rules” for how the game progresses on State Transition Diagrams. They are an excellent design tool to help understand what it is you are going to program before you hit the keyboard

The State Model We can view all kinds of entities as having a number of possible states. Think of a light switch – its state can be on or off. Events trigger the transition from one state to another. Pressing the switch turns it on or off. In transitioning from one state to another, an entity might also perform an action. Certain actions cause an event to happen for another entity. Turning on too many lights at once might cause the fuse to blow. The purpose of modelling state-behaviour is to capture and enforce the “business rules” of entities in the game.

Usefulness of States for Games States help us express complex behaviour or activity. Example: Pac-Man A simple state model for NPCs (ghosts) controls their behaviour. Changes in behaviour are triggered externally. Player swallows a power pill (event) Ghosts’ behaviour changes (transition) to a different state (from Hunter to Prey). Further events will have different outcomes according to the current state For example, when player and ghost collide http://www.youtube.com/watch?v=uswzriFIf_k

Pac-Man: NPC Ghost (simplified) Hunter Hunted Eaten Power Pill Collision Timer Resurrect Start

Pac-Man: Player Character (simplified) Hunted Hunter Dying Power Pill Timer Collision If lives > 0 Start

State Machines A complete model of the life-cycle of an entity like this is called a “state machine”. A state is a unique set of properties. Particular behaviour will be executed in response to particular input when in a given state. State Input (event) Response (action and/or transition to a new state) In combination they form a unique configuration, this particular state with this particular input will lead to that particular outcome. E.g. collision between Ghost and Pac-Man has different outcomes in different states…

Why are states used? States describe the game extremely well. You are able to understand this aspect of the game mechanics. State Machines are flexible but also extremely powerful. A good way to implement the mechanics of a game (and other types of software). It is easy to store the states of entities as variables: One part of the program can respond to events by updating the values of the state variables; Another part of the program will decide how entities should behave according to those state values.

Why are states used? Aesthetically pleasing. The player experiences smooth game-play. Unobtrusive. Typically a player will not perceive the existence of the state machine. Even if it is the case that they do, state machines are accepted approach. Also used extensively in AI. You may look at this again (along with other AI techniques) next year. Can provide strong signals to the player, i.e. what has happened, what to do next, etc.

Multiple State Machines A program may have many different state machines. There may be state machines for the player character and different non-player characters. Each of these things can be considered an object (or entity) within the program. This approach allows us to easily describe (and therefore implement) the life cycle of an individual object. The game itself may have an overall state machine.

Pac-Man example re-visited For the one of the Ghost NPCs: In Hunter state Ghost will move towards Player (behaviour) Collision (event) will cause (action) Player to die (on Player’s STD as event causing transition) In Hunted state Ghost will move away from player (behaviour) Collision (same event – different effect) will cause Ghost to be eaten (transition to new state) and turn into a pair of eyes (action) In the Eaten state Ghost will move towards home area (behaviour) Arrival at home (event) will resurrect Ghost (transition and action)

Interacting State Machines There is interplay between the states of different entities within the game: An action performed by one entity may be regarded as an event for another entity. This insight leads on to aspects of Unified Modelling Language – UML You have seen some UML already with Lesley Used to support a design method used for Object Oriented Programming. You will see lots of this in CO2401 next year...

Basic UML STD notation State machines are visualised in UML as State Transition diagrams (STDs). Before creation Transition Event causing a state change Event not causing a state change Entity destroyed State showing action performed on entry Action performed by entity on transition State Action Event 1 / Action Event 3 Event 2 Start State End State

State Transition Diagrams are NOT Flow Charts (or Activity Diagrams)! State charts and Flow charts show quite different things. A flow chart is a way of modelling a business process. In UML this is done with an activity diagram. STDs are not algorithmic in the same way. The important thing to remember is that the EVENTS take place when the system is doing (or has done) something. Depending on the system, you can sometimes think of it as “resting” when it has arrived in any given state. The names you choose for states should reflect this… This is the opposite of flow or activity diagrams, where the boxes show “processing” happening.

STDs as a Code design tool It is a good idea to draw STDs to help you understand how entities will behave BEFORE you start coding. The state of an entity can be captures as a variable The possible values of the variable represent the different possible states. These can be taken straight from the diagram. Example: A sphere moving left and right between limits Left Right Reach left limit / change skin Reach right limit / change skin

Enumerated types in C++ enums are an elegant way to define values for states. The enum defines a new type, along with the possible values which can be assigned to variables of that type. enum DirectionState {Right, Left, Up, Down}; In the same way we can declare an int called foo with the value 5, we can declare variables of the new type. Int foo = 5; DirectionState sphereDirection = Right; Type Name Value The value of the variable is the current state.

Implementing STDs Faithfully Events cause state transitions and actions. Code is needed to detect events An event could be a key press, a collision, a counter reaching a certain value, etc, etc. Example: if (sphere->GetX() > rightLimit) { sphereDirection = Left; sphere->setSkin(“newSkin.jpg”); } This is a simple example - Note that the transition may be dependent on the current state. Event causing a state change Transition to a new state Action takes place

Behaviour according to State Once you have a state variable, it is easy to see what state the game/entity is in. Behaviour and transitions are determined by the state. if (sphereDirection == Right) { sphere->MoveX(speed); sphere->RotateZ(rotation); } else if (sphereDirection == Left) sphere->MoveX(-speed); sphere->RotateZ(-rotation); Appropriate behaviour Test to determine current state

Example outline structure of code Setup section: Define state variables (and initial states) Game loop section: Detect events update state variables perform transition actions Check state behave appropriately (e.g. Move objects etc.)