diff --git a/roguebasin/geometry.py b/roguebasin/geometry.py new file mode 100644 index 0000000..d92bb16 --- /dev/null +++ b/roguebasin/geometry.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# Eryn Wells + +class Point: + __slots__ = ('x', 'y') + + def __init__(self, x: int = 0, y: int = 0): + self.x = x + self.y = y + + def __str__(self): + return f'(x:{self.x}, y:{self.y})' + + def __repr__(self): + return f'Point({self.x}, {self.y})' + +class Vector: + __slots__ = ('dx', 'dy') + + def __init__(self, dx: int = 0, dy: int = 0): + self.dx = dx + self.dy = dy + + def __str__(self): + return f'(δx:{self.x}, δy:{self.y})' + + def __repr__(self): + return f'Vector({self.x}, {self.y})' + +class Size: + __slots__ = ('width', 'height') + + def __init__(self, width: int = 0, height: int = 0): + self.width = width + self.height = height + + def __str__(self): + return f'(w:{self.width}, h:{self.height})' + + def __repr__(self): + return f'Size({self.width}, {self.height})' + +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) + + @property + def max_x(self) -> int: + return self.origin.x + self.size.width + + @property + def max_y(self) -> int: + return self.origin.y + self.size.height + + def __str__(self): + return f'({self.origin}, {self.size})' + + def __repr__(self): + return f'Rect({self.origin.x}, {self.origin.y}, {self.size.width}, {self.size.height})' \ No newline at end of file diff --git a/roguebasin/object.py b/roguebasin/object.py index a748939..1f02bfb 100644 --- a/roguebasin/object.py +++ b/roguebasin/object.py @@ -2,6 +2,7 @@ # Eryn Wells import tcod +from .geometry import Point, Vector class Object: '''A drawable object with a symbol and (x, y) position.''' @@ -28,15 +29,15 @@ class Object: def y(self, value): self.__y = int(value) - def move(self, dx: int, dy: int): + def move(self, delta: Vector): '''Move this object by (dx, dy).''' - self.__x += dx - self.__y += dy + self.__x += delta.dx + self.__y += delta.dy - def move_to(self, x: int, y: int) -> None: + def move_to(self, point: Point) -> None: '''Move this object directly to the given position.''' - self.__x = x - self.__y = y + self.__x = point.x + self.__y = point.y def print(self, console: tcod.Console) -> None: console.print(x=self.__x, y=self.__y, string=self.__symbol, fg=self.__color)