diff --git a/scripts/ball/ball.gd b/scripts/ball/ball.gd index 8d8f0cf..b0640f8 100644 --- a/scripts/ball/ball.gd +++ b/scripts/ball/ball.gd @@ -1,26 +1,41 @@ class_name Ball extends CharacterBody2D -signal notify_enemy -const Y_OFFSET = -10 +const y_offset = -10 +const player_path = "/root/Main/TileMap/Player" +const enemy_path = "/root/Main/TileMap/Enemy" +signal notify_enemy(new_target) 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) - player.hit_ball.connect(_on_player_hit_ball) + target.y += y_offset + connect_player() + connect_enemy() func _physics_process(delta): position = position.move_toward(target, delta * speed) -func _on_player_hit_ball(): - 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 update_target(new_target: Vector2i): + target = tile_map.map_to_local(new_target) + Vector2(0, y_offset) + # Debug + tile_map.reset_and_set_target_cell(new_target) -func aim_to_bottom() -> bool: - return tile_map.is_in_bottom_area(target) +func _on_player_hit_ball(): + var new_target = tile_map.get_random_top_cell() + update_target(new_target) + notify_enemy.emit(new_target) + +func _on_enemy_hit_ball(): + update_target(tile_map.get_random_bottom_cell()) + +func connect_player(): + var player = get_node(player_path) + assert(player) + player.hit_ball.connect(_on_player_hit_ball) + +func connect_enemy(): + var enemy = get_node(enemy_path) + assert(enemy) + enemy.hit_ball.connect(_on_enemy_hit_ball)