2023-06-12 18:50:58 +02:00
|
|
|
class_name Enemy
|
2023-06-11 23:38:54 +02:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
2023-07-01 13:26:45 +02:00
|
|
|
const ball_scene = preload("res://scenes/ball.tscn")
|
|
|
|
const ball_name = "Ball"
|
|
|
|
const y_spawn_offset = -8
|
|
|
|
signal hit_ball
|
|
|
|
var speed = 80
|
2023-07-01 17:12:02 +02:00
|
|
|
var next_destination
|
2023-06-27 21:25:32 +02:00
|
|
|
var collide_with_ball = false
|
2023-07-01 13:26:45 +02:00
|
|
|
var hit_ball_animation_finished = false
|
2023-07-01 17:12:02 +02:00
|
|
|
var ball_aims_to_bottom = 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():
|
2023-07-01 13:26:45 +02:00
|
|
|
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)
|
2023-06-12 22:06:48 +02:00
|
|
|
|
2023-07-01 13:26:45 +02:00
|
|
|
func move_to(destination):
|
|
|
|
var delta = get_physics_process_delta_time()
|
|
|
|
position = position.move_toward(destination, delta * speed)
|
|
|
|
|
|
|
|
func throw_ball():
|
2023-06-12 22:06:48 +02:00
|
|
|
var ball = ball_scene.instantiate()
|
2023-07-01 13:26:45 +02:00
|
|
|
ball.name = ball_name
|
2023-06-12 22:06:48 +02:00
|
|
|
ball.position = position
|
2023-07-01 13:26:45 +02:00
|
|
|
ball.notify_enemy.connect(_on_notify_enemy)
|
2023-06-12 22:06:48 +02:00
|
|
|
tile_map.add_child(ball)
|
2023-07-01 13:26:45 +02:00
|
|
|
hit_ball.emit()
|
|
|
|
|
|
|
|
func return_ball():
|
|
|
|
hit_ball.emit()
|
2023-06-27 21:25:32 +02:00
|
|
|
|
2023-07-01 13:26:45 +02:00
|
|
|
func flip_sprite(destination):
|
|
|
|
sprite.flip_h = position.x > destination.x
|
|
|
|
|
|
|
|
func play_hit_ball_animation():
|
|
|
|
hit_ball_animation_finished = false
|
2023-06-27 21:25:32 +02:00
|
|
|
animation_player.speed_scale = 0.8
|
2023-07-01 13:26:45 +02:00
|
|
|
animation_player.play("hit_ball")
|
2023-06-27 21:25:32 +02:00
|
|
|
animation_player.speed_scale = 1
|
|
|
|
|
2023-07-01 13:26:45 +02:00
|
|
|
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
|
|
|
|
|
2023-07-01 17:12:02 +02:00
|
|
|
func _on_notify_enemy(ball_target: Vector2i):
|
|
|
|
if tile_map.is_in_bottom_area(ball_target):
|
|
|
|
ball_aims_to_bottom = true
|
|
|
|
else:
|
|
|
|
ball_aims_to_bottom = false
|
|
|
|
# TODO: add to ball_target an offset depending from where the enemy come from
|
|
|
|
next_destination = tile_map.map_to_local(ball_target)
|
2023-06-27 21:25:32 +02:00
|
|
|
|
|
|
|
func _on_area_2d_body_entered(_body):
|
|
|
|
collide_with_ball = true
|
|
|
|
|
|
|
|
func _on_area_2d_body_exited(_body):
|
|
|
|
collide_with_ball = false
|
2023-07-01 13:26:45 +02:00
|
|
|
|
|
|
|
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)
|