forked from adangert/JoustMania
/
joust.py
409 lines (351 loc) · 15.4 KB
/
joust.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
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
import common
import psmove
import time
import psutil, os
import random
import numpy
from piaudio import Audio
from enum import Enum
from multiprocessing import Process, Value, Array
# How fast/slow the music can go
SLOW_MUSIC_SPEED = 0.5
#this was 0.5
FAST_MUSIC_SPEED = 2.0
# The min and max timeframe in seconds for
# the speed change to trigger, randomly selected
MIN_MUSIC_FAST_TIME = 4
MAX_MUSIC_FAST_TIME = 8
MIN_MUSIC_SLOW_TIME = 10
MAX_MUSIC_SLOW_TIME = 23
END_MIN_MUSIC_FAST_TIME = 6
END_MAX_MUSIC_FAST_TIME = 10
END_MIN_MUSIC_SLOW_TIME = 8
END_MAX_MUSIC_SLOW_TIME = 12
#Sensitivity of the contollers
SLOW_MAX = 1
SLOW_WARNING = 0.28
FAST_MAX = 1.8
FAST_WARNING = 0.8
#Sensitivity of the werewolf contollers
WERE_SLOW_MAX = 1.4
WERE_SLOW_WARNING = 0.5
WERE_FAST_MAX = 2.3
WERE_FAST_WARNING = 1.2
#How long the speed change takes
INTERVAL_CHANGE = 1.5
#How long the winning moves shall sparkle
END_GAME_PAUSE = 4
def track_move(move_serial, move_num, game_mode, team, team_num, dead_move, force_color, music_speed, werewolf_reveal):
#proc = psutil.Process(os.getpid())
#proc.nice(3)
#explosion = Audio('audio/Joust/sounds/Explosion34.wav')
#explosion.start_effect()
start = False
no_rumble = time.time() + 1
move_last_value = None
move = common.get_move(move_serial, move_num)
team_colors = common.generate_colors(team_num)
werewolf = False
if team < 0:
team = (team + 1) * -1
werewolf = True
#keep on looping while move is not dead
while True:
if sum(force_color) != 0:
no_rumble_time = time.time() + 5
time.sleep(0.01)
move.set_leds(*force_color)
if sum(force_color) > 75:
if werewolf:
move.set_rumble(80)
else:
if sum(force_color) == 30:
if werewolf:
move.set_leds(255, 0, 0)
else:
move.set_leds(0, 0, 0)
move.set_rumble(0)
move.update_leds()
no_rumble = time.time() + 0.5
elif dead_move.value == 1 and werewolf_reveal.value > 0:
if move.poll():
ax, ay, az = move.get_accelerometer_frame(psmove.Frame_SecondHalf)
total = sum([ax, ay, az])
if move_last_value is not None:
change = abs(move_last_value - total)
speed_percent = (music_speed.value - SLOW_MUSIC_SPEED)/(FAST_MUSIC_SPEED - SLOW_MUSIC_SPEED)
if werewolf:
warning = common.lerp(WERE_SLOW_WARNING, WERE_FAST_WARNING, speed_percent)
threshold = common.lerp(WERE_SLOW_MAX, WERE_FAST_MAX, speed_percent)
else:
warning = common.lerp(SLOW_WARNING, FAST_WARNING, speed_percent)
threshold = common.lerp(SLOW_MAX, FAST_MAX, speed_percent)
if change > threshold:
if time.time() > no_rumble:
move.set_leds(0,0,0)
move.set_rumble(90)
dead_move.value = 0
elif change > warning:
if time.time() > no_rumble:
move.set_leds(20,50,100)
move.set_rumble(110)
else:
if game_mode == common.Games.WereJoust.value:
if werewolf_reveal.value == 2 and werewolf:
move.set_leds(255,0,0)
else:
move.set_leds(100,100,100)
else:
move.set_leds(*team_colors[team])
move.set_rumble(0)
move_last_value = total
move.update_leds()
class Joust():
def __init__(self, game_mode, moves, teams):
self.move_serials = moves
self.game_mode = game_mode
self.tracked_moves = {}
self.dead_moves = {}
self.music_speed = Value('d', 1.5)
self.running = True
self.force_move_colors = {}
self.teams = teams
self.team_num = 6
self.game_mode = game_mode
self.werewolf_timer = 35
self.start_timer = time.time()
self.audio_cue = 0
self.num_dead = 0
self.werewolf_reveal = Value('i', 2)
if game_mode == common.Games.JoustFFA.value:
self.team_num = len(moves)
if game_mode == common.Games.JoustRandomTeams.value:
#this should be 3 for smaller number of controllers
self.team_num = 4
if game_mode == common.Games.WereJoust.value:
self.werewolf_reveal.value = 0
self.team_num = 1
if game_mode != common.Games.JoustTeams.value:
self.generate_random_teams(self.team_num)
if game_mode == common.Games.WereJoust.value:
#were_num = int((len(moves)+2)/4)
were_num = int((len(moves)*7)/16)
if were_num <= 0:
were_num = 1
self.choose_werewolf(were_num)
music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))
self.start_beep = Audio('audio/Joust/sounds/start.wav')
self.start_game = Audio('audio/Joust/sounds/start3.wav')
self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
fast_resample = False
end = False
self.audio = Audio(music, end)
#self.change_time = self.get_change_time(speed_up = True)
self.change_time = time.time() + 8
self.speed_up = True
self.currently_changing = False
self.game_end = False
self.winning_moves = []
self.game_loop()
def choose_werewolf(self, were_num):
for were in range(were_num):
werewolf = random.choice(self.move_serials)
while self.teams[werewolf] < 0:
werewolf = random.choice(self.move_serials)
self.teams[werewolf] = (self.teams[werewolf] * -1) - 1
def generate_random_teams(self, team_num):
team_pick = list(range(team_num))
print (str(team_pick))
for serial in self.move_serials:
random_choice = random.choice(team_pick)
self.teams[serial] = random_choice
team_pick.remove(random_choice)
if not team_pick:
team_pick = list(range(team_num))
def track_moves(self):
for move_num, move_serial in enumerate(self.move_serials):
time.sleep(0.02)
dead_move = Value('i', 1)
force_color = Array('i', [1] * 3)
proc = Process(target=track_move, args=(move_serial,
move_num,
self.game_mode,
self.teams[move_serial],
self.team_num,
dead_move,
force_color,
self.music_speed,
self.werewolf_reveal))
proc.start()
self.tracked_moves[move_serial] = proc
self.dead_moves[move_serial] = dead_move
self.force_move_colors[move_serial] = force_color
def change_all_move_colors(self, r, g, b):
for color in self.force_move_colors.values():
common.change_color(color, r, g, b)
#need to do the count_down here
def count_down(self):
self.change_all_move_colors(80, 0, 0)
self.start_beep.start_effect()
time.sleep(0.75)
self.change_all_move_colors(70, 100, 0)
self.start_beep.start_effect()
time.sleep(0.75)
self.change_all_move_colors(0, 70, 0)
self.start_beep.start_effect()
time.sleep(0.75)
self.change_all_move_colors(0, 0, 0)
self.start_game.start_effect()
def get_change_time(self, speed_up):
min_moves = len(self.move_serials) - 2
if min_moves <= 0:
min_moves = 1
game_percent = (self.num_dead/min_moves)
if game_percent > 1.0:
game_percent = 1.0
min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME, END_MIN_MUSIC_FAST_TIME, game_percent)
max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME, END_MAX_MUSIC_FAST_TIME, game_percent)
min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME, END_MIN_MUSIC_SLOW_TIME, game_percent)
max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME, END_MAX_MUSIC_SLOW_TIME, game_percent)
if speed_up:
added_time = random.uniform(min_music_fast, max_music_fast)
else:
added_time = random.uniform(min_music_slow, max_music_slow)
return time.time() + added_time
def change_music_speed(self, fast):
change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
if fast:
self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
elif not fast:
self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
self.audio.change_ratio(self.music_speed.value)
def check_music_speed(self):
if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
self.change_music_speed(self.speed_up)
self.currently_changing = True
self.audio.change_chunk_size(True)
elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
self.speed_up = not self.speed_up
self.change_time = self.get_change_time(speed_up = self.speed_up)
self.audio.change_ratio(self.music_speed.value)
self.currently_changing = False
self.audio.change_chunk_size(False)
def get_real_team(self, team):
if team < 0:
return -1
else:
return team
def reveal(self):
self.werewolf_reveal.value = 2
def werewolf_audio_cue(self):
if self.game_mode == common.Games.WereJoust.value:
#print self.werewolf_timer - (time.time() - self.start_timer)
if self.werewolf_timer - (time.time() - self.start_timer) <= 30 and self.audio_cue == 0:
Audio('audio/Joust/sounds/30 werewolf.wav').start_effect()
self.audio_cue = 1
if self.werewolf_timer - (time.time() - self.start_timer) <= 10 and self.audio_cue == 1:
Audio('audio/Joust/sounds/10 werewolf.wav').start_effect()
self.audio_cue = 2
if self.werewolf_timer - (time.time() - self.start_timer) <= 0 and self.audio_cue == 2:
Audio('audio/Joust/sounds/werewolf reveal 2.wav').start_effect()
self.reveal()
self.audio_cue = 3
def check_end_game(self):
winning_team = -100
team_win = True
for move_serial, dead in self.dead_moves.items():
#if we are alive
if dead.value == 1:
if winning_team == -100:
winning_team = self.get_real_team(self.teams[move_serial])
elif self.get_real_team(self.teams[move_serial]) != winning_team:
team_win = False
if dead.value == 0:
#This is to play the sound effect
self.num_dead += 1
dead.value = -1
self.explosion.start_effect()
if team_win:
self.end_game_sound(winning_team)
for move_serial in self.teams.keys():
if self.get_real_team(self.teams[move_serial]) == winning_team:
self.winning_moves.append(move_serial)
self.game_end = True
def stop_tracking_moves(self):
for proc in self.tracked_moves.values():
proc.terminate()
proc.join()
time.sleep(0.02)
def end_game(self):
self.audio.stop_audio()
end_time = time.time() + END_GAME_PAUSE
h_value = 0
while (time.time() < end_time):
time.sleep(0.01)
win_color = common.hsv2rgb(h_value, 1, 1)
for win_move in self.winning_moves:
win_color_array = self.force_move_colors[win_move]
common.change_color(win_color_array, *win_color)
h_value = (h_value + 0.01)
if h_value >= 1:
h_value = 0
self.running = False
def end_game_sound(self, winning_team):
if self.game_mode == common.Games.JoustTeams.value:
if winning_team == 0:
team_win = Audio('audio/Joust/sounds/yellow team win.wav')
if winning_team == 1:
team_win = Audio('audio/Joust/sounds/green team win.wav')
if winning_team == 2:
team_win = Audio('audio/Joust/sounds/cyan team win.wav')
if winning_team == 3:
team_win = Audio('audio/Joust/sounds/blue team win.wav')
if winning_team == 4:
team_win = Audio('audio/Joust/sounds/magenta team win.wav')
if winning_team == 5:
team_win = Audio('audio/Joust/sounds/red team win.wav')
team_win.start_effect()
if self.game_mode == common.Games.JoustRandomTeams.value:
if winning_team == 0:
team_win = Audio('audio/Joust/sounds/yellow team win.wav')
if winning_team == 1:
team_win = Audio('audio/Joust/sounds/cyan team win.wav')
if winning_team == 2:
team_win = Audio('audio/Joust/sounds/magenta team win.wav')
if winning_team == 3:
team_win = Audio('audio/Joust/sounds/red team win.wav')
team_win.start_effect()
if self.game_mode == common.Games.WereJoust.value:
if winning_team == -1:
team_win = Audio('audio/Joust/sounds/werewolf win.wav')
else:
team_win = Audio('audio/Joust/sounds/human win.wav')
team_win.start_effect()
#self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
def werewolf_intro(self):
Audio('audio/Joust/sounds/werewolf intro.wav').start_effect()
time.sleep(3)
self.change_all_move_colors(80, 0, 0)
time.sleep(2)
self.change_all_move_colors(30, 0, 0)
time.sleep(18)
self.change_all_move_colors(20, 20, 20)
time.sleep(2)
self.start_timer = time.time()
def game_loop(self):
self.track_moves()
if self.game_mode == common.Games.WereJoust.value:
self.werewolf_intro()
self.werewolf_reveal.value = 1
self.count_down()
time.sleep(0.02)
self.audio.start_audio_loop()
time.sleep(0.8)
while self.running:
self.check_music_speed()
self.check_end_game()
self.werewolf_audio_cue()
if self.game_end:
self.end_game()
self.stop_tracking_moves()