2023-06-18 20:30:25 +02:00
|
|
|
class_name Ball
|
|
|
|
extends CharacterBody2D
|
|
|
|
|
2023-07-01 13:27:47 +02:00
|
|
|
const y_offset = -10
|
|
|
|
const player_path = "/root/Main/TileMap/Player"
|
|
|
|
const enemy_path = "/root/Main/TileMap/Enemy"
|
2023-07-01 17:12:02 +02:00
|
|
|
signal notify_enemy(ball_target: Vector2i)
|
2023-06-18 20:30:25 +02:00
|
|
|
var speed = 100
|
|
|
|
var target = Vector2.ZERO
|
|
|
|
@onready var tile_map: TileMap = get_parent()
|
|
|
|
|
|
|
|
func _ready():
|
2023-07-01 13:27:47 +02:00
|
|
|
target.y += y_offset
|
|
|
|
connect_player()
|
|
|
|
connect_enemy()
|
2023-06-18 20:30:25 +02:00
|
|
|
|
|
|
|
func _physics_process(delta):
|
|
|
|
position = position.move_toward(target, delta * speed)
|
|
|
|
|
2023-07-01 13:27:47 +02:00
|
|
|
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)
|
|
|
|
|
2023-06-28 21:29:55 +02:00
|
|
|
func _on_player_hit_ball():
|
2023-07-01 17:12:02 +02:00
|
|
|
var new_target: Vector2i = tile_map.get_random_top_cell()
|
2023-07-01 13:27:47 +02:00
|
|
|
update_target(new_target)
|
|
|
|
notify_enemy.emit(new_target)
|
|
|
|
|
|
|
|
func _on_enemy_hit_ball():
|
2023-07-01 17:12:02 +02:00
|
|
|
var new_target: Vector2i = tile_map.get_random_bottom_cell()
|
|
|
|
update_target(new_target)
|
|
|
|
notify_enemy.emit(new_target)
|
2023-07-01 13:27:47 +02:00
|
|
|
|
|
|
|
func connect_player():
|
|
|
|
var player = get_node(player_path)
|
|
|
|
assert(player)
|
|
|
|
player.hit_ball.connect(_on_player_hit_ball)
|
2023-06-27 21:25:32 +02:00
|
|
|
|
2023-07-01 13:27:47 +02:00
|
|
|
func connect_enemy():
|
|
|
|
var enemy = get_node(enemy_path)
|
|
|
|
assert(enemy)
|
|
|
|
enemy.hit_ball.connect(_on_enemy_hit_ball)
|