Complete player and enemy movement.

This commit is contained in:
Timothy VanderZee 2020-04-20 17:54:52 -05:00
parent d676ea01be
commit 248293aa65
4 changed files with 30 additions and 5 deletions

View File

@ -1,3 +1,3 @@
extends Node2D extends Node2D
var type = "Empty" var type = "Empty"

View File

@ -80,7 +80,7 @@ func _physics_process(delta):
mv_delta = 0 mv_delta = 0
var state = get_parent().get_state() var state = get_parent().get_state()
var player_pos = state.player_pos var player_pos = state.player_pos
var path = pathfind(Vector2(1, 5)) var path = pathfind(player_pos)
if path == null: if path == null:
return return
var mv_target = path[-1] var mv_target = path[-1]
@ -92,4 +92,4 @@ func _physics_process(delta):
func get_pos(): func get_pos():
var pos = self.global_position var pos = self.global_position
pos = Vector2(pos.x/50, pos.y/50) pos = Vector2(pos.x/50, pos.y/50)
return pos return pos

View File

@ -17,10 +17,17 @@ func _physics_process(_delta):
if Input.is_action_just_pressed(input): if Input.is_action_just_pressed(input):
var offset = dir_offsets[input] var offset = dir_offsets[input]
var mv_target = get_pos() + offset var mv_target = get_pos() + offset
var old_pos = get_pos()
if state.get_type(mv_target) == "Empty": if state.get_type(mv_target) == "Empty":
self.translate(offset * Vector2(speed, speed)) 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: 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() state.player_pos = get_pos()
func get_pos(): func get_pos():

View File

@ -21,6 +21,24 @@ func get_type(pos):
print(level_map[pos.y][pos.x].type) print(level_map[pos.y][pos.x].type)
return 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 # We only expect having to set Empty and Rock types
func set_pos(pos, type): func set_pos(pos, type):
if type == "Rock": if type == "Rock":
@ -45,4 +63,4 @@ func set_pos(pos, type):
set_pos(Vector2(pos.x, current_space), "Rock") set_pos(Vector2(pos.x, current_space), "Rock")
else: else:
current_space = y current_space = y