Permalink
Browse files

fix discrete event system bugs, better time abs & rel

  • Loading branch information...
FinneyTang committed Oct 27, 2016
1 parent 01f535d commit cec0c2fe2ade578e332a7022413a185b418d87ef
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TsiU
namespace TsiU
{
public struct TTimeAbs
{
@@ -14,11 +9,7 @@ public struct TTimeAbs
public TTimeAbs(float t)
{
_time = (ulong)(t * (float)S_TO_MS + 0.5f);
}
public TTimeAbs(ulong t)
{
_time = t;
_time = (ulong)(t * S_TO_MS + 0.5f);
}
public TTimeAbs(TTimeAbs t)
{
@@ -38,18 +29,6 @@ public float ToSeconds()
public static bool operator <(TTimeAbs a, TTimeAbs b) { return a._time < b._time; }
public static bool operator >=(TTimeAbs a, TTimeAbs b) { return a._time >= b._time; }
public static bool operator <=(TTimeAbs a, TTimeAbs b) { return a._time <= b._time; }
public static bool operator ==(TTimeAbs a, ulong b) { return a._time == b; }
public static bool operator !=(TTimeAbs a, ulong b) { return a._time != b; }
public static bool operator >(TTimeAbs a, ulong b) { return a._time > b; }
public static bool operator <(TTimeAbs a, ulong b) { return a._time < b; }
public static bool operator >=(TTimeAbs a, ulong b) { return a._time >= b; }
public static bool operator <=(TTimeAbs a, ulong b) { return a._time <= b; }
public static bool operator ==(ulong b, TTimeAbs a) { return a._time == b; }
public static bool operator !=(ulong b, TTimeAbs a) { return a._time != b; }
public static bool operator >(ulong b, TTimeAbs a) { return a._time > b; }
public static bool operator <(ulong b, TTimeAbs a) { return a._time < b; }
public static bool operator >=(ulong b, TTimeAbs a) { return a._time >= b; }
public static bool operator <=(ulong b, TTimeAbs a) { return a._time <= b; }
public static bool operator ==(TTimeAbs a, float b) { return a.ToSeconds() == b; }
public static bool operator !=(TTimeAbs a, float b) { return a.ToSeconds() != b; }
public static bool operator >(TTimeAbs a, float b) { return a.ToSeconds() > b; }
@@ -16,10 +16,6 @@ public TTimeRel(float t)
{
_time = (ulong)(t * (float)S_TO_MS + 0.5f);
}
public TTimeRel(ulong t)
{
_time = t;
}
public TTimeRel(TTimeRel t)
{
_time = t._time;
@@ -38,18 +34,6 @@ public float ToSeconds()
public static bool operator <(TTimeRel a, TTimeRel b) { return a._time < b._time; }
public static bool operator >=(TTimeRel a, TTimeRel b) { return a._time >= b._time; }
public static bool operator <=(TTimeRel a, TTimeRel b) { return a._time <= b._time; }
public static bool operator ==(TTimeRel a, ulong b) { return a._time == b; }
public static bool operator !=(TTimeRel a, ulong b) { return a._time != b; }
public static bool operator >(TTimeRel a, ulong b) { return a._time > b; }
public static bool operator <(TTimeRel a, ulong b) { return a._time < b; }
public static bool operator >=(TTimeRel a, ulong b) { return a._time >= b; }
public static bool operator <=(TTimeRel a, ulong b) { return a._time <= b; }
public static bool operator ==(ulong b, TTimeRel a) { return a._time == b; }
public static bool operator !=(ulong b, TTimeRel a) { return a._time != b; }
public static bool operator >(ulong b, TTimeRel a) { return a._time > b; }
public static bool operator <(ulong b, TTimeRel a) { return a._time < b; }
public static bool operator >=(ulong b, TTimeRel a) { return a._time >= b; }
public static bool operator <=(ulong b, TTimeRel a) { return a._time <= b; }
public static bool operator ==(TTimeRel a, float b) { return a.ToSeconds() == b; }
public static bool operator !=(TTimeRel a, float b) { return a.ToSeconds() != b; }
public static bool operator >(TTimeRel a, float b) { return a.ToSeconds() > b; }
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TsiU
namespace TsiU
{
class TDiscreteEvent
{
internal delegate int DiscreteEventAction(TAny workingData);
internal delegate void DiscreteEventAction(TTimeAbs curTime, TAny workingData, object param);
internal TTimeAbs TriggeredTime { get; set; }
internal int Priority { get; set; }
internal DiscreteEventAction EventAction { get; set;}
internal DiscreteEventAction EventAction { get; set; }
internal object Param { get; set; }
}
}
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace TsiU
{
@@ -11,10 +8,9 @@ enum EDiscreteEventPriority : int
};
enum EDiscreteEventSystemState : int
{
Empty = 0,
NotEmpty = 1,
Empty = 0,
NotEmpty = 1,
}
class TDiscreteEventSystem
{
private LinkedList<TDiscreteEvent> _events;
@@ -43,19 +39,23 @@ public void ClearAfter(TTimeAbs gameTime)
}
}
}
public EDiscreteEventSystemState Process(TTimeAbs gameTime, TAny workingData)
public EDiscreteEventSystemState Process(TAny workingData, ref TTimeAbs curTime)
{
while(true)
bool hasPoped = false;
while (true)
{
if (_events.Count == 0)
{
return EDiscreteEventSystemState.Empty;
}
TDiscreteEvent evt = _events.First.Value;
if (evt.TriggeredTime <= gameTime)
if (hasPoped == false || curTime == evt.TriggeredTime)
{
evt.EventAction(workingData);
_events.RemoveFirst();
curTime = evt.TriggeredTime;
evt.EventAction(curTime, workingData, evt.Param);
hasPoped = true;
}
else
{
@@ -64,7 +64,7 @@ public EDiscreteEventSystemState Process(TTimeAbs gameTime, TAny workingData)
}
return EDiscreteEventSystemState.NotEmpty;
}
public bool PushEvent(TTimeAbs occurredTime, TDiscreteEvent.DiscreteEventAction action, int priority = (int)EDiscreteEventPriority.Default)
public bool PushEvent(TTimeAbs occurredTime, TDiscreteEvent.DiscreteEventAction action, object param = null, int priority = (int)EDiscreteEventPriority.Default)
{
LinkedListNode<TDiscreteEvent> result = _events.Last;
while (result != null)
@@ -79,7 +79,8 @@ public bool PushEvent(TTimeAbs occurredTime, TDiscreteEvent.DiscreteEventAction
newEvt.TriggeredTime = occurredTime;
newEvt.EventAction = action;
newEvt.Priority = priority;
if(result == null)
newEvt.Param = param;
if (result == null)
{
_events.AddFirst(newEvt);
}
@@ -89,10 +90,10 @@ public bool PushEvent(TTimeAbs occurredTime, TDiscreteEvent.DiscreteEventAction
}
return true;
}
public bool PushEvent(TTimeAbs currentTime, TTimeRel timeAfter, TDiscreteEvent.DiscreteEventAction action, int priority = (int)EDiscreteEventPriority.Default)
public bool PushEvent(TTimeAbs currentTime, TTimeRel timeAfter, TDiscreteEvent.DiscreteEventAction action, object param = null, int priority = (int)EDiscreteEventPriority.Default)
{
TTimeAbs occurredTime = currentTime + timeAfter;
return PushEvent(occurredTime, action, priority);
return PushEvent(occurredTime, action, param, priority);
}
}
}
Binary file not shown.
View
BIN +0 Bytes (100%) Assets/Scenes/demo1.unity
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -1,2 +1,2 @@
m_EditorVersion: 5.3.5f1
m_EditorVersion: 5.4.1f1
m_StandardAssetsVersion: 0

0 comments on commit cec0c2f

Please sign in to comment.