SanAndreasUnity/Assets/Scripts/Behaviours/Projectile.cs

91 lines
2.6 KiB
C#
Raw Normal View History

2020-12-20 20:51:59 +00:00
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.Net;
using SanAndreasUnity.Utilities;
using UnityEngine;
using Object = UnityEngine.Object;
2020-12-20 20:53:55 +00:00
namespace SanAndreasUnity.Behaviours
2020-12-20 20:51:59 +00:00
{
2020-12-20 20:53:55 +00:00
public class Projectile : MonoBehaviour
{
public GameObject explosionPrefab;
public float explosionDamageAmount = 1000;
public float explosionDamageRadius = 5;
public LayerMask collisionLayerMask;
public float speed = 10;
public float lifeTime = 30;
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
private bool m_alreadyExploded = false;
private Rigidbody m_rigidBody;
private AudioSource m_audioSource;
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
public static Projectile Create(
GameObject prefab, Vector3 position, Quaternion rotation, AudioClip audioClip)
{
var go = Instantiate(prefab, position, rotation);
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
var projectile = go.GetComponentOrThrow<Projectile>();
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
if (audioClip != null)
{
projectile.m_audioSource.clip = audioClip;
projectile.m_audioSource.Play();
}
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
return projectile;
}
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
void Awake()
{
m_rigidBody = this.GetComponentOrThrow<Rigidbody>();
m_audioSource = this.GetComponentOrThrow<AudioSource>();
}
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
private void Start()
{
Destroy(this.gameObject, this.lifeTime);
m_rigidBody.velocity = this.transform.forward * this.speed;
}
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
private void OnCollisionEnter(Collision other)
{
if (m_alreadyExploded)
return;
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
if (((1 << other.gameObject.layer) & this.collisionLayerMask.value) == 0)
return;
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
m_alreadyExploded = true;
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
Object.Destroy(this.gameObject);
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
Vector3 contactPoint = other.contacts[0].point;
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
// inflict damage to nearby objects
Damageable.InflictDamageToObjectsInArea(
contactPoint,
this.explosionDamageRadius,
this.explosionDamageAmount,
VehicleManager.Instance.explosionDamageOverDistanceCurve,
DamageType.Explosion);
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
// create explosion - this includes effects, physics force, sound
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
GameObject explosionGo = Object.Instantiate(
this.explosionPrefab,
contactPoint,
this.transform.rotation);
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
if (NetStatus.IsServer)
NetManager.Spawn(explosionGo);
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
// assign explosion sound
F.RunExceptionSafe(() => Vehicle.AssignExplosionSound(explosionGo));
2020-12-20 20:51:59 +00:00
2020-12-20 20:53:55 +00:00
}
2020-12-20 20:51:59 +00:00
}
}