class_name SceneManager extends Node2D
@onready var game = $".."
@onready var crates = $"../Crates"
var grenade = preload("res://Scenes/grenade.tscn")
var dropplet = preload("res://Scenes/dropplet.tscn")
const DROP_PT_X = 93
const DROP_PT_Y = 301

func _ready() -> void:
	print("Hello")
	game.destroySignal.connect(destroy)
	buildLevel()
	
func destroy(body)->void:
	if body is Crate:
		body.queue_free()
		
func buildLevel()->void:
	updateCrates()

func updateCrates()->void:
	#is there a crate holder?
	var _crateTotal:int = 0
	if crates:
		for obj in crates.get_children():
			if obj is Crate:
				if not obj.tree_exited.is_connected(updateCrates):
					obj.tree_exited.connect(updateCrates)
				_crateTotal +=1
	print("Number of crates"+str(_crateTotal))
	game.totalCrates(_crateTotal)
	
func makeGrenade(_grenadePostion, _grenadeDirection):
	print("SM wants to make a grenade")
	var myGrenade:Grenade = grenade.instantiate()
	owner.add_child(myGrenade)
	myGrenade.transform = _grenadePostion
	myGrenade.apply_central_impulse(Vector2(_grenadeDirection, -1)*200)
func addDropplet():
	var myDropplet = dropplet.instantiate()
	$"../FluidContainer".add_child(myDropplet)
	myDropplet.global_position = Vector2(DROP_PT_X, DROP_PT_Y)
	
