Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class tower : MonoBehaviour {
  6.  
  7. public float clock;
  8. public float Timer;
  9. // public UnityEngine.UI.Text TimerLabel;
  10.  
  11. public monsterEnquence zoo;
  12. bool standBy;
  13. public float power;
  14. public Transform thisTransform;
  15. public Transform insPoint;
  16. public pool pool;
  17. public monster target;
  18.  
  19. // Update is called once per frame
  20. void Update ()
  21. {
  22. if (!standBy)
  23. {
  24. var a = Timer -= GameManager.deltaTime;
  25. if (a > 0)
  26. {
  27. Timer = a;
  28. }
  29. else
  30. {
  31. Timer = 0;
  32. standBy = true;
  33. }
  34. }
  35. else
  36. {
  37. shoot ();
  38. }
  39. // TimerLabel.text = ((int)Timer).ToString();
  40. }
  41.  
  42. void shoot()
  43. {
  44. // #region 舊版
  45. // if (zoo.monsterAll.Count != 0)
  46. // {
  47. // //把射程內的目標另外轉成一個list
  48. // var inin = zoo.monsterAll.FindAll (Check);
  49. // //沒目標的話就延後射擊
  50. // if (inin.Count != 0)
  51. // {
  52. // //我另外擴充的方法也是vector3.dis算距離的 不過不看y只看x,z 依照距離排序 那[0]就是靠砲塔最近的
  53. // inin.Sort ((a,b)=>thisTransform.position.newdistance2 (b.refence.thisTransform.position).CompareTo(thisTransform.position.newdistance2 (a.refence.thisTransform.position)));
  54. // var bGameObject=pool.refTakeOut(pool.objStyle.bullet);
  55. // var bullet = bGameObject.GetComponent<bullet> ();
  56. // bullet.refence.thisTransform.position = insPoint.position;
  57. // bullet.monster = inin [0];
  58. // bullet.forTransform = inin [0].refence.thisTransform;
  59. //
  60. // Timer = clock;
  61. // standBy = false;
  62. // }
  63. // }
  64. // #endregion
  65.  
  66. //鎖定目標 把他打死或他跑到螢幕外才換下一隻 而不是總是打最近的
  67.  
  68. //如果之前鎖定的目標跑到射程外了 或是他確定死了 就視為沒有目標 重新定義目標
  69. if (!target||!Check(target)||target.sign||!target.Self())
  70. {
  71. target = null;
  72.  
  73. //舊版內容
  74. if (zoo.monsterAll.Count != 0)
  75. {
  76. //把射程內的目標另外轉成一個list
  77. var inin = zoo.monsterAll.FindAll (Check);
  78. //沒目標的話就延後射擊
  79. if (inin.Count != 0)
  80. {
  81. //我另外擴充的方法也是vector3.dis算距離的 不過不看y只看x,z 依照距離排序 那[0]就是靠砲塔最近的
  82. inin.Sort ((a, b) => thisTransform.position.newdistance2 (b.refence.thisTransform.position).CompareTo (thisTransform.position.newdistance2 (a.refence.thisTransform.position)));
  83. target = inin [0];
  84.  
  85. }
  86. else return;
  87.  
  88. //直接彈出 避免沒怪還發射子彈
  89. }
  90. else return;
  91.  
  92. }
  93.  
  94.  
  95. var bGameObject=pool.refTakeOut(pool.objStyle.bullet);
  96. var bullet = bGameObject.GetComponent<bullet> ();
  97. bullet.refence.thisTransform.position = insPoint.position;
  98. bullet.monster = target;
  99. bullet.forTransform = target.refence.thisTransform;
  100.  
  101. Timer = clock;
  102. standBy = false;
  103. }
  104.  
  105. // bool Check(List<monster> m)
  106. // {
  107. // for (int i = 0; i < m.Count; i++)
  108. // {
  109. // if (thisTransform.position.newdistance2 (m [i].refence.thisTransform.position) <= power)
  110. // {
  111. // return true;
  112. // }
  113. // }
  114. //
  115. // return false;
  116. // }
  117.  
  118. bool Check(monster m)
  119. {
  120. if (thisTransform.position.newdistance2 (m.refence.thisTransform.position) <= power)
  121. {
  122. return true;
  123. }
  124.  
  125. return false;
  126. }
  127. }
Add Comment
Please, Sign In to add comment