extends KinematicBody2D

var vitesse = Vector2.ZERO

var score = 0
var score_goal = 10

export var jump_vitesse = 600.0
export var gravite = 20.0

enum {
	RUN,
	JUMP,
	IDLE
}

enum {
	SCORE,
	TIME
}

var jump_key = "dialogic_next"

var player_enabled = false
var game_mode = "score"

var state = RUN
var au_sol = true

# Declare member variables here. Examples:
# var a = 2
# var b = "text"
onready var animation = $AnimatedSprite

# Called when the node enters the scene tree for the first time.
func _ready():
	stop()
	Signals.connect("gain",self,"increase_score")
	Signals.connect("die",self,"player_die")

func _physics_process(delta):
	if player_enabled:
		match state:
			RUN:
				animation.play("run")
			JUMP:
				vitesse = Vector2.ZERO
				vitesse.y -= jump_vitesse
				animation.play("jump")
				state = IDLE
			IDLE:
				pass
		vitesse.y += gravite
		move_and_collide(vitesse*delta)


func _input(event):
	if player_enabled and state == RUN and event.is_action_pressed(jump_key):
			state = JUMP


func _on_Area2D_body_entered(body):
	if body is StaticBody2D:
		state = RUN
		


func _on_Area2D_body_exited(body):
	if body is StaticBody2D:
		state = JUMP


func increase_score(scoretoadd):
	match game_mode:
		"score":
			score+=scoretoadd
			Signals.emit_signal("update_score",score)
			if score >= score_goal:
				Signals.emit_signal("win")
		"time":
			pass
		_:
			print("game_mode not recognized by player2 start func")
		
	
	

func player_win():
	stop()

func player_die():
	stop()


func start(mode: String):
	show()
	match mode:
		"score":
			game_mode = "score"
		"time":
			game_mode = "time"
		_:
			print("game_mode not recognized by player2 start func")
	score = 0
	player_enabled = true


func stop():
	hide()
	player_enabled = false
