using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DynamicDamageSource : MonoBehaviour, IDamageSource
{
public List<GameObject> i_sources { get => m_sources;
set => m_sources = value; }
public float i_damage { get => m_damage; set => m_damage = value; }
[SerializeField]
private List<GameObject> m_sources;
[SerializeField]
private float m_damage;
private bool m_isAttacking;
//Keeps a list of who it's hurt this cycle, to prevent enemies being hit
//multiple times in one attack action
private List<IHarmable> m_harmedThisAttack;
private void Awake()
{
m_isAttacking = false;
m_harmedThisAttack = new List<IHarmable>();
}
public void InflictDamage(IHarmable target)
{
if (m_isAttacking && !m_harmedThisAttack.Contains(target))
{
m_harmedThisAttack.Add(target);
target.TakeDamage(m_damage);
}
}
public void HarmOther(GameObject other)
{
IHarmable harmable = other.GetComponent<IHarmable>();
if (harmable != null && !m_sources.Contains(gameObject))
{
InflictDamage(harmable);
}
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.gameObject.name);
HarmOther(collision.gameObject);
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
HarmOther(other.gameObject);
}
//Allows tracking timed attacks and also manual tracking attack progress so
//the owner can tell when an attack cycle is in progress
public void StartAttacking(float duration = 0f)
{
Debug.Log(duration);
if (duration > 0f)
{
StartCoroutine(AttackDurationTimer(duration));
}
else
{
m_isAttacking = true;
}
}
//stop manually
public void StopAttacking()
{
m_isAttacking = false;
m_harmedThisAttack.Clear();
}
//Stops when timer has passed
IEnumerator AttackDurationTimer(float duration)
{
m_isAttacking = true;
float timer = 0f;
while (timer < duration)
{
timer += Time.deltaTime;
yield return null;
}
StopAttacking();
}
}