Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class tower : MonoBehaviour {
- public float clock;
- public float Timer;
- // public UnityEngine.UI.Text TimerLabel;
- public monsterEnquence zoo;
- bool standBy;
- public float power;
- public Transform thisTransform;
- public Transform insPoint;
- public pool pool;
- public monster target;
- // Update is called once per frame
- void Update ()
- {
- if (!standBy)
- {
- var a = Timer -= GameManager.deltaTime;
- if (a > 0)
- {
- Timer = a;
- }
- else
- {
- Timer = 0;
- standBy = true;
- }
- }
- else
- {
- shoot ();
- }
- // TimerLabel.text = ((int)Timer).ToString();
- }
- void shoot()
- {
- // #region 舊版
- // if (zoo.monsterAll.Count != 0)
- // {
- // //把射程內的目標另外轉成一個list
- // var inin = zoo.monsterAll.FindAll (Check);
- // //沒目標的話就延後射擊
- // if (inin.Count != 0)
- // {
- // //我另外擴充的方法也是vector3.dis算距離的 不過不看y只看x,z 依照距離排序 那[0]就是靠砲塔最近的
- // inin.Sort ((a,b)=>thisTransform.position.newdistance2 (b.refence.thisTransform.position).CompareTo(thisTransform.position.newdistance2 (a.refence.thisTransform.position)));
- // var bGameObject=pool.refTakeOut(pool.objStyle.bullet);
- // var bullet = bGameObject.GetComponent<bullet> ();
- // bullet.refence.thisTransform.position = insPoint.position;
- // bullet.monster = inin [0];
- // bullet.forTransform = inin [0].refence.thisTransform;
- //
- // Timer = clock;
- // standBy = false;
- // }
- // }
- // #endregion
- //鎖定目標 把他打死或他跑到螢幕外才換下一隻 而不是總是打最近的
- //如果之前鎖定的目標跑到射程外了 或是他確定死了 就視為沒有目標 重新定義目標
- if (!target||!Check(target)||target.sign||!target.Self())
- {
- target = null;
- //舊版內容
- if (zoo.monsterAll.Count != 0)
- {
- //把射程內的目標另外轉成一個list
- var inin = zoo.monsterAll.FindAll (Check);
- //沒目標的話就延後射擊
- if (inin.Count != 0)
- {
- //我另外擴充的方法也是vector3.dis算距離的 不過不看y只看x,z 依照距離排序 那[0]就是靠砲塔最近的
- inin.Sort ((a, b) => thisTransform.position.newdistance2 (b.refence.thisTransform.position).CompareTo (thisTransform.position.newdistance2 (a.refence.thisTransform.position)));
- target = inin [0];
- }
- else return;
- //直接彈出 避免沒怪還發射子彈
- }
- else return;
- }
- var bGameObject=pool.refTakeOut(pool.objStyle.bullet);
- var bullet = bGameObject.GetComponent<bullet> ();
- bullet.refence.thisTransform.position = insPoint.position;
- bullet.monster = target;
- bullet.forTransform = target.refence.thisTransform;
- Timer = clock;
- standBy = false;
- }
- // bool Check(List<monster> m)
- // {
- // for (int i = 0; i < m.Count; i++)
- // {
- // if (thisTransform.position.newdistance2 (m [i].refence.thisTransform.position) <= power)
- // {
- // return true;
- // }
- // }
- //
- // return false;
- // }
- bool Check(monster m)
- {
- if (thisTransform.position.newdistance2 (m.refence.thisTransform.position) <= power)
- {
- return true;
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment