CannonController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEngine;
  2. using System.Collections;
  3. using MathNet.Numerics.Optimization;
  4. using TMPro;
  5. public class CannonController : MonoBehaviour
  6. {
  7. [SerializeField] private GamingManager _gamingManager;
  8. [SerializeField] private const float Cannon_pos_z = 137.0f;
  9. [SerializeField] private const float Cannon_pos_y = 15.0f;
  10. private const float Cannon_move_time = 7.0f;
  11. private const float Cannon_x_limit = 30f;
  12. private const float Cannon_y_limit = 20f;
  13. private float CurrentMoveTime = 0f;
  14. private float LastYTarget = 0f;
  15. private float CurrentYTarget = 0f;
  16. private bool bMove_positive = true; // Controlling moving direction along x axis
  17. [SerializeField] private GameObject FireParticle;
  18. [SerializeField] private GameObject _GOCannon;
  19. private Animator _animator;
  20. public delegate void CannonFireDelegate();
  21. public event CannonFireDelegate OnCannonFire;
  22. private bool bFireAnimating = false;
  23. void Awake()
  24. {
  25. Debug.Assert(_gamingManager);
  26. }
  27. void Start()
  28. {
  29. _animator = _GOCannon.GetComponent<Animator>();
  30. }
  31. public void ResetPos()
  32. {
  33. gameObject.transform.position = new Vector3(0.0f, Cannon_pos_y, Cannon_pos_z);
  34. }
  35. public Vector3 GetFiringPoint()
  36. {
  37. return gameObject.transform.Find("FiringPoint").position;
  38. }
  39. void Update()
  40. {
  41. // Cannon move
  42. if (_gamingManager.IsGameStarted() && !_gamingManager.IsGamePaused())
  43. {
  44. CurrentMoveTime += Time.deltaTime;
  45. float target_x = bMove_positive ? Cannon_x_limit : -Cannon_x_limit;
  46. float current_x = Mathf.Lerp(-target_x, target_x, CurrentMoveTime / Cannon_move_time);
  47. float current_y = Mathf.Lerp(LastYTarget, CurrentYTarget, CurrentMoveTime / Cannon_move_time);
  48. gameObject.transform.position = new Vector3(current_x, current_y, Cannon_pos_z);
  49. if (CurrentMoveTime > Cannon_move_time)
  50. {
  51. bMove_positive = !bMove_positive;
  52. LastYTarget = CurrentYTarget;
  53. CurrentYTarget = Random.Range(Cannon_pos_y - Cannon_y_limit, Cannon_pos_y + Cannon_y_limit);
  54. CurrentMoveTime = 0f;
  55. }
  56. }
  57. }
  58. public void TriggerShot()
  59. {
  60. if (_GOCannon.activeSelf)
  61. {
  62. // active the fire animation, the rest will be done by animation events
  63. _animator.SetBool("Shot", true);
  64. bFireAnimating = true;
  65. }
  66. }
  67. public void ExitShot()
  68. {
  69. if (_GOCannon.activeSelf)
  70. {
  71. _animator.SetBool("Shot", false);
  72. bFireAnimating = false;
  73. }
  74. }
  75. public void CannonFireAtPosition()
  76. {
  77. GameObject explosion = Instantiate(FireParticle, gameObject.transform.Find("FiringPoint"));
  78. GamingManager.DelayDestroy(this, explosion, 3f);
  79. AudioSource fireAudio = gameObject.GetComponent<AudioSource>();
  80. fireAudio.clip = _gamingManager.GetAudioManager().GetRandomSound(SoundCategory.CannonFire);
  81. if (UserSettings.ins.openEffect) fireAudio.Play();
  82. OnCannonFire.Invoke();
  83. }
  84. public bool IsFireAnimating()
  85. {
  86. return bFireAnimating;
  87. }
  88. }