demo recording stuff p2
This commit is contained in:
95
Source/Game/PlayerInputLocal.cs
Normal file
95
Source/Game/PlayerInputLocal.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using FlaxEngine;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class PlayerInputLocal : PlayerInput
|
||||
{
|
||||
protected List<PlayerInputState> buffer = new List<PlayerInputState>();
|
||||
protected FileStream demoFileStream;
|
||||
|
||||
public bool IsRecording { get { return demoFileStream != null; } }
|
||||
|
||||
public PlayerInputLocal()
|
||||
{
|
||||
}
|
||||
|
||||
public PlayerInputLocal(string demoPath)
|
||||
{
|
||||
demoFileStream = File.Open(demoPath, FileMode.Create, FileAccess.Write);
|
||||
//stream.Position = 0;
|
||||
//stream.SetLength(0);
|
||||
demoFileStream.WriteByte(DemoVer);
|
||||
demoFileStream.WriteByte((byte)Marshal.SizeOf(typeof(PlayerInputState)));
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
lastState = currentState;
|
||||
|
||||
// Record camera angles here?
|
||||
currentState.input.viewDeltaX = InputManager.GetAxisRaw("Mouse X");
|
||||
currentState.input.viewDeltaY = InputManager.GetAxisRaw("Mouse Y");
|
||||
}
|
||||
|
||||
public override void OnFixedUpdate()
|
||||
{
|
||||
// Record intent here
|
||||
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 override void OnEndFrame()
|
||||
{
|
||||
if (IsRecording)
|
||||
{
|
||||
currentState.input.frame = frame;
|
||||
buffer.Add(currentState.input);
|
||||
}
|
||||
frame++;
|
||||
}
|
||||
|
||||
public void FlushDemo()
|
||||
{
|
||||
if (!IsRecording)
|
||||
return;
|
||||
|
||||
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);
|
||||
demoFileStream.Write(bytes, 0, bytes.Length * sizeof(byte));
|
||||
}
|
||||
|
||||
buffer.Clear();
|
||||
}
|
||||
|
||||
public void StopRecording()
|
||||
{
|
||||
if (!IsRecording)
|
||||
return;
|
||||
|
||||
FlushDemo();
|
||||
demoFileStream.Close();
|
||||
demoFileStream = null;
|
||||
|
||||
Debug.Write(LogType.Info, "demo, wrote states: " + buffer.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user