1
0
Fork 0
python-roguelike/entity.py

45 lines
1.1 KiB
Python
Raw Normal View History

2022-01-07 15:52:53 -05:00
from __future__ import annotations
import copy
from typing import Tuple, TypeVar, TYPE_CHECKING
if TYPE_CHECKING:
from game_map import GameMap
T = TypeVar("T", bound="Entity")
2022-01-06 11:19:56 -05:00
class Entity:
"""
A generic object to represent players, enemies, items, etc.
"""
2022-01-07 15:52:53 -05:00
def __init__(
self,
x: int = 0,
y: int = 0,
char: str = "?",
color: Tuple[int, int, int] = (255, 255, 255),
name: str = "<Unnamed>",
blocks_movement: bool = False,
):
2022-01-06 11:19:56 -05:00
self.x = x
self.y = y
self.char = char
self.color = color
2022-01-07 15:52:53 -05:00
self.name = name
self.blocks_movement = blocks_movement
def spawn(self: T, gamemap: GameMap, x: int, y: int) -> T:
"""Spawn a copy of this instance at the given location."""
clone = copy.deepcopy(self)
clone.x = x
clone.y = y
gamemap.entities.add(clone)
return clone
2022-01-06 11:19:56 -05:00
def move(self, dx: int, dy: int):
# Move the entity by a given amount
self.x += dx
self.y += dy