Clean up enemy

This commit is contained in:
Mathilde Grapin 2023-07-01 13:26:45 +02:00
parent a08afcb147
commit a56ffa78c5
13 changed files with 90 additions and 99 deletions

View file

@ -2,6 +2,10 @@ class_name CanGoToBallCondition
extends ConditionLeaf
func tick(actor, _blackboard):
if actor.next_target != null && actor.has_thrown_ball && !actor.collide_with_ball:
if (
actor.next_target != null &&
actor.ball_in_game() &&
!actor.collide_with_ball
):
return SUCCESS
return FAILURE

View file

@ -3,10 +3,9 @@ extends ConditionLeaf
func tick(actor, _blackboard):
if (
actor.has_thrown_ball
&& actor.next_target != null
&& actor.collide_with_ball
&& !actor.current_ball.aim_to_bottom()
actor.ball_in_game() &&
actor.next_target != null &&
actor.collide_with_ball
):
return SUCCESS
return FAILURE

View file

@ -2,6 +2,6 @@ class_name CanThrowBallCondition
extends ConditionLeaf
func tick(actor, _blackboard):
if !actor.has_thrown_ball:
if !actor.ball_in_game():
return SUCCESS
return FAILURE

View file

@ -2,10 +2,9 @@ class_name CanWaitCodition
extends ConditionLeaf
func tick(actor, _blackboard):
if actor.next_target != null:
return FAILURE
var num = randi_range(0, 1)
if num == 1:
return FAILURE
return SUCCESS
if (
actor.next_target == null
):
return SUCCESS
return FAILURE

View file

@ -2,12 +2,5 @@ class_name GetBallDestinationAction
extends ActionLeaf
func tick(actor: Node, blackboard: Blackboard):
# var rand_cell: Vector2i = actor.tile_map.get_random_top_cell()
# actor.tile_map.reset_and_set_destination_cell(rand_cell)
#
# var destination = actor.tile_map.map_to_local(rand_cell)
# destination.y += actor.Y_SPAWN_OFFSET
#
blackboard.set_value("destination", actor.next_target)
return SUCCESS

View file

@ -1,12 +0,0 @@
class_name GetRandomTargetAction
extends ActionLeaf
func tick(actor, blackboard):
var rand_cell: Vector2i = actor.tile_map.get_random_bottom_cell()
actor.tile_map.reset_and_set_target_cell(rand_cell)
var target = actor.tile_map.map_to_local(rand_cell)
blackboard.set_value("target", target)
return SUCCESS

View file

@ -3,20 +3,19 @@ extends ActionLeaf
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.flip_sprite(destination)
actor.play_walk_animation()
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(destination, delta * actor.speed)
actor.move_to(destination)
if actor.position == destination:
return SUCCESS
return RUNNING
func after_run(actor, blackboard):
actor.play_idle_animation()
# Debug
var destination = blackboard.get_value("destination")
destination.y -= actor.Y_SPAWN_OFFSET
var cell = actor.tile_map.local_to_map(destination)
actor.tile_map.set_cell(0, cell, 0, Vector2i(0, 0), 0) # debug purpose
actor.reset_tile(destination)

View file

@ -2,15 +2,13 @@ class_name ReturnBallAction
extends ActionLeaf
func before_run(actor, _blackboard):
actor.play_throw_animation()
actor.play_hit_ball_animation()
func tick(actor, blackboard):
if !actor.is_throw_animation_finished:
func tick(actor, _blackboard):
if !actor.hit_ball_animation_finished:
return RUNNING
else:
var target = blackboard.get_value("target")
actor.return_ball(target)
return SUCCESS
actor.return_ball()
return SUCCESS
func after_run(actor, _blackboard):
actor.animation_player.play("idle")

View file

@ -2,16 +2,13 @@ class_name ThrowBallAction
extends ActionLeaf
func before_run(actor, _blackboard):
actor.play_throw_animation()
actor.play_hit_ball_animation()
func tick(actor, blackboard):
if !actor.is_throw_animation_finished:
if !actor.hit_ball_animation_finished:
return RUNNING
else:
var target = blackboard.get_value("target")
actor.throw_ball(target)
actor.has_thrown_ball = true
return SUCCESS
actor.throw_ball()
return SUCCESS
func after_run(actor, _blackboard):
actor.animation_player.play("idle")
actor.play_idle_animation()

View file

@ -2,7 +2,7 @@ class_name WaitAction
extends ActionLeaf
func before_run(actor, _blackboard):
actor.animation_player.play("idle")
actor.play_idle_animation()
func tick(_actor, _blackboard):
return RUNNING

View file

@ -1,50 +1,72 @@
class_name Enemy
extends CharacterBody2D
signal go_to_ball
const Y_SPAWN_OFFSET = -8
@export var speed = 80
var has_thrown_ball = false
var ball_scene = preload("res://scenes/ball.tscn")
var current_ball: Ball
const ball_scene = preload("res://scenes/ball.tscn")
const ball_name = "Ball"
const y_spawn_offset = -8
signal hit_ball
var speed = 80
var next_target
var collide_with_ball = false
var is_throw_animation_finished = false
var hit_ball_animation_finished = false
@onready var tile_map: TileMap = get_parent()
@onready var animation_player = $AnimationPlayer
@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
animation_player.animation_finished.connect(_on_animation_finished)
var spawn_cell = tile_map.get_top_spawn_cell()
position = tile_map.map_to_local(spawn_cell) + Vector2(0, y_spawn_offset)
animation_player.animation_finished.connect(_on_hit_ball_animation_finished)
func throw_ball(target: Vector2):
func move_to(destination):
var delta = get_physics_process_delta_time()
position = position.move_toward(destination, delta * speed)
func throw_ball():
var ball = ball_scene.instantiate()
ball.name = ball_name
ball.position = position
ball.target = target
ball.notify_enemy.connect(_on_notify_enemy)
tile_map.add_child(ball)
current_ball = ball
current_ball.notify_enemy.connect(_on_notify_enemy)
hit_ball.emit()
func play_throw_animation():
is_throw_animation_finished = false
func return_ball():
hit_ball.emit()
next_target = null
func flip_sprite(destination):
sprite.flip_h = position.x > destination.x
func play_hit_ball_animation():
hit_ball_animation_finished = false
animation_player.speed_scale = 0.8
animation_player.play("throw")
animation_player.play("hit_ball")
animation_player.speed_scale = 1
func return_ball(target: Vector2):
current_ball.target = target
func _on_animation_finished(_anim_name):
is_throw_animation_finished = true
func _on_notify_enemy():
next_target = current_ball.target
func play_idle_animation():
animation_player.play("idle")
func play_walk_animation():
animation_player.play("walk")
func _on_hit_ball_animation_finished(_anim_name):
hit_ball_animation_finished = true
func _on_notify_enemy(new_target):
# TODO: add to new_target an offset depending from where the enemy come from
next_target = tile_map.map_to_local(new_target)
func _on_area_2d_body_entered(_body):
collide_with_ball = true
func _on_area_2d_body_exited(_body):
collide_with_ball = false
func ball_in_game() -> bool:
return tile_map.has_node(ball_name)
# Debug functions
func reset_tile(destination):
destination.y -= y_spawn_offset
var cell = tile_map.local_to_map(destination)
tile_map.set_cell(0, cell, 0, Vector2i(0, 0), 0)