Add a behavior tree to the enemy

Add the Beehave addon.
Make the enemy do the same thing as before but with a behavior tree.
This commit is contained in:
Mathilde Grapin 2023-06-12 16:48:35 +02:00
parent 09f6925a00
commit 1aed988149
92 changed files with 4025 additions and 25 deletions

View file

@ -4,33 +4,9 @@ extends CharacterBody2D
@onready var animation_player = $AnimationPlayer
@export var speed = 80
var y_spawn_offset = -8
var is_moving = false
var destination: Vector2 = Vector2.ZERO
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
func _process(_delta):
if is_moving:
animation_player.play("walk")
else:
animation_player.stop()
func _physics_process(delta):
move_to_rand_cell(delta)
func move_to_rand_cell(delta):
if !is_moving:
var rand_cell: Vector2i = tile_map.get_random_top_cell()
tile_map.set_cell(0, Vector2i(rand_cell.x, rand_cell.y), 1, Vector2i(0, 0), 0) # debug purpose
destination = tile_map.map_to_local(rand_cell)
destination.y += y_spawn_offset
is_moving = true
position = position.move_toward(destination, delta * speed)
if position == destination:
is_moving = false

View file

@ -0,0 +1,11 @@
class_name GetRandomTopCell
extends ActionLeaf
func tick(actor, _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
return SUCCESS

View file

@ -0,0 +1,5 @@
class_name IsIdleCondition
extends ConditionLeaf
func tick(actor, _blackboard):
return SUCCESS

View file

@ -0,0 +1,16 @@
class_name MoveToTargetAction
extends ActionLeaf
func before_run(actor, blackboard):
actor.animation_player.play("walk")
func tick(actor, _blackboard):
var delta = get_physics_process_delta_time()
actor.position = actor.position.move_toward(actor.destination, delta * actor.speed)
if actor.position == actor.destination:
return SUCCESS
return RUNNING
func after_run(actor, blackboard):
actor.animation_player.stop()