hurrmmm/godot/TAS_system/TASable.cs

94 lines
3.1 KiB
C#
Raw Normal View History

2023-11-11 14:47:07 -07:00
using Godot;
using System;
using System.Collections.Generic;
2023-11-11 14:47:07 -07:00
using System.Globalization;
using System.Threading;
2023-11-11 14:47:07 -07:00
public partial class TASable : Node
{
[Export] RigidBody3D _assignedRigidBody3D;
public RigidBody3D AssignedRigidBody3D
{
get { return this._assignedRigidBody3D; }
}
// Frame data
public Dictionary<int, Vector3> framePositions = new Dictionary<int, Vector3>();
public Dictionary<int, Vector3> frameRotations = new Dictionary<int, Vector3>();
public Dictionary<int, Vector3> frameLinearVelocities = new Dictionary<int, Vector3>();
public Dictionary<int, Vector3> frameAngularVelocities = new Dictionary<int, Vector3>();
public virtual void SaveState(int frame)
{
this.framePositions[frame] = this.AssignedRigidBody3D.Position;
this.frameRotations[frame] = this.AssignedRigidBody3D.Rotation;
this.frameLinearVelocities[frame] = this.AssignedRigidBody3D.LinearVelocity;
this.frameAngularVelocities[frame] = this.AssignedRigidBody3D.AngularVelocity;
}
public virtual void LoadState(int frame)
{
this.AssignedRigidBody3D.Position = this.framePositions[frame];
this.AssignedRigidBody3D.Rotation = this.frameRotations[frame];
this.AssignedRigidBody3D.LinearVelocity = this.frameLinearVelocities[frame];
this.AssignedRigidBody3D.AngularVelocity = this.frameAngularVelocities[frame];
}
2023-11-11 14:47:07 -07:00
public override void _Ready()
{
if (this._assignedRigidBody3D is null)
{
this._assignedRigidBody3D = this.GetParent<RigidBody3D>();
}
2023-11-11 14:47:07 -07:00
TAS_System TAS = GetNode<TAS_System>("/root/TAS_System");
TAS.FrameIncremented += this.OnFrameIncremented;
TAS.FramesAdvanced += this.OnFramesAdvanced;
TAS.FramesRegressed += this.OnFramesRegressed;
TAS.StartedIncrementing += this.OnStartedIncrementing;
TAS.StoppedIncrementing += this.OnStoppedIncrementing;
TAS.FramesReset += this.OnFramesReset;
this.SaveState(0);
2023-11-11 14:47:07 -07:00
}
public virtual void OnFrameIncremented(int newFrame)
{
this.SaveState(newFrame);
2023-11-11 19:58:56 -07:00
// GD.Print($"Frame advanced to {newFrame}, called from node {this.AssignedRigidBody3D.Name}");
2023-11-11 14:47:07 -07:00
}
public virtual void OnFramesAdvanced(int startFrame, int endFrame)
{
this.LoadState(endFrame);
2023-11-11 19:58:56 -07:00
// GD.Print($"Frames advanced from {startFrame} to {endFrame}, called from node {this.AssignedRigidBody3D.Name}");
2023-11-11 14:47:07 -07:00
}
public virtual void OnFramesRegressed(int startFrame, int endFrame)
{
this.LoadState(endFrame);
2023-11-11 19:58:56 -07:00
// GD.Print($"Frames regressed from {startFrame} to {endFrame}, called from node {this.AssignedRigidBody3D.Name}");
}
public virtual void OnStartedIncrementing()
{
this.AssignedRigidBody3D.Freeze = false;
2023-11-11 19:58:56 -07:00
this.AssignedRigidBody3D.LockRotation = false;
}
public virtual void OnStoppedIncrementing()
{
this.AssignedRigidBody3D.Freeze = true;
2023-11-11 19:58:56 -07:00
this.AssignedRigidBody3D.LockRotation = true;
2023-11-11 14:47:07 -07:00
}
public virtual void OnFramesReset()
{
this.SaveState(0);
}
2023-11-11 14:47:07 -07:00
}