-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
356 lines (238 loc) · 11.6 KB
/
main.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from bge import logic, events, types, render
import math, mathutils
import numpy
import aud
import random
import utils
#sound = aud.Factory.file(logic.expandPath("//sound/Adventure Meme.mp3")).volume(.1).loop(-1)
#sound = aud.Factory.file(logic.expandPath("//sound/Unwritten Return.mp3")).volume(.5).loop(-1)
sound = aud.Factory.file(logic.expandPath("//sound/Destiny Day.mp3")).volume(.5).loop(-1)
music = aud.device().play(sound)
dict = logic.globalDict
playername = "player"
scene = logic.getCurrentScene()
levels = [{"name": 'level1',
"music_path": '//sound/Destiny Day.mp3',
"volume": .5},
{"name": 'level2',
"music_path": '//sound/Destiny Day.mp3',
"volume": .5},
{"name": 'level3',
"music_path": '//sound/Unwritten Return.mp3',
"volume": .5},
{"name": 'level4',
"music_path": '//sound/Unwritten Return.mp3',
"volume": .5},
{"name": 'level5',
"music_path": '//sound/Adventure Meme.mp3',
"volume": .1},
]
dict["current_level"] = 0
class Sentry(types.KX_GameObject):
def __init__(self, own):
self.cont = self.controllers[0]
self.target = logic.getCurrentScene().objects[playername]
self.fireRate = self.get("fireRate", .1)
self.range = self.get("range", 20)
self.leading_factor = self.get("leading_factor", 0)
self.projectile_speed = 1 # multiplier for default projectile speed
self.projectile_type = self.get("projectile", "standard_projectile")
print(self.name, "using", self.projectile_type)
self.firenow = random.random()
self.mparts = []
for child in self.children:
if "firepoint" in child.name:
self.firepoint = child
if "mpart" in child.name:
self.mparts.append(child)
def aim(self):
distance = utils.velocity2speed(self.target.worldPosition - self.worldPosition)
if distance < self.range:
hit = self.rayCast(self.target, self, 0.0, "solid", 0, 1, 0)
if hit[0] == self.target:
self["cansee"] = True
leading_vec = mathutils.Vector(self.worldPosition.lerp(self.target.worldPosition + self.target.getLinearVelocity() * self.leading_factor, 100))
self.alignAxisToVect(-leading_vec, 0, 1)
else:
self["cansee"] = False
else:
self["cansee"] = False
return self["cansee"]
def fire(self):
projectile = logic.getCurrentScene().addObject(self.projectile_type, self.firepoint, 0)
projectile.worldOrientation = self.worldOrientation
# projectile.speed = projectile.speed * self.projectile_speed
for part in self.mparts:
part.playAction(name="sentry", start_frame=1, end_frame=4)
self.firenow = 0
def main(self):
aim = self.aim()
if self.firenow > 1 and aim:
self.fire()
self.firenow += 1*self.fireRate*logic.getTimeScale()
class Projectile(types.KX_GameObject):
def __init__(self, own, sound):
player = logic.getCurrentScene().objects[playername]
self.sound = sound
self.speed = self.get("speed", 30)*-1
self.homing_factor = 0
self.collisionCallbacks.append(self.on_collision)
self.sound_device = aud.device()
self.sound_device.distance_model = aud.AUD_DISTANCE_MODEL_LINEAR
self.sound_device.listener_location = player.worldPosition
self.sound_device.listener_velocity = player.getLinearVelocity()
self.sound_device.doppler_factor = 3
self.sound_handle = self.sound_device.play(self.sound)
self.sound_handle.relative = False
self.sound_handle.location = self.worldPosition
self.sound_handle.velocity = self.getLinearVelocity()
self.sound_handle.distance_maximum = 100
self.sound_handle.distance_reference = 1
def on_collision(self, object, point, normal):
if "solid" in object:
# Flip sign of normal if collision with triangle mesh
# to workaround this bug: https://developer.blender.org/T47036
if "floor_triangle_mesh" in object.name:
normal = normal*-1
d = mathutils.Vector(self.getLinearVelocity())
n = mathutils.Vector(normal)
# d = d * d.dot(n)
# determine reflection direction
r = d - ((2 * d * n) / (n*n)) * n
vectsplosion = logic.getCurrentScene().addObject("vectsplosion")
vectsplosion["point"] = point
vectsplosion["direction"] = r
vectsplosion["color"] = (1, .5, .5)
if object == logic.getCurrentScene().objects[playername]:
player_death()
else:
self.endObject()
def main(self):
self.setLinearVelocity((self.speed, 0, 0), True)
if self.sound_handle:
self.sound_handle.pitch = utils.map_range(logic.getTimeScale() + utils.map_range(random.random(), to_min=-.1, to_max=.1), .05, 1, .2, 1)
class Door(types.KX_GameObject):
def __init__(self, own):
self.total_time = 0
self.cycle_time = 0
self.open_duration = self.get("open_duration", 100)
self.close_duration = self.get("close_duration", 100)
self.transition_duration = self.get("transition_duration", 50)
self.doorstate = "open"
self.parts = []
self.consts = []
for o in self.children:
if "constant" not in o:
self.parts.append(o)
else:
self.consts.append(o)
def main(self):
if self.doorstate == "open":
if self.cycle_time > self.open_duration:
self.doorstate = "closing"
self.cycle_time = 0
elif self.doorstate == "closed":
if self.cycle_time > self.close_duration:
self.doorstate = "opening"
self.cycle_time = 0
elif self.doorstate == "opening":
time = 1-(self.cycle_time/self.transition_duration)
for o in self.parts:
o['anim_time'] = time
if self.cycle_time > self.transition_duration:
self.doorstate = "open"
for o in self.parts:
o['anim_time'] = 0
elif self.doorstate == "closing":
time = self.cycle_time/self.transition_duration
for o in self.parts:
o['anim_time'] = time
if self.cycle_time > self.transition_duration:
self.doorstate = "closed"
for o in self.parts:
o['anim_time'] = 1
for o in self.consts:
o['anim_time'] = self.total_time
self.cycle_time += 1 * logic.getTimeScale()
self.total_time += 1 * logic.getTimeScale()
class Vectsplosion(types.KX_GameObject):
def __init__(self, own):
self.origin = mathutils.Vector(self["point"])
self.direction = mathutils.Vector(self["direction"])
self.rcolor = self["color"]
self.time = 0
self.lines = []
for i in range(random.randrange(3, 5)):
self.lines.append(self.origin + self.direction + mathutils.Vector(numpy.random.normal(0, 5, 3)))
def main(self):
for line in self.lines:
render.drawLine(self.origin.lerp(line, utils.clamp(self.time*2-.5)), self.origin.lerp(line, self.time), self.rcolor)
self.time += .1*logic.getTimeScale()
if self.time >= 1:
self.endObject()
class Player(types.KX_GameObject):
keyboard = logic.keyboard
def __init__(self, own):
self.alive = True
self.recouperating = False
self.movement_speed = 20
self.light = logic.getCurrentScene().objects["player_light"]
self.trail = logic.getCurrentScene().addObject("light_trail")
self.counter = 1
def movement(self, keyboard=keyboard):
ACTIVE = logic.KX_INPUT_ACTIVE
movement = self.getLinearVelocity()
# forward
if keyboard.events[events.WKEY] == ACTIVE:
movement[1] = utils.clamp(movement[1] + self.movement_speed*.1, -self.movement_speed, self.movement_speed)
# backward
if keyboard.events[events.SKEY] == ACTIVE:
movement[1] = utils.clamp(movement[1] + -self.movement_speed*.1, -self.movement_speed, self.movement_speed)
# left
if keyboard.events[events.AKEY] == ACTIVE:
movement[0] = utils.clamp(movement[0] + -self.movement_speed*.1, -self.movement_speed, self.movement_speed)
# right
if keyboard.events[events.DKEY] == ACTIVE:
movement[0] = utils.clamp(movement[0] + self.movement_speed*.1, -self.movement_speed, self.movement_speed)
#self.setLinearVelocity(movement * ((logic.getTimeScale()-1)**4+1))
self.setLinearVelocity(movement)
# cosmetic spinny stuff
if self.getAngularVelocity() < mathutils.Vector((1,1,1)):
self.setAngularVelocity(numpy.random.normal(0, 5, 3))
def main(self):
if self.alive:
velocity = self.getLinearVelocity()
speed = utils.velocity2speed(velocity)
timescale = utils.clamp((speed/self.movement_speed)**2, .05, 1)
if timescale != logic.getTimeScale():
logic.setTimeScale(timescale)
self.light.energy = (math.sin(logic.getRealTime())*.3) + .7
self.movement()
else:
self.light.energy = self.light.energy * utils.clamp(float(self.counter), .5, 1)
music.pitch = utils.clamp(self.counter)
self.counter -= .01
if self.alive == False and self.counter < 0:
logic.sendMessage("player_death")
self.alive = True
self.recouperating = True
if self.recouperating and self.alive == True:
self.light.energy = ((math.sin(logic.getRealTime())*.3) + .7) * self.counter
music.pitch = utils.clamp(self.counter)
self.counter += .05
if self.counter >= 1:
self.recouperating = False
music.pitch = 1
def nextlevel():
global music
dict["current_level"] += 1
lvl = dict["current_level"]
print("going to level", lvl)
logic.getCurrentScene().replace(levels[lvl]['name'])
music_factory = aud.Factory.file(logic.expandPath(levels[lvl]['music_path'])).volume(levels[lvl]['volume']).loop(-1)
if levels[lvl]['music_path'] != levels[lvl-1]['music_path']:
music.stop()
music = aud.device().play(music_factory)
def player_death():
logic.setTimeScale(.03)
logic.getCurrentScene().objects[playername].alive = False