149 lines
4.0 KiB
C#
149 lines
4.0 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class TAS_System : Node
|
|
{
|
|
private double _frameLength = 0.1;
|
|
private bool _isIncrementingFrames = false;
|
|
private double _timeSinceLastFrame;
|
|
private int _currentFrame = 0;
|
|
private int _lastAdvancedFrame = 0;
|
|
|
|
public double FrameLength
|
|
{
|
|
get { return this._frameLength; }
|
|
set { this._frameLength = value; }
|
|
}
|
|
|
|
public bool IsIncrementingFrames
|
|
{
|
|
get { return this._isIncrementingFrames; }
|
|
private set { this._isIncrementingFrames = value; }
|
|
}
|
|
|
|
private double TimeSinceLastFrame
|
|
{
|
|
get { return this._timeSinceLastFrame; }
|
|
set { this._timeSinceLastFrame = value; }
|
|
}
|
|
|
|
public int CurrentFrame
|
|
{
|
|
get { return this._currentFrame; }
|
|
private set { this._currentFrame = value; }
|
|
}
|
|
|
|
public int AdvancedFramesRemaining
|
|
{
|
|
get { return this.LastAdvancedFrame - this.CurrentFrame; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The last frame that can be advanced to. This will
|
|
/// only be greater than CurrentFrame after the player
|
|
/// regresses frames, and will be equal to CurrentFrame
|
|
/// when the player has just done an action.
|
|
/// </summary>
|
|
/// <value></value>
|
|
public int LastAdvancedFrame
|
|
{
|
|
get { return this._lastAdvancedFrame; }
|
|
private set { this._lastAdvancedFrame = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signal is emitted whenever the current frame is incremented.
|
|
/// </summary>
|
|
/// <param name="newFrame"></param>
|
|
[Signal]
|
|
public delegate void FrameIncrementedEventHandler(int newFrame);
|
|
|
|
[Signal]
|
|
public delegate void FramesAdvancedEventHandler(int startFrame, int endFrame);
|
|
|
|
[Signal]
|
|
public delegate void FramesRegressedEventHandler(int startFrame, int endFrame);
|
|
|
|
[Signal]
|
|
public delegate void StartedIncrementingEventHandler();
|
|
|
|
[Signal]
|
|
public delegate void StoppedIncrementingEventHandler();
|
|
|
|
[Signal]
|
|
public delegate void FramesResetEventHandler();
|
|
|
|
|
|
public void StartIncrementingFrames()
|
|
{
|
|
this.IsIncrementingFrames = true;
|
|
EmitSignal(SignalName.StartedIncrementing);
|
|
}
|
|
|
|
public void StopIncrementingFrames()
|
|
{
|
|
this.IsIncrementingFrames = false;
|
|
EmitSignal(SignalName.StoppedIncrementing);
|
|
}
|
|
|
|
public void ResetFrameCount()
|
|
{
|
|
this.CurrentFrame = 0;
|
|
this.LastAdvancedFrame = 0;
|
|
EmitSignal(SignalName.FramesReset);
|
|
}
|
|
|
|
// Returns how many frames it could successfully advance
|
|
public int Advance(int numFrames)
|
|
{
|
|
int newFrame = Math.Min(this.CurrentFrame + numFrames, this.LastAdvancedFrame);
|
|
int framesAdvanced = newFrame - this.CurrentFrame;
|
|
EmitSignal(SignalName.FramesAdvanced, this.CurrentFrame, newFrame);
|
|
this.CurrentFrame = newFrame;
|
|
return framesAdvanced;
|
|
}
|
|
|
|
// Returns how many frames it could successfully regress
|
|
public int Regress(int numFrames)
|
|
{
|
|
int newFrame = Math.Max(this.CurrentFrame - numFrames, 0);
|
|
int framesRegressed = newFrame - this.CurrentFrame;
|
|
EmitSignal(SignalName.FramesRegressed, this.CurrentFrame, newFrame);
|
|
this.CurrentFrame = newFrame;
|
|
return framesRegressed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets rid of advanced frames.
|
|
/// </summary>
|
|
public void DiscardAdvancedFrames()
|
|
{
|
|
this.LastAdvancedFrame = this.CurrentFrame;
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
if (this.IsIncrementingFrames)
|
|
{
|
|
this.TimeSinceLastFrame += delta;
|
|
if (this.TimeSinceLastFrame >= this.FrameLength)
|
|
{
|
|
this.CurrentFrame++;
|
|
this.LastAdvancedFrame = this.CurrentFrame;
|
|
EmitSignal(SignalName.FrameIncremented, this.CurrentFrame);
|
|
this.TimeSinceLastFrame = 0 + (this.TimeSinceLastFrame - this.FrameLength);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
{
|
|
this.FrameLength = 0.1;
|
|
this.StartIncrementingFrames();
|
|
|
|
}
|
|
}
|