Convert the geometry types to frozen dataclasses
This commit is contained in:
parent
a072ad507e
commit
8b9897f0cc
2 changed files with 18 additions and 52 deletions
|
|
@ -1,78 +1,46 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# Eryn Wells <eryn@erynwells.me>
|
# 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:
|
class Point:
|
||||||
__slots__ = ('x', 'y')
|
x: int = 0
|
||||||
|
y: int = 0
|
||||||
def __init__(self, x: int = 0, y: int = 0):
|
|
||||||
self.x = x
|
|
||||||
self.y = y
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __add__(self, other: 'Vector') -> 'Point':
|
def __add__(self, other: 'Vector') -> 'Point':
|
||||||
...
|
...
|
||||||
|
|
||||||
def __add__(self, other) -> 'Point':
|
def __add__(self, other: Any) -> 'Point':
|
||||||
if not isinstance(other, Vector):
|
if not isinstance(other, Vector):
|
||||||
raise TypeError('Only Vector can be added to a Point')
|
raise TypeError('Only Vector can be added to a Point')
|
||||||
return Point(self.x + other.dx, self.y + other.dy)
|
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):
|
def __str__(self):
|
||||||
return f'(x:{self.x}, y:{self.y})'
|
return f'(x:{self.x}, y:{self.y})'
|
||||||
|
|
||||||
def __repr__(self):
|
@dataclass(frozen=True)
|
||||||
return f'Point({self.x}, {self.y})'
|
|
||||||
|
|
||||||
class Vector:
|
class Vector:
|
||||||
__slots__ = ('dx', 'dy')
|
dx: int = 0
|
||||||
|
dy: int = 0
|
||||||
def __init__(self, dx: int = 0, dy: int = 0):
|
|
||||||
self.dx = dx
|
|
||||||
self.dy = dy
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'(δx:{self.x}, δy:{self.y})'
|
return f'(δx:{self.x}, δy:{self.y})'
|
||||||
|
|
||||||
def __repr__(self):
|
@dataclass(frozen=True)
|
||||||
return f'Vector({self.x}, {self.y})'
|
|
||||||
|
|
||||||
class Size:
|
class Size:
|
||||||
__slots__ = ('width', 'height')
|
width: int = 0
|
||||||
|
height: int = 0
|
||||||
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)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'(w:{self.width}, h:{self.height})'
|
return f'(w:{self.width}, h:{self.height})'
|
||||||
|
|
||||||
def __repr__(self):
|
@dataclass(frozen=True)
|
||||||
return f'Size({self.width}, {self.height})'
|
|
||||||
|
|
||||||
class Rect:
|
class Rect:
|
||||||
__slots__ = ('origin', 'size')
|
origin: Point
|
||||||
|
size: 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)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def min_x(self) -> int:
|
def min_x(self) -> int:
|
||||||
|
|
@ -108,8 +76,6 @@ class Rect:
|
||||||
def midpoint(self) -> Point:
|
def midpoint(self) -> Point:
|
||||||
return Point(self.mid_x, self.mid_y)
|
return Point(self.mid_x, self.mid_y)
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return f'[{self.origin}, {self.size}]'
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __str__(self):
|
||||||
return f'{self.__class__.__name__}({self.origin.x}, {self.origin.y}, {self.size.width}, {self.size.height})'
|
return f'[{self.origin}, {self.size}]'
|
||||||
|
|
@ -90,7 +90,7 @@ class RoomsAndCorridorsGenerator(MapGenerator):
|
||||||
self.rng.randint(5, min(15, max(5, node.height - 2))))
|
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)),
|
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)))
|
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}')
|
LOG.debug(f'{" " * indent}`-> {bounds}')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue