Bugfix to track enemy position in state.

This commit is contained in:
Timothy VanderZee 2020-04-20 19:36:57 -05:00
parent 60f4acce54
commit c3d6226808
2 changed files with 10 additions and 2 deletions

View File

@ -43,9 +43,12 @@ func make_dist_tree():
dist += 1
func pathfind(player_pos):
if get_pos() == player_pos:
return null
var dist_tree = make_dist_tree()
if not player_pos in dist_tree:
# The player cannot be reached.
print("No path.")
return null
var last_dist = dist_tree[player_pos]
var path = [player_pos]
@ -80,13 +83,18 @@ func _physics_process(delta):
mv_delta = 0
var state = get_parent().get_state()
var player_pos = state.player_pos
var path = pathfind(player_pos)
var goal_pos = state.goal_pos
var target_pos = player_pos
var path = pathfind(target_pos)
if path == null:
return
var mv_target = path[-1]
for offset in directions:
if get_pos() + offset == mv_target:
var old_pos = get_pos()
self.translate(offset * Vector2(speed, speed))
var new_pos = get_pos()
state.move(old_pos, new_pos)
break
func get_pos():

View File

@ -11,6 +11,7 @@ var enemy = null
var player_pos = Vector2()
var enemy_pos = Vector2()
var goal_pos = Vector2()
func _init(lvlmap, plr, enm):
level_map = lvlmap
@ -18,7 +19,6 @@ func _init(lvlmap, plr, enm):
enemy = enm
func get_type(pos):
print(level_map[pos.y][pos.x].type)
return level_map[pos.y][pos.x].type
func clear_tile(pos):