extends Node2D

onready var spawnPoints = get_tree().get_nodes_in_group("spawn_point")

var bucket = load("res://Scene/Clopinette.tscn")

func spawnBucket():
	var newBucket = bucket.instance()
	self.get_parent().add_child(newBucket)
	newBucket.add_to_group("bucket")
	newBucket.set_position(spawnPoints[randi() % spawnPoints.size()].get_position())

func _process(_delta):
	if get_tree().get_nodes_in_group("bucket").empty():
		spawnBucket()
 
func endGame():
	get_node("/root/World/CanvasLayer/Window").showEndGameWindow(getWinner())
	
func getWinner():
	var players = get_tree().get_nodes_in_group("player")
	
	for player in players:
		if player.curLife > 0:
			return player.name
	return "Player1"
