Files
RAGECOOP-V/Client/Sync/Entities/SyncedProjectile.cs

81 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GTA;
2022-05-23 19:19:56 +08:00
namespace RageCoop.Client
{
internal class SyncedProjectile:SyncedEntity
{
public bool Exploded { get; set; } = false;
public Projectile MainProjectile { get; set; }
2022-05-23 19:19:56 +08:00
public int ShooterID { get; set; }
public WeaponHash Hash { get; set; }
2022-05-23 19:19:56 +08:00
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)
2022-05-23 19:19:56 +08:00
{ return; }
if (_creatingProjectile) { return; }
if (MainProjectile == null || !MainProjectile.Exists())
{
2022-05-23 19:19:56 +08:00
CreateProjectile();
return;
}
2022-05-23 19:19:56 +08:00
MainProjectile.Position=Position+Velocity*Networking.Latency;
MainProjectile.Velocity=Velocity;
MainProjectile.Rotation=Rotation;
if (Exploded)
{
if (Exploded)
{
if(MainProjectile != null && MainProjectile.Exists())
{
MainProjectile.Explode();
return;
}
}
}
2022-05-23 19:19:56 +08:00
}
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) =>
{
2022-05-23 19:19:56 +08:00
if (Main.Ticked<=_projectileShotTime+1)
{
2022-05-23 19:19:56 +08:00
if (e.Projectile.WeaponHash==Hash)
{
MainProjectile=e.Projectile;
_creatingProjectile=false;
SyncEvents.OnProjectileShot-=checker;
}
}
else
{
2022-05-23 19:19:56 +08:00
_creatingProjectile=false;
SyncEvents.OnProjectileShot-=checker;
}
2022-05-23 19:19:56 +08:00
};
SyncEvents.OnProjectileShot+=checker;
}
}
}