hurrmmm/godot/TAS_system/TAS_System.cs

130 lines
3.5 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; }
}
/// <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);
public void StartIncrementingFrames()
{
this.IsIncrementingFrames = true;
}
public void StopIncrementingFrames()
{
this.IsIncrementingFrames = false;
}
public void ResetFrameCount()
{
this.CurrentFrame = 0;
this.LastAdvancedFrame = 0;
}
// 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++;
EmitSignal(SignalName.FrameIncremented, this.CurrentFrame);
this.TimeSinceLastFrame = 0 + (this.TimeSinceLastFrame - this.FrameLength);
}
}
// GD.Print(CurrentFrame);
}
// public void Test()
// {
// GD.Print("test123");
// // EmitSignal(SignalName.TestSignal, "test456");
// }
}