#pragma strict

var Action : String;

var startPos : Vector3;

var timeLeft : float = 10.0f;
var prevInt : int;
var counting = true;

function Start() {
	prevInt = Mathf.Floor(timeLeft);
	startPos = transform.position;
	Action = "WalkForward";
}
function Update () {
	if (counting == true) {
		Tick();
	}
}
function Tick() {
	var t : TextMesh = GameObject.Find("counter").GetComponent(TextMesh);

	if (timeLeft > 0.0f) {
		timeLeft -= Time.deltaTime;
		t.text = timeLeft.ToString();
		if (prevInt > Mathf.Floor(timeLeft)) {
			prevInt = Mathf.Floor(timeLeft);
		}
	} else {
		t.text = "0.00000";
		counting = false;
		StartWalking();
	}
}


function Reset() {
	timeLeft = 10.0f;
	counting = true;
	transform.position = startPos;
}

function SetAction(nextStr : String) {
	//Debug.Log(nextStr);
	Action = nextStr;
}

var gameObjects : GameObject[];
var tileObjects : GameObject[];

function OnMouseUpAsButton() {
//	StartWalking();
}

function StartWalking() {
	gameObjects = GameObject.FindGameObjectsWithTag ("arrows");
	for(var i = 0 ; i < gameObjects.length ; i ++) {
		Destroy(gameObjects[i]);
	}
	DoNextAction();
}

function DoNextAction() {
	SendMessage(Action);
}

function WalkForward() {
	iTween.MoveBy(gameObject, {"y": -2.05, "time": 1, "easetype": "linear", "oncomplete": "DoNextAction"});
}
function LeapForward() {
	rigidbody.velocity.y=5;
	iTween.MoveBy(gameObject, {"y": -4.1, "time": 1, "easetype": "linear", "oncomplete": "DoNextAction"});
}

function HopLeft() {
	iTween.MoveBy(gameObject, {"x": -2.0, "time": 1, "easetype": "linear", "oncomplete": "DoNextAction"});
}
function HopRight() {
	iTween.MoveBy(gameObject, {"x": 2.0, "time": 1, "easetype": "linear", "oncomplete": "DoNextAction"});
}

function EndReached() {
	tileObjects = GameObject.FindGameObjectsWithTag ("Tile");
	for(var i = 0 ; i < tileObjects.length ; i ++) {
		Destroy(tileObjects[i]);
	}
	GameObject.Find("LevelBase").GetComponent("LevelGen").SendMessage("NextLevel");
	Reset();
}

function OnCollisionEnter(collision : Collision) {
	if (collision.gameObject.tag == "Endgame") {
		Application.LoadLevel("fail");
	}
}
