-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoleChangerSystem.py
80 lines (61 loc) · 3.04 KB
/
PoleChangerSystem.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
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.animation import Animation
from kivent_core.systems.gamesystem import GameSystem
from kivy.factory import Factory
class PoleChangerSystem(GameSystem):
def __init__(self, **kwargs):
super(PoleChangerSystem, self).__init__(**kwargs)
def update(self, dt):
attractor_id = App.get_running_app().game.attractor_id
attractor = self.gameworld.entities[attractor_id]
for component in self.components:
if component is not None:
entity_id = component.entity_id
entity = self.gameworld.entities[entity_id]
pos = entity.position
size = entity.pole_changer.size
to_charge = entity.pole_changer.to
# Skip if Attractor is already to_charge
if attractor.charge.charge == to_charge:
continue
attractor_pos = attractor.position
if entity.pole_changer.rect is None:
r, g, b = 0.33, 0.33, 0.33
r1, g1, b1 = 0.22, 0.22, 0.22
with App.get_running_app().game.ids.play_camera.canvas.before:
color = Color(r1, g1, b1, 0.25)
entity.pole_changer.rect = Rectangle(size=(size[0],
size[1]),
pos=(pos.x - size[0]/2,
pos.y - size[1]/2))
color = Color(r, g, b, 0.25)
entity.pole_changer.rect = Rectangle(size=(size[0] - 6,
size[1] - 6),
pos=(pos.x - (size[0] - 6)/2,
pos.y - (size[1] - 6)/2))
if self.in_range(pos, size, attractor_pos):
attractor.attractor.to_change = to_charge
attractor.charge.charge = to_charge
game = App.get_running_app().game
game.play_sound(game.change_sound, 1.5)
def in_range(self, pos, size, attractor_pos):
left = pos.x - size[0]/2
right = pos.x + size[0]/2
top = pos.y + size[1]/2
bottom = pos.y - size[1]/2
x = attractor_pos.x
y = attractor_pos.y
if (left < x) and (x < right) and (bottom < y) and (y < top):
return True
else:
return False
def clear_component(self, component_index):
entity_id = self.components[component_index].entity_id
entity = self.gameworld.entities[entity_id]
insruction = entity.pole_changer.rect
if insruction is None:
return
canvas = App.get_running_app().game.ids.play_camera.canvas
canvas.before.remove(insruction)
Factory.register('PoleChangerSystem', cls=PoleChangerSystem)