OldBlueWater/BlueWater/Assets/02.Scripts/Player/Canon.cs
2023-10-23 12:12:13 +09:00

72 lines
2.0 KiB
C#

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<Rigidbody>();
launchPoint = transform.Find("FirePoint");
canonLookAtConstraint = GetComponent<LookAtConstraint>();
}
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<ConstraintSource>
{
new ConstraintSource
{
sourceTransform = predictedPos,
weight = 1
}
});
}
}
}