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

94 lines
3.3 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using GTA;
using GTA.Math;
using RageCoop.Core;
2022-05-23 19:19:56 +08:00
namespace RageCoop.Client
{
internal class SyncedProjectile : SyncedEntity
{
public SyncedProjectile(Projectile p)
{
ID=EntityPool.RequestNewID();
MainProjectile = p;
Origin=p.Position;
2022-06-02 12:44:05 +08:00
var shooter = EntityPool.GetPedByHandle((p.Owner?.Handle).GetValueOrDefault());
2022-06-17 11:33:53 +08:00
if (shooter==null)
2022-05-26 17:11:37 +08:00
{
// Owner will be the vehicle if projectile is shot with a vehicle
2022-06-02 12:44:05 +08:00
var shooterVeh = EntityPool.GetVehicleByHandle((p.Owner?.Handle).GetValueOrDefault());
2022-05-26 17:11:37 +08:00
if (shooterVeh!=null && shooterVeh.MainVehicle.Driver!=null)
{
2022-06-17 11:33:53 +08:00
shooter=shooterVeh.MainVehicle.Driver?.GetSyncEntity();
2022-05-26 17:11:37 +08:00
}
else
{
2022-06-02 12:44:05 +08:00
Main.Logger.Warning($"Could not find owner for projectile:{Hash}");
2022-05-26 17:11:37 +08:00
}
}
2022-07-20 17:50:01 +08:00
if (shooter != null)
2022-06-17 11:33:53 +08:00
{
if (shooter.MainPed!=null && (p.AttachedEntity==shooter.MainPed.Weapons.CurrentWeaponObject || p.AttachedEntity== shooter.MainPed))
{
// Reloading
IsValid=false;
}
ShooterID=shooter.ID;
IsLocal=shooter.IsLocal;
2022-06-17 11:33:53 +08:00
}
2022-07-20 17:50:01 +08:00
}
public SyncedProjectile(int id)
{
ID= id;
IsLocal=false;
}
2022-06-16 11:57:48 +08:00
public bool IsValid { get; private set; } = true;
public new bool IsLocal { get; private set; } = false;
public bool Exploded { get; set; } = false;
public Projectile MainProjectile { get; set; }
2022-05-23 19:19:56 +08:00
public int ShooterID { get; set; }
2022-07-20 17:50:01 +08:00
private SyncedPed Shooter { get; set; }
public Vector3 Origin { get; set; }
/// <summary>
/// Invalid property for projectile.
/// </summary>
2022-07-20 17:50:01 +08:00
private new int OwnerID { set { } }
public WeaponHash Hash { get; set; }
2022-05-23 19:19:56 +08:00
private WeaponAsset Asset { get; set; }
internal override void Update()
{
// Skip update if no new sync message has arrived.
2022-07-20 17:50:01 +08:00
if (!NeedUpdate) { return; }
2022-05-23 19:19:56 +08:00
if (MainProjectile == null || !MainProjectile.Exists())
{
2022-05-23 19:19:56 +08:00
CreateProjectile();
return;
}
2022-06-17 11:08:49 +08:00
MainProjectile.Velocity=Velocity+(Position+Networking.Latency*Velocity-MainProjectile.Position);
2022-05-23 19:19:56 +08:00
MainProjectile.Rotation=Rotation;
LastUpdated=Main.Ticked;
2022-05-23 19:19:56 +08:00
}
private void CreateProjectile()
{
Asset=new WeaponAsset(Hash);
if (!Asset.IsLoaded) { Asset.Request(); }
2022-07-20 17:50:01 +08:00
World.ShootBullet(Position, Position+Velocity, (Shooter=EntityPool.GetPedByID(ShooterID))?.MainPed, Asset, 0);
var ps = World.GetAllProjectiles();
MainProjectile=ps[ps.Length-1];
2022-06-02 15:51:58 +08:00
if (Hash==(WeaponHash)VehicleWeaponHash.Tank)
{
var v = Shooter?.MainPed?.CurrentVehicle;
if (v!=null)
{
World.CreateParticleEffectNonLooped(SyncEvents.CorePFXAsset, "muz_tank", v.GetMuzzleInfo().Position, v.Bones[35].ForwardVector.ToEulerRotation(v.Bones[35].UpVector), 1);
}
}
EntityPool.Add(this);
}
}
}