using System; using System.Collections; using System.Collections.Generic; using Blobcreate.ProjectileToolkit; using UnityEngine; using UnityEngine.Animations; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public class Canon : MonoBehaviour { public float power; public float reloadTime = 1f; public RadarTargetUI radarTargetUI; private bool isReloading; private LookAtConstraint canonLookAtConstraint; public Rigidbody projectilePrefab; public Transform launchPoint; public float timeOfFlight = 1f; public Transform predictedPos; private void Init() { projectilePrefab = DataManager.Inst.grenadeFire.GetComponent(); launchPoint = transform.Find("FirePoint"); canonLookAtConstraint = GetComponent(); } private void Awake() { Init(); } private void Update() { if (!GameManager.Inst.ShipPlayer.IsTargeting) return; if(!radarTargetUI.gameObject.activeInHierarchy && !isReloading) StartCoroutine(ReloadCoroutine()); } private IEnumerator ReloadCoroutine() { isReloading = true; yield return new WaitForSeconds(reloadTime); radarTargetUI.Reactivate(); isReloading = false; } public void Fire() { var myRigid = Instantiate(projectilePrefab, launchPoint.position, launchPoint.rotation); var v = Projectile.VelocityByTime(myRigid.transform.position, predictedPos.position, timeOfFlight); myRigid.AddForce(v, ForceMode.VelocityChange); } public void LookAtTarget() { canonLookAtConstraint.SetSources(new List { new ConstraintSource { sourceTransform = predictedPos, weight = 1 } }); } } }