squirrel-battle/dungeonbattle/entities/monsters.py

59 lines
1.6 KiB
Python

from random import choice
from .player import Player
from ..interfaces import FightingEntity, Map
class Monster(FightingEntity):
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.
"""
target = None
for entity in m.entities:
if self.distance_squared(entity) <= 25 and \
isinstance(entity, Player):
target = entity
break
# 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:
# 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)
else:
for _ in range(100):
if choice([self.move_up, self.move_down,
self.move_left, self.move_right])():
break
class Beaver(Monster):
name = "beaver"
maxhealth = 30
strength = 2
class Hedgehog(Monster):
name = "hedgehog"
maxhealth = 10
strength = 3
class Rabbit(Monster):
name = "rabbit"
maxhealth = 15
strength = 1
class TeddyBear(Monster):
name = "teddy_bear"
maxhealth = 50
strength = 0