-
Notifications
You must be signed in to change notification settings - Fork 1
/
statemachine.py
123 lines (76 loc) · 2.9 KB
/
statemachine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
statemachine.py
Contains the classes Manager and State for implementing state machine sprites.
"""
import pygame
from pygame.sprite import Sprite
class Manager(Sprite):
"""Manager is a Sprite which serves as a universal accessor for the states.
It contains integer constants for identifying the states as well as the methods
for updating, drawing and transitioning.
Children of Manager will need to implement the transition function
"""
#default state number; means that the manager does not have an active state
STATELESS = -1
def __init(self):
Sprite.__init__(self)
self.current_state = None
self.rect = None
self.image = None
self.mask = None
def update(self):
if self.current_state is None: return
self.current_state.update()
self.update_sprite_attributes()
def update_sprite_attributes(self):
"""updates image, rect, and mask to those of the current state
"""
self.image = self.current_state.get_image()
self.rect = self.current_state.get_rect()
self.mask = self.current_state.get_mask()
def draw(self, screen):
screen.blit(self.image, self.rect)
def start_transition(self, new_state_number):
"""causes the current state to transition to the state
corresponding to new_state_number
"""
return self.current_state.transition_to(new_state_number)
def change_state(self, new_state):
self.current_state = new_state
self.update_sprite_attributes()
def get_state(self):
return self.current_state
def get_state_number(self):
"""Returns a number corresponding to the current state.
"""
return self.current_state.STATE_NUMBER
def get_rect(self):
return self.rect
class State():
"""State contains the behavior of one state as well as
that state's rules for transitioning to other states.
Note: State.sprite is the actual Sprite being drawn,
but most behavior will be handled within State
"""
def __init__(self, manager):
self.manager = manager
self.sprite = None
self.state_number = Manager.STATELESS
def update(self):
pass
def get_rect(self):
if self.sprite is None: return None
return self.sprite.rect
def get_image(self):
if self.sprite is None: return None
return self.sprite.image
def get_mask(self):
if self.sprite is None: return None
return self.sprite.mask
def transition_to(self, new_state_number):
"""Contains the state's transition rules and behavior. This will generally
involve calling some method in the Manager to start the new state.
returns True if the transition to new_state_number's state is allowed
otherwise returns False
"""
return False