#pragma strict

public var GM : GameObject;

public var maxSpeed : float = 10f;
public var jumpForce : float = 700f;
private var facingRight : boolean = true;
var anim : Animator;

public var groundCheck : Transform;
public var wallCheck : Transform;
public var DeathSound : GameObject;

private var grounded : boolean = false;
private var groundRadius : float = 0.2;
private var wallRadius : float = 0.2;
public var whatIsGround : LayerMask;
public var whatIsWall : LayerMask;

private var contact : boolean = false;
private var fell : boolean = false;
private var walking : boolean = false;
private var gameOver : boolean = false;
private var goMainMenu : boolean = false;

public var jumpClips : AudioClip[];
public var collectClips : AudioClip[];
public var walkClip : AudioClip;
public var tebrikClip : AudioClip;
var jumpSound : AudioSource;
var walkSound : AudioSource;
var collectSound : AudioSource;
var tebrikSound : AudioSource;


function Start () {

	anim = GetComponent(Animator);
	
    var audios = gameObject.GetComponents(AudioSource);
    jumpSound = audios[0];
    walkSound = audios[1];
    collectSound = audios[2];
    tebrikSound = audios[3];
}

function FixedUpdate () {

	grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
	anim.SetBool("Ground", grounded);
	
	anim.SetFloat("vSpeed", rigidbody2D.velocity.y);

	var move : float = Input.GetAxis("Horizontal");
	
	anim.SetFloat("Speed", Mathf.Abs(move));
	
	if (!gameOver) {
		rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
	} else {
		rigidbody2D.velocity = new Vector2(0, 0);
	}
	
	if (move > 0 && !facingRight) {
		Flip();
	}
	else if (move < 0 && facingRight) {
		Flip();
	}

}

function Update () {

	if (!fell && grounded) {
		fell = true;
		//PlayRandom(groundSound);
	} else if (fell && !grounded) {
		fell = false;
	}
	
	if (Input.GetAxis("Horizontal")) {
		if (!walking && grounded) {
			walkSound.clip = walkClip;
			walkSound.Play();
		}
		walking = true;
	}
	if (!Input.GetAxis("Horizontal") || !grounded) {
		walkSound.Stop();
		walking = false;
	}
	
	// JUMP //
	if (Input.GetButtonDown("Jump") && grounded && !gameOver) {
		jumpSound.clip = jumpClips[Random.Range(0, jumpClips.length)];
		jumpSound.Play();
		anim.SetBool("Ground", false);
		rigidbody2D.AddForce(new Vector2(0, jumpForce));
	}
	
	if (Mathf.Floor(transform.position.y) > GM.GetComponent(GameController).score && grounded) {
		GM.GetComponent(GameController).score = Mathf.Floor(transform.position.y);
	}
	
	if (gameObject.transform.position.y > 160 && gameObject.transform.position.x > 9.5 && grounded && !gameOver) {
		gameOver = true;
		var singularity : GameObject = GameObject.Find("singularity");
		singularity.SendMessage("GameOver", 1.0);
		tebrikSound.clip = tebrikClip;
		tebrikSound.Play();
		var uiback = GameObject.Find("UIBack");
		var congrats = GameObject.Find("Congrats");
		var anykey = GameObject.Find("AnyKey");
		uiback.GetComponent(CanvasGroup).alpha = 1;
		congrats.GetComponent(CanvasGroup).alpha = 1;
		anykey.GetComponent(CanvasGroup).alpha = 1;
		Invoke("canChangeScene", 1);
	}
	
	if (gameOver && goMainMenu && Input.anyKey) {
		Application.LoadLevel("MainMenu");
	}
	
}

function Flip() {

	facingRight = !facingRight;
	var theScale : Vector3 =  transform.localScale;
	theScale.x *= -1;
	transform.localScale = theScale;

}

function canChangeScene () {
	goMainMenu = true;
}

function Death (num : float) {
	//Debug.Log("DEATH!");
	Instantiate(DeathSound, Vector2(0, 0), Quaternion.identity);
	Destroy(gameObject);
}

function Collect (num : float) {
	Debug.Log("Heeeeyyyy!");
	if (num == 5) {
		collectSound.clip = collectClips[1];
	} else {
		collectSound.clip = collectClips[0];
	}
	collectSound.Play();
}