extends Node2D

var texture_new_sprite : Texture

var biom_n_w = preload("res://assets/Ground/transisions/n_w.png")
var biom_w_n = preload("res://assets/Ground/transisions/w_n.png")
var biom_n_s = preload("res://assets/Ground/transisions/n_s.png")
var biom_s_n = preload("res://assets/Ground/transisions/s_n.png")

var biom_n = preload("res://assets/Ground/n.png")
var biom_w = preload("res://assets/Ground/w.png")
var biom_s = preload("res://assets/Ground/s.png")
var biom_f = preload("res://assets/Ground/f.png")

var active_biom = "n"
var biom_type = "n"#normal,o=other,ui=übergangin,uo=übergangout
var change_in = 3
var passed

func probability(prozent):
	return randf() > 1 - (0.01 * prozent)

func change_biom():
	#if active_biom == "w_n":
	#	active_biom = "n"
	#elif active_biom == "s_n":
	#	active_biom = "n"
	#elif active_biom == "n_w":
	#	active_biom = "w"
	#elif active_biom == "n_s":
	#	active_biom = "s"

	if biom_type == "n":
		change_in -= 1
		if probability(60):
			active_biom = "n"
		else:
			active_biom = "f"
	elif biom_type == "o":
		change_in -= 1
		biom_type = "o"
	elif biom_type == "ui":
		if active_biom == "n_w":
			active_biom = "w"
		elif active_biom == "n_s":
			active_biom = "s"
		biom_type = "o"
	elif biom_type == "uo":
		if active_biom == "w":
			active_biom = "w_n"
			biom_type = "n"
		elif active_biom == "s":
			active_biom = "s_n"
			biom_type = "n"
		biom_type = "n"

	if change_in <= 0: # bei übergängen
		change_in = randi_range(3,7)
		if biom_type == "n": #bei normalen biom auf neues biom oder wald
			if probability(10): # 50% auf Biomwechsel
				biom_type = "n"
			else:
				biom_type = "ui" #übergang in anderes biom
				if probability(50):
					active_biom = "n_w"
				else:
						active_biom = "n_s"
		elif biom_type == "o": #bei anderen biomen auf normale Biome
			biom_type = "uo"
	
	#print("type  : ", biom_type)
	#print("active: ", active_biom)
	#print("change: ", change_in)
	#print("                  ")


func set_biom_texture():
	if active_biom == "w":
		texture_new_sprite = biom_w
	elif active_biom == "n":
		texture_new_sprite = biom_n
	elif active_biom == "s":
		texture_new_sprite = biom_s
	elif active_biom == "f":
		texture_new_sprite = biom_f
	elif active_biom == "w_n":
		texture_new_sprite = biom_w_n
	elif active_biom == "n_w":
		texture_new_sprite = biom_n_w
	elif active_biom == "s_n":
		texture_new_sprite = biom_s_n
	elif active_biom == "n_s":
		texture_new_sprite = biom_n_s
func _spawn(last_x_position) -> void:
	var new_ground = preload("res://scenes/ground/ground.tscn").instantiate()
	new_ground.texture = texture_new_sprite
	new_ground.global_position.x = last_x_position + 400
	add_child(new_ground)
	new_ground.connect("screen_entered", _on_ground_sprite_screen_entered)

func _on_ground_sprite_screen_entered() -> void:
	change_biom()
	$"/root/Global".active_biom = active_biom
	set_biom_texture()
	_spawn(get_child(0).position.x)
