class_name BalleSpectrale
extends Balle


const BalleDeSpectraleScene: PackedScene = preload("res://composants/objets/balle/balle_spectrale/balle_spectrale.tscn")
const CALQUE_COLLISION_BRIQUE_INDEXE: int = 5
const CALQUE_COLLISION_BRIQUE_ACTIVATION: int = pow(2, CALQUE_COLLISION_BRIQUE_INDEXE - 1)

var _est_tangible: bool = true

@onready var image_tangible: Sprite2D = $ImageTangible
@onready var image_spectre: Sprite2D = $ImageSpectre


func rendre_tangible(doit_etre_tangible: bool) -> void:
	if doit_etre_tangible == _est_tangible:
		return # etat demande deja atteint

	image_tangible.visible = doit_etre_tangible
	image_spectre.visible = not doit_etre_tangible

	if doit_etre_tangible:
		collision_mask += CALQUE_COLLISION_BRIQUE_ACTIVATION
	else:
		collision_mask -= CALQUE_COLLISION_BRIQUE_ACTIVATION

	_est_tangible = doit_etre_tangible # l'etat courant a change


## La spectrale redevient tangible au premier rebond
func _rebondir(impact: KinematicCollision2D) -> void:
	rendre_tangible(not impact.get_collider() is Raquette)
	super._rebondir(impact)


## Transforme une balle quelconque en balle de feu
static func transformer(balle: Balle) -> void:
	var balle_spectrale: BalleSpectrale = BalleDeSpectraleScene.instantiate()
	balle.add_sibling.call_deferred(balle_spectrale) # Appeler plus tard sinon Godot pas content
	balle_spectrale.recopier(balle)
	balle.queue_free()
