Move Monster to the object module

This commit is contained in:
Eryn Wells 2022-05-08 09:48:05 -07:00
parent cf0b120fad
commit cf9ec2d17e
2 changed files with 36 additions and 17 deletions

View file

@ -1,11 +1,12 @@
# Eryn Wells <eryn@erynwells.me>
'''Defines the Species type, which represents a class of monsters, and all the monster types the hero can encounter in
the dungeon.'''
from dataclasses import dataclass
from typing import Tuple
from .geometry import Point
from .object import Entity
# pylint: disable=too-many-instance-attributes
@dataclass(frozen=True)
class Species:
'''A kind of monster.
@ -30,19 +31,19 @@ class Species:
symbol: str
maximum_hit_points: int
sight_radius: int
# TODO: Rename these two attributes something better
attack_power: int
defense: int
foreground_color: Tuple[int, int, int]
background_color: Tuple[int, int, int] = None
class Monster(Entity):
'''An instance of a Species.'''
def __init__(self, species: Species, position: Point = None):
super().__init__(species.symbol, position=position, fg=species.foreground_color, bg=species.background_color)
self.species: Species = species
self.hit_points: int = species.maximum_hit_points
def __str__(self) -> str:
return f'{self.symbol}[{self.species.name}][{self.position}][{self.hit_points}/{self.species.maximum_hit_points}]'
Orc = Species(name='Orc', symbol='o', foreground_color=(63, 127, 63), maximum_hit_points=10, sight_radius=4)
Troll = Species(name='Troll', symbol='T', foreground_color=(0, 127, 0), maximum_hit_points=20, sight_radius=4)
Orc = Species(name='Orc', symbol='o',
foreground_color=(63, 127, 63),
maximum_hit_points=10,
sight_radius=4,
attack_power=4, defense=1)
Troll = Species(name='Troll', symbol='T',
foreground_color=(0, 127, 0),
maximum_hit_points=16,
sight_radius=4,
attack_power=3, defense=0)

View file

@ -1,11 +1,16 @@
#!/usr/bin/env python3
# Eryn Wells <eryn@erynwells.me>
from typing import Optional, Tuple
from typing import TYPE_CHECKING, Optional, Tuple, Type
import tcod
from .components import Fighter
from .geometry import Point
from .monsters import Species
if TYPE_CHECKING:
from .ai import AI
class Entity:
'''A single-tile drawable entity with a symbol and position.'''
@ -31,3 +36,16 @@ class Entity:
class Hero(Entity):
def __init__(self, position: Point):
super().__init__('@', position=position, fg=tuple(tcod.white))
class Monster(Entity):
'''An instance of a Species.'''
def __init__(self, species: Species, ai_class: Type['AI'], position: Point = None):
super().__init__(species.symbol, ai=ai_class(self), position=position, fg=species.foreground_color, bg=species.background_color)
self.species = species
self.fighter = Fighter(maximum_hit_points=species.maximum_hit_points,
attack_power=species.attack_power,
defense=species.defense)
def __str__(self) -> str:
return f'{self.symbol}[{self.species.name}][{self.position}][{self.fighter.hit_points}/{self.fighter.maximum_hit_points}]'