From 248293aa65a945e62ca36d9d307f457dbf80bad0 Mon Sep 17 00:00:00 2001 From: Timothy VanderZee Date: Mon, 20 Apr 2020 17:54:52 -0500 Subject: [PATCH] Complete player and enemy movement. --- Empty.gd | 2 +- Enemy.gd | 4 ++-- Player.gd | 9 ++++++++- State.gd | 20 +++++++++++++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Empty.gd b/Empty.gd index 9527257..c977fd6 100644 --- a/Empty.gd +++ b/Empty.gd @@ -1,3 +1,3 @@ extends Node2D -var type = "Empty" \ No newline at end of file +var type = "Empty" diff --git a/Enemy.gd b/Enemy.gd index 43f4fa5..616b2f7 100644 --- a/Enemy.gd +++ b/Enemy.gd @@ -80,7 +80,7 @@ func _physics_process(delta): mv_delta = 0 var state = get_parent().get_state() var player_pos = state.player_pos - var path = pathfind(Vector2(1, 5)) + var path = pathfind(player_pos) if path == null: return var mv_target = path[-1] @@ -92,4 +92,4 @@ func _physics_process(delta): func get_pos(): var pos = self.global_position pos = Vector2(pos.x/50, pos.y/50) - return pos \ No newline at end of file + return pos diff --git a/Player.gd b/Player.gd index 3ce84dc..39d1152 100644 --- a/Player.gd +++ b/Player.gd @@ -17,10 +17,17 @@ func _physics_process(_delta): if Input.is_action_just_pressed(input): var offset = dir_offsets[input] var mv_target = get_pos() + offset + var old_pos = get_pos() if state.get_type(mv_target) == "Empty": self.translate(offset * Vector2(speed, speed)) + var new_pos = get_pos() + state.move(old_pos, new_pos) + elif state.get_type(mv_target) == "Dirt" and not digging: + self.translate(offset * Vector2(speed, speed)) + var new_pos = get_pos() + state.move(old_pos, new_pos) elif state.get_type(mv_target) == "Dirt" and digging: - state.set_pos(mv_target, "Empty") + state.clear_tile(mv_target) state.player_pos = get_pos() func get_pos(): diff --git a/State.gd b/State.gd index 5b2574c..25a705d 100644 --- a/State.gd +++ b/State.gd @@ -21,6 +21,24 @@ func get_type(pos): print(level_map[pos.y][pos.x].type) return level_map[pos.y][pos.x].type +func clear_tile(pos): + var old_instance = level_map[pos.y][pos.x] + old_instance.queue_free() + var new_instance = empty.instance() + new_instance.global_position = Vector2(pos * Vector2(SPACE, SPACE)) + level_map[pos.y][pos.x] = new_instance + +func move(pos1, pos2): + var instance1 = level_map[pos1.y][pos1.x] + var instance2 = level_map[pos2.y][pos2.x] + instance2.queue_free() + var new_instance = empty.instance() + new_instance.global_position = Vector2(pos1 * Vector2(SPACE, SPACE)) + level_map[pos1.y][pos1.x] = new_instance + level_map[pos2.y][pos2.x] = instance1 + + + # We only expect having to set Empty and Rock types func set_pos(pos, type): if type == "Rock": @@ -45,4 +63,4 @@ func set_pos(pos, type): set_pos(Vector2(pos.x, current_space), "Rock") else: current_space = y - \ No newline at end of file +