Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Duality;
namespace Duality.RewindTime
{
public struct PointInTime
{
public Vector3 Position { get; }
public float Rotation { get; }
public Vector2 Velocity { get; }
public float AngularVelocity { get; }
public PointInTime(Vector3 pos, float rotation, Vector2 velocity, float angularVelocity)
{
Position = pos;
Rotation = rotation;
Velocity = velocity;
AngularVelocity = angularVelocity;
}
}
}
using System.Collections.Generic;
using Duality;
using Duality.Components.Physics;
using Duality.Input;
namespace Duality.RewindTime
{
[RequiredComponent(typeof(RigidBody))]
public class TimeBody : Component, ICmpInitializable, ICmpUpdatable
{
public float RecordTime
{
get { return recordTime; }
set { recordTime = value; }
}
private float recordTime = 250f;
[DontSerialize]
private Stack<PointInTime> pointsInTime;
[DontSerialize]
private bool isRewinding = false;
private RigidBody body;
public void OnInit(InitContext context)
{
if (context == InitContext.Activate)
{
body = GameObj.GetComponent<RigidBody>();
pointsInTime = new Stack<PointInTime>();
}
}
public void OnShutdown(ShutdownContext context)
{
}
public void Rewind()
{
if (pointsInTime.Count > 0)
{
var pointInTime = pointsInTime.Peek();
GameObj.Transform.Pos = pointInTime.Position;
GameObj.Transform.Angle = pointInTime.Rotation;
body.LinearVelocity = pointInTime.Velocity;
body.AngularVelocity = pointInTime.AngularVelocity;
pointsInTime.Pop();
}
else
{
StopRewind();
}
}
public void Record()
{
if (pointsInTime.Count > MathF.Round(recordTime / Time.TimeMult))
{
pointsInTime.Pop();
}
pointsInTime.Push(new PointInTime(GameObj.Transform.Pos, GameObj.Transform.Angle, body.LinearVelocity, body.AngularVelocity));
}
public void StartRewind()
{
isRewinding = true;
}
public void StopRewind()
{
isRewinding = false;
}
public void OnUpdate()
{
if (DualityApp.Keyboard.KeyPressed(Key.BackSpace))
StartRewind();
if (DualityApp.Keyboard.KeyReleased(Key.BackSpace))
StopRewind();
if (isRewinding)
Rewind();
else
Record();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.