2023-06-12 18:50:58 +02:00
|
|
|
class_name Enemy
|
2023-06-11 23:38:54 +02:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
2023-06-27 21:25:32 +02:00
|
|
|
signal go_to_ball
|
2023-06-12 18:50:58 +02:00
|
|
|
const Y_SPAWN_OFFSET = -8
|
|
|
|
@export var speed = 80
|
2023-06-12 22:06:48 +02:00
|
|
|
var has_thrown_ball = false
|
|
|
|
var ball_scene = preload("res://scenes/ball.tscn")
|
2023-06-27 21:25:32 +02:00
|
|
|
var current_ball: Ball
|
|
|
|
var next_target
|
|
|
|
var collide_with_ball = false
|
|
|
|
var is_throw_animation_finished = false
|
2023-06-11 23:38:54 +02:00
|
|
|
@onready var tile_map: TileMap = get_parent()
|
|
|
|
@onready var animation_player = $AnimationPlayer
|
2023-06-12 18:50:58 +02:00
|
|
|
@onready var sprite = $Sprite2D
|
2023-06-11 23:38:54 +02:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
var spawn_cell: Vector2i = tile_map.get_top_spawn_cell()
|
|
|
|
position = tile_map.map_to_local(spawn_cell)
|
2023-06-12 18:50:58 +02:00
|
|
|
position.y += Y_SPAWN_OFFSET
|
2023-06-27 21:25:32 +02:00
|
|
|
animation_player.animation_finished.connect(_on_animation_finished)
|
2023-06-12 22:06:48 +02:00
|
|
|
|
2023-06-18 20:30:25 +02:00
|
|
|
func throw_ball(target: Vector2):
|
2023-06-12 22:06:48 +02:00
|
|
|
var ball = ball_scene.instantiate()
|
|
|
|
ball.position = position
|
2023-06-18 20:30:25 +02:00
|
|
|
ball.target = target
|
2023-06-12 22:06:48 +02:00
|
|
|
tile_map.add_child(ball)
|
2023-06-27 21:25:32 +02:00
|
|
|
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
|