Convert the geometry types to frozen dataclasses

This commit is contained in:
Eryn Wells 2022-05-01 17:32:48 -07:00
parent a072ad507e
commit 8b9897f0cc
2 changed files with 18 additions and 52 deletions

View file

@ -1,78 +1,46 @@
#!/usr/bin/env python3
# Eryn Wells <eryn@erynwells.me>
from typing import Tuple, overload
from dataclasses import dataclass
from typing import Any, Tuple, overload
@dataclass(frozen=True)
class Point:
__slots__ = ('x', 'y')
def __init__(self, x: int = 0, y: int = 0):
self.x = x
self.y = y
x: int = 0
y: int = 0
@overload
def __add__(self, other: 'Vector') -> 'Point':
...
def __add__(self, other) -> 'Point':
def __add__(self, other: Any) -> 'Point':
if not isinstance(other, Vector):
raise TypeError('Only Vector can be added to a Point')
return Point(self.x + other.dx, self.y + other.dy)
@overload
def __eq__(self, other: 'Point') -> bool:
...
def __eq__(self, other):
if not isinstance(other, Point):
raise TypeError('Points can only be compared to other Points')
return self.x == other.x and self.y == other.y
def __str__(self):
return f'(x:{self.x}, y:{self.y})'
def __repr__(self):
return f'Point({self.x}, {self.y})'
@dataclass(frozen=True)
class Vector:
__slots__ = ('dx', 'dy')
def __init__(self, dx: int = 0, dy: int = 0):
self.dx = dx
self.dy = dy
dx: int = 0
dy: int = 0
def __str__(self):
return f'(δx:{self.x}, δy:{self.y})'
def __repr__(self):
return f'Vector({self.x}, {self.y})'
@dataclass(frozen=True)
class Size:
__slots__ = ('width', 'height')
def __init__(self, width: int = 0, height: int = 0):
self.width = width
self.height = height
@property
def as_tuple(self) -> Tuple[int, int]:
return (self.width, self.height)
width: int = 0
height: int = 0
def __str__(self):
return f'(w:{self.width}, h:{self.height})'
def __repr__(self):
return f'Size({self.width}, {self.height})'
@dataclass(frozen=True)
class Rect:
__slots__ = ('origin', 'size')
def __init__(self, x: int = 0, y: int = 0, w: int = 0, h: int = 0):
'''
Construct a new rectangle.
'''
self.origin = Point(x, y)
self.size = Size(w, h)
origin: Point
size: Size
@property
def min_x(self) -> int:
@ -108,8 +76,6 @@ class Rect:
def midpoint(self) -> Point:
return Point(self.mid_x, self.mid_y)
def __str__(self):
return f'[{self.origin}, {self.size}]'
def __repr__(self):
return f'{self.__class__.__name__}({self.origin.x}, {self.origin.y}, {self.size.width}, {self.size.height})'
def __str__(self):
return f'[{self.origin}, {self.size}]'

View file

@ -90,7 +90,7 @@ class RoomsAndCorridorsGenerator(MapGenerator):
self.rng.randint(5, min(15, max(5, node.height - 2))))
origin = Point(node.x + self.rng.randint(1, max(1, node.width - size.width - 1)),
node.y + self.rng.randint(1, max(1, node.height - size.height - 1)))
bounds = Rect(origin.x, origin.y, size.width, size.height)
bounds = Rect(origin, size)
LOG.debug(f'{" " * indent}`-> {bounds}')