BloodAndGore/Assets/Scripts/Effects/Explodable.cs

85 lines
3.4 KiB
C#
Raw Permalink Normal View History

2019-08-14 22:20:54 +02:00
using UnityEngine;
2019-08-14 16:17:48 +02:00
using Saltosion.OneWeapon.Utils;
using Saltosion.OneWeapon.Player;
2019-08-04 00:48:41 +02:00
2019-08-14 16:17:48 +02:00
namespace Saltosion.OneWeapon.Effects {
2019-08-04 00:48:41 +02:00
public class Explodable : MonoBehaviour {
public GameObject[] BodypartPrefabs;
public bool DebugExplode = false;
2019-08-14 22:20:54 +02:00
public int ParticlesPerBodypart = 70;
public float BodypartMinCount = 1;
public float BodypartMaxCount = 1;
2019-08-14 23:27:14 +02:00
public DebrisType DebrisType;
public bool IsFurniture = true;
2019-08-04 00:48:41 +02:00
private CameraFX CameraFX;
private PlayerFun PlayerFun;
2019-08-22 18:59:36 +02:00
private Transform ExplodableContainer;
private bool DestroyedAlready = false;
2019-08-04 00:48:41 +02:00
private void Start() {
foreach (GameObject Obj in BodypartPrefabs) {
Rigidbody2D Body = Obj.GetComponent<Rigidbody2D>();
if (Body == null) {
Debug.LogError($"'{Obj.name}' is an explodable bodypart, and does not have a Rigidbody2D!\n\n" +
"Therefore it cannot be exploded. Please provide one.\n" +
"Following is the GameObject that needs to get a Rigidbody2D.");
}
}
CameraFX = Camera.main.GetComponent<CameraFX>();
PlayerFun = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerFun>();
2019-08-22 18:59:36 +02:00
ExplodableContainer = GameObject.FindGameObjectWithTag("BodypartContainer").transform;
2019-08-04 00:48:41 +02:00
}
private void Update() {
if (DebugExplode) {
DebugExplode = false;
Explode(false, false);
2019-08-04 00:48:41 +02:00
}
}
public void Explode(bool destroyGameObject, bool causedByPlayerAttack) {
2019-08-22 18:59:36 +02:00
// Destroying the object during explosion implies that it can explode only once,
// but Destroy() doesn't happen instantly, so here's a guard for that.
if (destroyGameObject && DestroyedAlready) {
return;
}
int Count = (int)(BodypartMinCount + (BodypartMaxCount - BodypartMinCount) * Random.value);
2019-08-04 20:09:00 +02:00
for (int i = 0; i < Count; i++) {
2019-08-14 23:27:14 +02:00
GameObject Obj = BodypartPrefabs[i % BodypartPrefabs.Length];
2019-08-04 20:09:00 +02:00
2019-08-04 18:57:06 +02:00
float DirectionRadians = Random.value * Mathf.PI * 2.0f;
Vector2 Direction = new Vector2(Mathf.Cos(DirectionRadians), Mathf.Sin(DirectionRadians));
2019-08-14 23:27:14 +02:00
if (!Options.CensorGore || DebrisType != DebrisType.Blood) {
2019-08-22 18:59:36 +02:00
GameObject NewObj = Instantiate(Obj, transform.position, new Quaternion(), ExplodableContainer);
2019-08-04 01:45:32 +02:00
Rigidbody2D Bodypart = NewObj.GetComponent<Rigidbody2D>();
if (Bodypart == null) {
continue;
}
float Force = 0.5f + Random.value * 0.5f;
2019-08-04 18:57:06 +02:00
Bodypart.AddForce(Direction * Force, ForceMode2D.Impulse);
2019-08-04 01:45:32 +02:00
Bodypart.AddTorque((Random.value - 0.5f) * Force, ForceMode2D.Impulse);
2019-08-04 00:48:41 +02:00
}
2019-08-14 23:27:14 +02:00
DebrisLauncher.Splatter(DebrisType, transform.position + (Vector3)Direction * 0.5f, Vector2.zero, ParticlesPerBodypart, 50f, 360f);
2019-08-04 00:48:41 +02:00
}
2019-08-04 02:00:18 +02:00
if (causedByPlayerAttack) {
if (!IsFurniture) {
PlayerFun.GainKill();
}
PlayerFun.Explosion(IsFurniture);
}
CameraFX.ScreenShake(10);
2019-08-04 02:00:18 +02:00
if (destroyGameObject) {
Destroy(gameObject);
2019-08-22 18:59:36 +02:00
DestroyedAlready = true;
2019-08-04 02:00:18 +02:00
}
2019-08-04 00:48:41 +02:00
}
}
}