/
Enemy.py
203 lines (137 loc) · 4.67 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
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
import nilutils as nl
from mathutils import Vector
import random as rand
from bge import logic
from bge import logic
import aud
class Follow(nl.Component):
def __init__(self, gobj):
super().__init__(gobj)
self.state("track")
def track(self):
players = [p for p in self.objects if "player" in p and \
not self.gobj.rayCast(p, self.gobj, 0, "wall")[0] and \
self.gobj.getDistanceTo(p) < self.gobj.sight_distance]
players.sort(key=lambda p: self.gobj.getDistanceTo(p))
if len(players):
closest = players[0]
self.gobj.closest_player = closest
else:
self.gobj.closest_player = None
class Hurting(nl.Component):
def __init__(self, gobj):
super().__init__(gobj)
self.device = aud.device()
sound = logic.expandPath("//sounds/enemy_hurt.ogg")
factory = aud.Factory(sound)
self.factory = aud.Factory.buffer(factory)
self.collision = self.gobj.collision
self.state("damage_calc")
def damage_calc(self):
self.gobj.got_hit = False
if self.collision.positive:
hit_list = [b for b in self.collision.hitObjectList if "bullet" in b and\
b['bullet']]
self.gobj.health -= len(hit_list)
if len(hit_list):
self.gobj.got_hit = True
for b in hit_list:
b.endObject()
handle = self.device.play(self.factory)
handle.volume = 0.3
return
if self.gobj.health <= 0:
self.gobj.dead = True
return
class Movement(nl.Component):
def __init__(self, gobj):
super().__init__(gobj)
self.patrol_ticker = 0
self.patrol_sets = [Vector((1 ,0 , 0)), Vector((0, 1, 0))]
self.patrol_speed = 5
self.patrol_vect = Vector((0 ,0 ,0))
self.collision = self.gobj.sensors['Collision']
self.follow_speed = self.patrol_speed * 1.6
self.state("randomize_patrol")
def patrol(self):
self.gobj.attack = False
self.gobj.worldLinearVelocity = self.patrol_vect*self.patrol_speed
self.gobj.alignAxisToVect(self.patrol_vect, 1, 0.5)
self.gobj.alignAxisToVect(Vector((0, 0, 1)), 2, 1)
if self.patrol_ticker():
self.patrol_vect *= -1
if self.gobj.closest_player:
self.gobj.worldLinearVelocity.magnitude = 0
self.state("follow_player")
if self.gobj.dead:
self.state("dead")
def track_to(self, other):
delta = other.worldPosition - self.gobj.worldPosition
delta.normalize()
self.gobj.alignAxisToVect(delta, 1, 1)
self.gobj.alignAxisToVect(Vector((0 , 0, 1)), 2, 1)
def dead(self):
self.gobj.components = None
self.gobj.endObject()
return
def follow_player(self):
hit_list = []
if self.collision.positive:
hit_list = [p for p in self.collision.hitObjectList if "player" in p]
self.gobj.attack = True
if self.gobj.closest_player:
if self.gobj.getDistanceTo(self.gobj.closest_player) < self.gobj.stop_distance or\
len(hit_list):
self.gobj.localLinearVelocity.y = 0
else:
self.gobj.localLinearVelocity.y = self.follow_speed
self.gobj.localLinearVelocity.magnitude = self.follow_speed
self.track_to(self.gobj.closest_player)
else:
self.state("randomize_patrol")
self.gobj.localLinearVelocity.magnitude = 0
return
if self.gobj.dead:
self.state("dead")
def randomize_patrol(self):
self.patrol_ticker = nl.Ticker(rand.randint(2,4))
self.patrol_vect = rand.choice(self.patrol_sets)
self.state("patrol")
class Animation(nl.Component):
def __init__(self, gobj):
super().__init__(gobj)
self.armature = [a for a in self.gobj.children if "armature" in a][0]
self.mode = logic.KX_ACTION_MODE_LOOP
self.attack_animation = self.gobj['who']+"_attack"
self.movement_animation = self.gobj['who']+"_movement"
self.attack_frames = self.gobj.attack_frames
self.movement_frames = self.gobj.movement_frames
self.state("wait")
def wait(self):
if self.gobj.attack:
self.armature.playAction(self.attack_animation, \
self.attack_frames[0], \
self.attack_frames[1], 1, 1, 1.0, self.mode, 0, 1, 1)
else:
self.armature.playAction(self.movement_animation, \
self.movement_frames[0], \
self.movement_frames[1], 1, 1, 1.0, self.mode, 0, 1, 1)
class Enemy(nl.GameObject):
def __init__(self, own):
self.health_max = 20
self.health = self.health_max
self.dead = False
self.attack_frames = [0, 0]
self.movement_frames = [0, 0]
self.closest_player = None
self.stop_distance = self['stop_dist']
self.sight_distance = self['sight_dist']
self.collision = self.sensors['Collision']
follow = Follow(self)
hurting = Hurting(self)
self.attack = False
self.got_hit = False
self.components = [follow, hurting]
if self['enemy'] != "goat":
movement = Movement(self)
self.components.append(movement)