Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Local Stopwatch.cs #61

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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)
{
if (_startTime > 0)
{
_elapsedTime += currentTime - _startTime;
}
_startTime = currentTime;
Nytra marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
_startTime = -1.0;
Nytra marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

private IOperation DoReset(FrooxEngineContext context)
Nytra marked this conversation as resolved.
Show resolved Hide resolved
{
_isRunning = false;
_elapsedTime = 0.0;
_startTime = -1.0;
return OnReset.Target;
}
}
}
Loading