Move to closest player if it is close

This commit is contained in:
Yohann D'ANELLO
2020-11-10 22:59:02 +01:00
parent 12ee436f4d
commit f9f02b6621
3 changed files with 48 additions and 5 deletions

View File

@ -1,5 +1,6 @@
from random import choice
from .player import Player
from ..interfaces import FightingEntity, Map
@ -9,12 +10,32 @@ class Monster(FightingEntity):
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.
"""
# TODO If a player is close, move to the player
for _ in range(100):
if choice([self.move_up, self.move_down,
self.move_left, self.move_right])():
target = None
for entity in m.entities:
if self.distance_squared(entity) <= 25 and \
isinstance(entity, Player):
target = entity
break
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
class Hedgehog(Monster):
name = "hedgehog"