﻿using UnityEngine;
using System.Collections;

public class hero7 : MonoBehaviour {
	public float hero_speed = 100f;//英雄速度
	public Vector2 current_position;
	Animator animator;
	public Grid grid = new Grid(14,10);
	public AStar astar;
	public float time = 0;//总共所用时间
	public bool finish = false;//是否已经完成

	public bool walk = false;//是否已经开始
	private bool findroad = false;//是否找到路径
	private int hasdone; //已经走过的寻路结点
	public background7 bg;//调用脚本background中的地图

	public monkey7 monkeyscript;
	
	// Use this for initialization
	void Start () {
		GameObject backgd = GameObject.Find ("background"); //调用脚本background中的地图
		bg = (background7)backgd.GetComponent (typeof(background7));

		GameObject moneyobj = GameObject.Find ("enemy"); //调用脚本background中的地图
		monkeyscript = (monkey7)moneyobj.GetComponent (typeof(monkey7));

		astar = new AStar();
		animator = GetComponent<Animator> ();
		hasdone = 1;
	}
	public void InitGame()//初始化寻路网格
	{
		for (int i =0; i< 14; i++) {//初始化地图是否可走
			for (int j =0; j< 10; j++) {
				if(bg.level7.map [i, j]!= 0)
					grid.SetWalkbale(i,j,false);
				else
					grid.SetWalkbale(i,j,true);
			}
		}
		grid.SetStartNode (3,4);
		grid.SetEndNode (7,4);
	}

	void FixedUpdate()
	{
		if(bg.stspt.click == true && walk == false)
		//if (Input.GetButton ("Jump"))
		{//开始键被按下
			InitGame();
			walk = true;
			if(astar.findPath(grid) == true)
			{
				findroad = true;
				//print(astar._path.Count);
			}
		}
		else if (walk == true && hasdone < astar._path.Count) {
			time += Time.deltaTime;
			Vector2 des;//目的地
			des.x = astar._path[hasdone].x*64 + 32;
			des.y = astar._path[hasdone].y*64 + 50;
			//print(astar._path[hasdone].x + " " + astar._path[hasdone].y);
			if(WalkTo (des) == true)
			{
				hasdone++;
			}
		}
		if (astar!=null && astar._path!=null && hasdone >= astar._path.Count) 
		{
			finish = true;
			walk = false;
			transform.localScale = new Vector3(0,0,0);
			bg.stspt.click = false;
			//Destroy(gameObject);
		}
	}
	void TurnRight()//向右转弯的时候打开所有通向右方向的动画和关闭从右出去的动画
	{
		animator.Play ("run_right");
	}

	void TurnLeft()
	{
		animator.Play ("run_left");
	}

	void TurnBack()
	{
		animator.Play ("run_back");
	}

	void TurnFront()
	{
		animator.Play ("run_front");
	}

	void GoRight(Vector2 CurPos)
	{
		TurnRight ();
		Vector2 target = Vector2.right*hero_speed+ CurPos;
		transform.position = Vector2.Lerp( CurPos, target, Time.deltaTime );
	}
	void GoLeft(Vector2 CurPos)
	{
		TurnLeft ();
		Vector2 target = -Vector2.right*hero_speed + CurPos;
		transform.position = Vector2.Lerp( CurPos, target, Time.deltaTime );
	}
	void GoBack(Vector2 CurPos)
	{
		TurnBack ();
		Vector2 target = Vector2.up*hero_speed + CurPos;
		transform.position = Vector2.Lerp( CurPos, target, Time.deltaTime );
	}
	void GoFront(Vector2 CurPos)
	{
		TurnFront ();
		Vector2 target = -Vector2.up*hero_speed + CurPos;
		transform.position = Vector2.Lerp( CurPos, target, Time.deltaTime );
	}

	bool WalkTo(Vector2 target)//根据目标点调用向哪个方向走
	{
		current_position = transform.position;
		if (current_position.x < target.x && Mathf.Abs(current_position.x - target.x) >5 ) {
			GoRight (current_position);
			return false;
		} 
		else if (current_position.x > target.x && Mathf.Abs(current_position.x - target.x) >5) {
			GoLeft (current_position);
			return false;
		} 
		else if (current_position.y < target.y && Mathf.Abs(current_position.y - target.y) >5) {
			GoBack (current_position);
			return false;
		}
		else if (current_position.y > target.y && Mathf.Abs(current_position.y - target.y) >5) {
			GoFront(current_position);
			return false;
		}
		return true;
	}
}
