92 lines
2.4 KiB
C#
92 lines
2.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using Lightbug.CharacterControllerPro.Core;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.Entities.Player.Movement
|
|
{
|
|
public class ProxyCharacterController : EntityBehavior,IEntityBinaryComponent
|
|
{
|
|
private struct Model
|
|
{
|
|
public float3 Position;
|
|
public float3 Velocity;
|
|
public float RotationY;
|
|
public float RotationYDelta;
|
|
public bool IsGrounded;
|
|
}
|
|
|
|
[SerializeField] private bool disabled;
|
|
[SerializeField] private CharacterActor actor;
|
|
[SerializeReference, SubclassSelector] private ITicker ticker;
|
|
|
|
public int Id { get; } = new ConstantHash(nameof(IEntityMovement));
|
|
private bool _updateThisFrame;
|
|
private double _lastSyncTime;
|
|
[SerializeField,ReadOnly]private float _syncDelta;
|
|
|
|
private readonly Dictionary<int,Model> _modelHistory = new();
|
|
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
var model = new Model
|
|
{
|
|
Position = actor.Position,
|
|
Velocity = actor.Velocity,
|
|
RotationY = MathV.TransientRotationAxis(actor.Rotation.eulerAngles.y),
|
|
RotationYDelta = 0,
|
|
IsGrounded = actor.IsGrounded
|
|
};
|
|
_modelHistory.Set(0,model);
|
|
_modelHistory.Set(-1,model);
|
|
_modelHistory.Set(-2,model);
|
|
}
|
|
|
|
public void Serialize(BinaryWriter writer)
|
|
{
|
|
|
|
}
|
|
public void Deserialize(BinaryReader reader)
|
|
{
|
|
var model = new Model
|
|
{
|
|
Position = reader.ReadFloat3(),
|
|
Velocity = reader.ReadFloat3(),
|
|
RotationY = reader.ReadSingle(),
|
|
RotationYDelta = reader.ReadSingle(),
|
|
IsGrounded = reader.ReadBoolean()
|
|
};
|
|
var _1 = _modelHistory[0];
|
|
var _2 = _modelHistory[-1];
|
|
|
|
_modelHistory.Set(0,model);
|
|
_modelHistory.Set(-1,_1);
|
|
_modelHistory.Set(-2,_2);
|
|
|
|
_syncDelta = (float) (BITApp.Time.TimeAsDouble - _lastSyncTime);
|
|
_lastSyncTime = BITApp.Time.TimeAsDouble;
|
|
}
|
|
|
|
|
|
public override void OnFixedUpdate(float deltaTime)
|
|
{
|
|
base.OnFixedUpdate(deltaTime);
|
|
if (disabled) return;
|
|
|
|
if(_modelHistory.TryGetValue(-2,out var modelA) is false || _modelHistory.TryGetValue(-1,out var modelB) is false) return;
|
|
var position = math.lerp(modelA.Position,modelB.Position,0.5f);
|
|
var velocity = math.lerp(modelA.Velocity,modelB.Velocity,0.5f);
|
|
|
|
//actor.Position = position;
|
|
actor.Velocity = velocity+(position-(float3)actor.Position);
|
|
}
|
|
}
|
|
}
|
|
|