Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. '''
  2. Created on Oct 20, 2017
  3. Simple State Machine
  4.  
  5. @author: Begining Game Development with Python using Pygame Book
  6. '''
  7.  
  8. class State(object):
  9.    
  10.     def __init__(self, name):        
  11.         self.name = name
  12.        
  13.     def update(self, dt_s):
  14.         pass
  15.        
  16.     def check_conditions(self):        
  17.         pass    
  18.    
  19.     def entry_actions(self):        
  20.         pass    
  21.    
  22.     def exit_actions(self):        
  23.         pass
  24.  
  25.  
  26. class State_Machine(object):
  27.    
  28.     def __init__(self):
  29.        
  30.         self.states = {}
  31.         self.active_state = None
  32.  
  33.     def add_state(self, state):
  34.         self.states[state.name] = state
  35.          
  36.     def update(self, dt):
  37.  
  38.         if self.active_state is None:
  39.             return
  40.  
  41.         self.active_state.update(dt)        
  42.        
  43.         new_state_name = self.active_state.check_conditions()
  44.  
  45.         if new_state_name is not None:
  46.             self.set_state(new_state_name)
  47.        
  48.    
  49.     def set_state(self, new_state_name):
  50.        
  51.         if self.active_state is not None:
  52.             self.active_state.exit_actions()
  53.            
  54.         self.active_state = self.states[new_state_name]        
  55.         self.active_state.entry_actions()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement