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 random import choice
from .player import Player
from ..interfaces import FightingEntity, Map from ..interfaces import FightingEntity, Map
@ -9,12 +10,32 @@ class Monster(FightingEntity):
By default, a monster will move randomly where it is possible 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. 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 target = None
for _ in range(100): for entity in m.entities:
if choice([self.move_up, self.move_down, if self.distance_squared(entity) <= 25 and \
self.move_left, self.move_right])(): isinstance(entity, Player):
target = entity
break 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): class Hedgehog(Monster):
name = "hedgehog" name = "hedgehog"

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from enum import Enum, auto from enum import Enum, auto
from math import sqrt
from random import randint from random import randint
from typing import List from typing import List, Tuple
from dungeonbattle.display.texturepack import TexturePack from dungeonbattle.display.texturepack import TexturePack
@ -172,6 +173,22 @@ class Entity:
""" """
pass pass
def distance_squared(self, other: "Entity") -> int:
"""
Get the square of the distance to another entity.
Useful to check distances since square root takes time.
"""
return (self.y - other.y) ** 2 + (self.x - other.x) ** 2
def distance(self, other: "Entity") -> float:
"""
Get the cartesian distance to another entity.
"""
return sqrt(self.distance_squared(other))
def vector(self, other: "Entity") -> Tuple[int, int]:
return other.y - self.y, other.x - self.x
class FightingEntity(Entity): class FightingEntity(Entity):
maxhealth: int maxhealth: int

View File

@ -23,6 +23,11 @@ class TestEntities(unittest.TestCase):
self.assertEqual(entity.x, 64) self.assertEqual(entity.x, 64)
self.assertIsNone(entity.act(self.map)) self.assertIsNone(entity.act(self.map))
other_entity = Entity()
other_entity.move(45, 68)
self.assertEqual(entity.distance_squared(other_entity), 25)
self.assertEqual(entity.distance(other_entity), 5)
def test_fighting_entities(self) -> None: def test_fighting_entities(self) -> None:
""" """
Test some random stuff with fighting entities. Test some random stuff with fighting entities.