extends CharacterBody2D

var speed = 80000
var schwerkraft = 600
var sprung = 600
var trampolin =1300 
var aktueler_status
var trampolin2 =100000
var die_coldown = true
var state_machine

var liste =["klopapir","nutela","äpfel","birnen"]
enum anim_states { STEHEN,RESET}

var rendem =RandomNumberGenerator.new()

func _ready() -> void:
	grün_rot_weis()
	state_machine =$AnimationTree["parameters/playback"]
	die_coldown == true


func grün_rot_weis():
	liste.append("grün")
	liste.append("rot")
	liste.append("weis")


func _physics_process(delta: float) -> void:
	velocity.x = 0

	for i in get_slide_collision_count():
		var collision = get_slide_collision(i)
		if collision.get_collider().name.find("ziel") == 0:
			Globel.check_point_position =collision.get_collider().position 
			print(Globel.check_point_position)




		if collision.get_collider().name.find("trampolin1") == 0:
			velocity.y = -trampolin 


		if collision.get_collider().name.find("trampolin2") == 0:
			velocity.y = +trampolin


		if collision.get_collider().name.find("trampolin3") == 0:
			velocity.x = -trampolin2
			

		if collision.get_collider().name.find("trampolin4") == 0:
			velocity.x = +trampolin2



		if collision.get_collider().name.find("gefaren") == 0:
			die()


		if collision.get_collider().name.find("end_ziel") == 0:
				get_tree().change_scene_to_file("2_level")
			


	if Input.is_action_just_pressed("ui_down") and is_on_floor():
		Globel.level+=1
		get_tree().change_scene_to_file("res://level/" + str (Globel.level) + ".tscn")
	

	if not is_on_floor():
		speed=45000

	if is_on_floor():
		speed=80000

	if position.y >= 2000:
		die()



	if not is_on_floor():
		velocity.y += schwerkraft * delta
	


	if Input.is_action_just_pressed("springen") and is_on_floor():
		velocity.y = -sprung 
		$JumpSound.play()


	set_state(anim_states.STEHEN)

 
	if Input.is_action_pressed("ui_left"):
		velocity.x -= speed *delta
		set_state(anim_states.RESET)

 
	if Input.is_action_pressed("ui_right"):
		velocity.x += speed *delta
		set_state(anim_states.RESET)



	move_and_slide ()




func _on_area_2d_area_entered(area: Area2D) -> void:
	if area.is_in_group ("Gegner"):
		die ()

	if area.is_in_group("tötung"):
		velocity.y = -sprung 

	if area.is_in_group ("ziel"):
		Globel.check_point_position =area.position

	if area.is_in_group ("end_ziel"):
		Globel.level+=1
		get_tree().change_scene_to_file("res://level/" + str (Globel.level) + ".tscn")



func die():
	Globel.leben = false
	$Herunterladen.play()
	position = Globel.check_point_position
	print(Globel.check_point_position)
	velocity.y=0
	Globel.dieconter +=1 
	$Timer.start() 
	die_coldown = false


func set_state(state : anim_states) -> void:
	if state == anim_states.RESET:
		state_machine.travel ("RESET")
		return
	if state == anim_states.STEHEN:
		state_machine.travel ("STEHEN")


func _on_timer_timeout() -> void:
	die_coldown = true
	Globel.leben = true
