Files
RAGECOOP-V/Client/Scripts/Sync/Entities/SyncedEntity.cs

131 lines
3.6 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System.Diagnostics;
using GTA;
using GTA.Math;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
2022-07-01 14:39:43 +08:00
/// <summary>
/// </summary>
public abstract class SyncedEntity
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
private float _accumulatedOff;
2022-07-02 17:14:56 +08:00
2022-10-23 19:02:39 +08:00
private int _ownerID;
2022-07-02 17:14:56 +08:00
2022-07-01 14:39:43 +08:00
/// <summary>
2022-10-23 19:02:39 +08:00
/// Indicates whether the current player is responsible for syncing this entity.
2022-07-01 14:39:43 +08:00
/// </summary>
2023-02-13 20:44:50 +08:00
public bool IsLocal => OwnerID == LocalPlayerID;
2022-08-08 17:03:41 +08:00
2022-10-23 19:02:39 +08:00
/// <summary>
/// Network ID for this entity
/// </summary>
public int ID { get; internal set; }
2022-08-08 17:03:41 +08:00
2022-07-01 14:39:43 +08:00
/// <summary>
/// </summary>
2022-08-08 17:03:41 +08:00
public int OwnerID
{
2022-08-11 14:48:19 +02:00
get => _ownerID;
2022-08-08 17:03:41 +08:00
internal set
{
2022-10-23 19:02:39 +08:00
if (value == _ownerID && Owner != null) return;
2022-08-08 17:03:41 +08:00
_ownerID = value;
2022-09-06 21:46:35 +08:00
Owner = PlayerList.GetPlayer(value);
2022-10-23 19:02:39 +08:00
if (this is SyncedPed && Owner != null) Owner.Character = (SyncedPed)this;
2022-08-08 17:03:41 +08:00
}
}
2022-08-14 17:08:43 +08:00
internal virtual Player Owner { get; private set; }
2022-10-23 19:02:39 +08:00
2022-07-01 14:39:43 +08:00
/// <summary>
2022-05-22 15:55:26 +08:00
/// </summary>
2023-02-13 20:44:50 +08:00
public bool IsOutOfSync => Ticked - LastSynced > 200 && ID != 0;
2022-08-04 17:38:28 +08:00
2022-10-23 19:02:39 +08:00
internal bool IsReady => LastSynced > 0 || LastFullSynced == 0;
2022-08-04 17:38:28 +08:00
2022-10-23 19:02:39 +08:00
internal bool IsInvincible { get; set; } = false;
internal bool NeedUpdate => LastSynced >= LastUpdated;
2022-05-22 15:55:26 +08:00
2022-07-20 17:50:01 +08:00
public bool SendNextFrame { get; set; } = false;
public bool SendFullNextFrame { get; set; } = false;
internal Model Model { get; set; }
internal Vector3 Position { get; set; }
internal Vector3 Rotation { get; set; }
internal Quaternion Quaternion { get; set; }
internal Vector3 Velocity { get; set; }
internal abstract void Update();
2022-10-23 19:02:39 +08:00
internal void PauseUpdate(ulong frames)
2022-05-31 09:55:54 +08:00
{
2023-02-13 20:44:50 +08:00
LastUpdated = Ticked + frames;
2022-05-31 09:55:54 +08:00
}
2022-10-23 19:02:39 +08:00
2022-08-22 21:59:01 +08:00
protected Vector3 Predict(Vector3 input)
{
return Diff() + input;
}
protected Vector3 Diff()
{
return (Owner.PacketTravelTime + 0.001f * LastSyncedStopWatch.ElapsedMilliseconds) * Velocity;
2022-08-22 21:59:01 +08:00
}
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
protected bool IsOff(float thisOff, float tolerance = 3, float limit = 30)
2022-08-22 21:59:01 +08:00
{
_accumulatedOff += thisOff - tolerance;
2022-10-23 19:02:39 +08:00
if (_accumulatedOff < 0)
{
_accumulatedOff = 0;
}
2022-09-06 21:46:35 +08:00
else if (_accumulatedOff >= limit)
2022-08-22 21:59:01 +08:00
{
_accumulatedOff = 0;
return true;
}
2022-10-23 19:02:39 +08:00
2022-08-22 21:59:01 +08:00
return false;
}
2022-10-23 19:02:39 +08:00
#region LAST STATE
public void SetLastSynced(bool full)
{
LastSyncInterval = LastSentStopWatch.ElapsedMilliseconds;
2023-02-13 20:44:50 +08:00
LastSynced = Ticked;
if (full)
{
2023-02-13 20:44:50 +08:00
LastFullSynced = Ticked;
}
LastSyncedStopWatch.Restart();
}
public Stopwatch LastSyncedStopWatch = new Stopwatch();
/// <summary>
/// The interval between last sync and the earlier one
/// </summary>
public long LastSyncInterval;
2022-10-23 19:02:39 +08:00
/// <summary>
/// Last time a new sync message arrived.
/// </summary>
public ulong LastSynced { get; set; } = 0;
/// <summary>
/// Last time a new sync message arrived.
/// </summary>
public ulong LastFullSynced { get; internal set; } = 0;
/// <summary>
/// Last time the local entity has been updated,
/// </summary>
public ulong LastUpdated { get; set; }
internal Stopwatch LastSentStopWatch { get; set; } = Stopwatch.StartNew();
#endregion
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
}