OldBlueWater/BlueWater/Assets/02.Scripts/DestructibleObject.cs

74 lines
2.2 KiB
C#

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