You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.9 KiB

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace uc
{
public enum ActivityEventType
{
Enter,
Finish,
Leave,
Killed
}
public class ActivityBase : MonoBehaviour
{
bool is_finished = false;
public bool IsFinished { set { is_finished = value; } get { return is_finished; } }
float activity_duration = 0.0f;
public float Duration { get { return activity_duration; } }
bool is_entering = false;
public bool IsEntering { get { return is_entering; } }
float stamp = 0.0f;
[Serializable]
public class Entry
{
public ActivityEventType eventID = ActivityEventType.Enter;
public UnityEvent callback = new UnityEvent();
}
[SerializeField]
private List<Entry> m_Delegates;
public List<Entry> triggers
{
get
{
if (m_Delegates == null)
m_Delegates = new List<Entry>();
return m_Delegates;
}
set { m_Delegates = value; }
}
protected virtual void OnEnter() { }
public void Enter()
{
gameObject.SetActive(true);
is_entering = true;
is_finished = false;
stamp = Time.time;
activity_duration = 0.0f;
OnEnter();
OnEnterEvent();
}
protected virtual void OnUpdate() { }
private void Update()
{
activity_duration = Time.time - stamp;
OnUpdate();
}
protected virtual void OnLeave() { }
public void Leave()
{
is_entering = false;
is_finished = true;
stamp = Time.time;
OnLeave();
OnLeaveEvent();
}
protected virtual void OnFinish() { }
public void Finish()
{
OnFinish();
OnFinishEvent();
}
protected virtual void OnKilled() { }
public void Killed()
{
Leave();
OnKilled();
OnKillEvent();
}
private void Execute(ActivityEventType id)
{
for (int i = 0, imax = triggers.Count; i < imax; ++i)
{
var ent = triggers[i];
if (ent.eventID == id && ent.callback != null)
ent.callback.Invoke();
}
}
private void OnEnterEvent()
{
Execute(ActivityEventType.Enter);
}
private void OnFinishEvent()
{
Execute(ActivityEventType.Finish);
}
private void OnLeaveEvent()
{
Execute(ActivityEventType.Leave);
}
private void OnKillEvent()
{
Execute(ActivityEventType.Killed);
}
}
}