2023-06-18 20:30:25 +02:00
|
|
|
class_name Ball
|
|
|
|
extends CharacterBody2D
|
|
|
|
|
2023-06-27 21:25:32 +02:00
|
|
|
signal notify_enemy
|
2023-06-18 20:30:25 +02:00
|
|
|
const Y_OFFSET = -10
|
|
|
|
var speed = 100
|
|
|
|
var target = Vector2.ZERO
|
|
|
|
@onready var tile_map: TileMap = get_parent()
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
target.y += Y_OFFSET
|
|
|
|
var player = get_node("/root/Main/TileMap/Player")
|
|
|
|
assert(player)
|
2023-06-28 21:29:55 +02:00
|
|
|
player.hit_ball.connect(_on_player_hit_ball)
|
2023-06-18 20:30:25 +02:00
|
|
|
|
|
|
|
func _physics_process(delta):
|
|
|
|
position = position.move_toward(target, delta * speed)
|
|
|
|
|
2023-06-28 21:29:55 +02:00
|
|
|
func _on_player_hit_ball():
|
2023-06-18 20:30:25 +02:00
|
|
|
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)
|
2023-06-27 21:25:32 +02:00
|
|
|
notify_enemy.emit()
|
|
|
|
|
|
|
|
func aim_to_bottom() -> bool:
|
|
|
|
return tile_map.is_in_bottom_area(target)
|