squirrel-battle/dungeonbattle/entities/monsters.py

85 lines
2.1 KiB
Python
Raw Normal View History

from random import choice
2020-11-10 21:59:02 +00:00
from .player import Player
2020-11-06 14:33:26 +00:00
from ..interfaces import FightingEntity, Map
2020-10-23 14:51:48 +00:00
class Monster(FightingEntity):
"""
The class for all monsters in the dungeon
"""
def __init__(self) -> None:
super().__init__()
2020-11-06 15:13:28 +00:00
def act(self, m: Map) -> None:
"""
By default, a monster will move randomly where it is possible
And if a player is close to the monster, the monster run on the player.
"""
2020-11-10 21:59:02 +00:00
target = None
for entity in m.entities:
if self.distance_squared(entity) <= 25 and \
isinstance(entity, Player):
target = entity
break
2020-10-23 14:51:48 +00:00
# A Dijkstra algorithm has ran that targets the player.
# With that way, monsters can simply follow the path.
# If they can't move and they are already close to the player,
# They hit.
if target and (self.y, self.x) in target.paths:
2020-11-10 21:59:02 +00:00
# Move to target player
next_y, next_x = target.paths[(self.y, self.x)]
moved = self.check_move(next_y, next_x, True)
if not moved and self.distance_squared(target) <= 1:
self.hit(target)
2020-11-10 21:59:02 +00:00
else:
for _ in range(100):
if choice([self.move_up, self.move_down,
self.move_left, self.move_right])():
break
2020-11-06 14:33:26 +00:00
2020-11-11 16:39:48 +00:00
class Beaver(Monster):
"""
A beaver monster
"""
def __init__(self) -> None:
super().__init__()
2020-11-11 16:39:48 +00:00
name = "beaver"
maxhealth = 30
strength = 2
2020-11-10 20:47:36 +00:00
class Hedgehog(Monster):
"""
A really mean hedgehog monster
"""
def __init__(self) -> None:
super().__init__()
2020-11-10 20:47:36 +00:00
name = "hedgehog"
maxhealth = 10
strength = 3
2020-11-11 16:39:48 +00:00
class Rabbit(Monster):
"""
A rabbit monster
"""
def __init__(self) -> None:
super().__init__()
2020-11-11 16:39:48 +00:00
name = "rabbit"
maxhealth = 15
strength = 1
class TeddyBear(Monster):
"""
A cute teddybear monster
"""
def __init__(self) -> None:
super().__init__()
2020-11-11 16:39:48 +00:00
name = "teddy_bear"
2020-11-13 13:07:27 +00:00
maxhealth = 50
2020-11-11 16:39:48 +00:00
strength = 0