Skip to content

Commit d443e6d

Browse files
committed
Revert state refactors
1 parent 77b8f53 commit d443e6d

5 files changed

Lines changed: 17 additions & 59 deletions

File tree

g.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
"""This module stores globally mutable variables used by this program."""
22
from __future__ import annotations
33

4-
import tcod.console
54
import tcod.context
65
import tcod.ecs
76

8-
import game.state
9-
10-
console: tcod.console.Console
11-
"""The main console."""
12-
137
context: tcod.context.Context
148
"""The window managed by tcod."""
159

16-
states: list[game.state.State] = []
17-
"""A stack of states with the last item being the active state."""
18-
1910
world: tcod.ecs.Registry
2011
"""The active ECS registry and current session."""

game/state.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

game/state_tools.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

game/states.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545

4646

47-
@attrs.define()
47+
@attrs.define(eq=False)
4848
class InGame:
4949
"""Primary in-game state."""
5050

@@ -59,7 +59,8 @@ def on_event(self, event: tcod.event.Event) -> None:
5959
# Auto pickup gold
6060
for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]):
6161
player.components[Gold] += gold.components[Gold]
62-
print(f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g")
62+
text = f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g"
63+
g.world[None].components[("Text", str)] = text
6364
gold.clear()
6465

6566
def on_draw(self, console: tcod.console.Console) -> None:
@@ -70,3 +71,6 @@ def on_draw(self, console: tcod.console.Console) -> None:
7071
continue
7172
graphic = entity.components[Graphic]
7273
console.rgb[["ch", "fg"]][pos.y, pos.x] = graphic.ch, graphic.fg
74+
75+
if text := g.world[None].components.get(("Text", str)):
76+
console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0))

main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import tcod.console
66
import tcod.context
7+
import tcod.event
78
import tcod.tileset
89

910
import g
10-
import game.state_tools
1111
import game.states
1212
import game.world_tools
1313

@@ -18,11 +18,17 @@ def main() -> None:
1818
"data/Alloy_curses_12x12.png", columns=16, rows=16, charmap=tcod.tileset.CHARMAP_CP437
1919
)
2020
tcod.tileset.procedural_block_elements(tileset=tileset)
21-
g.console = tcod.console.Console(80, 50)
22-
g.states = [game.states.InGame()]
21+
console = tcod.console.Console(80, 50)
22+
state = game.states.InGame()
2323
g.world = game.world_tools.new_world()
24-
with tcod.context.new(console=g.console, tileset=tileset) as g.context:
25-
game.state_tools.main_loop()
24+
with tcod.context.new(console=console, tileset=tileset) as g.context:
25+
while True: # Main loop
26+
console.clear() # Clear the console before any drawing
27+
state.on_draw(console) # Draw the current state
28+
g.context.present(console) # Render the console to the window and show it
29+
for event in tcod.event.wait(): # Event loop, blocks until pending events exist
30+
print(event)
31+
state.on_event(event) # Dispatch events to the state
2632

2733

2834
if __name__ == "__main__":

0 commit comments

Comments
 (0)