/
controller.py
125 lines (101 loc) · 3.35 KB
/
controller.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
import sys
from mrpg.core.game import Game, State
from mrpg.gui import wrapper
from mrpg.gui.commons import Color
from mrpg.gui.gui import GUI
class Controller():
def __init__(self, width=960, height=540):
self.window = wrapper.init(self, width, height, caption="MRPG")
self.game = Game()
self.gui = GUI(self.window)
self.update_choices()
cursor = self.window.get_system_mouse_cursor("crosshair")
self.window.set_mouse_cursor(cursor)
self.update_text()
def resize(self, w, h):
self.gui.resize(w, h)
def run(self):
wrapper.run()
def draw(self):
self.gui.draw()
def update(self, dt):
self.gui.update(dt)
def mouse_motion(self, x, y, dx, dy):
pass
def mouse_press(self, x, y, button, modifiers):
pass
def mouse_release(self, x, y, button, modifiers):
pass
def mouse_drag(self, x, y, dx, dy, buttons, modifiers):
pass
def update_text(self):
state = self.game.state
self.gui.header.text = ""
self.gui.display.text = ""
if state == State.MAIN_MENU:
if not self.gui.has_output():
self.gui.header.text = "MRPG Prototype"
elif state == State.GAME_MENU:
if not self.gui.has_output():
self.gui.display.text = self.game.player.string_long()
elif state == State.BATTLE:
if self.game.battle:
self.gui.display.text = self.game.battle.stats()
else:
raise AssertionError
if self.gui.has_output():
self.gui.menu.display = False
else:
self.gui.menu.display = True
def resolve_to_output(self):
while self.game.events:
outputs = self.game.get_event().resolve()
outputs = [s.strip() for s in outputs if s.strip()]
if outputs:
return outputs
return None
def progress_to_output(self):
output = self.resolve_to_output()
if output:
return output
while self.game.battle and self.game.battle.turn:
self.game.progress()
output = self.resolve_to_output()
if output:
return output
if self.game.adventure and not self.game.battle:
self.game.progress()
output = self.resolve_to_output()
if output:
return output
return None
def update_choices(self):
if self.game.menu:
self.gui.menu.choices(*self.game.menu.choices)
def enter(self):
output = self.progress_to_output()
if output:
self.gui.set_output(output)
self.update_text()
self.update_choices()
return
if self.gui.has_output():
self.gui.set_output("")
self.update_text()
return
choice = self.gui.menu.pick()
self.game.submit(choice)
output = self.progress_to_output()
if output:
self.gui.set_output(output)
self.update_choices()
self.update_text()
def key_press(self, inp):
actions = {
"up": self.gui.menu.up,
"down": self.gui.menu.down,
"escape": sys.exit,
"enter": self.enter
}
if inp in actions:
actions[inp]()