Space-Smash-Out/Assets/Scripts/Characters/HitDetection.cs

68 lines
1.7 KiB
C#

using System;
using UnityEngine;
/// <summary>
/// Used on vulnerable trigger zones for ships.
/// </summary>
public class HitDetection : MonoBehaviour
{
[SerializeField] private TackleKind tackleKind;
public Action<TackleKind, Collider> TackledResponse;
public Action TacklingResponse;
[SerializeField] private HitKind hitKind = HitKind.IncomingNormal;
public Action<HitKind, ProjectileDamage> HitResponse;
//ID of Owner GameObject of this hit detecting zone
[HideInInspector]
public int OwnerId = 0;
void Awake()
{
OwnerId = transform.parent.gameObject.GetInstanceID();
}
/// <summary>
/// Invokes the fitting tackle response on trigger entered.
/// </summary>
/// <param name="collider"></param>
void OnTriggerEnter(Collider collider)
{
if (!(collider.tag == "Spike" ||
collider.tag == "Bumper" ||
collider.tag == "Vulnerable"))
{
return;
}
switch (collider.tag)
{
case "Spike":
// Critical for weak spots
TackledResponse.Invoke(tackleKind, collider);
break;
case "Bumper":
// Always normal tackle
TackledResponse.Invoke(TackleKind.IncomingNormal, collider);
break;
case "Vulnerable":
if (tackleKind == TackleKind.OutgoingCritical ||
tackleKind == TackleKind.OutgoingNormal)
TacklingResponse.Invoke();
break;
}
}
public void RegisterHit(ProjectileDamage damage)
{
HitResponse(hitKind, damage);
}
}
public enum TackleKind { IncomingCritical, IncomingNormal, OutgoingCritical, OutgoingNormal }
public enum HitKind { IncomingCritical, IncomingNormal }
public struct ProjectileDamage
{
public float DamageValue;
public float ImpactMagnitude;
public Vector3 ImpactPoint;
public Vector3 ImpactDirection;
}