Flip enemy and use blackboard

Use blackboard for destination.
This commit is contained in:
Mathilde Grapin 2023-06-12 18:50:58 +02:00
parent 02026ab3e9
commit 5970aa2ac2
3 changed files with 17 additions and 14 deletions

View file

@ -1,12 +1,13 @@
class_name Enemy
extends CharacterBody2D extends CharacterBody2D
const Y_SPAWN_OFFSET = -8
@export var speed = 80
@onready var tile_map: TileMap = get_parent() @onready var tile_map: TileMap = get_parent()
@onready var animation_player = $AnimationPlayer @onready var animation_player = $AnimationPlayer
@export var speed = 80 @onready var sprite = $Sprite2D
var y_spawn_offset = -8
var destination: Vector2 = Vector2.ZERO
func _ready(): func _ready():
var spawn_cell: Vector2i = tile_map.get_top_spawn_cell() var spawn_cell: Vector2i = tile_map.get_top_spawn_cell()
position = tile_map.map_to_local(spawn_cell) position = tile_map.map_to_local(spawn_cell)
position.y += y_spawn_offset position.y += Y_SPAWN_OFFSET

View file

@ -1,11 +1,13 @@
class_name GetRandomTopCellAction class_name GetRandomTopCellAction
extends ActionLeaf extends ActionLeaf
func tick(actor, _blackboard): func tick(actor: Node, blackboard: Blackboard):
var rand_cell: Vector2i = actor.tile_map.get_random_top_cell() 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.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) var destination = actor.tile_map.map_to_local(rand_cell)
actor.destination.y += actor.y_spawn_offset destination.y += actor.Y_SPAWN_OFFSET
blackboard.set_value("destination", destination)
return SUCCESS return SUCCESS

View file

@ -1,16 +1,16 @@
class_name MoveToTargetAction class_name MoveToTargetAction
extends ActionLeaf 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") 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() 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 SUCCESS
return RUNNING return RUNNING
func after_run(actor, _blackboard):
actor.animation_player.stop()