BloodAndGore/Assets/Scripts/Effects/Explodable.cs

77 lines
3.0 KiB
C#
Raw 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-14 22:20:54 +02:00
public enum ExplodableType {
Blood, Debris
}
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 ExplodableType Type;
2019-08-04 00:48:41 +02:00
private CameraFX CameraFX;
private PlayerFun PlayerFun;
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-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-04 20:09:00 +02:00
int Count = Mathf.Clamp((int)(BodypartPrefabs.Length * Random.value + 1), 1, BodypartPrefabs.Length);
for (int i = 0; i < Count; i++) {
GameObject Obj = BodypartPrefabs[i];
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 22:20:54 +02:00
if (!Options.CensorGore || Type != ExplodableType.Blood) {
2019-08-04 01:45:32 +02:00
GameObject NewObj = Instantiate(Obj, transform.position, new Quaternion(), null);
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 22:20:54 +02:00
switch (Type) {
case ExplodableType.Blood:
BloodLauncher.Splatter(transform.position + (Vector3)Direction * 0.5f, Vector2.zero, ParticlesPerBodypart, 50f, 360f);
break;
case ExplodableType.Debris:
BloodLauncher.DebrisExplode(transform.position + (Vector3)Direction * 0.5f, Vector2.zero, ParticlesPerBodypart, 60f, 360f);
break;
}
2019-08-04 00:48:41 +02:00
}
2019-08-04 02:00:18 +02:00
PlayerFun.Explosion(false);
CameraFX.ScreenShake(10);
2019-08-04 02:00:18 +02:00
if (destroyGameObject) {
Destroy(gameObject);
}
2019-08-04 00:48:41 +02:00
}
}
}