diff --git a/scripts/enemy.gd b/scripts/enemy.gd index b187ffe..0b15ff3 100644 --- a/scripts/enemy.gd +++ b/scripts/enemy.gd @@ -1,12 +1,13 @@ +class_name Enemy extends CharacterBody2D +const Y_SPAWN_OFFSET = -8 +@export var speed = 80 @onready var tile_map: TileMap = get_parent() @onready var animation_player = $AnimationPlayer -@export var speed = 80 -var y_spawn_offset = -8 -var destination: Vector2 = Vector2.ZERO +@onready var sprite = $Sprite2D func _ready(): var spawn_cell: Vector2i = tile_map.get_top_spawn_cell() position = tile_map.map_to_local(spawn_cell) - position.y += y_spawn_offset + position.y += Y_SPAWN_OFFSET diff --git a/scripts/get_random_top_cell_action.gd b/scripts/get_random_top_cell_action.gd index ddd9cf2..f247d65 100644 --- a/scripts/get_random_top_cell_action.gd +++ b/scripts/get_random_top_cell_action.gd @@ -1,11 +1,13 @@ class_name GetRandomTopCellAction extends ActionLeaf -func tick(actor, _blackboard): +func tick(actor: Node, blackboard: Blackboard): var rand_cell: Vector2i = actor.tile_map.get_random_top_cell() actor.tile_map.set_cell(0, Vector2i(rand_cell.x, rand_cell.y), 1, Vector2i(0, 0), 0) # debug purpose - actor.destination = actor.tile_map.map_to_local(rand_cell) - actor.destination.y += actor.y_spawn_offset + var destination = actor.tile_map.map_to_local(rand_cell) + destination.y += actor.Y_SPAWN_OFFSET + + blackboard.set_value("destination", destination) return SUCCESS diff --git a/scripts/move_to_target_action.gd b/scripts/move_to_target_action.gd index d01e679..71b412b 100644 --- a/scripts/move_to_target_action.gd +++ b/scripts/move_to_target_action.gd @@ -1,16 +1,16 @@ class_name MoveToTargetAction extends ActionLeaf -func before_run(actor, _blackboard): +func before_run(actor, blackboard): + var destination = blackboard.get_value("destination") + actor.sprite.flip_h = actor.position.x > destination.x actor.animation_player.play("walk") -func tick(actor, _blackboard): +func tick(actor: Node, blackboard: Blackboard): + var destination = blackboard.get_value("destination") var delta = get_physics_process_delta_time() - actor.position = actor.position.move_toward(actor.destination, delta * actor.speed) + actor.position = actor.position.move_toward(destination, delta * actor.speed) - if actor.position == actor.destination: + if actor.position == destination: return SUCCESS return RUNNING - -func after_run(actor, _blackboard): - actor.animation_player.stop()