Add some basic geometry primitives
Point, Vector, Size, and Rect. These are modeled on the CoreGraphics types.
This commit is contained in:
parent
9ddeef2561
commit
4f6d04456c
2 changed files with 72 additions and 6 deletions
65
roguebasin/geometry.py
Normal file
65
roguebasin/geometry.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
#!/usr/bin/env python3
|
||||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
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})'
|
|
@ -2,6 +2,7 @@
|
|||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue