First steps
This commit is contained in:
parent
81190c687e
commit
1e731f5a80
191 changed files with 65 additions and 7 deletions
|
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
|||
from typing import Final, Self
|
||||
|
||||
import attrs
|
||||
import math
|
||||
import tcod.ecs.callbacks
|
||||
from tcod.ecs import Entity
|
||||
|
||||
|
|
@ -18,9 +19,19 @@ class Position:
|
|||
|
||||
def __add__(self, direction: tuple[int, int]) -> Self:
|
||||
"""Add a vector to this position."""
|
||||
match direction:
|
||||
case Position(x, y):
|
||||
return self.__class__(self.x + x, self.y + y)
|
||||
x, y = direction
|
||||
return self.__class__(self.x + x, self.y + y)
|
||||
|
||||
def __sub__(self, direction: tuple[int, int]):
|
||||
match direction:
|
||||
case Position(x, y):
|
||||
return self.__class__(self.x - x, self.y - y)
|
||||
x, y = direction
|
||||
return self.__class__(self.x - x, self.y - y)
|
||||
def length(self):
|
||||
return math.sqrt(self.x**2+self.y**2)
|
||||
|
||||
@tcod.ecs.callbacks.register_component_changed(component=Position)
|
||||
def on_position_changed(entity: Entity, old: Position | None, new: Position | None) -> None:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ from typing import Final
|
|||
|
||||
from tcod.event import KeySym
|
||||
|
||||
from game.world_tools import unlock_door
|
||||
|
||||
DIRECTION_KEYS: Final = {
|
||||
# Arrow keys
|
||||
KeySym.LEFT: (-1, 0),
|
||||
|
|
@ -34,3 +36,7 @@ DIRECTION_KEYS: Final = {
|
|||
KeySym.u: (1, -1),
|
||||
KeySym.n: (1, 1),
|
||||
}
|
||||
ACTION_KEYS: Final = {
|
||||
KeySym.SPACE: unlock_door
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,9 +12,9 @@ import g
|
|||
import game.menus
|
||||
import game.world_tools
|
||||
from game.components import Gold, Graphic, Position
|
||||
from game.constants import DIRECTION_KEYS
|
||||
from game.constants import DIRECTION_KEYS, ACTION_KEYS
|
||||
from game.state import Push, Reset, State, StateResult
|
||||
from game.tags import IsItem, IsPlayer
|
||||
from game.tags import IsItem, IsPlayer, IsWall, IsDoor
|
||||
|
||||
|
||||
@attrs.define()
|
||||
|
|
@ -25,10 +25,17 @@ class InGame(State):
|
|||
"""Handle events for the in-game state."""
|
||||
(player,) = g.world.Q.all_of(tags=[IsPlayer])
|
||||
match event:
|
||||
case tcod.event.KeyDown(sym=sym) if sym in ACTION_KEYS:
|
||||
for door in g.world.Q.all_of(tags=[IsDoor]):
|
||||
if (player.components[Position] - door.components[Position]).length() < 2:
|
||||
ACTION_KEYS[sym](door)
|
||||
case tcod.event.Quit():
|
||||
raise SystemExit()
|
||||
case tcod.event.KeyDown(sym=sym) if sym in DIRECTION_KEYS:
|
||||
player.components[Position] += DIRECTION_KEYS[sym]
|
||||
new_pos = player.components[Position] + DIRECTION_KEYS[sym]
|
||||
if g.world.Q.all_of(tags=[new_pos, IsWall]):
|
||||
return None
|
||||
player.components[Position] = new_pos
|
||||
# Auto pickup gold
|
||||
for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]):
|
||||
player.components[Gold] += gold.components[Gold]
|
||||
|
|
|
|||
|
|
@ -12,3 +12,9 @@ IsActor: Final = "IsActor"
|
|||
|
||||
IsItem: Final = "IsItem"
|
||||
"""Entity is an item."""
|
||||
|
||||
IsWall: Final = "IsWall"
|
||||
"""Entity is a wall."""
|
||||
|
||||
IsDoor: Final = "IsDoor"
|
||||
"""Entiy is a door."""
|
||||
|
|
|
|||
|
|
@ -4,16 +4,17 @@ from __future__ import annotations
|
|||
|
||||
from random import Random
|
||||
|
||||
from tcod.ecs import Registry
|
||||
import g
|
||||
|
||||
from tcod.ecs import Registry, Entity
|
||||
|
||||
from game.components import Gold, Graphic, Position
|
||||
from game.tags import IsActor, IsItem, IsPlayer
|
||||
from game.tags import IsActor, IsItem, IsPlayer, IsWall, IsDoor
|
||||
|
||||
|
||||
def new_world() -> Registry:
|
||||
"""Return a freshly generated world."""
|
||||
world = Registry()
|
||||
|
||||
rng = world[None].components[Random] = Random()
|
||||
|
||||
player = world[object()]
|
||||
|
|
@ -29,4 +30,21 @@ def new_world() -> Registry:
|
|||
gold.components[Gold] = rng.randint(1, 10)
|
||||
gold.tags |= {IsItem}
|
||||
|
||||
for i in range(20):
|
||||
if i == 5 or i == 9:
|
||||
door = world[object()]
|
||||
door.components[Position] = Position(10, i)
|
||||
door.components[Graphic] = Graphic(ord("\\"))
|
||||
door.tags |= {IsDoor}
|
||||
continue
|
||||
wall = world[object()]
|
||||
wall.components[Position] = Position(10, i)
|
||||
wall.components[Graphic] = Graphic(ord("#"))
|
||||
wall.tags |= {IsWall}
|
||||
|
||||
return world
|
||||
|
||||
|
||||
def unlock_door(door: Entity):
|
||||
door.components[Graphic] = Graphic(ord("_"))
|
||||
door.tags.discard(IsDoor)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue