-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentity.py
More file actions
188 lines (143 loc) · 4.92 KB
/
entity.py
File metadata and controls
188 lines (143 loc) · 4.92 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
from config import SCREEN_SIZE, SPIDER_HEALTH
from random import randint
from gameobjects.vector2 import Vector2
from state import StateMachine, AntStateExploring, AntStateSeeking, AntStateDelivering, AntStateHunting
class SimulationEntity(object):
"""
The Base Class for the simulation Entity.
"""
def __init__(self, world, name, image):
"""
Constructor of the SimulationEntity instance.
:param world: The world which the entity belongs
:param name: Name of the entity
:param image: Sprite of the entity
"""
self.world = world
self.name = name
self.image = image
self.location = Vector2(0, 0)
self.destination = Vector2(0, 0)
self.speed = 0.
self.id = 0
self.brain = StateMachine()
def render(self, surface):
"""
Draw the entity.
:param surface: pygame surface object
"""
x, y = self.location
w, h = self.image.get_size()
surface.blit(self.image, (x - w / 2, y - h / 2))
def process(self, time_passed):
"""
Process the entity data.
:param time_passed: Time passed since the last render
"""
self.brain.think()
if self.speed > 0. and self.location != self.destination:
vec_to_destination = self.destination - self.location
distance_to_destination = vec_to_destination.get_length()
heading = vec_to_destination.get_normalized()
travel_distance = min(distance_to_destination, time_passed * self.speed)
self.location += travel_distance * heading
class Ant(SimulationEntity):
"""
The Ant Entity Class
"""
def __init__(self, world, image):
"""
Constructor of the Ant instance.
:param world: The world which the entity belongs
:param image: Sprite of the Ant
"""
SimulationEntity.__init__(self, world, "ant", image)
exploring_state = AntStateExploring(self)
seeking_state = AntStateSeeking(self)
delivering_state = AntStateDelivering(self)
hunting_state = AntStateHunting(self)
self.brain.add_state(exploring_state)
self.brain.add_state(seeking_state)
self.brain.add_state(delivering_state)
self.brain.add_state(hunting_state)
self.carry_image = None
def carry(self, image):
"""
Set carry image.
:param image: the carry image
"""
self.carry_image = image
def drop(self, surface):
if self.carry_image:
x, y = self.location
w, h = self.carry_image.get_size()
surface.blit(self.carry_image, (x - w, y - h / 2))
self.carry_image = None
def render(self, surface):
"""
Draw the ant.
:param surface: pygame surface object
"""
SimulationEntity.render(self, surface)
if self.carry_image:
x, y = self.location
w, h = self.carry_image.get_size()
surface.blit(self.carry_image, (x - w, y - h / 2))
class Leaf(SimulationEntity):
"""
The Leaf Entity Class
"""
def __init__(self, world, image):
"""
Constructor of the Leaf instance.
:param world: The world which the entity belongs
:param image: Sprite of the Leaf
"""
SimulationEntity.__init__(self, world, "leaf", image)
class Spider(SimulationEntity):
"""
The Spider Entity Class
"""
def __init__(self, world, image, dead_image):
"""
Constructor of the Spider instance.
:param world: The world which the entity belongs
:param image: Sprite of the default spider
:param dead_image: Sprite of the dead spider
"""
SimulationEntity.__init__(self, world, "spider", image)
self.dead_image = dead_image
self.health = SPIDER_HEALTH
self.speed = 50. + randint(-20, 20)
def bitten(self):
"""
Execute when spider has been bitten.
"""
self.health -= 1
if self.health <= 0:
self.speed = 0.
self.image = self.dead_image
self.speed = 140.
def render(self, surface):
"""
Draw the spider.
:param surface: pygame surface object
"""
SimulationEntity.render(self, surface)
# Draw a health bar
x, y = self.location
w, h = self.image.get_size()
bar_x = x - 12
bar_y = y + h / 2
surface.fill((255, 0, 0), (bar_x, bar_y, 25, 4))
surface.fill((0, 255, 0), (bar_x, bar_y, self.health, 4))
def process(self, time_passed):
"""
Process the spider data.
:param time_passed: Time passed since the last render
"""
x, y = self.location
if x > SCREEN_SIZE[0] + 2:
self.world.remove_entity(self)
return
SimulationEntity.process(self, time_passed)