-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.py
114 lines (77 loc) · 2.64 KB
/
enemy.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
from OpenGL.GL import *
import numpy
import entity
import model
import bullets
import particle
class Alien1(entity.Entity):
"""The first alien enemy. Slow. Shoots at the player every once in a
while"""
model = None
# This alien's starting health
health = 3
def __init__(self, target):
if Alien1.model is None:
# Initialize the model for this ship
Alien1.model = model.ObjModel("alien_torus.obj", scale=10)
super(Alien1, self).__init__(Alien1.model, 20)
# This is who we're shooting at
self.player = target
self.health = Alien1.health
# Choose a position to spawn
self.pos = numpy.array([0,0,0],dtype=float)
# Rotation around its axis, this simply increases with time
self.rot = 0
# its velocity
self.vel = numpy.zeros((3,))
# When this countdown reaches 0, the alien ship will re-direct itself
# towards the player
self.redirect_countdown = 100
self.bullet_countdown = 80
self.bullets = bullets.Bullets(color=(1,0,0,1))
def damage(self, amt):
"""Damages the alien.
Returns True if the alien is destroyed.
"""
self.health -= amt
if self.health <= 0:
particle.explosion(self.pos, (10,0,0), self.vel)
return True
return False
def update(self):
self.bullets.update()
self.rot += 5
if self.rot >= 360:
self.rot -= 360
self.pos += self.vel
self.redirect_countdown -= 1
self.bullet_countdown -= 1
# Gradually slow down our speed
self.vel *= 0.995
if self.redirect_countdown <= 0:
self.redirect_countdown = 200
newdir = self.player.pos - self.pos
# normalize to a constant speed
newdir /= numpy.linalg.norm(newdir)
newdir *= 3
self.vel = newdir
if self.bullet_countdown <= 0:
# Fire a bullet towards the player
self.bullet_countdown = 80
bulvel = self.player.pos - self.pos
# normalize to a constant speed
bulvel /= numpy.linalg.norm(bulvel)
bulvel *= self.bullets.speed
self.bullets.fire(self.pos, bulvel)
def draw(self):
glPushMatrix()
# Translate to the right pos
glTranslated(*self.pos)
# Rotate slightly for a better view
#glRotated(20, 1, 0, 0)
# The turning rotations
glRotated(self.rot, 0, 1, 0)
# Finally, draw the thing
self.model.draw()
glPopMatrix()
self.bullets.draw()