SanAndreasUnity/Assets/Scripts/Behaviours/Weapon.cs

856 lines
23 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using SanAndreasUnity.Importing.Conversion;
using SanAndreasUnity.Importing.Items;
using SanAndreasUnity.Importing.Items.Definitions;
using SanAndreasUnity.Importing.Weapons;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Behaviours.Weapons;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using SanAndreasUnity.Importing.Animation;
using System.Reflection;
2019-07-06 15:36:17 +00:00
using SanAndreasUnity.Net;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.Behaviours
{
public static class WeaponSlot
{
public static readonly int Hand = 0,
Melee = 1,
Pistol = 2,
Shotgun = 3,
Submachine = 4, // uzi, mp5, tec9
Machine = 5, // ak47, m4
Rifle = 6,
Heavy = 7, // rocket launcher, flame thrower, minigun
SatchelCharge = 8,
Misc = 9, // spraycan, extinguisher, camera
Misc2 = 10, // dildo, vibe, flowers, cane
Special = 11, // parachute, goggles
Detonator = 12,
Count = 13;
}
public static class WeaponId
{
public static readonly int Pistol = 346;
public static readonly int PistolSilenced = 347;
public static readonly int DesertEagle = 348;
public static readonly int Shotgun = 349;
public static readonly int SawnOff = 350;
public static readonly int SPAS12 = 351;
public static readonly int MicroUzi = 352;
public static readonly int Tec9 = 372;
public static readonly int MP5 = 353;
public static readonly int AK47 = 355;
public static readonly int M4 = 356;
public static readonly int CountryRifle = 357;
public static readonly int SniperRifle = 358;
public static readonly int RocketLauncher = 359;
public static readonly int RocketLauncherHS = 360;
public static readonly int FlameThrower = 361;
public static readonly int MiniGun = 362;
2020-12-21 00:50:31 +00:00
public static readonly int RocketProjectile = 345;
2020-05-31 17:07:22 +00:00
}
// public class WeaponData
// {
// public string type = "";
// public string fireType = "";
// public float targetRange = 0;
// public float weaponRange = 0;
// public int modelId = -1;
// public int slot = -1;
// public AnimGroup animGroup = AnimGroup.None;
// public int clipCapacity = 0;
// public int damage = 0;
// }
public struct WeaponAttackParams
{
public List<int> LayersToIgnoreWhenRaycasting { get; set; }
public List<GameObject> GameObjectsToIgnoreWhenRaycasting { get; set; }
public static WeaponAttackParams Default { get => new WeaponAttackParams(); }
}
2020-05-31 17:07:22 +00:00
public class Weapon : MonoBehaviour
{
private WeaponDef definition = null;
public WeaponDef Definition { get { return this.definition; } }
private WeaponData data = null;
public WeaponData Data { get { return this.data; } }
private WeaponData.GunAimingOffset gunAimingOffset;
public WeaponData.GunAimingOffset GunAimingOffset { get { return this.gunAimingOffset; } }
public int SlotIndex { get { return this.Data.weaponslot; } }
public bool IsGun { get { return this.data.gunData != null; } }
public int AmmoClipSize { get { return this.data.gunData != null ? this.data.gunData.ammoClip : 0 ; } }
2019-07-06 15:40:59 +00:00
public int AmmoInClip { get => m_netWeapon.AmmoInClip; set => m_netWeapon.AmmoInClip = value; }
public int AmmoOutsideOfClip { get => m_netWeapon.AmmoOutsideOfClip; set => m_netWeapon.AmmoOutsideOfClip = value; }
2020-05-31 17:07:22 +00:00
public int TotalAmmo { get { return this.AmmoInClip + this.AmmoOutsideOfClip; } }
public Texture2D HudTexture { get; private set; }
2019-07-06 15:50:26 +00:00
protected Ped m_ped => this.PedOwner;
public Ped PedOwner { get { return m_netWeapon.PedOwner; } internal set { m_netWeapon.PedOwner = value; } }
2020-05-31 17:07:22 +00:00
private static List<System.Type> s_weaponTypes = new List<System.Type> ();
protected WeaponsManager WeaponsSettings { get { return WeaponsManager.Instance; } }
private static GameObject s_weaponsContainer = null;
public static Texture2D CrosshairTexture { get; set; }
2020-12-21 00:00:05 +00:00
public static Texture2D RocketCrosshairTexture { get; set; }
2020-05-31 17:07:22 +00:00
public static Texture2D FistTexture { get; set; }
public AnimationState AimAnimState { get; set; }
private float m_aimAnimTimeForAimWithArmWeapon = 0f;
//public bool IsInsideFireAnim { get { return this.AimAnimState != null && this.AimAnimState.enabled && this.AimAnimState.time > this.AimAnimMaxTime; } }
public Transform GunFlash { get; private set; }
public float LastTimeWhenFired { get; protected set; } = float.NegativeInfinity;
public float TimeSinceFired => Time.time - this.LastTimeWhenFired;
2020-05-31 17:07:22 +00:00
// weapon sounds are located in SFX -> GENRL -> BANK 137
// these indexes represent indexes of sounds in that bank
public static Dictionary<int, int> weaponSoundIndexes = new Dictionary<int, int>() {
{WeaponId.Pistol, 6}, // not correct
{WeaponId.PistolSilenced, 24},
{WeaponId.DesertEagle, 6},
{WeaponId.Shotgun, 21},
{WeaponId.SawnOff, 21},
{WeaponId.SPAS12, 22},
{WeaponId.Tec9, 1},
{WeaponId.MicroUzi, 0},
{WeaponId.MP5, 18},
{WeaponId.AK47, 4},
{WeaponId.M4, 3},
{WeaponId.CountryRifle, 26},
{WeaponId.SniperRifle, 26},
{WeaponId.MiniGun, 11},
// {WeaponId.RocketLauncher, 68},
// {WeaponId.RocketLauncherHS, 68},
};
2019-07-14 00:45:15 +00:00
static Dictionary<int, AudioClip> s_loadedAudioClips = new Dictionary<int, AudioClip>();
2020-05-31 17:07:22 +00:00
// used to play weapon sound
AudioSource m_audioSource;
2019-07-06 15:36:17 +00:00
NetworkedWeapon m_netWeapon;
2019-07-06 20:59:48 +00:00
public NetworkedWeapon NetWeapon => m_netWeapon;
2019-07-06 15:36:17 +00:00
// "size of the array determines how many raycasts will occur" - I don't think this is true
static readonly RaycastHit[] s_raycastHitBuffer = new RaycastHit[100];
WeaponAttackParams m_lastRaycastWeaponAttackParams = WeaponAttackParams.Default;
2020-12-20 20:51:59 +00:00
static readonly HashSet<int> s_weaponsUsingProjectile = new HashSet<int>()
{
WeaponId.RocketLauncher,
WeaponId.RocketLauncherHS,
};
2020-12-21 00:50:31 +00:00
private Geometry.GeometryParts m_projectileModel;
2020-12-21 02:01:20 +00:00
private static AudioClip s_projectileSound;
2020-12-21 00:50:31 +00:00
2020-05-31 17:07:22 +00:00
static Weapon ()
{
// obtain all weapon types
var myType = typeof (Weapon);
foreach (Assembly a in System.AppDomain.CurrentDomain.GetAssemblies())
{
s_weaponTypes.AddRange (a.GetTypes ().Where (t => t.IsSubclassOf (myType)));
}
}
public static Weapon Create (int modelId, Ped initialPedOwner)
2020-05-31 17:07:22 +00:00
{
2019-07-06 15:36:17 +00:00
NetStatus.ThrowIfNotOnServer();
WeaponDef def;
WeaponData weaponData;
2019-07-09 13:58:06 +00:00
GameObject go = CreatePart1(modelId, null, out def, out weaponData);
2019-07-06 15:36:17 +00:00
if (null == go)
return null;
// asign syncvars before spawning it
var networkedWeapon = go.GetComponentOrThrow<NetworkedWeapon>();
networkedWeapon.ModelId = modelId;
networkedWeapon.PedOwner = initialPedOwner;
2019-07-06 15:36:17 +00:00
// spawn game object here
NetManager.Spawn(go);
Weapon weapon = CreatePart2(go, def, weaponData);
if (initialPedOwner != null)
initialPedOwner.WeaponHolder.AddWeapon(weapon);
return weapon;
2019-07-06 15:36:17 +00:00
}
2019-07-09 13:58:06 +00:00
static GameObject CreatePart1(int modelId, GameObject go, out WeaponDef def, out WeaponData weaponData)
2019-07-06 15:36:17 +00:00
{
def = null;
weaponData = null;
def = Item.GetDefinition<WeaponDef> (modelId);
2020-05-31 17:07:22 +00:00
if (null == def)
return null;
2019-07-06 15:36:17 +00:00
var defCopyRef = def;
weaponData = WeaponData.LoadedWeaponsData.FirstOrDefault (wd => wd.modelId1 == defCopyRef.Id);
2020-05-31 17:07:22 +00:00
if (null == weaponData)
return null;
var geoms = Geometry.Load (def.ModelName, def.TextureDictionaryName);
if (null == geoms)
return null;
if (null == s_weaponsContainer) {
s_weaponsContainer = new GameObject ("Weapons");
// weaponsContainer.SetActive (false);
}
2019-07-09 13:58:06 +00:00
if (null == go)
go = Object.Instantiate(WeaponsManager.Instance.weaponPrefab);
go.name = def.ModelName;
2020-05-31 17:07:22 +00:00
go.transform.SetParent (s_weaponsContainer.transform);
geoms.AttachFrames (go.transform, MaterialFlags.Default);
2019-07-06 15:36:17 +00:00
return go;
}
static Weapon CreatePart2(GameObject go, WeaponDef def, WeaponData weaponData)
{
int modelId = def.Id;
2020-05-31 17:07:22 +00:00
Weapon weapon = AddWeaponComponent (go, weaponData);
weapon.definition = def;
weapon.data = weaponData;
// cache gun aiming offset
if (weapon.data.gunData != null)
weapon.gunAimingOffset = weapon.data.gunData.aimingOffset;
// load hud texture
try {
weapon.HudTexture = TextureDictionary.Load( def.TextureDictionaryName ).GetDiffuse( def.TextureDictionaryName + "icon" ).Texture;
} catch {
Debug.LogErrorFormat ("Failed to load hud icon for weapon: model {0}, txd {1}", def.ModelName, def.TextureDictionaryName);
}
// weapon sound
F.RunExceptionSafe (() => {
if (weaponSoundIndexes.ContainsKey (modelId))
{
var audioSource = go.GetOrAddComponent<AudioSource> ();
audioSource.playOnAwake = false;
2019-07-14 00:45:15 +00:00
AudioClip audioClip = null;
if (s_loadedAudioClips.ContainsKey(modelId))
{
audioClip = s_loadedAudioClips[modelId];
}
else
{
2019-08-28 17:12:34 +00:00
audioClip = Audio.AudioManager.CreateAudioClipFromSfx ("GENRL", 136, weaponSoundIndexes [modelId]);
2019-07-14 00:45:15 +00:00
s_loadedAudioClips[modelId] = audioClip;
}
2020-05-31 17:07:22 +00:00
audioSource.clip = audioClip;
weapon.m_audioSource = audioSource;
}
});
weapon.InitWeapon();
return weapon;
}
2020-12-21 00:50:31 +00:00
void LoadProjectileModel()
{
var def = Item.GetDefinition<WeaponDef>(WeaponId.RocketProjectile);
m_projectileModel = Geometry.Load (def.ModelName, def.TextureDictionaryName);
}
2020-12-21 02:01:20 +00:00
void LoadProjectileAudio()
{
if (null == s_projectileSound)
{
s_projectileSound = Audio.AudioManager.CreateAudioClipFromSfx("GENRL", 136, 68);
}
}
2019-07-06 21:50:39 +00:00
internal static void OnWeaponCreatedByServer(NetworkedWeapon networkedWeapon)
2019-07-06 15:36:17 +00:00
{
WeaponDef def;
WeaponData weaponData;
2019-07-09 13:58:06 +00:00
GameObject go = CreatePart1(networkedWeapon.ModelId, networkedWeapon.gameObject, out def, out weaponData);
2019-07-06 15:36:17 +00:00
if (null == go)
return;
2019-07-06 18:11:46 +00:00
Weapon weapon = CreatePart2(go, def, weaponData);
weapon.AssignGunFlashTransform();
2019-07-06 15:36:17 +00:00
if (weapon.PedOwner != null)
weapon.PedOwner.WeaponHolder.AddWeapon(weapon);
2019-07-06 15:36:17 +00:00
}
2020-05-31 17:07:22 +00:00
private static Weapon AddWeaponComponent (GameObject go, WeaponData data)
{
// find type which inherits Weapon class, and whose name matches the one in data
string typeName = data.weaponType.Replace ("_", "");
var type = s_weaponTypes.Where (t => 0 == string.Compare (t.Name, typeName, true)).FirstOrDefault ();
if (type != null) {
return (Weapon)go.AddComponent (type);
} else {
return go.AddComponent<Weapon> ();
}
}
public bool HasFlag( GunFlag gunFlag ) {
if (this.data != null && this.data.gunData != null)
return this.data.gunData.HasFlag (gunFlag);
return false;
}
public static string ExtractAnimGroupName(string assocGroupId)
{
if( assocGroupId.EndsWith( "bad" ) )
return assocGroupId.Substring( 0, assocGroupId.Length - 3 );
if( assocGroupId.EndsWith( "pro" ) )
return assocGroupId.Substring( 0, assocGroupId.Length - 3 );
return assocGroupId;
}
protected virtual void Awake ()
{
2019-07-06 15:36:17 +00:00
m_netWeapon = this.GetComponentOrThrow<NetworkedWeapon>();
2019-07-06 18:11:46 +00:00
this.AssignGunFlashTransform();
2020-05-31 17:07:22 +00:00
}
/// <summary>
/// Called after creating a weapon and assigning it's parameters, such are WeaponData and HUD texture.
/// Use this method to assign aim animations and timings, or initialize anything else related to weapon.
/// </summary>
protected virtual void InitWeapon()
{
// set default weapon anims and other params
string animGroup = ExtractAnimGroupName( this.Data.gunData.AssocGroupId );
this.CanCrouchAim = this.HasFlag( GunFlag.CROUCHFIRE );
if( this.HasFlag( GunFlag.CROUCHFIRE ) )
this.CrouchAimAnim = new AnimId( animGroup, animGroup + "_crouchfire" );
else
this.CrouchAimAnim = new AnimId( "RIFLE", "RIFLE_crouchfire" );
this.CrouchAimAnimMaxTime = WeaponsManager.ConvertAnimTime (this.Data.gunData.animLoop2Start);
this.CrouchAimAnimFireMaxTime = WeaponsManager.ConvertAnimTime (this.Data.gunData.animLoop2End);
this.CrouchSpineRotationOffset = WeaponsSettings.crouchSpineRotationOffset;
2020-12-20 20:51:59 +00:00
this.FiresProjectile = s_weaponsUsingProjectile.Contains(this.Data.modelId1);
if (this.FiresProjectile)
{
2020-12-20 20:51:59 +00:00
this.ProjectilePrefab = WeaponsSettings.projectilePrefab;
this.ReloadTime = WeaponsSettings.projectileReloadTime;
2020-12-21 00:50:31 +00:00
F.RunExceptionSafe(this.LoadProjectileModel);
2020-12-21 02:01:20 +00:00
F.RunExceptionSafe(this.LoadProjectileAudio);
}
2020-12-20 20:51:59 +00:00
2020-05-31 17:07:22 +00:00
}
protected virtual void Start ()
{
}
protected virtual void Update ()
{
if (WeaponsSettings.drawLineFromGun)
{
if (m_ped != null && m_ped.CurrentWeapon == this)
{
Vector3 start, end;
this.GetLineFromGun (out start, out end, m_lastRaycastWeaponAttackParams);
GLDebug.DrawLine (start, end, Color.red, 0, true);
}
2020-05-31 17:07:22 +00:00
}
}
public virtual bool CanSprintWithIt {
get {
if (this.HasFlag (GunFlag.AIMWITHARM))
return true;
if (this.SlotIndex == WeaponSlot.Heavy || this.SlotIndex == WeaponSlot.Machine || this.SlotIndex == WeaponSlot.Rifle
|| this.SlotIndex == WeaponSlot.Shotgun)
return false;
return true;
}
}
public bool IsHeavy { get { return this.HasFlag (GunFlag.HEAVY); } }
public bool CanCrouchAim { get; set; }
public virtual bool CanTurnInDirectionOtherThanAiming {
get {
if (this.HasFlag (GunFlag.AIMWITHARM))
return true;
return false;
}
}
public virtual AnimId IdleAnim {
get {
if (this.HasFlag (GunFlag.AIMWITHARM)) {
return new AnimId (AnimGroup.WalkCycle, AnimIndex.Idle);
} else {
return new AnimId (AnimGroup.MyWalkCycle, AnimIndex.IdleArmed);
}
}
}
public virtual AnimId WalkAnim {
get {
if (this.HasFlag (GunFlag.AIMWITHARM)) {
return new AnimId (AnimGroup.WalkCycle, AnimIndex.Walk);
} else {
return new AnimId (AnimGroup.Gun, AnimIndex.WALK_armed);
}
}
}
public virtual AnimId RunAnim {
get {
if (this.HasFlag (GunFlag.AIMWITHARM)) {
return new AnimId (AnimGroup.WalkCycle, AnimIndex.Run);
} else {
return new AnimId (AnimGroup.Gun, AnimIndex.run_armed);
}
}
}
public virtual AnimId GetAnimBasedOnMovement (bool canSprint)
{
Ped ped = m_ped;
if (ped.IsRunOn) {
return this.RunAnim;
} else if (ped.IsWalkOn) {
return this.WalkAnim;
} else if (ped.IsSprintOn) {
if (canSprint) {
return new AnimId (AnimGroup.MyWalkCycle, AnimIndex.sprint_civi);
} else {
return this.IdleAnim;
}
} else {
// player is standing
return this.IdleAnim;
}
}
public virtual AnimId AimAnim {
get {
return new AnimId (AnimGroup.Rifle, AnimIndex.RIFLE_fire);
}
}
public virtual AnimId AimAnimLowerPart
{
get
{
return new AnimId(AnimGroup.MyWalkCycle, AnimIndex.GUN_STAND);
}
}
public AnimId CrouchAimAnim { get; set; }
public virtual float AimAnimMaxTime {
get {
return Weapons.WeaponsManager.ConvertAnimTime (this.data.gunData.animLoopStart);
}
}
public virtual float AimAnimFireMaxTime {
get {
return Weapons.WeaponsManager.ConvertAnimTime (this.data.gunData.animLoopEnd);
}
}
public float CrouchAimAnimMaxTime { get; set; }
public float CrouchAimAnimFireMaxTime { get; set; }
public virtual float GunFlashDuration {
get {
return Weapons.WeaponsManager.Instance.GunFlashDuration;
}
}
public Vector3 CrouchSpineRotationOffset { get; set; }
2020-12-20 20:51:59 +00:00
/// <summary>
/// True if weapon doesn't inflict damage instantly, but instead fires a projectile.
/// </summary>
public bool FiresProjectile { get; set; } = false;
public GameObject ProjectilePrefab { get; set; }
public float ReloadTime { get; set; } = 0;
2020-05-31 17:07:22 +00:00
// TODO: this function should be removed, and new one should be created: OnAnimsUpdated
public virtual void UpdateAnimWhileHolding ()
{
Ped ped = m_ped;
ped.PlayerModel.PlayAnim (this.GetAnimBasedOnMovement (this.CanSprintWithIt));
}
2019-07-06 18:11:46 +00:00
void AssignGunFlashTransform()
{
this.GunFlash = this.transform.FindChildRecursive("gunflash");
}
2020-05-31 17:07:22 +00:00
public virtual void EnableOrDisableGunFlash ()
{
2019-07-11 16:07:59 +00:00
if (null == this.GunFlash)
return;
2020-05-31 17:07:22 +00:00
// enable/disable gun flash
2019-07-11 16:07:59 +00:00
Ped ped = m_ped;
bool shouldBeVisible = false;
2020-05-31 17:07:22 +00:00
2019-07-11 16:07:59 +00:00
if (ped != null && ped.IsFiring)
{
if (AimAnimState != null && AimAnimState.enabled) {
// aim anim is being played
2020-05-31 17:07:22 +00:00
2019-07-11 16:07:59 +00:00
if (AimAnimState.time.BetweenExclusive (this.AimAnimMaxTime, this.AimAnimMaxTime + this.GunFlashDuration)) {
// muzzle flash should be visible
shouldBeVisible = true;
2020-05-31 17:07:22 +00:00
}
}
}
2019-07-11 16:07:59 +00:00
this.GunFlash.gameObject.SetActive (shouldBeVisible);
2020-05-31 17:07:22 +00:00
}
public virtual void UpdateGunFlashRotation ()
{
if (null == this.GunFlash)
return;
if (!this.GunFlash.gameObject.activeInHierarchy)
return;
float randomFactor = Random.Range (0.75f, 1.25f);
float delta = WeaponsManager.Instance.GunFlashRotationSpeed * Time.deltaTime * randomFactor;
this.GunFlash.rotation *= Quaternion.AngleAxis (delta, Vector3.right);
}
#region Firing
public float MaxRange { get { return this.Data.weaponRange; } }
public float Damage { get { return this.Data.gunData.damage; } }
public virtual void PlayFireSound ()
{
if (m_audioSource && m_audioSource.clip)
{
if(!m_audioSource.isPlaying)
{
// int bankIndex = weaponSoundIndexes [this.Definition.Id];
// Audio.AudioManager.SfxGENRL137Timings [bankIndex];
// int startTimeMs = 0;
// int endTimeMs = 0;
//
// float startTime = startTimeMs / 1000f;
// float endTime = endTimeMs / 1000f;
// Debug.LogFormat("playing weapon sound, start time {0}, end time {1}, bank index {2}", startTime, endTime, bankIndex);
// m_audioSource.Stop();
// m_audioSource.time = startTime;
// m_audioSource.Play();
// m_audioSource.SetScheduledEndTime( AudioSettings.dspTime + (endTime - startTime) );
// Debug.LogFormat("playing weapon sound");
m_audioSource.Stop();
m_audioSource.time = 0f;
m_audioSource.Play();
}
}
}
public void FireProjectile (WeaponAttackParams parameters)
2020-05-31 17:07:22 +00:00
{
this.FireProjectile(this.GetFirePos(), this.GetFireDir(parameters), parameters);
2019-07-12 16:41:38 +00:00
}
2020-05-31 17:07:22 +00:00
public virtual void FireProjectile (Vector3 firePos, Vector3 fireDir, WeaponAttackParams parameters)
2019-07-12 16:41:38 +00:00
{
this.LastTimeWhenFired = Time.time;
2020-12-20 20:51:59 +00:00
if (this.FiresProjectile)
{
2020-12-21 02:01:20 +00:00
Projectile.Create(this.ProjectilePrefab, firePos, Quaternion.LookRotation(fireDir), s_projectileSound, m_projectileModel, m_ped);
2020-12-20 20:51:59 +00:00
return;
}
2020-05-31 17:07:22 +00:00
// raycast against all (non-breakable ?) objects
RaycastHit hit;
if (this.ProjectileRaycast (firePos, fireDir, out hit, parameters))
2020-05-31 17:07:22 +00:00
{
// if target object has damageable script, inflict damage to it
var damageable = hit.collider.gameObject.GetComponentInParent<Damageable> ();
2020-05-31 17:07:22 +00:00
if (damageable)
{
// ray hit something that can be damaged
// damage it
2019-07-12 22:53:13 +00:00
// first check if target object is owner ped
if (m_ped != null && m_ped.gameObject == damageable.gameObject)
{
// ray hit owner ped
// don't do anything
}
else
{
damageable.Damage(new DamageInfo()
{
amount = this.Damage,
raycastHitTransform = hit.collider.transform,
hitPoint = hit.point,
hitNormal = hit.normal,
attacker = m_ped,
});
2019-07-12 22:53:13 +00:00
}
2020-05-31 17:07:22 +00:00
}
}
}
public bool ProjectileRaycast (Vector3 source, Vector3 dir, out RaycastHit hit, WeaponAttackParams parameters)
2020-05-31 17:07:22 +00:00
{
m_lastRaycastWeaponAttackParams = parameters;
if (null == parameters.GameObjectsToIgnoreWhenRaycasting)
return Physics.Raycast(source, dir, out hit, this.MaxRange, WeaponsManager.Instance.projectileRaycastMask);
int numHits = Physics.RaycastNonAlloc (source, dir, s_raycastHitBuffer, this.MaxRange, WeaponsManager.Instance.projectileRaycastMask);
if (numHits < 1)
{
hit = new RaycastHit();
return false;
}
// "Note that the order of the results is undefined"
// - that's why we need to search for closest hit
var validHits = s_raycastHitBuffer
.Take(numHits)
.Where(h => h.collider != null && !ShouldIgnoreObjectWhenRaycasting(h.collider, parameters));
if (!validHits.Any())
{
hit = new RaycastHit();
return false;
}
hit = validHits.Aggregate((h1, h2) => h1.distance <= h2.distance ? h1 : h2);
return true;
}
2020-05-31 17:07:22 +00:00
private static bool ShouldIgnoreObjectWhenRaycasting(Collider collider, WeaponAttackParams parameters)
{
if (!parameters.LayersToIgnoreWhenRaycasting.Contains(collider.gameObject.layer))
return false;
if (parameters.GameObjectsToIgnoreWhenRaycasting.Exists(go => go == collider.gameObject))
return true;
if (parameters.GameObjectsToIgnoreWhenRaycasting.Exists(go => go.transform.IsParentOf(collider.transform)))
return true;
return false;
}
public void GetLineFromGun (out Vector3 start, out Vector3 end, WeaponAttackParams parameters)
2020-05-31 17:07:22 +00:00
{
float distance = this.MaxRange;
Vector3 firePos = this.GetFirePos ();
Vector3 fireDir = this.GetFireDir (parameters);
2020-05-31 17:07:22 +00:00
RaycastHit hit;
if (this.ProjectileRaycast (firePos, fireDir, out hit, parameters))
2020-05-31 17:07:22 +00:00
{
distance = hit.distance;
}
start = firePos;
end = firePos + fireDir * distance;
}
Vector3 GetFirePos()
{
return m_ped != null ? m_ped.FirePosition : this.GetFirePosWithoutPed();
}
public Vector3 GetFirePosWithoutPed ()
2020-05-31 17:07:22 +00:00
{
Vector3 firePos;
if (this.GunFlash != null)
firePos = this.GunFlash.transform.position;
2019-07-12 23:36:50 +00:00
else if (this.Data.gunData != null)
2020-05-31 17:07:22 +00:00
firePos = this.transform.TransformPoint (this.Data.gunData.fireOffset);
2019-07-12 23:36:50 +00:00
else
firePos = this.transform.position;
2020-05-31 17:07:22 +00:00
return firePos;
}
Vector3 GetFireDir (WeaponAttackParams parameters)
2020-05-31 17:07:22 +00:00
{
if (m_ped != null)
2020-05-31 17:07:22 +00:00
{
return m_ped.FireDirection;
2020-05-31 17:07:22 +00:00
}
else
{
return this.GetFireDirWithoutPed();
}
2020-05-31 17:07:22 +00:00
}
public Vector3 GetFireDirWithoutPed()
{
if (this.GunFlash)
return this.GunFlash.transform.right;
else
return this.transform.right;
}
#endregion
2020-05-31 17:07:22 +00:00
public virtual void OnDrawGizmosSelected ()
2020-05-31 17:07:22 +00:00
{
// draw rays from gun
Vector3 firePos = this.GetFirePos ();
// ray based on transform
Gizmos.color = Color.yellow;
GizmosDrawCastedRay (firePos, this.transform.right);
// ray based on gun flash transform
if (this.GunFlash != null)
{
Gizmos.color = F.OrangeColor;
GizmosDrawCastedRay (firePos, this.GunFlash.transform.right);
}
// ray based on aiming direction
if (m_ped != null)
{
Gizmos.color = Color.red;
GizmosDrawCastedRay (firePos, m_ped.WeaponHolder.AimDirection);
}
// ray based on firing direction
Gizmos.color = Color.black;
GizmosDrawCastedRay (firePos, this.GetFireDir (WeaponAttackParams.Default));
2020-05-31 17:07:22 +00:00
}
public void GizmosDrawCastedRay (Vector3 source, Vector3 dir)
{
float distance = 100f;
RaycastHit hit;
if (this.ProjectileRaycast (source, dir, out hit, WeaponAttackParams.Default))
2020-05-31 17:07:22 +00:00
{
distance = hit.distance;
}
Gizmos.DrawLine (source, source + dir * distance);
}
}
}