BloodAndGore/Assets/Scripts/Effects/Explodable.cs

77 lines
3.0 KiB
C#
Raw Normal View History

2019-08-04 00:48:41 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-08-14 16:17:48 +02:00
using Saltosion.OneWeapon.Utils;
using Saltosion.OneWeapon.Player;
using Saltosion.OneWeapon.Enemies;
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-07 20:13:54 +02:00
[Tooltip("This only applies if this GameObject also has the Health component.")]
public bool KillOnZeroHealth = true;
2019-08-04 00:48:41 +02:00
private CameraFX CameraFX;
private PlayerFun PlayerFun;
2019-08-07 20:13:54 +02:00
private Health Health;
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-07 20:13:54 +02:00
Health = GetComponent<Health>();
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-07 20:13:54 +02:00
if (Health != null && Health.CurrentHealth <= 0 && KillOnZeroHealth) {
Explode(true);
}
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 01:45:32 +02:00
// No body parts flying in censored mode
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-04 01:45:32 +02:00
if (!Options.CensorGore) {
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-04 01:45:32 +02:00
// Blood is replaced by flowers though, no problem with that
BloodLauncher.Splatter(transform.position + (Vector3)Direction * 0.5f, Vector2.zero, 70, 50f, 360f);
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
}
}
}