174 lines
4.1 KiB
C#
174 lines
4.1 KiB
C#
using System;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class Explosive : MonoBehaviour
|
|
{
|
|
public float range = 10f;
|
|
|
|
public ParticleSystem particleExplosionDefault;
|
|
|
|
public ParticleSystem particleExplosionWater;
|
|
|
|
public ParticleSystem particleHitWater;
|
|
|
|
public GameObject model;
|
|
|
|
public GameObject pin;
|
|
|
|
public GameObject explosionArea;
|
|
|
|
public bool showExplosionArea = true;
|
|
|
|
public string soundExplosion = "Explosion_01";
|
|
|
|
public string soundExplosionWater = "Explosion_Water_01";
|
|
|
|
public string soundRemovePin = "Explosive_Pin_01";
|
|
|
|
public string soundCollision = "Explosive_Collision_01";
|
|
|
|
public LayerMask fishLayer = -1;
|
|
|
|
public bool explodeOnCollision = true;
|
|
|
|
public bool explodeOnWaterCollision;
|
|
|
|
public float explodeTime = 5f;
|
|
|
|
public float waterDrag = 20f;
|
|
|
|
[HideInInspector]
|
|
public bool isThrown;
|
|
|
|
[HideInInspector]
|
|
public bool hasExploded;
|
|
|
|
[HideInInspector]
|
|
public bool hitWater;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody rigidbody;
|
|
|
|
public void Awake()
|
|
{
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
explosionArea.SetActive(false);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (hasExploded || !isThrown)
|
|
{
|
|
return;
|
|
}
|
|
if (explodeTime > 0f)
|
|
{
|
|
explodeTime -= Time.deltaTime;
|
|
if (explodeTime <= 0f)
|
|
{
|
|
explodeTime = 0f;
|
|
Explode();
|
|
}
|
|
}
|
|
if (!hitWater && base.transform.position.y <= 0f)
|
|
{
|
|
AudioController.Play("Explosive_HitWater_01", base.transform);
|
|
if (explodeOnWaterCollision)
|
|
{
|
|
Explode();
|
|
}
|
|
else
|
|
{
|
|
rigidbody.drag = waterDrag;
|
|
}
|
|
ParticleSystem particleSystem = UnityEngine.Object.Instantiate(particleHitWater);
|
|
particleSystem.transform.parent = base.transform;
|
|
particleSystem.transform.position = new Vector3(base.transform.position.x, 0f, base.transform.position.z);
|
|
particleSystem.transform.eulerAngles = new Vector3(-90f, 0f, 0f);
|
|
particleSystem.Play(true);
|
|
hitWater = true;
|
|
}
|
|
}
|
|
|
|
public void Throw()
|
|
{
|
|
rigidbody.isKinematic = false;
|
|
rigidbody.useGravity = true;
|
|
AudioController.Play("Explosive_Throw_01", base.transform);
|
|
isThrown = true;
|
|
}
|
|
|
|
public void RemovePin()
|
|
{
|
|
if ((bool)pin)
|
|
{
|
|
pin.SetActive(false);
|
|
AudioController.Play(soundRemovePin, base.transform);
|
|
}
|
|
}
|
|
|
|
public void OnCollisionEnter(Collision col)
|
|
{
|
|
if (explodeOnCollision)
|
|
{
|
|
Explode();
|
|
}
|
|
if (!hitWater)
|
|
{
|
|
AudioController.Play(soundCollision, base.transform);
|
|
}
|
|
Debug.Log("Explosive collide with: " + col.gameObject.name + " at pos: " + base.transform.position.y);
|
|
}
|
|
|
|
[Button]
|
|
public void Explode()
|
|
{
|
|
if (hasExploded)
|
|
{
|
|
return;
|
|
}
|
|
if (base.transform.position.y > 0f && (bool)particleExplosionDefault)
|
|
{
|
|
AudioController.Play(soundExplosion, base.transform);
|
|
}
|
|
else if (base.transform.position.y <= 0f && (bool)particleExplosionWater)
|
|
{
|
|
ParticleSystem particleSystem = UnityEngine.Object.Instantiate(particleExplosionWater);
|
|
particleSystem.transform.parent = base.transform;
|
|
particleSystem.transform.position = new Vector3(base.transform.position.x, 0f, base.transform.position.z);
|
|
particleSystem.transform.eulerAngles = new Vector3(-90f, 0f, 0f);
|
|
particleSystem.Play(true);
|
|
AudioController.Play(soundExplosionWater, base.transform);
|
|
AudioController.Play(soundExplosion, base.transform, 0.5f);
|
|
Collider[] array = Physics.OverlapSphere(base.transform.position, range, fishLayer);
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
if ((bool)array[i].GetComponent<Fish>())
|
|
{
|
|
array[i].GetComponent<Fish>().Kill();
|
|
}
|
|
}
|
|
if (showExplosionArea)
|
|
{
|
|
explosionArea.SetActive(true);
|
|
explosionArea.transform.localScale = new Vector3(range * 2f, range * 2f, range * 2f);
|
|
}
|
|
}
|
|
ParticleSystem particleSystem2 = UnityEngine.Object.Instantiate(particleExplosionDefault);
|
|
particleSystem2.transform.parent = base.transform;
|
|
particleSystem2.transform.localPosition = Vector3.zero;
|
|
particleSystem2.transform.localRotation = Quaternion.identity;
|
|
particleSystem2.Play(true);
|
|
model.SetActive(false);
|
|
rigidbody.useGravity = false;
|
|
rigidbody.isKinematic = true;
|
|
GetComponent<Collider>().enabled = false;
|
|
hasExploded = true;
|
|
LeanTween.delayedCall(8f, (Action)delegate
|
|
{
|
|
UnityEngine.Object.Destroy(base.gameObject);
|
|
});
|
|
}
|
|
}
|