-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscene.py
More file actions
executable file
·569 lines (462 loc) · 16.5 KB
/
scene.py
File metadata and controls
executable file
·569 lines (462 loc) · 16.5 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env python
import functools
import glm
import pygame
from game.base.signal import Signal, Slot, SlotList
from game.base.when import When
from os import path
from pygame import Color
from game.constants import *
from glm import vec3, vec4, ivec4, ivec3
from game.base.script import Script
from game import util
from game.util import (
clamp,
ncolor,
pg_color,
rand_RGB,
rgb_mix,
noise_surf,
noise_surf_dense_bottom,
)
from game.entities.cloud import Cloud
from game.entities.rock import Rock
from game.entities.rain import Rain
from game.entities.star import Star
from game.entities.ground import Ground
from random import randint
import math
import weakref
import random
# key function to do depth sort
z_compare = functools.cmp_to_key(lambda a, b: a.get().position.z - b.get().position.z)
class Scene(Signal):
def __init__(self, app, state, script=None, script_args=None):
super().__init__()
self.max_particles = 32
self.app = app
self.state = state
self.when = When()
self.slotlist = SlotList()
self._sky_color = None
self.ground = None
self._ground_color = None
self._script = None
self.scripts = Signal(lambda fn: Script(self.app, self, fn))
self.lightning_slot = None
self.lightning_density = 0
self.player = None
self.rock_slot = None
self.rain_slot = None
self.has_clouds = False
self.lowest_fps = 1000
self.on_render = Signal()
# self.script_paused = False
# self.script_slots = []
# self.stars_visible = 0
# star_density = 80
# self.star_pos = [
# (randint(0, 200), randint(0, 200)) for i in range(star_density)
# ]
# color change delays when using opt funcs
self.delay_t = 0
self.delay = 0.5
self.time = 0
self.sky_color = None
# self.ground_color = GREEN
self.dt = 0
self.sounds = {}
# self.script_fn = script
# self.event_slot = self.app.on_event.connect(self.even)
# self.script_resume_condition = None
# The below wrapper is just to keep the interface the same with signal
# on_collision.connect -> on_collision_connect
# class CollisionSignal:
# pass
# self.on_collision = CollisionSignal()
# self.on_collision.connect = self.on_collision_connect
# self.on_collision.once = self.on_collision_once
# self.on_collision.enter = self.on_collision_enter
# self.on_collision.leave = self.on_collision_leave
self._music = None
if script:
self.script = script # trigger setter
self.when.every(1, self.stabilize, weak=False)
def iter_entities(self, *types):
for slot in self.slots:
ent = slot.get()
if ent and isinstance(ent, types):
yield ent
def cloudy(self):
if self.has_clouds:
return
pv = self.player.velocity if self.player else vec3(0)
if hasattr(self.app.state, "player"):
velz = pv.z
else:
velz = 0
for i in range(30):
x = randint(-3000, 3000)
y = randint(0, 300)
z = randint(-4000, -1300)
pos = vec3(x, y, z)
self.add(Cloud(self.app, self, pos, velz))
self.has_clouds = True
def lightning_strike(self):
oldsky = vec4(self.sky_color) if self.sky_color else None
self.sky_color = "white"
self.when.once(
0.1, lambda oldsky=oldsky: self.set_sky_color(oldsky), weak=False
)
self.play_sound("lightning.wav")
def lightning_script(self, script):
yield
while True:
if self.lowest_fps <= 25:
break
yield script.sleep(1)
yield script.sleep((1 / self.lightning_density) * random.random())
self.lightning_strike()
def lightning(self, density=0.5):
if density < EPSILON:
self.lightning_slot = None
return
if self.lowest_fps <= 25:
return
self.lightning_density = density
self.lightning_slot = self.scripts.connect(self.lightning_script)
def add_rock(self):
if self.lowest_fps <= 25:
return
velz = self.player.velocity.z if self.player else vec3(0)
x = randint(-500, 500)
y = GROUND_HEIGHT - 15
z = -4000
ppos = self.player.position if self.player else vec3(0)
pos = vec3(ppos.x, 0, ppos.z) + vec3(x, y, z)
self.add(Rock(self.app, self, pos, velz))
def add_rain_drop(self):
velz = self.player.velocity.z if self.player else 0
x = randint(-400, 400)
y = randint(0, 300)
z = randint(-5000, -2000)
ppos = self.player.position if self.player else vec3(0)
pos = vec3(ppos.x, 0, ppos.z) + vec3(x, y, z)
self.add(Rain(self.app, self, pos, velz, particle=True))
def rain(self, density=50):
if density:
self.rain_slot = self.when.every(1 / density, self.add_rain_drop)
else:
self.rain_slot = None
def rocks(self, density=10):
if density:
self.rock_slot = self.when.every(1 / density, self.add_rock)
else:
self.rock_slot = None
def stars(self):
if hasattr(self.app.state, "player"):
velz = self.player.velocity.z
else:
velz = 0
for i in range(50):
x = randint(-500, 500)
y = -200 + (random.random() ** 0.5 * 800)
z = -3000
pos = vec3(x, y, z)
self.add(Star(self.app, self, pos, velz))
def draw_sky(self):
width, height = self.app.size / 8
self.sky = pygame.Surface((width, height))
sky_color = self.sky_color or ncolor(pygame.Color("blue"))
sky_color = [255 * s for s in sky_color]
# for y in range(height):
# interp = (1 - y / height) * 2
# for x in range(width):
# rand = rand_RGB()
# color = rgb_mix(sky_color, rand, 0.02)
# c = (sk * 0.98 + rand * 0.02) / i**1.1
# if interp == 0:
# color = (255, 255, 255)
# else:
# color = [min(int(c / interp ** 1.1), 255) for c in color]
# self.sky.set_at((x, y), color)
# Draw gradient
for y in range(height):
interp = (1 - y / height) * 2
base = [min(int(c / interp ** 1.1), 255) for c in sky_color]
pygame.draw.line(self.sky, base, (0, y), (width, y))
noise = noise_surf_dense_bottom(self.sky.get_size(), random.randrange(5))
self.sky.blit(
noise, (0, 0), None,
)
self.sky = pygame.transform.scale(self.sky, self.app.size)
# if self.stars_visible:
# self.draw_stars(self.sky, self.star_pos)
self.sky = pygame.transform.scale(self.sky, self.app.size)
# def draw_stars(self, surface, star_positions):
# size = 1
# for pos in star_positions:
# star = pygame.Surface((size, size))
# star.fill((255, 255, 255))
# star.set_alpha(175)
# surface.blit(star, pos)
def remove_sound(self, filename):
if filename in self.sounds:
self.sounds[filename][0].stop()
del self.sounds[filename]
return True
return False
def ensure_sound(self, filename, callback=None, *args):
"""
Ensure a sound is playing. If it isn't, play it.
"""
if filename in self.sounds:
return None, None, None
return self.play_sound(filename, callback, *args)
def play_sound(self, filename, callback=None, *args):
"""
Plays the sound with the given filename (relative to SOUNDS_DIR).
Returns sound, channel, and callback slot.
"""
if filename in self.sounds:
self.sounds[filename][0].stop()
del self.sounds[filename]
filename = path.join(SOUNDS_DIR, filename)
sound = self.app.load(filename, lambda: pygame.mixer.Sound(filename))
if not sound:
return None, None, None
channel = pygame.mixer.find_channel()
if not channel:
return None, None, None
channel.set_volume(SOUND_VOLUME)
if callback:
self.when.once(self.sounds[0].get_length(), callback, weak=False)
else:
slot = None
self.sounds[filename] = (sound, channel, slot)
channel.play(sound, *args)
self.when.once(sound.get_length(), lambda: self.remove_sound(sound), weak=False)
return sound, channel, slot
@property
def script(self):
return self._script
@script.setter
def script(self, fn):
self._script = (
Script(self.app, self, fn, use_input=True, script_args=(self.app, self))
if fn
else None
)
@property
def music(self):
return self._music
@property
def ground_color(self):
return self._ground_color
@ground_color.setter
def ground_color(self, color):
if color is None:
return
self._ground_color = ncolor(color)
if not self.ground:
self.ground = self.add(Ground(self.app, self, GROUND_HEIGHT))
self.ground.color = color
if "ROCK" in self.app.cache:
del self.app.cache["ROCK"] # rocks need to reload their color
@music.setter
def music(self, filename):
self._music = filename
if self._music:
pygame.mixer.music.load(path.join(MUSIC_DIR, filename))
pygame.mixer.music.play(-1)
def on_collision_connect(self, A, B, func, once=True):
"""
during collision (touching)
"""
pass
def on_collision_once(self, A, B, func, once=True):
"""
trigger only once
"""
pass
def on_collision_enter(self, A, B, func, once=True):
"""
trigger upon enter collision
"""
pass
def on_collision_leave(self, A, B, func, once=True):
"""
trigger upon leave collision
"""
pass
# @property
# def script(self):
# return self.script_fn
# @script.setter
# def script(self, script):
# print("Script:",script)
# if isinstance(script, str):
# local = {}
# exec(open(path.join(SCRIPTS_DIR, script + ".py")).read(), globals(), local)
# self._script = local["script"](self.app, self)
# elif isinstance(script, type):
# # So we can pass a Level class
# self._script = iter(script(self.app, self))
# elif script is None:
# self._script = None
# else:
# raise TypeError
# def sleep(self, t):
# return self.when.once(t, self.resume)
def add(self, entity):
slot = self.connect(entity, weak=False)
entity.slot = weakref.ref(slot)
# self.slotlist += slot
return entity
@property
def sky_color(self):
return self._sky_color
@sky_color.setter
def sky_color(self, c):
self._sky_color = ncolor(c) if c else None
self.draw_sky()
# reset ground gradient (depend on sky color)
self.ground_color = self._ground_color
# for scripts to call when.fade(1, set_sky_color)
def set_sky_color(self, c):
self.sky_color = ncolor(c) if c else None
def set_sky_color_opt(self, c):
"""
Optimized for fades.
"""
if self.delay_t > EPSILON:
return False
# print("delay")
self.delay_t = self.delay
self._sky_color = ncolor(c) if c else None
if self._sky_color:
self.draw_sky()
# reset ground gradient (depend on sky color)
self.set_ground_color_opt(self.ground_color)
return True
def set_ground_color(self, c):
self.ground_color = ncolor(c) if c else None
def set_ground_color_opt(self, c):
"""
Optimized for fades.
"""
if not self.ground:
self.ground = self.add(Ground(self.app, self, GROUND_HEIGHT))
if c:
self.ground.fade_opt(ncolor(c))
else:
self.ground = None
def remove(self, entity):
# self.slotlist -= entity
super().disconnect(entity)
# def resume(self):
# self.script_paused = False
def invalid_size(self, size):
"""Checks component for 0 or NaNs"""
return any(c != c or abs(c) < EPSILON for c in size)
def update_collisions(self, dt):
# cause all scene operations to be queueed
self.blocked += 1
for slot in self.slots:
a = slot.get()
# only check if a is solid
if not a or not a.solid:
continue
if self.invalid_size(a.collision_size):
continue
# for each slot, loop through each slot
for slot2 in self.slots:
b = slot2.get()
# only check if b is solid
if not b or not b.solid:
continue
if slot is not slot2:
if not a.has_collision and not b.has_collision:
continue
if self.invalid_size(b.collision_size):
continue
a_min = a.position - a.collision_size / 2
a_max = a.position + a.collision_size / 2
b_min = b.position - b.collision_size / 2
b_max = b.position + b.collision_size / 2
col = not (
b_min.x > a_max.x
or b_max.x < a_min.x
or b_min.y > a_max.y
or b_max.y < a_min.y
or b_min.z > a_max.z
or b_max.z < a_min.z
)
if col:
if a.has_collision:
a.collision(b, dt)
if b.has_collision:
b.collision(a, dt)
self.blocked -= 1
# run pending slot queue
if self.blocked == 0:
self.refresh()
def stabilize(self):
""""
Stablize FPS by setting max partilces
Called every second (see when.once(1, self.stabilize in init)
"""
self.lowest_fps = min(self.app.fps, self.lowest_fps)
if self.app.fps < 45:
self.max_particles = max(self.max_particles / 2, 4)
if self.lowest_fps >= 60:
if self.app.fps > 120:
self.max_particles = min(self.max_particles * 2, 64)
def filter_script(self, slot):
if isinstance(slot, weakref.ref):
wref = slot
slot = wref()
if not slot:
return False
if slot.get().done():
return False
return True
def update(self, dt):
self.time += dt
# print(self.time)
self.delay_t = max(0, self.delay_t - dt)
# do time-based events
self.when.update(dt)
self.update_collisions(dt)
self.refresh()
# main level script
if self._script:
self._script.update(dt)
# extra scripts
if self.scripts:
self.scripts.each(lambda x, dt: x.update(dt), dt)
self.scripts.slots = list(filter(self.filter_script, self.scripts.slots))
# self.sort(lambda a, b: a.z < b.z)
# self.slots = sorted(self.slots, key=z_compare)
self.slots.sort(key=z_compare)
self.slots = list(filter(lambda x: not x.get().removed, self.slots))
# call update(dt) on each entity
self.each(lambda x, dt: x.update(dt), dt)
# update particles (remove if too many)
self.blocked += 1
particle_count = 0
for i, slot in enumerate(reversed(self.slots)):
e = slot.get()
if e.particle:
particle_count += 1
if particle_count >= self.max_particles:
slot.disconnect()
self.blocked -= 1
self.clean()
def render(self, camera):
# call render(camera) on all scene entities
if self.sky_color is not None:
self.app.screen.blit(self.sky, (0, 0))
# call render on each entity
self.each(lambda x: x.render(camera))
self.on_render(camera)