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

117 lines
3.4 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using GTA;
using GTA.Math;
2022-08-04 17:38:28 +08:00
using System.Diagnostics;
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-07-02 17:14:56 +08:00
2022-05-22 15:55:26 +08:00
/// <summary>
/// Indicates whether the current player is responsible for syncing this entity.
/// </summary>
2022-07-02 17:14:56 +08:00
public bool IsLocal
2022-05-22 15:55:26 +08:00
{
2022-08-11 14:48:19 +02:00
get => OwnerID == Main.LocalPlayerID;
2022-05-22 15:55:26 +08:00
}
2022-07-02 17:14:56 +08:00
2022-07-01 14:39:43 +08:00
/// <summary>
/// Network ID for this entity
/// </summary>
2022-07-20 17:50:01 +08:00
public int ID { get; internal set; }
2022-08-08 17:03:41 +08:00
private int _ownerID;
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-09-06 21:46:35 +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);
if (this is SyncedPed && Owner != null)
{
Owner.Character = ((SyncedPed)this);
2022-08-20 11:32:35 +08:00
}
2022-08-08 17:03:41 +08:00
}
}
2022-08-14 17:08:43 +08:00
internal virtual Player Owner { get; private set; }
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
2022-05-22 15:55:26 +08:00
public bool IsOutOfSync
{
2022-08-11 14:48:19 +02:00
get => Main.Ticked - LastSynced > 200 && ID != 0;
2022-05-22 15:55:26 +08:00
}
internal bool IsReady
{
2022-08-11 14:48:19 +02:00
get => LastSynced > 0 || LastFullSynced == 0;
}
2022-07-02 17:14:56 +08:00
internal bool IsInvincible { get; set; } = false;
internal bool NeedUpdate
{
2022-08-11 14:48:19 +02:00
get => LastSynced >= LastUpdated;
}
2022-05-22 15:55:26 +08:00
#region LAST STATE
/// <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;
2022-05-22 15:55:26 +08:00
/// <summary>
/// Last time the local entity has been updated,
/// </summary>
public ulong LastUpdated { get; set; } = 0;
2022-08-04 17:38:28 +08:00
internal Stopwatch LastSentStopWatch { get; set; } = Stopwatch.StartNew();
2022-05-22 15:55:26 +08:00
#endregion
2022-07-20 17:50:01 +08:00
public bool SendNextFrame { get; set; } = false;
public bool SendFullNextFrame { get; set; } = false;
2022-07-02 17:14:56 +08:00
/// <summary>
///
/// </summary>
2022-09-06 21:46:35 +08:00
protected internal bool _lastFrozen = 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; }
2022-08-22 21:59:01 +08:00
public Stopwatch LastSyncedStopWatch = new Stopwatch();
internal abstract void Update();
internal void PauseUpdate(ulong frames)
2022-05-31 09:55:54 +08:00
{
2022-09-06 21:46:35 +08:00
LastUpdated = Main.Ticked + frames;
2022-05-31 09:55:54 +08:00
}
2022-08-22 21:59:01 +08:00
protected Vector3 Predict(Vector3 input)
{
return (Owner.PacketTravelTime + 0.001f * LastSyncedStopWatch.ElapsedMilliseconds) * Velocity + input;
}
2022-09-06 21:46:35 +08:00
private float _accumulatedOff = 0;
protected bool IsOff(float thisOff, float tolerance = 3, float limit = 30)
2022-08-22 21:59:01 +08:00
{
_accumulatedOff += thisOff - tolerance;
2022-09-06 21:46:35 +08:00
if (_accumulatedOff < 0) { _accumulatedOff = 0; }
else if (_accumulatedOff >= limit)
2022-08-22 21:59:01 +08:00
{
_accumulatedOff = 0;
return true;
}
return false;
}
2022-05-22 15:55:26 +08:00
}
}