150 lines
3.2 KiB
C#
150 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class DungeonStoneWall : MonoBehaviour, IBreakable
|
|
{
|
|
[SerializeField] private GameObject healthyVisual;
|
|
[SerializeField] private GameObject damagedVisual;
|
|
|
|
[SerializeField] private ParticleSystem hitEffect;
|
|
[SerializeField] private ParticleSystem damageEffect;
|
|
[SerializeField] private ParticleSystem breakingEffect;
|
|
|
|
[SerializeField] private int hitsNeededFromNormalToDamaged;
|
|
[SerializeField] private int hitsNeededFromDamagedToBreak;
|
|
|
|
[SerializeField] private GameEvent onWallBroken;
|
|
|
|
NavMeshObstacle obstacle;
|
|
Collider collider;
|
|
|
|
private int receivedHits = 0;
|
|
|
|
bool isBroken = false;
|
|
bool isDamaged = false;
|
|
|
|
Coroutine stateChangeRoutine;
|
|
|
|
private void Awake()
|
|
{
|
|
receivedHits = 0;
|
|
isDamaged = false;
|
|
isBroken = false;
|
|
|
|
obstacle = GetComponent<NavMeshObstacle>();
|
|
collider = GetComponent<Collider>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
SetHealthyVisual();
|
|
}
|
|
|
|
[ContextMenu("DebugHit")]
|
|
public void Hit()
|
|
{
|
|
if (isBroken) return;
|
|
|
|
receivedHits++;
|
|
|
|
|
|
PlayDamageEffect();
|
|
|
|
if (receivedHits >= hitsNeededFromNormalToDamaged)
|
|
Damage();
|
|
|
|
if (receivedHits >= hitsNeededFromDamagedToBreak)
|
|
Break();
|
|
}
|
|
|
|
public void Damage()
|
|
{
|
|
PlayDamageEffect();
|
|
if (isDamaged) return;
|
|
|
|
stateChangeRoutine = StartCoroutine(SetStateWithDelay(BreakableState.Damaged, 0.2f));
|
|
isDamaged = true;
|
|
}
|
|
|
|
public void Break()
|
|
{
|
|
isBroken = true;
|
|
|
|
PlayBreakingEffect();
|
|
|
|
if (stateChangeRoutine != null)
|
|
{
|
|
StopCoroutine(stateChangeRoutine);
|
|
stateChangeRoutine = null;
|
|
}
|
|
|
|
stateChangeRoutine = StartCoroutine(SetStateWithDelay(BreakableState.Broken, 0.2f));
|
|
|
|
obstacle.enabled = false;
|
|
collider.enabled = false;
|
|
|
|
onWallBroken.Raise();
|
|
}
|
|
|
|
|
|
private void PlayHitEffect()
|
|
{
|
|
hitEffect.Play();
|
|
}
|
|
private void PlayDamageEffect()
|
|
{
|
|
damageEffect.Play();
|
|
}
|
|
private void PlayBreakingEffect()
|
|
{
|
|
breakingEffect.Play();
|
|
}
|
|
|
|
IEnumerator SetStateWithDelay(BreakableState state, float delay)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
switch (state)
|
|
{
|
|
case BreakableState.Healthy:
|
|
SetHealthyVisual();
|
|
break;
|
|
case BreakableState.Damaged:
|
|
SetDamagedVisual();
|
|
break;
|
|
case BreakableState.Broken:
|
|
SetBrokenVisual();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void SetHealthyVisual()
|
|
{
|
|
healthyVisual.SetActive(true);
|
|
damagedVisual.SetActive(false);
|
|
}
|
|
private void SetDamagedVisual()
|
|
{
|
|
healthyVisual.SetActive(false);
|
|
damagedVisual.SetActive(true);
|
|
}
|
|
private void SetBrokenVisual()
|
|
{
|
|
PlayHitEffect();
|
|
healthyVisual.SetActive(false);
|
|
damagedVisual.SetActive(false);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public enum BreakableState
|
|
{
|
|
Healthy,
|
|
Damaged,
|
|
Broken
|
|
} |