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;
|
2019-08-16 20:17:46 +02:00
|
|
|
|
public float BodypartMinCount = 1;
|
|
|
|
|
public float BodypartMaxCount = 1;
|
2019-08-14 23:27:14 +02:00
|
|
|
|
public DebrisType DebrisType;
|
2019-08-04 00:48:41 +02:00
|
|
|
|
|
2019-08-04 20:09:52 +02:00
|
|
|
|
private CameraFX CameraFX;
|
|
|
|
|
private PlayerFun PlayerFun;
|
2019-08-22 18:59:36 +02:00
|
|
|
|
private Transform ExplodableContainer;
|
2019-08-04 20:09:52 +02:00
|
|
|
|
|
2019-08-18 21:27:37 +02:00
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-04 20:09:52 +02:00
|
|
|
|
|
|
|
|
|
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;
|
2019-08-04 02:00:18 +02:00
|
|
|
|
Explode(false);
|
2019-08-04 00:48:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 02:00:18 +02:00
|
|
|
|
public void Explode(bool destroyGameObject) {
|
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;
|
2019-08-18 21:27:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 20:17:46 +02:00
|
|
|
|
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
|
|
|
|
|
2019-08-04 20:09:52 +02:00
|
|
|
|
PlayerFun.Explosion(false);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|