OldBlueWater/BlueWater/Assets/02.Scripts/DestructiveObject.cs
NTG_Lenovo 9fc730b520 Closes #100 Ocean 씬에 바다, 파도 설정
+ 파도에 맞게 Ship 움직임 변경
+ Physics 변경
2023-12-21 14:40:48 +09:00

88 lines
2.3 KiB
C#

using RayFire;
using Sirenix.OdinInspector;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class DestructiveObject : MonoBehaviour
{
[Title("초기화 방식")]
[SerializeField] private bool autoInit = true;
[Title("기본 설정")]
[field: SerializeField] public int Strength { get; private set; } = 100;
[SerializeField] private float power = 10f;
[SerializeField] private float damageCooldown = 2f;
[SerializeField] private bool isHitting;
[SerializeField] private RayfireRigid rayfire;
[SerializeField] private Rigidbody rb;
[Button("셋팅 초기화")]
private void Init()
{
rayfire = GetComponent<RayfireRigid>();
}
private void Awake()
{
if (autoInit)
{
Init();
}
}
private void Start()
{
rb = GetComponent<Rigidbody>();
//rb.isKinematic = true;
}
private void OnCollisionEnter(Collision other)
{
if (other.collider.CompareTag("ShipPlayer"))
{
var iDestructible = other.transform.GetComponent<IDestructible>();
var otherStrength = iDestructible.Strength;
if (otherStrength > Strength)
{
DestroyObject();
}
else if (otherStrength < Strength)
{
if (isHitting) return;
Hit(iDestructible);
}
else
{
DestroyObject();
Hit(iDestructible);
}
}
else if (other.collider.CompareTag("Missile"))
{
DestroyObject();
}
}
private void DestroyObject()
{
rb.isKinematic = false;
rayfire.Demolish();
}
private void Hit(IDestructible iDestructible)
{
isHitting = true;
iDestructible.TakeDamage(power);
if (!gameObject.activeSelf) return;
StartCoroutine(Utils.CoolDown(damageCooldown, () => isHitting = false));
}
}
}