using UnityEngine;
using System.Collections;
using System.Diagnostics;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
public class SnakeMove : MonoBehaviour {
List<Transform> Body = new List<Transform> ();//存放Transform數據類型的數組Body
public GameObject BodyObject;/ /定義一個游戲物體 蛇
public GameObject sFood;//// 定義一個游戲物體 食物
//updown Y軸 left down是x軸 forward back是z 軸
Vector3 postion = Vector3.up; //Vector3.up的簡稱 Vertor3(0,1,0)
private bool s = false;
// Use this for initialization
public float speed=0.1f;
public float time = 0;
//public float time0 =1;
// public float xlimit = 25.5f;
// public float ylimit = 25.5f;
public int xlimit = 25;
public int ylimit = 25;
//傷害數值
public int Value;
//目標位置
private Vector3 mTarget;
//屏幕坐標
private Vector3 mScreen;
//文本寬度
public float ContentWidth = 100;
//文本高度
public float ContentHeight = 50;
//GUI坐標
private Vector2 mPoint;
//炫酷的字體
GUIStyle frontStyle = new GUIStyle();
public Text text;
Vector3 VPostion;
public GameObject body1;
public ArrayList list = new ArrayList();
private bool isEated = false;
void Start () {
//Time.timeScale=1;
//time = Time.time + time;
//InvokeRepeating 從第一秒開始,每隔四秒調用一次
InvokeRepeating ("Food", 1, 4);1秒后 調用Food 之后每4秒調用一次
InvokeRepeating ("Move", speed, speed);
//獲取目標位置
mTarget =transform.position;
//獲取屏幕坐標
mScreen =Camera.main.WorldToScreenPoint(mTarget);
//將屏幕坐標轉化為GUI坐標
mPoint = new Vector2(mScreen.x,Screen.height-mScreen.y);
//
Value =0;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
Time.timeScale=1;
if (Input.GetKeyDown//(獲取鍵按下) (KeyCode.D)&&postion!=Vector3.left)
{
postion = Vector3.right;
}
if (Input.GetKeyDown (KeyCode.A)&&postion!=Vector3.right)
{
postion = Vector3.left;
}
if (Input.GetKeyDown (KeyCode.S)&&postion!=Vector3.up)
{
postion = Vector3.down;
}
if (Input.GetKeyDown (KeyCode.W)&&postion!=Vector3.down)
{
postion = Vector3.up;
}
//Time.tiem 系統時間
// if (time<=Time.time)
// {
// transform.Translate (postion);
// time += 1;
// //time 越小 速度越快
// }
//Random r = new Random ();
//OnTriggerEnter();
}
void Move()
{
//transform.Translate (postion);
VPostion = transform.position;
transform.Translate (postion); //Transform.Translate平移 向某方向移動物體多少距離
if (isEated)
{
GameObject g = (GameObject)Instantiate(body1,VPostion,Quaternion.identity);
g.GetComponent<MeshRenderer> ().material.color = new Color (Random.Range (0, 1.0f), Random.Range (0, 1.0f), Random.Range (0, 1.0f));
list.Insert (0, g);//將一個項插入指定索引處的 IList<(Of <(T>)>)。
//將元素插入 ArrayList 的指定索引處。 可在任意位置插入。
isEated = false;
}
else if (list.Count>0)
{
// //最后一個元素的位置賦值給新的位置
// //最后一個元素插入在第一個元素地址
// //刪除最后一個元素
((GameObject)list[list.Count-1]).transform.position = VPostion;
list.Insert (0, list [list.Count - 1]);//在0的位置插入一個
list.RemoveAt (list.Count-1);//移除 ArrayList 的指定索引處的元素。
}
}
void Food()
{
System.Random r = new System.Random ();
float x = r.Next (-xlimit,xlimit);
float y = r.Next (-ylimit,ylimit);
// float x = Random.Range(-xlimit,xlimit);
// float y = Random.Range (-ylimit, ylimit);
Instantiate (sFood, new Vector2 (x, y), Quaternion.identity);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Food"))
{
if(!isEated)
Value++;
isEated = true;
Destroy (other.gameObject);
}
else
{
Time.timeScale=0;
SceneManager.LoadScene (0);
}
text.text = "Score :" + Value;
}
void OnGUI()
{
//保證目標在攝像機前方
if (mScreen.z > 0)
{
//GUI.color = Color.blue;
//內部使用GUI坐標進行繪制
frontStyle.fontSize=40;
frontStyle.normal.background = null;//設置背景填充
frontStyle.normal.textColor = new Color (100, 0, 128);//設置字體顏色
GUI.Label(new Rect(30,0,ContentWidth,ContentHeight),"分數為"+Value.ToString(),frontStyle);
// mPoint.x,mPoint.y
}
}
}