-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameengine.py
158 lines (139 loc) · 5.9 KB
/
gameengine.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
import importlib
import time
from controllers import StillAnimator
from utils import coord_3d_to_linear
games = {
'tictactoe': (255, 0, 0),
'paintball': (0, 255, 0),
'animatic': (0, 0, 255),
}
class GameEngine:
def __init__(self, cube, r1, r2, sound, game):
self.cube = cube
self.sound = sound
self.r1 = r1
self.r2 = r2
self.game = game
self.player_colors = [
((255, 0, 0), (127, 0, 0)),
((0, 255, 0), (0, 127, 0)),
((0, 0, 255), (0, 0, 127)),
((0, 255, 255), (0, 127, 127)),
((255, 0, 255), (127, 0, 127)),
((255, 255, 0), (127, 127, 0)),
]
self.player_colors_sel = [3, 5]
self._state = self.nullstate
self.change_state(self.process_config)
self._timers = []
self.reset1 = False
self.reset2 = False
def nullstate(self, event):
pass
def change_state(self, next_state):
self._state('state:exit')
self._state = next_state
self._state('state:enter')
def timer_add(self, n, t):
self.timer_rm(n)
self._timers.append((time.time()+t, n))
def timer_rm(self, n):
self._timers = [x for x in self._timers if x[1] != n]
def player_color(self, p, mod=False):
if not p in (0, 1): return (0, 0, 0)
c = self.player_colors_sel[p]
return self.player_colors[c][mod]
def process_config(self, event):
def load_game():
try:
mod = importlib.import_module(self.game)
self._game = mod.Game(self)
except ModuleNotFoundError:
print('Invalid game')
def draw0():
for p in (coord_3d_to_linear(0, 0, 0), coord_3d_to_linear(1, 0, 0), coord_3d_to_linear(0, 1, 0), coord_3d_to_linear(0, 0, 1)):
self.cube.set_animator(p, StillAnimator(self.player_color(0)))
def draw1():
for p in (coord_3d_to_linear(3, 3, 3), coord_3d_to_linear(2, 3, 3), coord_3d_to_linear(3, 2, 3), coord_3d_to_linear(3, 3, 2)):
self.cube.set_animator(p, StillAnimator(self.player_color(1)))
def drawgame():
for p in (coord_3d_to_linear(3, 0, 0), coord_3d_to_linear(0, 3, 0), coord_3d_to_linear(0, 0, 3)):
self.cube.set_animator(p, StillAnimator(games[self.game]))
if event == 'state:enter':
for i in range(64): self.cube.set_animator(i, StillAnimator((0, 0, 0)))
draw0()
draw1()
drawgame()
elif event == 'player1:show' or event == 'player2:show':
gs = list(games.keys())
i = gs.index(self.game)
i = (i + 1) % len(gs)
self.game = gs[i]
drawgame()
elif event == 'player1:valid' or event == 'player2:valid':
load_game()
elif event in ('player1:x+', 'player1:x-', 'player1:y+', 'player1:y-', 'player1:z+', 'player1:z-'):
self.player_colors_sel[0] += 1
self.player_colors_sel[0] %= len(self.player_colors)
if self.player_colors_sel[0] == self.player_colors_sel[1]:
self.player_colors_sel[0] += 1
self.player_colors_sel[0] %= len(self.player_colors)
draw0()
elif event in ('player2:x+', 'player2:x-', 'player2:y+', 'player2:y-', 'player2:z+', 'player2:z-'):
self.player_colors_sel[1] += 1
self.player_colors_sel[1] %= len(self.player_colors)
if self.player_colors_sel[1] == self.player_colors_sel[0]:
self.player_colors_sel[1] += 1
self.player_colors_sel[1] %= len(self.player_colors)
draw1()
def process(self, rawevent):
def button_name(data):
if data == b'0': return 'off'
elif data == b'1': return 'x+'
elif data == b'2': return 'z+'
elif data == b'4': return 'y+'
elif data == b'8': return 'x-'
elif data == b'16': return 'z-'
elif data == b'32': return 'y-'
elif data == b'64': return 'cancel'
elif data == b'128': return 'show'
elif data == b'256': return 'valid'
elif data == b'129' or data == b'136': return 'show_x'
elif data == b'130' or data == b'144': return 'show_z'
elif data == b'132' or data == b'160': return 'show_y'
return None
if rawevent[0] == 'tic':
now = time.time()
removable = False
for t, n in self._timers:
if t <= now:
removable = True
if n == 'gameengine_reset1':
if self.reset1:
self.timer_rm('gameengine_reset1')
self.timer_rm('gameengine_reset2')
self.change_state(self.process_config)
elif n == 'gameengine_reset2':
if self.reset2:
self.timer_rm('gameengine_reset1')
self.timer_rm('gameengine_reset2')
self.change_state(self.process_config)
else:
self._state(f'timer:{n}')
if removable:
self._timers = [x for x in self._timers if x[0] > now]
elif rawevent[0] == 'remote1':
btn = button_name(rawevent[1])
self.reset1 = rawevent[1] == b'64'
if self.reset1: self.timer_add('gameengine_reset1', 5)
if btn:
self._state('player1:'+btn)
elif rawevent[0] == 'remote2':
btn = button_name(rawevent[1])
self.reset2 = rawevent[1] == b'64'
if self.reset2: self.timer_add('gameengine_reset2', 5)
if btn:
self._state('player2:'+btn)
elif rawevent[0] == 'console':
if rawevent[1]:
self._state(rawevent[1])