Files
FueraDeEscala/Assets/Game Kit Controller/Scripts/Combat System/Melee Combat System/Slice System/simpleSliceSystem.cs

251 lines
6.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class simpleSliceSystem : MonoBehaviour
{
2026-03-29 23:03:14 -07:00
[Header ("Main Settings")]
[Space]
2026-03-29 23:03:14 -07:00
public bool sliceEnabled = true;
2026-03-29 23:03:14 -07:00
public GameObject objectToSlice;
2026-03-29 23:03:14 -07:00
public GameObject alternatePrefab;
2026-03-29 23:03:14 -07:00
public Material infillMaterial;
2026-03-29 23:03:14 -07:00
public surfaceToSlice mainSurfaceToSlice;
2026-03-29 23:03:14 -07:00
public Transform [] severables = new Transform [0];
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Slice Ragdoll Settings")]
[Space]
2026-03-29 23:03:14 -07:00
public string prefabsPath = "";
2026-03-29 23:03:14 -07:00
public Material sliceMaterial;
2026-03-29 23:03:14 -07:00
public bool setTagOnSkeletonRigidbodies = true;
2026-03-29 23:03:14 -07:00
public string tagOnSkeletonRigidbodies = "box";
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Physics Settings")]
[Space]
2026-03-29 23:03:14 -07:00
public bool ignoreUpdateLastObjectSpeed;
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Other Settings")]
[Space]
2026-03-29 23:03:14 -07:00
public bool ignoreDestroyOriginalObject;
2026-03-29 23:03:14 -07:00
public UnityEvent eventsOnIgnoreDestroyOriginalObject;
2026-03-29 23:03:14 -07:00
[Space]
2026-03-29 23:03:14 -07:00
public bool setCustomIDOnSliceSpieces;
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Debug")]
[Space]
2026-03-29 23:03:14 -07:00
public bool showDebugPrint;
public bool sliceInitialized;
2026-03-29 23:03:14 -07:00
const string glyphs = "abcdefghijklmnopqrstuvwxyz0123456789";
2026-03-29 23:03:14 -07:00
string randomString = "";
2026-03-29 23:03:14 -07:00
public string getRandomString ()
{
return randomString;
}
2026-03-29 23:03:14 -07:00
public void setRandomString (string newValue)
{
if (randomString.Equals ("")) {
randomString = newValue;
}
}
2026-03-29 23:03:14 -07:00
public void setRandomString ()
{
if (setCustomIDOnSliceSpieces) {
if (randomString.Equals ("")) {
int charAmount = UnityEngine.Random.Range (10, 20); //set those to the minimum and maximum length of your string
2026-03-29 23:03:14 -07:00
for (int i = 0; i < charAmount; i++) {
randomString += glyphs [UnityEngine.Random.Range (0, glyphs.Length)];
}
}
}
}
2026-03-29 23:03:14 -07:00
public void destroyAllSlicedPiecesByRandomString ()
{
if (setCustomIDOnSliceSpieces && randomString != "") {
sliceSystemUtils.destroyAllSlicedPiecesByRandomString (randomString, gameObject);
}
}
2026-03-29 23:03:14 -07:00
void Awake ()
{
if (sliceEnabled) {
initializeValuesOnHackableComponent ();
}
}
2026-03-29 23:03:14 -07:00
public void initializeValuesOnHackableComponent ()
{
if (sliceInitialized) {
return;
}
2026-03-29 23:03:14 -07:00
sliceSystemUtils.initializeValuesOnHackableComponent (gameObject, this);
2026-03-29 23:03:14 -07:00
sliceInitialized = true;
}
2026-03-29 23:03:14 -07:00
public void setDestructionPending (bool state)
{
sliceSystemUtils.setDestructionPending (gameObject, state);
}
2026-03-29 23:03:14 -07:00
public void activateSlice (Collider objectCollider, Vector3 newNormalInWorldSpaceValue,
Vector3 positionInWorldSpace, Vector3 slicePosition, bool updateLastObjectSpeed, Vector3 lastSpeed)
{
if (!sliceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
Vector3 point = objectCollider.ClosestPointOnBounds (positionInWorldSpace);
2026-03-29 23:03:14 -07:00
if (mainSurfaceToSlice.useParticlesOnSlice) {
Quaternion particlesRotation = Quaternion.LookRotation (newNormalInWorldSpaceValue);
2026-03-29 23:03:14 -07:00
Instantiate (mainSurfaceToSlice.particlesOnSlicePrefab, slicePosition, particlesRotation);
}
2026-03-29 23:03:14 -07:00
sliceSystemUtils.sliceCharacter (objectToSlice, point, newNormalInWorldSpaceValue, updateLastObjectSpeed, lastSpeed);
2026-03-29 23:03:14 -07:00
if (showDebugPrint) {
print ("Activate Slice on Character " + objectToSlice.name);
}
}
2026-03-29 23:03:14 -07:00
public void searchBodyParts ()
{
if (objectToSlice == null) {
objectToSlice = gameObject;
}
2026-03-29 23:03:14 -07:00
List<GameObject> bodyPartsList = new List<GameObject> ();
2026-03-29 23:03:14 -07:00
Component [] childrens = objectToSlice.GetComponentsInChildren (typeof (Rigidbody));
2026-03-29 23:03:14 -07:00
int childrensLength = childrens.Length;
2026-03-29 23:03:14 -07:00
for (int i = 0; i < childrensLength; i++) {
2026-03-29 23:03:14 -07:00
Rigidbody child = childrens [i] as Rigidbody;
2026-03-29 23:03:14 -07:00
Collider currentCollider = child.GetComponent<Collider> ();
2026-03-29 23:03:14 -07:00
if (currentCollider != null && !currentCollider.isTrigger) {
2026-03-29 23:03:14 -07:00
bodyPartsList.Add (child.gameObject);
}
}
severables = new Transform [bodyPartsList.Count];
for (int i = 0; i < bodyPartsList.Count; i++) {
severables [i] = bodyPartsList [i].transform;
}
if (!Application.isPlaying) {
updateComponent ();
}
}
public void setTagOnBodyParts (string newTag)
{
for (int i = 0; i < severables.Length; i++) {
if (severables [i] != null) {
severables [i].tag = newTag;
}
}
}
2026-03-29 23:03:14 -07:00
public GameObject getMainAlternatePrefab ()
{
return alternatePrefab;
}
2026-03-29 23:03:14 -07:00
public Transform [] getSeverables ()
{
return severables;
}
2026-03-29 23:03:14 -07:00
public void setNewSeverablesList (GameObject newObject)
{
if (newObject != null) {
genericRagdollBuilder currentGenericRagdollBuilder = newObject.GetComponent<genericRagdollBuilder> ();
2026-03-29 23:03:14 -07:00
if (currentGenericRagdollBuilder != null) {
int bonesCount = currentGenericRagdollBuilder.bones.Count;
severables = new Transform [bonesCount];
for (int i = 0; i < bonesCount; i++) {
severables [i] = currentGenericRagdollBuilder.bones [i].anchor;
}
}
}
}
public void createRagdollPrefab ()
{
if (alternatePrefab == null) {
prefabsPath = pathInfoValues.getSliceObjectsPrefabsPath ();
alternatePrefab = GKC_Utils.createSliceRagdollPrefab (objectToSlice, prefabsPath, sliceMaterial, setTagOnSkeletonRigidbodies, tagOnSkeletonRigidbodies);
}
}
public void setSliceEnabledState (bool state)
{
sliceEnabled = state;
if (sliceEnabled) {
initializeValuesOnHackableComponent ();
}
}
public void setMainSurfaceToSlice (surfaceToSlice newSurface)
{
mainSurfaceToSlice = newSurface;
}
//EDITOR FUNCTIONS
public void setSliceEnabledStateFromEditor (bool state)
{
sliceEnabled = state;
updateComponent ();
}
void updateComponent ()
{
GKC_Utils.updateComponent (this);
GKC_Utils.updateDirtyScene ("Update Simple Slice System " + gameObject.name, gameObject);
}
}