-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
728 lines (602 loc) · 29.4 KB
/
Game.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import os
import json
import random
from kivy.utils import platform
from kivy.clock import Clock
# Kivy visuals
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.graphics import Rectangle
# Properties
from kivy.properties import NumericProperty
# Core
import kivent_core
import kivent_cymunk
# Plyer
if platform == 'android' or platform == 'ios':
import plyer
# Managers
from kivent_core.managers.resource_managers import texture_manager
from EntityFactory import EntityFactory
from LevelEditor import LevelEditorSystem
from ChargeSystem import ChargeSystem
from FinishSystem import FinishSystem
from PoleChangerSystem import PoleChangerSystem
from AttractorSystem import AttractorSystem
# Custom widgets
from TouchedTextBox import TouchedTextBox
from CircularButton import CircularButton
from BetterButton import BetterButton
# Load all .png in resources/png
png_list = []
png_source_list = []
for root, dirs, files in os.walk("resources/png"):
for asset in files:
if '.atlas' in asset:
texture_manager.load_atlas(os.path.join(root, asset))
if '.png' in asset:
texture_manager.load_image(os.path.join(root, asset))
png_list.append(asset)
png_source_list.append(os.path.join(root, asset))
class AttractorGame(Widget):
attractor_id = NumericProperty(-1)
current_level = 0
dd = None
clock = None
wall_sound = -1
change_sound = -1
unpause_button = None
def __init__(self, **kwargs):
super(AttractorGame, self).__init__(**kwargs)
self.gameworld.init_gameworld(['cymunk_physics',
'position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'play_camera',
'finish',
'pole_changer',
'attractor',
'charge'],
callback=self.init_game)
def init_game(self):
if not(platform == 'android' or platform == 'ios'):
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
self.setup_states()
self.gameworld.state = 'menu'
self.load_models()
self.load_sounds()
self.load_music()
self.populate_level_select_menu()
self.entity_factory = EntityFactory(self.gameworld, self.ids.cymunk_physics)
self.editor = self.gameworld.system_manager['editor']
self.editor.screen = self.ids.gamescreenmanager.ids.editor_screen
# self.load_level('test')
self.ids.play_camera.focus_entity = True
self.ids.play_camera.entity_to_focus = self.attractor_id
self.ids.cymunk_physics.collision_slop = 2
# self.gameworld.system_manager['cymunk_physics'].damping = 0.5
physics = self.ids.cymunk_physics
physics.add_collision_handler(1, 2, self.membrane_solver)
physics.add_collision_handler(1, 1, self.wall_solver)
self.gameworld.managers['sound_manager'].loop_music = True
self.update_track('title')
def _keyboard_closed(self):
pass
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
self.editor.handle_key_down(keycode[1])
def _on_keyboard_up(self, keyboard, keycode):
pass
def membrane_solver(self, space, arbiter):
attractor_id = arbiter.shapes[0].body.data
membrane_id = arbiter.shapes[1].body.data
attractor = self.gameworld.entities[attractor_id]
membrane = self.gameworld.entities[membrane_id]
if attractor.charge.charge == membrane.charge.charge:
return False
else:
# self.gameworld.managers['sound_manager'].play_direct(self.sound, 1.0)
return True
def wall_solver(self, space, arbiter):
# self.play_sound(self.wall_sound, 0.5)
return True
def setup_states(self):
self.gameworld.add_state(state_name='menu',
systems_added=[],
systems_removed=[],
systems_paused=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'finish',
'attractor',
'charge'],
systems_unpaused=[],
screenmanager_screen='menu_screen')
self.gameworld.add_state(state_name='play',
systems_added=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'finish',
'attractor',
'charge'],
systems_removed=[],
systems_paused=[],
systems_unpaused=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'finish',
'attractor',
'charge'],
screenmanager_screen='play_screen')
self.gameworld.add_state(state_name='editor',
systems_added=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'finish',
'attractor',
'charge'],
systems_removed=[],
systems_paused=[],
systems_unpaused=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'finish',
'attractor',
'charge'],
screenmanager_screen='editor_screen')
self.gameworld.add_state(state_name='finish',
systems_added=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'attractor',
'charge'],
systems_removed=['finish'],
systems_paused=['finish'],
systems_unpaused=['position',
'rotate',
'rotate_renderer',
'bg_renderer',
'mid_renderer',
'animation',
'cymunk_physics',
'play_camera',
'pole_changer',
'attractor',
'charge'],
screenmanager_screen='finish_screen')
def load_models(self):
model_manager = self.gameworld.model_manager
for png in png_list:
asset_name = png[:-4]
tex_key = texture_manager.get_texkey_from_name(asset_name)
width, height = texture_manager.get_size(tex_key)
model_manager.load_textured_rectangle('vertex_format_4f',
width, height,
asset_name,
asset_name)
tex_key = texture_manager.get_texkey_from_name(asset_name)
uv = texture_manager.get_uvs(tex_key)
uv = list((uv[0], uv[3], uv[2], uv[1]))
model = model_manager.models[asset_name]
model.set_textured_rectangle(width, height, uv)
def load_sounds(self):
manager = self.gameworld.managers['sound_manager']
with open('resources/sfx/sounds.json') as j_file:
data = json.load(j_file)
for sound in data['sounds']:
if sound['name'] == 'wall_sound':
self.wall_sound = manager.load_sound(sound['name'], sound['source'])
elif sound['name'] == 'change_sound':
self.change_sound = manager.load_sound(sound['name'], sound['source'])
def load_music(self):
manager = self.gameworld.managers['sound_manager']
for root, dirs, files in os.walk("resources/music/"):
for music_file in files:
manager.load_music(music_file.split('.')[0],
os.path.join(root, music_file))
def create_entities(self):
self.attractor_id = self.entity_factory.create_entity_at('attractor', 200, 200)
# attractor = self.gameworld.entities[self.attractor_id]
# Debug moving stuff
# self.moving_to = Body('INF', 'INF')
# self.moving_to.position = attractor.position.pos
# joint = PivotJoint(self.moving_to, attractor.cymunk_physics.body, (0, 0), (0, 0))
# joint.max_force = 5000.
# joint.max_bias = 5000.
# self.ids.cymunk_physics.space.add_constraint(joint)
#
# self.entity_factory.create_entity_at('posipole', 0, 100)
def go_to_menu_screen(self):
self.gameworld.gamescreenmanager.transition.direction = 'up'
if self.gameworld.state == 'editor':
self.toggle_level_editor()
if self.editor.asset_id != -1:
self.gameworld.remove_entity(self.editor.asset_id)
self.editor.asset_id = -1
elif self.gameworld.state == 'play' and self.unpause_button is None:
menu = self.ids.gamescreenmanager.ids.menu_screen
self.unpause_button = BetterButton(text='Unpause')
self.unpause_button.bind(on_release=self.go_to_play_screen)
menu.ids.button_container.add_widget(self.unpause_button)
else:
menu = self.ids.gamescreenmanager.ids.menu_screen
try:
menu.ids.button_container.remove_widget(self.unpause_button)
except AttributeError:
pass
self.gameworld.gamescreenmanager.transition.direction = 'left'
self.gameworld.state = 'menu'
def open_level_select_menu(self):
scr = self.ids.gamescreenmanager.ids.menu_screen
buttons = scr.level_buttons
scr.scroll_container.x = scr.width
anim = Animation(x=scr.width - scr.scroll_container.width, duration=0.25)
anim.start(scr.scroll_container)
def populate_level_select_menu(self):
h_int = 40
h = str(h_int) + 'dp'
scr = self.ids.gamescreenmanager.ids.menu_screen
buttons = scr.level_buttons
buttons.add_widget(Label(text='[b]Magnet[/b]', markup=True))
buttons.add_widget(Label())
for i in range(1, 5):
button = BetterButton(text=str(i),
size_hint_y=None,
height=h)
button.bind(on_release=self.play_level)
buttons.add_widget(button)
buttons.add_widget(Label(text='[b]Rotator[/b]', markup=True))
buttons.add_widget(Label())
for i in range(5, 9):
button = BetterButton(text=str(i),
size_hint_y=None,
height=h)
button.bind(on_release=self.play_level)
buttons.add_widget(button)
buttons.add_widget(Label(text='[b]Membrane[/b]', markup=True))
buttons.add_widget(Label())
for i in range(9, 13):
button = BetterButton(text=str(i),
size_hint_y=None,
height=h)
button.bind(on_release=self.play_level)
buttons.add_widget(button)
buttons.add_widget(Label(text='[b]Changer[/b]', markup=True))
buttons.add_widget(Label())
for i in range(13, 17):
button = BetterButton(text=str(i),
size_hint_y=None,
height=h)
button.bind(on_release=self.play_level)
buttons.add_widget(button)
buttons.size_y = len(buttons.children) * h_int + h_int/2
buttons.bind(minimum_height=buttons.setter('height'))
def go_to_play_screen(self, *args):
self.gameworld.gamescreenmanager.transition.direction = 'up'
self.gameworld.state = 'play'
def go_to_editor_screen(self):
if platform != 'android':
self.toggle_level_editor()
def change_attractor_charge(self, button, change_to):
button.background_color[3] = 0.15
c = button.background_color
anim = Animation(background_color=[c[0], c[1], c[2], 0], d=0.5, t='out_circ')
anim.start(button)
attractor = self.gameworld.entities[self.attractor_id]
attractor.attractor.to_change = change_to
attractor.charge.charge = change_to
self.editor.level.stats.changes += 1
self.ids.gamescreenmanager.play_screen.changes.text = \
str(self.editor.level.stats.changes) + ' / ' + \
str(self.editor.level.stats.ideal_changes)
self.play_sound(self.change_sound, 2.0)
def on_touch_down(self, touch):
if super(AttractorGame, self).on_touch_down(touch):
return True
if self.gameworld.state == 'editor':
self.editor.handle_touch(touch)
def toggle_level_editor(self):
if self.gameworld.state == 'editor':
self.gameworld.state = 'menu'
self.ids.play_camera.focus_entity = True
self.ids.play_camera.entity_to_focus = self.attractor_id
self.editor.remove_anchors()
else:
self.load_level('level1')
self.gameworld.state = 'editor'
self.ids.play_camera.focus_entity = False
self.editor.draw_anchors()
if self.dd is None:
self.dd = DropDown()
names = self.entity_factory.get_entity_names()
for ent_name in names:
butt = Button(text=ent_name,
size_hint=(None, None),
width=175,
height=40)
butt.bind(on_release=self.grab_entity_to_place)
self.dd.add_widget(butt)
main_butt = Button(text='Select',
size_hint_x=None,
width=175)
main_butt.bind(on_release=self.dd.open)
self.dd.bind(on_select=self.dd.dismiss)
self.editor.screen.ids.ui.add_widget(main_butt)
def grab_entity_to_place(self, instance):
self.dd.dismiss()
pos = self.ids.play_camera.get_camera_center()
if self.editor.asset_id != -1:
self.gameworld.remove_entity(self.editor.asset_id)
self.editor.asset_id = -1
self.editor.entity_to_place = ''
if not self.editor.deleting:
self.editor.entity_to_place = instance.text
self.editor.asset_id = self.entity_factory.create_entity_at(
instance.text,
pos[0],
pos[1],
int(self.editor.screen.ids.rotation.text))
self.editor.current_location = [pos[0], pos[1]]
def update_editor_rotation(self, r):
asset_id = self.editor.asset_id
name = self.editor.entity_to_place
try:
r_int = int(r)
except ValueError:
r_int = 0
if asset_id != -1:
self.gameworld.remove_entity(self.editor.asset_id)
self.asset_id = self.entity_factory.create_entity_at(name, 0, 0, r_int)
def save_level(self):
level = self.editor.level
level_file_name = self.editor.screen.ids.level_name.text
if level_file_name == '':
return
level_data = {'entities': []}
for name, point, rotation, ids in zip(level.names, level.points,
level.rotations, level.ids):
if name == 'attractor':
continue
level_data['entities'].append({'name': name,
'x': point.x,
'y': point.y,
'rotation': rotation})
if level.background is not None:
source = 'resources' + level.background.source.split('resources')[1]
level_data['background'] = {'x': level.background.pos[0],
'y': level.background.pos[1],
'w': level.background.size[0],
'h': level.background.size[1],
'source': source}
if level.background2 is not None:
source = 'resources' + level.background2.source.split('resources')[1]
level_data['background2'] = {'x': level.background2.pos[0],
'y': level.background2.pos[1],
'w': level.background2.size[0],
'h': level.background2.size[1],
'source': source}
level_data['ideal_time'] = self.editor.level.stats.ideal_time
level_data['ideal_changes'] = self.editor.level.stats.ideal_changes
level_data['attractor_pos'] = (int(self.editor.screen.ids.attractor_pos_x.text),
int(self.editor.screen.ids.attractor_pos_y.text))
with open('resources/levels/' + level_file_name + '.json', 'wb') as f:
json.dump(level_data, f, indent=2)
def play_level(self, button):
level_file_name = 'level' + button.text
self.current_level = int(button.text)
self.load_level(level_file_name)
self.go_to_play_screen()
Clock.schedule_once(lambda dt: self.hide_level_menu(), 1)
def hide_level_menu(self):
scr = self.ids.gamescreenmanager.ids.menu_screen
scr.scroll_container.x = -scr.scroll_container.width
def load_level(self, level_file_name=''):
if level_file_name == '':
level_file_name = self.editor.screen.ids.level_name.text
if level_file_name == '':
return
path = 'resources/levels/' + level_file_name + '.json'
if not os.path.isfile(path):
return
with open(path, 'rb') as f:
level_data = json.load(f)
if 'entities' not in level_data:
return
self.clear_level()
self.ids.play_camera.canvas.before.clear()
self.ids.play_camera.canvas.after.clear()
# Try to get background from level
try:
with self.ids.play_camera.canvas.before:
self.editor.level.background = Rectangle(pos=(level_data['background']['x'],
level_data['background']['y']),
size=(level_data['background']['w'],
level_data['background']['h']),
source=level_data['background']['source'])
with self.ids.play_camera.canvas.before:
self.editor.level.background2 = Rectangle(pos=(level_data['background2']['x'],
level_data['background2']['y']),
size=(level_data['background2']['w'],
level_data['background2']['h']),
source=level_data['background2']['source'])
except KeyError:
pass
# Try to get ideal time and changes from level
try:
self.editor.level.stats.ideal_time = level_data['ideal_time']
self.editor.level.stats.ideal_changes = level_data['ideal_changes']
except KeyError:
self.editor.level.stats.ideal_time = 0
self.editor.level.stats.ideal_changes = 0
# Try to get music track from level
try:
track = level_data['track']
except KeyError:
track = 'daflute'
# Try to get attractor position from level
try:
pos = level_data['attractor_pos']
attractor_pos = (int(pos[0]), int(pos[1]))
except KeyError:
attractor_pos = (200, 200)
self.editor.level.attractor_pos = attractor_pos
xbox = self.gameworld.gamescreenmanager.ids.editor_screen.ids.attractor_pos_x
ybox = self.gameworld.gamescreenmanager.ids.editor_screen.ids.attractor_pos_y
xbox.text = str(attractor_pos[0])
ybox.text = str(attractor_pos[1])
# Order entities
finish = None
corners= []
other = []
changers = []
for ent in level_data['entities']:
name = ent['name']
if name == 'finish':
finish = [ent]
elif name == 'wall_corner':
corners.append(ent)
elif 'changer' in name:
changers.append(ent)
else:
other.append(ent)
try:
ents = changers + other + corners + finish
except TypeError:
ents = level_data['entities']
for ent in ents:
name = ent['name']
x = ent['x']
y = ent['y']
rot = ent['rotation']
ids = self.entity_factory.create_entity_at(name, x, y, rot)
self.editor.level.add_entity(name, x, y, rot, ids)
self.create_attractor(attractor_pos)
if self.gameworld.state == 'editor':
self.ids.play_camera.focus_entity = False
self.editor.redraw_anchors()
else:
self.ids.play_camera.focus_entity = True
self.editor.level.stats.timer = 0
self.editor.level.stats.changes = 0
self.ids.gamescreenmanager.play_screen.time.text = '0 / ' + \
str(self.editor.level.stats.ideal_time)
self.ids.gamescreenmanager.play_screen.changes.text = '0 / ' + \
str(self.editor.level.stats.ideal_changes)
if self.clock is None:
self.clock = Clock.schedule_interval(self.update_timer, 1)
self.update_track(track)
def finish_level(self):
attractor = self.gameworld.entities[self.attractor_id]
attractor.attractor.to_change = 'f'
attractor.charge.charge = 'n'
attractor.cymunk_physics.body.velocity = (0, 0)
self.gameworld.gamescreenmanager.transition.direction = 'up'
self.gameworld.state = 'finish'
screen = self.ids.gamescreenmanager.ids.finish_screen
screen.ids.time.text = str(int(round(self.editor.level.stats.timer))) + \
' / ' + str(self.editor.level.stats.ideal_time)
screen.ids.changes.text = str(self.editor.level.stats.changes) + ' / ' + \
str(self.editor.level.stats.ideal_changes)
def clear_level(self):
self.gameworld.clear_entities()
self.ids.play_camera.canvas.before.clear()
self.editor.level.clear()
def create_attractor(self, pos):
self.attractor_id = self.entity_factory.create_entity_at('attractor',
pos[0],
pos[1],
0)
self.editor.level.add_entity('attractor',
pos[0],
pos[1],
0,
self.attractor_id)
def reset_attractor(self):
self.play_sound(self.change_sound, 2.0)
attractor = self.gameworld.entities[self.attractor_id]
attractor.attractor.to_change = 'r'
attractor.charge.charge = 'n'
attractor.cymunk_physics.body.velocity = (0, 0)
Clock.schedule_once(lambda dt: self.return_attractor_to_start_pos(), 0.5)
def return_attractor_to_start_pos(self):
attractor = self.gameworld.entities[self.attractor_id]
attractor.position.pos = self.editor.level.attractor_pos
attractor.cymunk_physics.body.position = self.editor.level.attractor_pos
attractor.cymunk_physics.body.velocity = (0, 0)
attractor.charge.charge = 'n'
attractor.attractor.to_change = 'n'
# self.play_sound(self.reset_sound, 1.0)
def update_timer(self, dt):
self.editor.level.stats.timer += dt
self.ids.gamescreenmanager.play_screen.time.text = \
str(int(round(self.editor.level.stats.timer))) + \
' / ' + str(self.editor.level.stats.ideal_time)
def go_to_next_level(self):
self.current_level = self.current_level + 1
self.load_level('level' + str(self.current_level))
self.gameworld.gamescreenmanager.transition.direction = 'down'
self.gameworld.state = 'play'
def update_track(self, name):
manager = self.gameworld.managers['sound_manager']
if manager.current_track is None:
self.fade_in_track(name)
elif manager.current_track != name:
self.fade_out_track()
Clock.schedule_once(lambda dt: self.fade_in_track(name), 3)
else:
pass
def fade_in_track(self, name):
manager = self.gameworld.managers['sound_manager']
manager.music_volume = 1.0
manager.play_track(name)
def fade_out_track(self):
manager = self.gameworld.managers['sound_manager']
anim = Animation(music_volume=0.0, duration=2)
anim.start(manager)
def play_sound(self, direct_num, vol=1.0):
self.gameworld.managers['sound_manager'].play_direct(direct_num, vol)