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

97 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GTA;
using GTA.Math;
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());
if(shooter != null)
{
2022-06-16 11:57:48 +08:00
if (shooter.MainPed!=null && (p.AttachedEntity==shooter.MainPed.Weapons.CurrentWeaponObject || p.AttachedEntity== shooter.MainPed))
{
IsValid=false;
}
ShooterID=shooter.ID;
2022-06-16 11:57:48 +08:00
IsMine=shooter.IsMine;
}
2022-05-26 17:11:37 +08:00
else
{
// 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)
{
ShooterID=shooterVeh.MainVehicle.Driver.GetSyncEntity().ID;
}
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-06-16 11:57:48 +08:00
}
public SyncedProjectile(int id)
{
ID= id;
IsMine=false;
}
2022-06-16 11:57:48 +08:00
public bool IsValid { get; private set; } = true;
public new bool IsMine { 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-06-02 15:51:58 +08:00
private SyncedPed Shooter { get;set; }
public Vector3 Origin { get; set; }
/// <summary>
/// Invalid property for projectile.
/// </summary>
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.
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-06-02 15:51:58 +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);
}
}
}