Big update! Projectile sync, latency and weapon fix

This commit is contained in:
sausage
2022-05-25 10:09:59 +08:00
parent 7854a47569
commit f4e9767184
25 changed files with 546 additions and 501 deletions

View File

@ -7,74 +7,56 @@ using GTA;
namespace RageCoop.Client
{
internal class SyncedProjectile:SyncedEntity
internal class SyncedProjectile : SyncedEntity
{
public SyncedProjectile(Projectile p)
{
ID=EntityPool.RequestNewID();
IsMine=true;
MainProjectile = p;
ShooterID=p.Owner.GetSyncEntity().ID;
}
public SyncedProjectile(int id)
{
ID= id;
IsMine=false;
}
public new bool IsMine { get; private set; }
public bool Exploded { get; set; } = false;
public Projectile MainProjectile { get; set; }
public int ShooterID { get; set; }
/// <summary>
/// Invalid property for projectile.
/// </summary>
private new int OwnerID{ set { } }
public WeaponHash Hash { get; set; }
private WeaponAsset Asset { get; set; }
private bool _creatingProjectile{ get;set; }=false;
private ulong _projectileShotTime { get;set; }
public override void Update()
{
// Check if all data avalible
if (!IsReady) { return; }
// Skip update if no new sync message has arrived.
if (!NeedUpdate)
{ return; }
if (_creatingProjectile) { return; }
if (!NeedUpdate){ return; }
if (MainProjectile == null || !MainProjectile.Exists())
{
CreateProjectile();
return;
}
MainProjectile.Position=Position+Velocity*Networking.Latency;
MainProjectile.PositionNoOffset=Position+Velocity*Networking.Latency;
MainProjectile.Velocity=Velocity;
MainProjectile.Rotation=Rotation;
if (Exploded)
{
if (Exploded)
{
if(MainProjectile != null && MainProjectile.Exists())
{
MainProjectile.Explode();
return;
}
}
}
LastUpdated=Main.Ticked;
}
private void CreateProjectile()
{
Asset=new WeaponAsset(Hash);
if (!Asset.IsLoaded) { Asset.Request(); }
World.ShootBullet(Position,Position+Velocity,EntityPool.GetPedByID(ShooterID)?.MainPed,Asset,0,Velocity.Length());
_projectileShotTime=Main.Ticked;
_creatingProjectile=true;
EventHandler<ProjectileShotEventArgs> checker = null;
checker= (sender, e) =>
{
if (Main.Ticked<=_projectileShotTime+1)
{
if (e.Projectile.WeaponHash==Hash)
{
MainProjectile=e.Projectile;
_creatingProjectile=false;
SyncEvents.OnProjectileShot-=checker;
}
}
else
{
_creatingProjectile=false;
SyncEvents.OnProjectileShot-=checker;
}
};
SyncEvents.OnProjectileShot+=checker;
World.ShootBullet(Position,Position+Velocity,EntityPool.GetPedByID(ShooterID)?.MainPed,Asset,0);
var ps = World.GetAllProjectiles();
MainProjectile=ps[ps.Length-1];
EntityPool.Add(this);
}
}
}