Implement basic enemy movement.

This commit is contained in:
Josiah VanderZee 2020-04-19 17:55:28 -05:00
parent 26073ca4e5
commit 32fbf2154e
2 changed files with 32 additions and 14 deletions

View File

@ -1,26 +1,45 @@
extends Node2D extends Node2D
var speed = 50
var type = "Enemy" var type = "Enemy"
func choose_move_target(player_pos): var mv_delta = 0
func choose_move_offset(player_pos):
var state = get_parent().get_state() var state = get_parent().get_state()
var target = null var mv_offset = null
var pos = global_position var my_pos = get_pos()
var delta_x = player_pos[0] - pos[0] var delta_x = player_pos.x - my_pos.x
var delta_y = player_pos[1] - pos[1] var delta_y = player_pos.y - my_pos.y
if delta_x > delta_y: if abs(delta_x) > abs(delta_y):
if delta_x > 0: if delta_x > 0:
target = Vector2(pos[0] + 1, pos[1]) mv_offset = Vector2(1, 0)
else: else:
target = Vector2(pos[0] - 1, pos[1]) mv_offset = Vector2(-1, 0)
else: else:
if delta_y > 0: if delta_y > 0:
target = Vector2(pos[0], pos[1] + 1) mv_offset = Vector2(0, 1)
else: else:
target = Vector2(pos[0], pos[1] - 1) mv_offset = Vector2(0, -1)
if state.is_open(target): var mv_target = my_pos + mv_offset
return target if state.get_type(mv_target) == "Empty":
return mv_offset
else: else:
# TODO follow a wall. # TODO follow a wall.
pass pass
func _physics_process(delta):
mv_delta += delta
if mv_delta >= 1:
mv_delta = 0
var state = get_parent().get_state()
var player_pos = state.player_pos
var mv_offset = choose_move_offset(player_pos)
if not mv_offset == null:
self.translate(mv_offset * Vector2(speed, speed))
func get_pos():
var pos = self.global_position
pos = Vector2(pos.x/50, pos.y/50)
return pos

View File

@ -11,4 +11,3 @@ margin_right = 40.0
margin_bottom = 40.0 margin_bottom = 40.0
texture = ExtResource( 2 ) texture = ExtResource( 2 )
expand = true expand = true