-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_state_machine.py
137 lines (119 loc) · 3.75 KB
/
game_state_machine.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class State:
"""
We define a state object which provides some utility functions for the
individual states within the state machine.
"""
def __init__(self):
# print('Processing current state:', str(self)) # This is printed every time a State is changed.
pass
def on_event(self, event):
"""
Handle events that are delegated to this State.
"""
pass
def __repr__(self):
"""
Leverages the __str__ method to describe the State.
"""
return self.__str__()
def __str__(self):
"""
Returns the name of the State.
"""
return self.__class__.__name__
class PlayerTurn(State):
def on_event(self, event):
if event == 'player_dead':
return PlayerDead()
elif event == 'player_acted':
return EnemyTurn()
elif event == 'skill_selected':
return TargetingState()
elif event == 'exit':
return Exit()
elif event == 'compare_items':
return CompareItems()
elif event == 'consume_soul':
return ConsumeSoul()
elif event == 'open_character_sheet':
return CharacterSheet()
elif event == 'victory':
return VictoryScreen()
return self
class Exit(State):
def on_event(self, event):
return self
class TargetingState(State):
def on_event(self, event):
if event == 'exit':
return Exit()
elif event == 'chose_direction':
return PlayerTurn()
elif event == 'cancel_targeting':
return PlayerTurn()
return self
class PlayerDead(State):
def on_event(self, event):
if event == 'exit':
return Exit()
return self
class EnemyTurn(State):
def on_event(self, event):
if event == 'exit':
return Exit()
elif event == 'enemies_acted':
return PlayerTurn()
return self
class CompareItems(State):
def on_event(self, event):
if event == 'exit':
return Exit()
elif event == 'done_comparing':
return PlayerTurn()
return self
class ConsumeSoul(State):
def on_event(self, event):
if event == 'exit':
return Exit()
elif event == 'done_consuming':
return PlayerTurn()
return self
class CharacterSheet(State):
def on_event(self, event):
if event == 'exit':
return Exit()
elif event == 'close_character_sheet':
return PlayerTurn()
return self
class VictoryScreen(State):
def on_event(self, event):
if event == 'exit':
return Exit()
return self
class OpeningScreen(State):
def on_event(self, event):
if event == 'exit':
return Exit()
elif event == 'begin':
return PlayerTurn()
return self
class GameStateMachine:
"""
A simple state machine that mimics the functionality of a device from a
high level.
"""
def __init__(self):
""" Initialize the components. """
# Start with a default state.
self.state = OpeningScreen()
def on_event(self, event):
"""
This is the bread and butter of the state machine. Incoming events are
delegated to the given states which then handle the event. The result is
then assigned as the new state.
"""
# The next state will be the result of the on_event function.
self.state = self.state.on_event(event)
# print('Current event:', str(event)) # This is printed every time a State is changed.
# print('Current state:', str(self.state)) # This is printed every time a State is changed.
return self.state