From 36d6b63c31d678dbabfd5cfd0c740e505e6fa45e Mon Sep 17 00:00:00 2001 From: staubsauger Date: Fri, 16 Aug 2024 22:25:11 +0200 Subject: [PATCH] center player and stuff --- game/components.py | 12 ++++++++++-- game/constants.py | 2 ++ game/states.py | 11 +++++++++-- main.py | 1 - 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/game/components.py b/game/components.py index 62e4224..f8f3fcb 100644 --- a/game/components.py +++ b/game/components.py @@ -17,19 +17,27 @@ class Position: x: int y: int - def __add__(self, direction: tuple[int, int]) -> Self: + def __add__(self, direction: tuple[int, int] | Position) -> 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]): + def __sub__(self, direction: tuple[int, int] | Position) -> Self: 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 mod(self, other: tuple[int,int] | Position | int) -> Self: + match other: + case Position(x,y): + return self.__class__(self.x//x, self.y//y) + case int(x): + return self.__class__(self.x//x, self.y//x) + x,y = other + return self.__class__(self.x//x, self.y//y) def length(self): return math.sqrt(self.x**2+self.y**2) diff --git a/game/constants.py b/game/constants.py index 4d05e3c..cfb3d87 100644 --- a/game/constants.py +++ b/game/constants.py @@ -40,3 +40,5 @@ ACTION_KEYS: Final = { KeySym.SPACE: unlock_door } + +WALL_CHAR: Final = ord('#') diff --git a/game/states.py b/game/states.py index faea2f6..07b3922 100644 --- a/game/states.py +++ b/game/states.py @@ -1,6 +1,7 @@ """A collection of game states.""" from __future__ import annotations +from functools import reduce import attrs import tcod.console @@ -50,9 +51,15 @@ class InGame(State): def on_draw(self, console: tcod.console.Console) -> None: """Draw the standard screen.""" + centers = g.world.Q.all_of(tags=[IsPlayer]) + centers = list(map(lambda a: a.components[Position], centers)) + center = reduce(lambda a,b: a.components[Position]+b.components[Position], centers) + center.mod(len(centers)) + def draw(e): - pos = e.components[Position] - if (0 <= pos.x < console.width and 0 <= pos.y < console.height): + pos = e.components[Position] - center + if (-console.width//2 <= pos.x < console.width//2\ + and -console.height//2 <= pos.y < console.height//2): graphic = e.components[Graphic] console.rgb[["ch", "fg"]][pos.y, pos.x] = graphic.ch, graphic.fg diff --git a/main.py b/main.py index ae0a4c4..176aefd 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,6 @@ import g import game.state_tools import game.states - def main() -> None: """Entry point function.""" tileset = tcod.tileset.load_tilesheet(