-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.py
More file actions
296 lines (225 loc) · 7.9 KB
/
state.py
File metadata and controls
296 lines (225 loc) · 7.9 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from random import randint
from gameobjects.vector2 import Vector2
from config import SCREEN_SIZE, NEST_POSITION, NEST_SIZE
class State(object):
"""
Base Class for a State
"""
def __init__(self, name):
self.name = name
def do_actions(self):
pass
def check_conditions(self):
pass
def entry_actions(self):
pass
def exit_actions(self):
pass
class StateMachine(object):
"""
The class that manage all states.
The StateMachine class stores an instance of each of the states in a dictionary and manages the currently active
state.
"""
def __init__(self):
"""
Class constructor that initialize states dictionary and set state to none.
"""
self.states = {}
self.active_state = None
def add_state(self, state):
"""
Add a instance to the internal dictionary.
:param state: the name of the state
"""
self.states[state.name] = state
def think(self):
"""
Runs once per frame,
:return:
"""
# Only continue if there is an active state
if self.active_state is None:
return
# Perform the actions of the active state, and check conditions
self.active_state.do_actions()
new_state_name = self.active_state.check_conditions()
if new_state_name is not None:
self.set_state(new_state_name)
def set_state(self, new_state_name):
"""
Change states and perform any exit / entry actions.
:param new_state_name: name of the state to be set
"""
if self.active_state is not None:
self.active_state.exit_actions()
self.active_state = self.states[new_state_name]
self.active_state.entry_actions()
class AntStateExploring(State):
"""
The Exploring State for Ants.
The ant moves randomly over the screen area.
"""
def __init__(self, ant):
"""
Call the base class constructor to initialize the State.
:param ant: ant instance that this state belongs to
"""
State.__init__(self, "exploring")
self.ant = ant
def random_destination(self):
"""
Select a point randomly in the screen
"""
w, h = SCREEN_SIZE
self.ant.destination = Vector2(randint(0, w), randint(0, h))
def do_actions(self):
"""
Change ant direction, 1 in 20 calls
"""
if randint(1, 20) == 1:
self.random_destination()
def check_conditions(self):
"""
Check conditions of the ant and environment to decide if state should change.
"""
# If there is a nearby leaf, switch to seeking state
leaf = self.ant.world.get_close_entity("leaf", self.ant.location)
if leaf is not None:
self.ant.leaf_id = leaf.id
return "seeking"
# If there is a nearby spider, switch to hunting state
spider = self.ant.world.get_close_entity("spider", NEST_POSITION, NEST_SIZE)
if spider is not None:
if self.ant.location.get_distance_to(spider.location) < 100.:
self.ant.spider_id = spider.id
return "hunting"
return None
def entry_actions(self):
"""
Actions executed by the ant when it enter explore state.
"""
# Start with random speed and heading
self.ant.speed = 120. + randint(-30, 30)
self.random_destination()
class AntStateSeeking(State):
"""
The Seeking State for Ants.
The ant moves toward a leaf that gets closer to it.
"""
def __init__(self, ant):
"""
Call the base class constructor to initialize the State.
:param ant: ant instance that this state belongs to
"""
State.__init__(self, "seeking")
self.ant = ant
self.leaf_id = None
def check_conditions(self):
"""
Check conditions of the ant and environment to decide if state should change.
"""
# If the leaf is gone, then go back to exploring
leaf = self.ant.world.get(self.ant.leaf_id)
if leaf is None:
return "exploring"
# If we are next to the leaf, pick it up and deliver it
if self.ant.location.get_distance_to(leaf.location) < 5.0:
self.ant.carry(leaf.image)
self.ant.world.remove_entity(leaf)
return "delivering"
return None
def entry_actions(self):
"""
Actions executed by the ant when it enter explore state.
"""
# Set the destination to the location of the leaf
leaf = self.ant.world.get(self.ant.leaf_id)
if leaf is not None:
self.ant.destination = leaf.location
self.ant.speed = 160. + randint(-20, 20)
class AntStateDelivering(State):
"""
The Delivering State for Ants.
The ant moves toward the nest with the collected object.
"""
def __init__(self, ant):
"""
Call the base class constructor to initialize the State.
:param ant: ant instance that this state belongs to
"""
State.__init__(self, "delivering")
self.ant = ant
def check_conditions(self):
"""
Check conditions of the ant and environment to decide if state should change.
"""
# If inside the nest, randomly drop the object
if Vector2(*NEST_POSITION).get_distance_to(self.ant.location) < NEST_SIZE:
if randint(1, 10) == 1:
self.ant.drop(self.ant.world.background)
return "exploring"
return None
def entry_actions(self):
"""
Actions executed by the ant when it enter explore state.
"""
# Move to a random point in the nest
self.ant.speed = 60.
random_offset = Vector2(randint(-20, 20), randint(-20, 20))
self.ant.destination = Vector2(*NEST_POSITION) + random_offset
class AntStateHunting(State):
"""
The Delivering State for Ants.
The ant moves toward the spider with random speed.
"""
def __init__(self, ant):
"""
Call the base class constructor to initialize the State.
:param ant: ant instance that this state belongs to
"""
State.__init__(self, "hunting")
self.ant = ant
self.got_kill = False
self.speed = 0
def do_actions(self):
"""
Check conditions of the ant and environment to decide if state should change.
"""
spider = self.ant.world.get(self.ant.spider_id)
if spider is None:
return
self.ant.destination = spider.location
if self.ant.location.get_distance_to(spider.location) < 15.:
# Give the spider a fighting chance
if randint(1, 5) == 1:
spider.bitten()
# If the spider is dead, move it back to the nest
if spider.health <= 0:
self.ant.carry(spider.image)
self.ant.world.remove_entity(spider)
self.got_kill = True
def check_conditions(self):
"""
Check conditions of the ant and environment to decide if state should change.
"""
if self.got_kill:
return "delivering"
spider = self.ant.world.get(self.ant.spider_id)
# If the spider has been killed then return to exploring state
if spider is None:
return "exploring"
# If the spider gets far enough away, return to exploring state
if spider.location.get_distance_to(NEST_POSITION) > NEST_SIZE * 3:
return "exploring"
return None
def entry_actions(self):
"""
Actions executed by the ant when it enter hunting state.
"""
self.speed = 160. + randint(0, 50)
def exit_actions(self):
"""
Exit state action.
"""
self.got_kill = False