-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreen.py
151 lines (115 loc) · 3.37 KB
/
screen.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
from __future__ import annotations
import tcod
import tcod.event
from tcod.ecs import World, Entity
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from engine import Engine
SIGNALS = {
tcod.event.KeySym.w: "up",
tcod.event.KeySym.UP: "up",
tcod.event.KeySym.KP_8: "up",
tcod.event.KeySym.a: "left",
tcod.event.KeySym.LEFT: "left",
tcod.event.KeySym.KP_4: "left",
tcod.event.KeySym.s: "down",
tcod.event.KeySym.DOWN: "down",
tcod.event.KeySym.KP_2: "down",
tcod.event.KeySym.d: "right",
tcod.event.KeySym.RIGHT: "right",
tcod.event.KeySym.KP_6: "right",
tcod.event.KeySym.RETURN: "confirm",
tcod.event.KeySym.KP_ENTER: "confirm",
tcod.event.KeySym.ESCAPE: "cancel",
tcod.event.KeySym.SPACE: "wait",
tcod.event.KeySym.i: "inventory",
}
class ScreenNames:
MAIN = "main"
TITLE = "title"
TEST_UI = "test_ui"
GAME_OVER = "game_over"
WIN = "win"
class Screen(tcod.event.EventDispatch):
"""
Describes a game screen.
Handles input, update, and drawing.
"""
def __init__(self, name: str, engine: Engine):
super().__init__()
self.__name = name
self.__engine = engine
def __repr__(self) -> str:
return f"Screen({self.name})"
def __str__(self) -> str:
return f"Screen({self.name})"
@property
def name(self) -> str:
return self.__name
@property
def engine(self) -> Engine:
return self.__engine
@property
def world(self) -> World:
return self.__engine.world
@property
def player(self) -> Entity:
return self.__engine.world["player"]
def setup(self):
pass
def on_quit(self):
self.engine.running = False
def on_draw(self, con: tcod.console.Console):
con.print(0, 0, f"This is the {self.name} screen.")
def on_update(self):
pass
def on_mouse_move(self, x: int, y: int):
pass
def on_mouse_click(self, x: int, y: int):
pass
def on_up(self):
pass
def on_down(self):
pass
def on_left(self):
pass
def on_right(self):
pass
def on_confirm(self):
pass
def on_cancel(self):
pass
def on_wait(self):
pass
def on_enter(self):
pass
def on_inventory(self):
pass
def ev_keydown(self, event: tcod.event.KeyDown):
signal = SIGNALS.get(event.sym)
match signal:
case "up":
return self.on_up()
case "down":
return self.on_down()
case "left":
return self.on_left()
case "right":
return self.on_right()
case "confirm":
return self.on_confirm()
case "cancel":
return self.on_cancel()
case "wait":
return self.on_wait()
case "inventory":
return self.on_inventory()
case _:
return None
def ev_quit(self, event: tcod.event.Quit):
return self.on_quit()
def ev_mousemotion(self, event: tcod.event.MouseMotion):
return self.on_mouse_move(event.tile.x, event.tile.y)
def ev_mousebuttonup(self, event: tcod.event.MouseButtonUp):
if event.button == tcod.event.MouseButton.LEFT:
return self.on_mouse_click(event.tile.x, event.tile.y)