hurrmmm/godot/TAS_system/TAS_System.cs

155 lines
4.2 KiB
C#
Raw Normal View History

2023-11-11 11:31:48 -07:00
using Godot;
using System;
public partial class TAS_System : Node
{
2023-11-11 13:11:27 -07:00
private double _frameLength = 0.1;
2023-11-11 14:47:07 -07:00
private bool _isIncrementingFrames = false;
private double _timeSinceLastFrame;
2023-11-11 13:11:27 -07:00
private int _currentFrame = 0;
private int _lastAdvancedFrame = 0;
public double FrameLength
{
get { return this._frameLength; }
2023-11-11 14:47:07 -07:00
set { this._frameLength = value; }
2023-11-11 13:11:27 -07:00
}
2023-11-11 14:47:07 -07:00
public bool IsIncrementingFrames
2023-11-11 13:11:27 -07:00
{
2023-11-11 14:47:07 -07:00
get { return this._isIncrementingFrames; }
private set { this._isIncrementingFrames = value; }
2023-11-11 13:11:27 -07:00
}
2023-11-11 14:47:07 -07:00
private double TimeSinceLastFrame
2023-11-11 13:11:27 -07:00
{
2023-11-11 14:47:07 -07:00
get { return this._timeSinceLastFrame; }
set { this._timeSinceLastFrame = value; }
2023-11-11 13:11:27 -07:00
}
public int CurrentFrame
{
get { return this._currentFrame; }
private set { this._currentFrame = value; }
}
public int AdvancedFramesRemaining
{
get { return this.LastAdvancedFrame - this.CurrentFrame; }
}
2023-11-11 13:11:27 -07:00
/// <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; }
}
2023-11-11 14:47:07 -07:00
/// <summary>
/// Signal is emitted whenever the current frame is incremented.
/// </summary>
/// <param name="newFrame"></param>
2023-11-11 13:11:27 -07:00
[Signal]
2023-11-11 14:47:07 -07:00
public delegate void FrameIncrementedEventHandler(int newFrame);
2023-11-11 13:11:27 -07:00
[Signal]
2023-11-11 14:47:07 -07:00
public delegate void FramesAdvancedEventHandler(int startFrame, int endFrame);
2023-11-11 13:11:27 -07:00
[Signal]
2023-11-11 14:47:07 -07:00
public delegate void FramesRegressedEventHandler(int startFrame, int endFrame);
2023-11-11 13:11:27 -07:00
[Signal]
public delegate void StartedIncrementingEventHandler();
[Signal]
public delegate void StoppedIncrementingEventHandler();
[Signal]
public delegate void FramesResetEventHandler();
2023-11-11 13:11:27 -07:00
2023-11-11 14:47:07 -07:00
public void StartIncrementingFrames()
2023-11-11 21:36:00 -07:00
{
if (this.IsIncrementingFrames == false)
{
this.IsIncrementingFrames = true;
EmitSignal(SignalName.StartedIncrementing);
}
2023-11-11 13:11:27 -07:00
}
2023-11-11 14:47:07 -07:00
public void StopIncrementingFrames()
2023-11-11 13:11:27 -07:00
{
2023-11-11 21:36:00 -07:00
if (this.IsIncrementingFrames == true)
{
this.IsIncrementingFrames = false;
EmitSignal(SignalName.StoppedIncrementing);
}
2023-11-11 13:11:27 -07:00
}
public void ResetFrameCount()
{
this.CurrentFrame = 0;
this.LastAdvancedFrame = 0;
EmitSignal(SignalName.FramesReset);
2023-11-11 13:11:27 -07:00
}
// 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;
2023-11-11 14:47:07 -07:00
EmitSignal(SignalName.FramesAdvanced, this.CurrentFrame, newFrame);
this.CurrentFrame = newFrame;
2023-11-11 13:11:27 -07:00
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;
2023-11-11 14:47:07 -07:00
EmitSignal(SignalName.FramesRegressed, this.CurrentFrame, newFrame);
this.CurrentFrame = newFrame;
2023-11-11 13:11:27 -07:00
return framesRegressed;
}
/// <summary>
/// Gets rid of advanced frames.
/// </summary>
public void DiscardAdvancedFrames()
{
this.LastAdvancedFrame = this.CurrentFrame;
}
2023-11-11 14:47:07 -07:00
public override void _PhysicsProcess(double delta)
{
if (this.IsIncrementingFrames)
{
this.TimeSinceLastFrame += delta;
if (this.TimeSinceLastFrame >= this.FrameLength)
{
this.CurrentFrame++;
2023-11-11 15:47:37 -07:00
this.LastAdvancedFrame = this.CurrentFrame;
2023-11-11 14:47:07 -07:00
EmitSignal(SignalName.FrameIncremented, this.CurrentFrame);
this.TimeSinceLastFrame = 0 + (this.TimeSinceLastFrame - this.FrameLength);
}
}
}
2023-11-11 13:11:27 -07:00
public override void _Ready()
{
this.FrameLength = 1/60;
this.StartIncrementingFrames();
}
2023-11-11 11:31:48 -07:00
}