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;
|
2023-11-12 11:45:10 -07:00
|
|
|
[Export]
|
|
|
|
public double min_speed = 1.0;
|
|
|
|
[Export]
|
|
|
|
public double max_speed = 10.0;
|
|
|
|
// Number of ms passed while speeding up
|
|
|
|
[Export]
|
|
|
|
public double speed = 0.0;
|
|
|
|
[Export]
|
|
|
|
public double exp_speed_base = 1.05;
|
2023-11-11 13:11:27 -07:00
|
|
|
|
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
2023-11-11 16:08:10 -07:00
|
|
|
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
|
|
|
|
2023-11-11 17:23:00 -07:00
|
|
|
[Signal]
|
|
|
|
public delegate void StartedIncrementingEventHandler();
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
public delegate void StoppedIncrementingEventHandler();
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
public delegate void FramesResetEventHandler();
|
|
|
|
|
2023-11-12 11:45:10 -07:00
|
|
|
[Signal]
|
|
|
|
public delegate void SpeedChangeEventHandler(float speed);
|
|
|
|
|
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;
|
2023-11-11 17:23:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-12 11:45:10 -07:00
|
|
|
public int AdvanceWithSpeedup(double time_delta) {
|
|
|
|
speed += time_delta;
|
|
|
|
speed = Math.Clamp(speed, min_speed, max_speed);
|
|
|
|
EmitSignal(SignalName.SpeedChange, speed);
|
|
|
|
return Advance((int)Math.Pow(exp_speed_base, speed));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int RegressWithSpeedup(double time_delta) {
|
|
|
|
speed += time_delta;
|
|
|
|
speed = Math.Clamp(speed, min_speed, max_speed);
|
|
|
|
EmitSignal(SignalName.SpeedChange, speed);
|
|
|
|
return Regress((int)Math.Pow(exp_speed_base, speed));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ResetSpeedup() {
|
|
|
|
speed = 0.0;
|
|
|
|
EmitSignal(SignalName.SpeedChange, speed);
|
|
|
|
}
|
|
|
|
|
2023-11-11 13:11:27 -07:00
|
|
|
// 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
|
|
|
|
|
|
|
|
2023-11-11 17:23:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2023-11-11 20:46:11 -07:00
|
|
|
this.FrameLength = 1/60;
|
2023-11-11 17:23:00 -07:00
|
|
|
this.StartIncrementingFrames();
|
|
|
|
|
|
|
|
}
|
2023-11-11 11:31:48 -07:00
|
|
|
}
|