BrokenTurretScript.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace Subworlds.Scripts
  4. {
  5. class BrokenTurretScript : EnemyScript
  6. {
  7. private Vector3 dir;
  8. private bool attacking;
  9. private void Awake()
  10. {
  11. base.trig[0] = gameObject.transform.GetChild(1).gameObject;
  12. base.trig[1] = gameObject.transform.GetChild(2).gameObject;
  13. base.Initialize(130, 7, 80, new int[] { 25, 25, 52 }, 60);
  14. }
  15. private void Update()
  16. {
  17. if (Network.isServer)
  18. {
  19. if (!this.attacking)
  20. {
  21. if (this.target)
  22. {
  23. if (Mathf.Abs(this.target.transform.position.x - this.t.position.x) < 45f)
  24. {
  25. this.attacking = true;
  26. base.StartCoroutine(this.Attack());
  27. }
  28. else
  29. {
  30. this.target = null;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. private int z = 0;
  37. private IEnumerator Seach(int count)
  38. {
  39. for (int i = 0; i < count; i++)
  40. {
  41. this.dir = this.target.transform.position - this.t.position;
  42. this.dir.Normalize();
  43. var newRotation = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  44. var diffAngle = (z - newRotation + 180) % 360 - 180;
  45. z = (int)(z - diffAngle / 6f);
  46. z = (z + 360) % 360;
  47. gameObject.transform.GetChild(0).GetChild(0).GetChild(0).localEulerAngles = new Vector3(0, ((z + 360 - 90) % 360) > 180 ? 0 : 180, ((z + 360 - 90) % 360) > 180 ? z : 180 - z);
  48. yield return new WaitForSeconds(0.01f);
  49. }
  50. }
  51. private IEnumerator Attack()
  52. {
  53. yield return Seach(100 + Random.Range(0, 30));
  54. for (int i = 0; i < 3; i++)
  55. {
  56. this.dir = this.target.transform.position - this.t.position;
  57. this.dir.Normalize();
  58. var newRotation = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  59. z = (int) newRotation;
  60. z = (z + 360) % 360;
  61. gameObject.transform.GetChild(0).GetChild(0).GetChild(0).localEulerAngles = new Vector3(0, ((z + 360 - 90) % 360) > 180 ? 0 : 180, ((z + 360 - 90) % 360) > 180 ? z : 180 - z);
  62. var pos = this.target.transform.position;
  63. var dist = 2.1f;
  64. var endPosition = new Vector3(this.t.position.x + Mathf.Cos((Mathf.PI / 180) * z) * dist, this.t.position.y + Mathf.Sin((Mathf.PI / 180) * z) * dist, this.t.position.z);
  65. var p = (GameObject)Network.Instantiate(Resources.Load("proj/pirateproj"), endPosition, Quaternion.identity, 0);
  66. p.SendMessage("EnemySet", pos, SendMessageOptions.DontRequireReceiver);
  67. yield return new WaitForSeconds(0.625f);
  68. yield return Seach(10);
  69. }
  70. this.attacking = false;
  71. yield break;
  72. }
  73. }
  74. }