2023-12-15 07:19:02 +00:00
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
|
|
|
public class Cannon : MonoBehaviour
|
|
|
|
{
|
2023-12-17 19:50:44 +00:00
|
|
|
[Title("초기화 방식")]
|
|
|
|
[SerializeField] private bool autoInit = true;
|
|
|
|
|
|
|
|
[Title("캐논 변수")]
|
2023-12-15 07:19:02 +00:00
|
|
|
[SerializeField] private GameObject projectileObj;
|
|
|
|
[SerializeField] private Transform firePos;
|
2024-01-11 07:17:29 +00:00
|
|
|
[SerializeField] private float fireAngle = 30f;
|
|
|
|
[SerializeField] private float speed = 20f;
|
2024-01-02 21:39:53 +00:00
|
|
|
[SerializeField] private float height = 50f;
|
|
|
|
[SerializeField] private Vector2 randomCatch = new(1, 4);
|
2023-12-15 07:19:02 +00:00
|
|
|
|
2023-12-17 19:50:44 +00:00
|
|
|
[Title("캐논 발사 카메라 효과")]
|
|
|
|
[SerializeField] private float cameraShakePower = 0.5f;
|
|
|
|
[SerializeField] private float cameraShakeDuration = 0.5f;
|
|
|
|
|
2024-01-02 21:39:53 +00:00
|
|
|
private float cannonRadius;
|
|
|
|
private LayerMask boidsLayer;
|
|
|
|
|
2023-12-17 19:50:44 +00:00
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
if (autoInit)
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 07:19:02 +00:00
|
|
|
|
|
|
|
[Button("셋팅 초기화")]
|
|
|
|
private void Init()
|
|
|
|
{
|
|
|
|
projectileObj = Utils.LoadFromFolder<GameObject>("Assets/05.Prefabs/Particles/GrenadeFire", "GrenadeFireOBJ", ".prefab");
|
|
|
|
firePos = transform.Find("FirePos");
|
2024-01-02 21:39:53 +00:00
|
|
|
cannonRadius = projectileObj.GetComponent<SphereCollider>()?.radius ??
|
|
|
|
projectileObj.GetComponent<ParticleWeapon>().colliderRadius;
|
|
|
|
boidsLayer = LayerMask.GetMask("Boids");
|
2023-12-15 07:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Fire(float chargingGauge)
|
|
|
|
{
|
2023-12-17 19:50:44 +00:00
|
|
|
VisualFeedbackManager.Inst.CameraShake(CameraManager.Inst.OceanCamera.BaseShipCam, cameraShakePower, cameraShakeDuration);
|
2024-01-11 07:17:29 +00:00
|
|
|
var projectile = Instantiate(projectileObj, firePos.position, firePos.rotation);
|
2024-01-02 21:39:53 +00:00
|
|
|
var particleWeapon = projectile.GetComponent<ParticleWeapon>();
|
|
|
|
particleWeapon.onHitAction.AddListener(HandleCannonHit);
|
2024-01-11 07:17:29 +00:00
|
|
|
projectile.GetComponent<Rigidbody>().AddForce(projectile.transform.forward * (chargingGauge * speed));
|
2023-12-15 07:19:02 +00:00
|
|
|
}
|
2024-01-02 21:39:53 +00:00
|
|
|
|
|
|
|
private void HandleCannonHit(RaycastHit hit, float power)
|
|
|
|
{
|
|
|
|
if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Water"))
|
|
|
|
{
|
|
|
|
var start = hit.point;
|
|
|
|
var direction = Vector3.down;
|
|
|
|
var radius = cannonRadius;
|
|
|
|
var maxDistance = height;
|
|
|
|
|
|
|
|
if (Physics.SphereCast(start, radius, direction, out var hitInfo, maxDistance,
|
|
|
|
boidsLayer, QueryTriggerInteraction.Collide))
|
|
|
|
{
|
|
|
|
Debug.DrawRay(start, direction * height, Color.green, 3f);
|
|
|
|
|
|
|
|
var hitBoids = hitInfo.collider.GetComponentInParent<Boids>();
|
|
|
|
hitBoids.CatchBoid(Random.Range((int)randomCatch.x, (int)randomCatch.y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.DrawRay(start, direction * height, Color.red, 3f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hit.transform.GetComponent<IDamageable>()?.TakeDamage(power);
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 07:19:02 +00:00
|
|
|
}
|
|
|
|
}
|