BrokenTurretScript.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. trig[0] = gameObject.transform.GetChild(1).gameObject;
  12. trig[1] = gameObject.transform.GetChild(2).gameObject;
  13. Initialize(130, 7, 80, new int[] { 25, 25, 52 }, 60);
  14. var network = gameObject.GetComponent<NetworkEnemyBasic>();
  15. network.isPirate = true;
  16. canKnockback = false;
  17. }
  18. private void Update()
  19. {
  20. if (Network.isServer)
  21. {
  22. if (!attacking)
  23. {
  24. if (target)
  25. {
  26. if (Mathf.Abs(target.transform.position.x - t.position.x) < 45f)
  27. {
  28. attacking = true;
  29. StartCoroutine(Attack());
  30. }
  31. else
  32. {
  33. target = null;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. private int z = 0;
  40. private IEnumerator Seach(int count)
  41. {
  42. for (int i = 0; i < count; i++)
  43. {
  44. dir = target.transform.position - t.position;
  45. dir.Normalize();
  46. var newRotation = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  47. var diffAngle = (z - newRotation + 180) % 360 - 180;
  48. z = (int)(z - diffAngle / 6f);
  49. z = (z + 360) % 360;
  50. 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);
  51. yield return new WaitForSeconds(0.01f);
  52. }
  53. }
  54. private IEnumerator Attack()
  55. {
  56. yield return Seach(Random.Range(0, 100));
  57. for (int i = 0; i < 3; i++)
  58. {
  59. dir = target.transform.position - t.position;
  60. dir.Normalize();
  61. var newRotation = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  62. z = (int) newRotation;
  63. z = (z + 360) % 360;
  64. 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);
  65. var pos = target.transform.position;
  66. var dist = 2.1f;
  67. var endPosition = new Vector3(t.position.x + Mathf.Cos((Mathf.PI / 180) * z) * dist, t.position.y + Mathf.Sin((Mathf.PI / 180) * z) * dist, t.position.z);
  68. var p = (GameObject)Network.Instantiate(Resources.Load("proj/pirateproj"), endPosition, Quaternion.identity, 0);
  69. p.SendMessage("EnemySet", pos, SendMessageOptions.DontRequireReceiver);
  70. yield return new WaitForSeconds(1.625f);
  71. yield return Seach(10);
  72. }
  73. yield return Seach(200 + Random.Range(0, 200));
  74. attacking = false;
  75. yield break;
  76. }
  77. }
  78. }