Play ball with the enemy
Enemy can return the ball to the player
This commit is contained in:
parent
332c0fb0e1
commit
e0633efcbe
14 changed files with 139 additions and 44 deletions
|
@ -1,6 +1,7 @@
|
|||
class_name Ball
|
||||
extends CharacterBody2D
|
||||
|
||||
signal notify_enemy
|
||||
const Y_OFFSET = -10
|
||||
var speed = 100
|
||||
var target = Vector2.ZERO
|
||||
|
@ -19,4 +20,7 @@ func _on_player_hit():
|
|||
var rand_cell: Vector2i = tile_map.get_random_top_cell()
|
||||
tile_map.reset_and_set_target_cell(rand_cell)
|
||||
target = tile_map.map_to_local(rand_cell) + Vector2(0, Y_OFFSET)
|
||||
|
||||
notify_enemy.emit()
|
||||
|
||||
func aim_to_bottom() -> bool:
|
||||
return tile_map.is_in_bottom_area(target)
|
||||
|
|
7
scripts/enemy/behavior_tree/can_go_to_ball_condition.gd
Normal file
7
scripts/enemy/behavior_tree/can_go_to_ball_condition.gd
Normal file
|
@ -0,0 +1,7 @@
|
|||
class_name CanGoToBallCondition
|
||||
extends ConditionLeaf
|
||||
|
||||
func tick(actor, _blackboard):
|
||||
if actor.next_target != null && actor.has_thrown_ball && !actor.collide_with_ball:
|
||||
return SUCCESS
|
||||
return FAILURE
|
12
scripts/enemy/behavior_tree/can_return_ball_condition.gd
Normal file
12
scripts/enemy/behavior_tree/can_return_ball_condition.gd
Normal file
|
@ -0,0 +1,12 @@
|
|||
class_name CanReturnBallCondition
|
||||
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()
|
||||
):
|
||||
return SUCCESS
|
||||
return FAILURE
|
|
@ -1,8 +1,11 @@
|
|||
class_name CanWaitCodition
|
||||
extends ConditionLeaf
|
||||
|
||||
func tick(_actor, _blackboard):
|
||||
func tick(actor, _blackboard):
|
||||
if actor.next_target != null:
|
||||
return FAILURE
|
||||
|
||||
var num = randi_range(0, 1)
|
||||
if num == 0:
|
||||
return SUCCESS
|
||||
return FAILURE
|
||||
if num == 1:
|
||||
return FAILURE
|
||||
return SUCCESS
|
||||
|
|
13
scripts/enemy/behavior_tree/get_ball_destination_action.gd
Normal file
13
scripts/enemy/behavior_tree/get_ball_destination_action.gd
Normal file
|
@ -0,0 +1,13 @@
|
|||
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
|
|
@ -1,13 +0,0 @@
|
|||
class_name GetRandomDestinationAction
|
||||
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", destination)
|
||||
|
||||
return SUCCESS
|
|
@ -1,5 +0,0 @@
|
|||
class_name IsIdleCondition
|
||||
extends ConditionLeaf
|
||||
|
||||
func tick(_actor, _blackboard):
|
||||
return SUCCESS
|
17
scripts/enemy/behavior_tree/return_ball_action.gd
Normal file
17
scripts/enemy/behavior_tree/return_ball_action.gd
Normal file
|
@ -0,0 +1,17 @@
|
|||
class_name ReturnBallAction
|
||||
extends ActionLeaf
|
||||
|
||||
func before_run(actor, _blackboard):
|
||||
actor.play_throw_animation()
|
||||
|
||||
func tick(actor, blackboard):
|
||||
if !actor.is_throw_animation_finished:
|
||||
return RUNNING
|
||||
else:
|
||||
print("enemy return ball!")
|
||||
var target = blackboard.get_value("target")
|
||||
actor.return_ball(target)
|
||||
return SUCCESS
|
||||
|
||||
func after_run(actor, _blackboard):
|
||||
actor.animation_player.play("idle")
|
|
@ -1,16 +1,11 @@
|
|||
class_name ThrowBallAction
|
||||
extends ActionLeaf
|
||||
|
||||
var is_animation_finished = false
|
||||
|
||||
func before_run(actor, _blackboard):
|
||||
actor.animation_player.animation_finished.connect(_on_animation_finished)
|
||||
actor.animation_player.speed_scale = 0.8
|
||||
actor.play_throw_animation()
|
||||
|
||||
func tick(actor, blackboard):
|
||||
actor.animation_player.play("throw")
|
||||
|
||||
if !is_animation_finished:
|
||||
if !actor.is_throw_animation_finished:
|
||||
return RUNNING
|
||||
else:
|
||||
var target = blackboard.get_value("target")
|
||||
|
@ -19,8 +14,4 @@ func tick(actor, blackboard):
|
|||
return SUCCESS
|
||||
|
||||
func after_run(actor, _blackboard):
|
||||
actor.animation_player.speed_scale = 1
|
||||
actor.animation_player.play("idle")
|
||||
|
||||
func _on_animation_finished(_anim_name):
|
||||
is_animation_finished = true
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
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
|
||||
var next_target
|
||||
var collide_with_ball = false
|
||||
var is_throw_animation_finished = false
|
||||
@onready var tile_map: TileMap = get_parent()
|
||||
@onready var animation_player = $AnimationPlayer
|
||||
@onready var sprite = $Sprite2D
|
||||
|
@ -13,9 +18,33 @@ 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)
|
||||
|
||||
func throw_ball(target: Vector2):
|
||||
var ball = ball_scene.instantiate()
|
||||
ball.position = position
|
||||
ball.target = target
|
||||
tile_map.add_child(ball)
|
||||
current_ball = ball
|
||||
current_ball.notify_enemy.connect(_on_notify_enemy)
|
||||
|
||||
func play_throw_animation():
|
||||
is_throw_animation_finished = false
|
||||
animation_player.speed_scale = 0.8
|
||||
animation_player.play("throw")
|
||||
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 _on_area_2d_body_entered(_body):
|
||||
collide_with_ball = true
|
||||
|
||||
func _on_area_2d_body_exited(_body):
|
||||
collide_with_ball = false
|
||||
|
|
|
@ -15,7 +15,7 @@ func _ready():
|
|||
position.y += y_spawn_offset
|
||||
|
||||
|
||||
func _on_area_2d_body_entered(body: Node2D):
|
||||
func _on_area_2d_body_entered(_body: Node2D):
|
||||
# As player’s Area2D only collide with balls
|
||||
# We only enter this function after colliding with a ball
|
||||
collide_with_ball.emit()
|
||||
|
|
|
@ -49,6 +49,16 @@ func get_random_bottom_cell() -> Vector2i:
|
|||
|
||||
return Vector2i(rand_width, rand_height)
|
||||
|
||||
func is_in_bottom_area(local_position: Vector2) -> bool:
|
||||
var map_position = local_to_map(local_position)
|
||||
|
||||
var middle_height = floor(map_height / 2.0)
|
||||
var bottom_min_height = middle_height + 1
|
||||
var bottom_max_height = map_height - 1
|
||||
|
||||
return map_position.y >= bottom_min_height && map_position.y <= map_height
|
||||
|
||||
|
||||
# Debug helper functions
|
||||
func reset_and_set_target_cell(cell: Vector2i):
|
||||
reset_and_set_cell(current_target_cell, cell, target_tile_source_id)
|
||||
|
@ -61,3 +71,4 @@ func reset_and_set_cell(current_cell: Vector2i, cell: Vector2i, tile_source_id:
|
|||
set_cell(0, current_cell, ground_tile_source_id, Vector2i(0, 0), 0)
|
||||
set_cell(0, cell, tile_source_id, Vector2i(0, 0), 0)
|
||||
current_cell = cell
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue