| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using UnityEngine;
- using System.Collections;
- using MathNet.Numerics.Optimization;
- using TMPro;
- public class CannonController : MonoBehaviour
- {
- [SerializeField] private GamingManager _gamingManager;
- [SerializeField] private const float Cannon_pos_z = 137.0f;
- [SerializeField] private const float Cannon_pos_y = 15.0f;
- private const float Cannon_move_time = 7.0f;
- private const float Cannon_x_limit = 30f;
- private const float Cannon_y_limit = 20f;
- private float CurrentMoveTime = 0f;
- private float LastYTarget = 0f;
- private float CurrentYTarget = 0f;
- private bool bMove_positive = true; // Controlling moving direction along x axis
- [SerializeField] private GameObject FireParticle;
- [SerializeField] private GameObject _GOCannon;
- private Animator _animator;
- public delegate void CannonFireDelegate();
- public event CannonFireDelegate OnCannonFire;
- private bool bFireAnimating = false;
- void Awake()
- {
- Debug.Assert(_gamingManager);
- }
- void Start()
- {
- _animator = _GOCannon.GetComponent<Animator>();
- }
- public void ResetPos()
- {
- gameObject.transform.position = new Vector3(0.0f, Cannon_pos_y, Cannon_pos_z);
- }
- public Vector3 GetFiringPoint()
- {
- return gameObject.transform.Find("FiringPoint").position;
- }
- void Update()
- {
- // Cannon move
- if (_gamingManager.IsGameStarted() && !_gamingManager.IsGamePaused())
- {
- CurrentMoveTime += Time.deltaTime;
- float target_x = bMove_positive ? Cannon_x_limit : -Cannon_x_limit;
- float current_x = Mathf.Lerp(-target_x, target_x, CurrentMoveTime / Cannon_move_time);
- float current_y = Mathf.Lerp(LastYTarget, CurrentYTarget, CurrentMoveTime / Cannon_move_time);
- gameObject.transform.position = new Vector3(current_x, current_y, Cannon_pos_z);
- if (CurrentMoveTime > Cannon_move_time)
- {
- bMove_positive = !bMove_positive;
- LastYTarget = CurrentYTarget;
- CurrentYTarget = Random.Range(Cannon_pos_y - Cannon_y_limit, Cannon_pos_y + Cannon_y_limit);
- CurrentMoveTime = 0f;
- }
- }
- }
- public void TriggerShot()
- {
- if (_GOCannon.activeSelf)
- {
- // active the fire animation, the rest will be done by animation events
- _animator.SetBool("Shot", true);
- bFireAnimating = true;
- }
- }
- public void ExitShot()
- {
- if (_GOCannon.activeSelf)
- {
- _animator.SetBool("Shot", false);
- bFireAnimating = false;
- }
- }
- public void CannonFireAtPosition()
- {
- GameObject explosion = Instantiate(FireParticle, gameObject.transform.Find("FiringPoint"));
- GamingManager.DelayDestroy(this, explosion, 3f);
- AudioSource fireAudio = gameObject.GetComponent<AudioSource>();
- fireAudio.clip = _gamingManager.GetAudioManager().GetRandomSound(SoundCategory.CannonFire);
- if (UserSettings.ins.openEffect) fireAudio.Play();
- OnCannonFire.Invoke();
- }
- public bool IsFireAnimating()
- {
- return bFireAnimating;
- }
- }
|