Use Dijkstra algorithm to describe monster paths

This commit is contained in:
Yohann D'ANELLO
2020-11-11 15:25:50 +01:00
parent 2f3a03dbf7
commit d08ff7061f
2 changed files with 34 additions and 13 deletions

View File

@ -17,20 +17,15 @@ class Monster(FightingEntity):
target = entity
break
if target:
# 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
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()
if self.distance_squared(target) <= 1:
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):