From 32fbf2154ea11851e8f58574d594be62f49303fd Mon Sep 17 00:00:00 2001 From: Josiah VanderZee Date: Sun, 19 Apr 2020 17:55:28 -0500 Subject: [PATCH] Implement basic enemy movement. --- Enemy.gd | 45 ++++++++++++++++++++++++++++++++------------- Enemy.tscn | 1 - 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Enemy.gd b/Enemy.gd index 8bb135a..45af072 100644 --- a/Enemy.gd +++ b/Enemy.gd @@ -1,26 +1,45 @@ extends Node2D +var speed = 50 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 target = null - var pos = global_position - var delta_x = player_pos[0] - pos[0] - var delta_y = player_pos[1] - pos[1] - if delta_x > delta_y: + var mv_offset = null + var my_pos = get_pos() + var delta_x = player_pos.x - my_pos.x + var delta_y = player_pos.y - my_pos.y + if abs(delta_x) > abs(delta_y): if delta_x > 0: - target = Vector2(pos[0] + 1, pos[1]) + mv_offset = Vector2(1, 0) else: - target = Vector2(pos[0] - 1, pos[1]) + mv_offset = Vector2(-1, 0) else: if delta_y > 0: - target = Vector2(pos[0], pos[1] + 1) + mv_offset = Vector2(0, 1) else: - target = Vector2(pos[0], pos[1] - 1) + mv_offset = Vector2(0, -1) - if state.is_open(target): - return target + var mv_target = my_pos + mv_offset + if state.get_type(mv_target) == "Empty": + return mv_offset else: # TODO follow a wall. - pass \ No newline at end of file + 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 diff --git a/Enemy.tscn b/Enemy.tscn index cea74b7..bb7af05 100644 --- a/Enemy.tscn +++ b/Enemy.tscn @@ -11,4 +11,3 @@ margin_right = 40.0 margin_bottom = 40.0 texture = ExtResource( 2 ) expand = true -