bovido/scripts/enemy/enemy.gd
Mathilde Grapin e0633efcbe Play ball with the enemy
Enemy can return the ball to the player
2023-06-27 21:25:32 +02:00

51 lines
1.3 KiB
GDScript

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
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