demo recording stuff p2
This commit is contained in:
@@ -10,6 +10,7 @@ namespace Game
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct PlayerInputState
|
||||
{
|
||||
public ulong frame;
|
||||
public float viewDeltaX, viewDeltaY;
|
||||
public float moveForward;
|
||||
public float moveRight;
|
||||
@@ -37,106 +38,20 @@ namespace Game
|
||||
{
|
||||
public PlayerState lastState;
|
||||
public PlayerState currentState;
|
||||
public int frame;
|
||||
public ulong frame;
|
||||
|
||||
private const byte DemoVer = 1;
|
||||
public const byte DemoVer = 1;
|
||||
|
||||
private List<PlayerInputState> buffer = new List<PlayerInputState>();
|
||||
|
||||
private bool demoPlayback = false;
|
||||
private bool demoRecording = true;
|
||||
|
||||
private string demoPath = @"C:\dev\GoakeFlax\testdemo.gdem";
|
||||
|
||||
public void OpenDemo()
|
||||
public virtual void OnUpdate()
|
||||
{
|
||||
if (!File.Exists(demoPath))
|
||||
return;
|
||||
|
||||
T RawDeserialize<T>(byte[] rawData, int position)
|
||||
{
|
||||
int rawsize = Marshal.SizeOf(typeof(T));
|
||||
if (rawsize > rawData.Length - position)
|
||||
throw new ArgumentException("Not enough data to fill struct. Array length from position: "+(rawData.Length-position) + ", Struct length: "+rawsize);
|
||||
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
|
||||
Marshal.Copy(rawData, position, buffer, rawsize);
|
||||
T retobj = (T)Marshal.PtrToStructure(buffer, typeof(T));
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
return retobj;
|
||||
}
|
||||
|
||||
var stream = File.OpenRead(demoPath);
|
||||
var ver = (int)stream.ReadByte();
|
||||
if (ver != DemoVer)
|
||||
{
|
||||
Console.WriteLine("demover doesn't match: " + ver + " != " + DemoVer);
|
||||
stream.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
var expectedSize = Marshal.SizeOf(typeof(PlayerInputState));
|
||||
byte[] b = new byte[expectedSize];
|
||||
var readBytes = stream.Read(b, 0, b.Length);
|
||||
if (readBytes < expectedSize)
|
||||
break;
|
||||
|
||||
buffer.Add(RawDeserialize<PlayerInputState>(b, 0));
|
||||
}
|
||||
|
||||
Console.WriteLine("demo numstates: " + buffer.Count);
|
||||
|
||||
demoPlayback = true;
|
||||
demoRecording = false;
|
||||
}
|
||||
|
||||
public void OnUpdate()
|
||||
public virtual void OnFixedUpdate()
|
||||
{
|
||||
lastState = currentState;
|
||||
|
||||
// Record camera angles here?
|
||||
if (!demoPlayback)
|
||||
{
|
||||
currentState.input.viewDeltaX = InputManager.GetAxisRaw("Mouse X");
|
||||
currentState.input.viewDeltaY = InputManager.GetAxisRaw("Mouse Y");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnFixedUpdate()
|
||||
public virtual void OnEndFrame()
|
||||
{
|
||||
// Record intent here
|
||||
|
||||
if (!demoPlayback)
|
||||
{
|
||||
currentState.input.moveForward = InputManager.GetAxis("Vertical");
|
||||
currentState.input.moveRight = InputManager.GetAxis("Horizontal");
|
||||
currentState.input.attacking = InputManager.GetAction("Attack");
|
||||
currentState.input.jumping = InputManager.GetAction("Jump");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEndFrame()
|
||||
{
|
||||
if (demoRecording)
|
||||
buffer.Add(currentState.input);
|
||||
|
||||
frame++;
|
||||
|
||||
if (demoPlayback)
|
||||
{
|
||||
if (frame < buffer.Count)
|
||||
{
|
||||
//var actorState = currentState.actor;
|
||||
currentState.input = buffer[frame];
|
||||
//currentState.actor = actorState;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("demo ended");
|
||||
demoPlayback = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordCurrentActorState(PlayerActorState actorState)
|
||||
@@ -153,37 +68,5 @@ namespace Game
|
||||
{
|
||||
return currentState.actor;
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
if (!demoRecording)
|
||||
return;
|
||||
|
||||
var stream = File.Open(demoPath, FileMode.Create, FileAccess.Write);
|
||||
//stream.Position = 0;
|
||||
//stream.SetLength(0);
|
||||
stream.WriteByte(DemoVer);
|
||||
|
||||
byte[] RawSerialize(object anything)
|
||||
{
|
||||
int rawSize = Marshal.SizeOf(anything);
|
||||
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
|
||||
Marshal.StructureToPtr(anything, buffer, false);
|
||||
byte[] rawDatas = new byte[rawSize];
|
||||
Marshal.Copy(buffer, rawDatas, 0, rawSize);
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
return rawDatas;
|
||||
}
|
||||
|
||||
foreach (var state in buffer)
|
||||
{
|
||||
var bytes = RawSerialize(state);
|
||||
stream.Write(bytes, 0, bytes.Length * sizeof(byte));
|
||||
}
|
||||
|
||||
stream.Close();
|
||||
|
||||
Debug.Write(LogType.Info, "demo, wrote states: " + buffer.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user