2020-06-20 14:16:11 +00:00
|
|
|
|
using System.Linq;
|
2020-06-29 17:08:04 +00:00
|
|
|
|
using SanAndreasUnity.Behaviours.Audio;
|
2020-07-01 17:36:39 +00:00
|
|
|
|
using SanAndreasUnity.Net;
|
2020-06-20 14:16:11 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2020-06-09 16:34:15 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours.Vehicles
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public partial class Vehicle
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public Damageable Damageable { get; private set; }
|
|
|
|
|
|
2020-06-09 17:45:25 +00:00
|
|
|
|
public float Health { get; set; } = 1000;
|
2020-06-09 16:34:15 +00:00
|
|
|
|
|
|
|
|
|
public float MaxHealth { get; set; } = 1000;
|
|
|
|
|
|
|
|
|
|
public bool IsUnderFlame { get; private set; } = false;
|
|
|
|
|
public bool IsUnderSmoke { get; private set; } = false;
|
|
|
|
|
|
2020-06-09 17:45:25 +00:00
|
|
|
|
bool m_alreadyExploded = false;
|
2021-02-06 20:12:41 +00:00
|
|
|
|
public bool ExplodedThisFrame => m_alreadyExploded;
|
2020-06-09 17:45:25 +00:00
|
|
|
|
|
2020-06-09 16:34:15 +00:00
|
|
|
|
public float TimeWhenBecameUnderFlame { get; private set; } = float.NegativeInfinity;
|
2020-06-21 14:35:39 +00:00
|
|
|
|
public float TimeSinceBecameUnderFlame => Time.time - this.TimeWhenBecameUnderFlame;
|
2020-06-09 16:34:15 +00:00
|
|
|
|
|
2020-06-20 22:33:55 +00:00
|
|
|
|
GameObject m_smokeGameObject;
|
2020-06-20 23:24:19 +00:00
|
|
|
|
GameObject m_flameGameObject;
|
2020-06-20 22:33:55 +00:00
|
|
|
|
|
2020-06-29 17:08:04 +00:00
|
|
|
|
public static AudioClip ExplosionSound { get; private set; }
|
|
|
|
|
|
2020-06-09 16:34:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Awake_Damage()
|
|
|
|
|
{
|
2020-06-09 17:45:25 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetupDamagable()
|
|
|
|
|
{
|
|
|
|
|
this.Damageable = this.HighDetailMeshesParent.gameObject.AddComponent<Damageable>();
|
2020-06-09 16:34:15 +00:00
|
|
|
|
this.Damageable.OnDamageEvent.AddListener(() => this.OnDamaged());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDamaged()
|
|
|
|
|
{
|
2020-07-01 17:36:39 +00:00
|
|
|
|
if (!NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-09 16:34:15 +00:00
|
|
|
|
var damageInfo = this.Damageable.LastDamageInfo;
|
|
|
|
|
|
|
|
|
|
if (this.Health <= 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.Health -= damageInfo.amount;
|
|
|
|
|
|
|
|
|
|
if (this.Health <= 0)
|
|
|
|
|
{
|
|
|
|
|
this.Explode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update_Damage()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
bool shouldBeUnderSmoke = this.MaxHealth * 0.33f >= this.Health;
|
|
|
|
|
if (shouldBeUnderSmoke != this.IsUnderSmoke)
|
|
|
|
|
{
|
|
|
|
|
// smoke status changed
|
|
|
|
|
this.IsUnderSmoke = shouldBeUnderSmoke;
|
|
|
|
|
// update vfx
|
2020-06-20 22:33:55 +00:00
|
|
|
|
this.UpdateSmokeVfx();
|
2020-06-09 16:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool shouldBeUnderFlame = this.MaxHealth * 0.1f >= this.Health;
|
|
|
|
|
if (shouldBeUnderFlame != this.IsUnderFlame)
|
|
|
|
|
{
|
|
|
|
|
// flame status changed
|
|
|
|
|
this.IsUnderFlame = shouldBeUnderFlame;
|
|
|
|
|
if (this.IsUnderFlame)
|
|
|
|
|
this.TimeWhenBecameUnderFlame = Time.time;
|
|
|
|
|
// update vfx
|
2020-06-20 23:24:19 +00:00
|
|
|
|
this.UpdateFlameVfx();
|
2020-06-09 16:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.IsUnderFlame && Time.time - this.TimeWhenBecameUnderFlame >= 5)
|
|
|
|
|
{
|
|
|
|
|
// enough time passed since vehicle flamed - explode it
|
2020-07-01 17:36:39 +00:00
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
{
|
|
|
|
|
this.Explode();
|
|
|
|
|
}
|
2020-06-09 16:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 22:33:55 +00:00
|
|
|
|
void UpdateSmokeVfx()
|
|
|
|
|
{
|
|
|
|
|
if (this.IsUnderSmoke)
|
|
|
|
|
{
|
|
|
|
|
if (null == m_smokeGameObject)
|
|
|
|
|
{
|
|
|
|
|
Transform parent = this.EngineTransform != null ? this.EngineTransform : this.transform;
|
|
|
|
|
m_smokeGameObject = Object.Instantiate(
|
|
|
|
|
VehicleManager.Instance.smokePrefab, parent.position, parent.rotation, parent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (null != m_smokeGameObject)
|
|
|
|
|
{
|
|
|
|
|
Object.Destroy(m_smokeGameObject);
|
|
|
|
|
m_smokeGameObject = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 23:24:19 +00:00
|
|
|
|
void UpdateFlameVfx()
|
|
|
|
|
{
|
|
|
|
|
if (this.IsUnderFlame)
|
|
|
|
|
{
|
|
|
|
|
if (null == m_flameGameObject)
|
|
|
|
|
{
|
|
|
|
|
Transform parent = this.EngineTransform != null ? this.EngineTransform : this.transform;
|
|
|
|
|
m_flameGameObject = Object.Instantiate(
|
|
|
|
|
VehicleManager.Instance.flamePrefab, parent.position, parent.rotation, parent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (null != m_flameGameObject)
|
|
|
|
|
{
|
|
|
|
|
Object.Destroy(m_flameGameObject);
|
|
|
|
|
m_flameGameObject = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 16:34:15 +00:00
|
|
|
|
public void Explode()
|
2020-06-20 18:17:22 +00:00
|
|
|
|
{
|
|
|
|
|
F.RunExceptionSafe(() => this.ExplodeInternal());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExplodeInternal()
|
2020-06-09 16:34:15 +00:00
|
|
|
|
{
|
2020-06-09 17:45:25 +00:00
|
|
|
|
if (m_alreadyExploded)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_alreadyExploded = true;
|
|
|
|
|
|
2020-06-20 15:56:17 +00:00
|
|
|
|
// destroy this game object before doing anything else
|
2020-06-09 17:45:25 +00:00
|
|
|
|
|
|
|
|
|
Object.Destroy(this.gameObject);
|
2020-06-09 16:34:15 +00:00
|
|
|
|
|
2020-07-01 17:25:59 +00:00
|
|
|
|
// detach vehicle parts
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
|
|
|
|
string[] startingNames = new string[] { "door_", "wheel_", "bonnet_", "boot_", "windscreen_", "exhaust_" };
|
2020-07-01 17:25:59 +00:00
|
|
|
|
|
2020-06-20 14:16:11 +00:00
|
|
|
|
foreach (var frame in _frames)
|
|
|
|
|
{
|
|
|
|
|
if (!frame.gameObject.activeInHierarchy)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!startingNames.Any(n => frame.gameObject.name.StartsWith(n)))
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
DetachFrameDuringExplosion(frame, VehicleManager.Instance.explosionLeftoverPartsMass, null);
|
2020-06-20 15:56:17 +00:00
|
|
|
|
}
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
2020-06-20 15:56:17 +00:00
|
|
|
|
// chassis need to be handled after all other objects are detached, because chassis can sometimes
|
|
|
|
|
// have other objects as children
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
2020-06-20 15:56:17 +00:00
|
|
|
|
Frame chassisFrame = _frames.FirstOrDefault(f => f.Name == "chassis");
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
2020-06-20 15:56:17 +00:00
|
|
|
|
if (null == chassisFrame)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"Chassis object not found on vehicle {this.DescriptionForLogging}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-01 21:36:01 +00:00
|
|
|
|
DetachFrameDuringExplosion(chassisFrame, this.HandlingData.Mass * 0.8f, null);
|
2020-06-20 15:56:17 +00:00
|
|
|
|
}
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
2020-06-21 16:21:15 +00:00
|
|
|
|
// inflict damage to nearby objects
|
|
|
|
|
|
2020-06-21 21:35:39 +00:00
|
|
|
|
Damageable.InflictDamageToObjectsInArea(
|
2020-07-01 17:25:59 +00:00
|
|
|
|
this.transform.position,
|
2020-06-28 14:35:35 +00:00
|
|
|
|
VehicleManager.Instance.explosionDamageRadius,
|
|
|
|
|
Mathf.Pow(this.HandlingData.Mass, VehicleManager.Instance.explosionMassToDamageExponent),
|
2020-06-29 15:50:33 +00:00
|
|
|
|
VehicleManager.Instance.explosionDamageOverDistanceCurve,
|
|
|
|
|
DamageType.Explosion);
|
2020-06-21 16:21:15 +00:00
|
|
|
|
|
2020-07-01 17:25:59 +00:00
|
|
|
|
// create explosion - this includes effects, physics force, sound
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
2020-06-21 13:43:19 +00:00
|
|
|
|
GameObject explosionGo = Object.Instantiate(VehicleManager.Instance.explosionPrefab, this.transform.position, this.transform.rotation);
|
2020-06-22 01:05:42 +00:00
|
|
|
|
|
2020-07-04 16:52:23 +00:00
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
NetManager.Spawn(explosionGo);
|
|
|
|
|
|
2020-06-21 13:43:19 +00:00
|
|
|
|
// modify strength of explosion based on vehicle mass
|
2020-06-22 01:05:42 +00:00
|
|
|
|
float forceFactor = Mathf.Sqrt(this.HandlingData.Mass) / Mathf.Sqrt(1500f);
|
2020-06-28 18:32:30 +00:00
|
|
|
|
var physicsForce = explosionGo.GetComponentOrThrow<ExplosionForce>();
|
2020-06-22 01:05:42 +00:00
|
|
|
|
physicsForce.explosionForce *= forceFactor;
|
|
|
|
|
physicsForce.upwardsModifier *= forceFactor;
|
2020-06-20 14:16:11 +00:00
|
|
|
|
|
2020-06-29 17:08:04 +00:00
|
|
|
|
// assign explosion sound
|
|
|
|
|
F.RunExceptionSafe(() => AssignExplosionSound(explosionGo));
|
|
|
|
|
|
2021-01-05 04:56:39 +00:00
|
|
|
|
// kill all peds inside
|
|
|
|
|
foreach (Ped ped in this.Seats
|
|
|
|
|
.Select(s => s.OccupyingPed)
|
|
|
|
|
.Where(p => p != null)
|
|
|
|
|
.ToList())
|
|
|
|
|
{
|
|
|
|
|
ped.Kill();
|
|
|
|
|
}
|
2020-06-09 16:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
public void DetachFrameDuringExplosion(string frameName, float mass, GameObject parentGo)
|
|
|
|
|
{
|
|
|
|
|
Frame frame = this.Frames.FirstOrDefault(f => f.gameObject.name == frameName);
|
|
|
|
|
if (null == frame)
|
2020-07-04 17:11:39 +00:00
|
|
|
|
{
|
2020-07-01 21:36:01 +00:00
|
|
|
|
Debug.LogError($"Failed to find frame by name: {frameName}");
|
2020-07-04 17:11:39 +00:00
|
|
|
|
}
|
2020-07-01 21:36:01 +00:00
|
|
|
|
else
|
2020-07-04 17:11:39 +00:00
|
|
|
|
{
|
2020-07-05 11:42:50 +00:00
|
|
|
|
DetachFrameDuringExplosion(frame, mass, parentGo);
|
2020-07-04 17:11:39 +00:00
|
|
|
|
}
|
2020-07-01 21:36:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DetachFrameDuringExplosion(Frame frame, float mass, GameObject parentGo)
|
2020-06-20 15:56:17 +00:00
|
|
|
|
{
|
2020-07-05 20:33:34 +00:00
|
|
|
|
DetachFrameFromTransformDuringExplosion(this.transform, frame, mass, parentGo, this.NetIdentity.netId, this.Definition.Id, this.Colors);
|
2020-07-05 16:37:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 20:33:34 +00:00
|
|
|
|
public static void DetachFrameFromTransformDuringExplosion(Transform tr, Frame frame, float mass, GameObject parentGo, uint vehicleNetId, int vehicleModelId, int[] vehicleColors)
|
2020-07-05 16:37:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (! tr.IsParentOf(frame.transform)) // already detached ?
|
2020-07-04 18:12:01 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2020-06-20 15:56:17 +00:00
|
|
|
|
var meshFilter = frame.GetComponentInChildren<MeshFilter>();
|
|
|
|
|
if (null == meshFilter)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!meshFilter.gameObject.activeInHierarchy)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
if (null == parentGo)
|
|
|
|
|
parentGo = Object.Instantiate(VehicleManager.Instance.explosionLeftoverPartPrefab, meshFilter.transform.position, meshFilter.transform.rotation);
|
|
|
|
|
|
|
|
|
|
parentGo.name = "vehicle_part_" + meshFilter.gameObject.name;
|
|
|
|
|
|
|
|
|
|
meshFilter.transform.SetParent(parentGo.transform, true);
|
2020-06-20 15:56:17 +00:00
|
|
|
|
meshFilter.gameObject.layer = UnityEngine.LayerMask.NameToLayer("Default");
|
|
|
|
|
var meshCollider = meshFilter.gameObject.GetOrAddComponent<MeshCollider>();
|
2020-07-04 17:11:09 +00:00
|
|
|
|
meshCollider.cookingOptions = MeshColliderCookingOptions.None;
|
2020-06-20 15:56:17 +00:00
|
|
|
|
meshCollider.convex = true;
|
|
|
|
|
meshCollider.sharedMesh = meshFilter.sharedMesh;
|
|
|
|
|
var rigidBody = meshFilter.gameObject.GetOrAddComponent<Rigidbody>();
|
2020-06-20 16:05:12 +00:00
|
|
|
|
rigidBody.mass = mass;
|
2020-06-20 15:56:17 +00:00
|
|
|
|
rigidBody.drag = 0.05f;
|
|
|
|
|
rigidBody.maxDepenetrationVelocity = VehicleManager.Instance.explosionLeftoverPartsMaxDepenetrationVelocity;
|
2021-02-05 22:40:30 +00:00
|
|
|
|
rigidBody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
|
2020-07-01 17:25:59 +00:00
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
{
|
|
|
|
|
Object.Destroy(parentGo, VehicleManager.Instance.explosionLeftoverPartsLifetime * Random.Range(0.8f, 1.2f));
|
|
|
|
|
|
|
|
|
|
var netScript = parentGo.GetComponentOrThrow<NetworkedVehicleDetachedPart>();
|
2020-07-05 20:33:34 +00:00
|
|
|
|
netScript.InitializeOnServer(vehicleNetId, vehicleModelId, vehicleColors, frame.gameObject.name, mass, rigidBody);
|
2020-07-01 21:36:01 +00:00
|
|
|
|
|
|
|
|
|
NetManager.Spawn(parentGo);
|
|
|
|
|
}
|
2020-06-20 15:56:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 16:52:23 +00:00
|
|
|
|
public static void AssignExplosionSound(GameObject explosionGo)
|
2020-06-29 17:08:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (null == ExplosionSound)
|
|
|
|
|
ExplosionSound = AudioManager.CreateAudioClipFromSfx("GENRL", 45, 1);
|
|
|
|
|
|
|
|
|
|
var audioSource = explosionGo.GetComponentOrThrow<AudioSource>();
|
|
|
|
|
audioSource.clip = ExplosionSound;
|
|
|
|
|
audioSource.Play();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 20:40:51 +00:00
|
|
|
|
void OnDrawGizmosSelected()
|
|
|
|
|
{
|
|
|
|
|
// draw sphere indicating explosion damage radius
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
Gizmos.DrawWireSphere(this.transform.position, VehicleManager.Instance.explosionDamageRadius);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 16:34:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|