Cleanup ball

This commit is contained in:
Mathilde Grapin 2023-07-01 13:27:47 +02:00
parent a56ffa78c5
commit 4658bf304f

View file

@ -1,26 +1,41 @@
class_name Ball class_name Ball
extends CharacterBody2D 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 speed = 100
var target = Vector2.ZERO var target = Vector2.ZERO
@onready var tile_map: TileMap = get_parent() @onready var tile_map: TileMap = get_parent()
func _ready(): func _ready():
target.y += Y_OFFSET target.y += y_offset
var player = get_node("/root/Main/TileMap/Player") connect_player()
assert(player) connect_enemy()
player.hit_ball.connect(_on_player_hit_ball)
func _physics_process(delta): func _physics_process(delta):
position = position.move_toward(target, delta * speed) position = position.move_toward(target, delta * speed)
func _on_player_hit_ball(): func update_target(new_target: Vector2i):
var rand_cell: Vector2i = tile_map.get_random_top_cell() target = tile_map.map_to_local(new_target) + Vector2(0, y_offset)
tile_map.reset_and_set_target_cell(rand_cell) # Debug
target = tile_map.map_to_local(rand_cell) + Vector2(0, Y_OFFSET) tile_map.reset_and_set_target_cell(new_target)
notify_enemy.emit()
func aim_to_bottom() -> bool: func _on_player_hit_ball():
return tile_map.is_in_bottom_area(target) 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)