diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 1f222f8..da5c232 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,5 +1,7 @@ -from .config import CONFIG, Position, Size +from .config import CONFIG from .log import log from .path import BASE_PATH +from .position import Position +from .size import Size __all__ = ["BASE_PATH", "CONFIG", "log", "Size", "Position"] diff --git a/src/utils/config.py b/src/utils/config.py index abf3ea3..49dc9ec 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -1,42 +1,12 @@ -from typing import NamedTuple, Union - from attr import define, field from .colors import TokyoNightNight +from .position import Position +from .size import Size PADDING = 20 -class Size(NamedTuple): - width: int | float - height: int | float - - def add(self, other: Union["Size", int, float]) -> "Size": - if isinstance(other, Size): - return Size(self.width + other.width, self.height + other.height) - return Size(self.width + other, self.height + other) - - def sub(self, other: Union["Size", int, float]) -> "Size": - if isinstance(other, Size): - return Size(self.width - other.width, self.height - other.height) - return Size(self.width - other, self.height - other) - - -class Position(NamedTuple): - x: int | float - y: int | float - - def add(self, other: Union["Position", int, float]) -> "Position": - if isinstance(other, Position): - return Position(self.x + other.x, self.y + other.y) - return Position(self.x + other, self.y + other) - - def sub(self, other: Union["Position", int, float]) -> "Position": - if isinstance(other, Position): - return Position(self.x - other.x, self.y - other.y) - return Position(self.x - other, self.y - other) - - @define class Game: columns: int = 10 @@ -44,8 +14,8 @@ class Game: line_width: int = 1 border_radius: int = 2 padding: int = PADDING - cell_size: int = 40 - size: Size = Size(columns * cell_size, rows * cell_size) + cell: Size = Size(40, 40) + size: Size = Size(columns * cell.width, rows * cell.width) pos: Position = Position(padding, padding) diff --git a/src/utils/position.py b/src/utils/position.py new file mode 100644 index 0000000..9dc1474 --- /dev/null +++ b/src/utils/position.py @@ -0,0 +1,43 @@ +from typing import NamedTuple, Union + + +class Position(NamedTuple): + x: int | float + y: int | float + + def add(self, other: Union["Position", int, float]) -> "Position": + if isinstance(other, Position): + return Position(self.x + other.x, self.y + other.y) + return Position(self.x + other, self.y + other) + + def sub(self, other: Union["Position", int, float]) -> "Position": + if isinstance(other, Position): + return Position(self.x - other.x, self.y - other.y) + return Position(self.x - other, self.y - other) + + def add_x(self, other: Union["Position", int, float]) -> "Position": + if isinstance(other, Position): + return Position(self.x + other.x, self.y) + return Position(self.x + other, self.y) + + def add_y(self, other: Union["Position", int, float]) -> "Position": + if isinstance(other, Position): + return Position(self.x, self.y + other.y) + return Position(self.x, self.y + other) + + def sub_x(self, other: Union["Position", int, float]) -> "Position": + if isinstance(other, Position): + return Position(self.x - other.x, self.y) + return Position(self.x - other, self.y) + + def sub_y(self, other: Union["Position", int, float]) -> "Position": + if isinstance(other, Position): + return Position(self.x, self.y - other.y) + return Position(self.x, self.y - other) + + def to_grid(self) -> "Position": + from .config import CONFIG + + return Position( + self.x * CONFIG.game.cell.width, self.y * CONFIG.game.cell.height + ) diff --git a/src/utils/size.py b/src/utils/size.py new file mode 100644 index 0000000..50114ac --- /dev/null +++ b/src/utils/size.py @@ -0,0 +1,16 @@ +from typing import NamedTuple, Union + + +class Size(NamedTuple): + width: int | float + height: int | float + + def add(self, other: Union["Size", int, float]) -> "Size": + if isinstance(other, Size): + return Size(self.width + other.width, self.height + other.height) + return Size(self.width + other, self.height + other) + + def sub(self, other: Union["Size", int, float]) -> "Size": + if isinstance(other, Size): + return Size(self.width - other.width, self.height - other.height) + return Size(self.width - other, self.height - other)