Skip to content

Commit

Permalink
Merge pull request #61 from Xlinka/Testing
Browse files Browse the repository at this point in the history
Create Local Stopwatch.cs
  • Loading branch information
Xlinka authored Dec 14, 2024
2 parents 58ccc19 + 56a8781 commit f255108
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using FrooxEngine.ProtoFlux;
using ProtoFlux.Core;
using ProtoFlux.Runtimes.Execution;

namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Time
{
[NodeCategory("Obsidian/Time")]
[NodeName("Local Stopwatch")]
public class LocalStopwatch : VoidNode<FrooxEngineContext>
{
[ContinuouslyChanging]
public readonly ValueOutput<float> ElapsedTime;

[ContinuouslyChanging]
public readonly ValueOutput<bool> IsRunning;

[PossibleContinuations(new string[] { "OnStart" })]
public readonly Operation Start;

[PossibleContinuations(new string[] { "OnStop" })]
public readonly Operation Stop;

[PossibleContinuations(new string[] { "OnReset" })]
public readonly Operation Reset;

public Continuation OnStart;
public Continuation OnStop;
public Continuation OnReset;

private double _startTime = -1.0;
private double _elapsedTime = 0.0;
private bool _isRunning = false;

public LocalStopwatch()
{
ElapsedTime = new ValueOutput<float>(this);
IsRunning = new ValueOutput<bool>(this);
Start = new Operation(this, 0);
Stop = new Operation(this, 1);
Reset = new Operation(this, 2);
}

protected override void ComputeOutputs(FrooxEngineContext context)
{
double currentTime = context.World.Time.WorldTime;

// Update elapsed time if running
if (_isRunning)
{
_elapsedTime += currentTime - _startTime;
_startTime = currentTime;
}

// Write outputs
ElapsedTime.Write((float)_elapsedTime, context);
IsRunning.Write(_isRunning, context);
}

private IOperation DoStart(FrooxEngineContext context)
{
_isRunning = true;
_startTime = context.World.Time.WorldTime;
return OnStart.Target;
}

private IOperation DoStop(FrooxEngineContext context)
{
_isRunning = false;
return OnStop.Target;
}

private IOperation DoReset(FrooxEngineContext context)
{
_elapsedTime = 0.0;
_startTime = context.World.Time.WorldTime;
return OnReset.Target;
}
}
}

0 comments on commit f255108

Please sign in to comment.