2020-11-10 21:34:12 +00:00
|
|
|
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-16 15:58:00 +00:00
|
|
|
|
2020-10-23 14:51:48 +00:00
|
|
|
class Monster(FightingEntity):
|
2020-11-06 15:13:28 +00:00
|
|
|
def act(self, m: Map) -> None:
|
2020-11-10 21:34:12 +00:00
|
|
|
"""
|
|
|
|
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
|
2020-11-10 21:34:12 +00:00
|
|
|
break
|
2020-10-23 14:51:48 +00:00
|
|
|
|
2020-11-10 21:59:02 +00:00
|
|
|
if target:
|
|
|
|
# Move to target player
|
|
|
|
y, x = self.vector(target)
|
|
|
|
if abs(y) > abs(x): # Move vertically
|
|
|
|
if y > 0:
|
|
|
|
self.move_down()
|
|
|
|
else:
|
|
|
|
self.move_up()
|
|
|
|
else: # Move horizontally
|
|
|
|
if x > 0:
|
|
|
|
self.move_right()
|
|
|
|
else:
|
|
|
|
self.move_left()
|
|
|
|
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-10 20:47:36 +00:00
|
|
|
class Hedgehog(Monster):
|
|
|
|
name = "hedgehog"
|
2020-10-16 15:58:00 +00:00
|
|
|
maxhealth = 10
|
|
|
|
strength = 3
|