-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.py
179 lines (105 loc) · 3.98 KB
/
Entity.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
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
from pygame import draw
from Vector2 import Vector2
from Circle import Circle
import random
import math
import weakref
# This class describes game objects in our game
class Entity(object):
def __init__(self):
# R G B
# default color is white
self.color = (255, 255, 255)
self.graphicsBounds = Circle(Vector2(0.0, 0.0), 1)
self.collider = Circle(Vector2(0.0, 0.0), 1)
self.position = Vector2(0.0, 0.0)
self.velocity = Vector2(0.0, 0.0)
self.acceleration = Vector2(0.0, 0.0)
self.max_speed = 400
def update(self, dt):
self.velocity += self.acceleration * dt
self.position += self.velocity * dt
# cap speed
if self.velocity.magnitude() >= self.max_speed:
self.velocity.set_magnitude(self.max_speed)
# Update graphical and physical bounds
self.graphicsBounds.center = self.position
self.collider.center = self.position
def render(self, screen):
center = self.graphicsBounds.center
radius = self.graphicsBounds.radius
# Pygame needs a 2-tuple of integers as the center of a circle
center_int = (int(center.x), int(center.y))
draw.circle(screen, self.color, center_int, radius)
class Bullet(Entity):
def __init__(self, range, position, velocity):
# Construct parent so we have all of the Entities properties
# like position, velocity...
super(Bullet, self).__init__()
# How far the bullet travels
self.range = range
self.position = Vector2(position.x, position.y)
self.velocity = Vector2(velocity.x, velocity.y)
# The time needed to travel a certain range
self.life = range / self.velocity.magnitude()
def update(self, dt):
# Call parent update so we can still move
super(Bullet, self).update(dt)
# Kill the bullet slowly
self.life -= dt
class Asteroid(Entity):
def __init__(self, target, homing_speed):
super(Asteroid, self).__init__()
self.target = weakref.ref(target)
self.homing_speed = homing_speed
def update(self, dt):
super(Asteroid, self).update(dt)
self.follow()
def follow(self):
if self.target() is None:
return
to_target = Vector2.get_normal(self.target().position - self.position)
self.acceleration = to_target * self.homing_speed
class Particle(Entity):
def __init__(self, life, position, velocity):
super(Particle, self).__init__()
self.life = life
self.position = Vector2(position.x, position.y)
self.velocity = Vector2(velocity.x, velocity.y)
self.active = False
def update(self, dt):
if not self.active:
return
super(Particle, self).update(dt)
self.life -= dt
if self.life <= 0:
self.active = False
def render(self, screen):
if self.active:
super(Particle, self).render(screen)
class BurstEmitter(Entity):
def __init__(self, particle_count, max_life):
self.particles = list()
for i in xrange(0, particle_count):
self.particles.append(Particle(1, Vector2(0, 0), Vector2(0, 0)))
self.life = max_life
def update(self, dt):
for p in self.particles:
p.update(dt)
self.life -= dt
def render(self, screen):
for p in self.particles:
p.render(screen)
def emit(self, position):
for p in self.particles:
p.active = True
p.position = Vector2(position.x, position.y)
p.velocity = Vector2(1, 0)
p.velocity.set_direction(random.uniform(0, 2 * math.pi))
p.velocity.set_magnitude(random.randint(200, 500))
size = random.randint(2, 8)
p.graphicsBounds.radius = size
p.life = random.uniform(0.2, 2.5)
p.color = (0, 255, 0)
def is_done(self):
return self.life <= 0