center player and stuff
This commit is contained in:
parent
6cd571476f
commit
36d6b63c31
4 changed files with 21 additions and 5 deletions
|
|
@ -17,19 +17,27 @@ class Position:
|
||||||
x: int
|
x: int
|
||||||
y: 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."""
|
"""Add a vector to this position."""
|
||||||
match direction:
|
match direction:
|
||||||
case Position(x, y):
|
case Position(x, y):
|
||||||
return self.__class__(self.x + x, self.y + y)
|
return self.__class__(self.x + x, self.y + y)
|
||||||
x, y = direction
|
x, y = direction
|
||||||
return self.__class__(self.x + x, self.y + y)
|
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:
|
match direction:
|
||||||
case Position(x, y):
|
case Position(x, y):
|
||||||
return self.__class__(self.x - x, self.y - y)
|
return self.__class__(self.x - x, self.y - y)
|
||||||
x, y = direction
|
x, y = direction
|
||||||
return self.__class__(self.x - x, self.y - y)
|
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):
|
def length(self):
|
||||||
return math.sqrt(self.x**2+self.y**2)
|
return math.sqrt(self.x**2+self.y**2)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,3 +40,5 @@ ACTION_KEYS: Final = {
|
||||||
KeySym.SPACE: unlock_door
|
KeySym.SPACE: unlock_door
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WALL_CHAR: Final = ord('#')
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""A collection of game states."""
|
"""A collection of game states."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from functools import reduce
|
||||||
|
|
||||||
import attrs
|
import attrs
|
||||||
import tcod.console
|
import tcod.console
|
||||||
|
|
@ -50,9 +51,15 @@ class InGame(State):
|
||||||
|
|
||||||
def on_draw(self, console: tcod.console.Console) -> None:
|
def on_draw(self, console: tcod.console.Console) -> None:
|
||||||
"""Draw the standard screen."""
|
"""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):
|
def draw(e):
|
||||||
pos = e.components[Position]
|
pos = e.components[Position] - center
|
||||||
if (0 <= pos.x < console.width and 0 <= pos.y < console.height):
|
if (-console.width//2 <= pos.x < console.width//2\
|
||||||
|
and -console.height//2 <= pos.y < console.height//2):
|
||||||
graphic = e.components[Graphic]
|
graphic = e.components[Graphic]
|
||||||
console.rgb[["ch", "fg"]][pos.y, pos.x] = graphic.ch, graphic.fg
|
console.rgb[["ch", "fg"]][pos.y, pos.x] = graphic.ch, graphic.fg
|
||||||
|
|
||||||
|
|
|
||||||
1
main.py
1
main.py
|
|
@ -11,7 +11,6 @@ import g
|
||||||
import game.state_tools
|
import game.state_tools
|
||||||
import game.states
|
import game.states
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""Entry point function."""
|
"""Entry point function."""
|
||||||
tileset = tcod.tileset.load_tilesheet(
|
tileset = tcod.tileset.load_tilesheet(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue