Advertisement
mvaganov

FungusTrigger.cs

Oct 15th, 2016
2,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.06 KB | None | 0 0
  1. // http://pastebin.com/UPzuCUM4
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class FungusTrigger : MonoBehaviour {
  7.  
  8.     [System.Serializable]
  9.     public class Broadcast {
  10.         public string messageToSend = "";
  11.         public void Trigger() { Fungus.Flowchart.BroadcastFungusMessage (messageToSend); }
  12.     }
  13.  
  14.     [Tooltip("broadcast a message to Fungus, which can start Fungus scripts")]
  15.     public Broadcast[] broadcast;
  16.  
  17.     public static Fungus.Variable FindVariable(Fungus.Flowchart flowchart, string variableName) {
  18.         List<Fungus.Variable> vars = flowchart.Variables;
  19.         for (int i = 0; i < vars.Count; ++i) {
  20.             if (vars [i].Key == variableName) { return vars [i]; }
  21.         }
  22.         return null;
  23.     }
  24.  
  25.     public static Fungus.Flowchart FindFlowchartWithVariable(string variableName, out Fungus.Variable foundVariable) {
  26.         Fungus.Flowchart flowchart = null;
  27.         Fungus.Flowchart[] flows = GameObject.FindObjectsOfType<Fungus.Flowchart> ();
  28.         foundVariable = null;
  29.         for (var f=0;f<flows.Length && foundVariable == null;++f) {
  30.             flowchart = flows [f]; // check to see if the variable in question is in this flowchart
  31.             foundVariable = FindVariable(flowchart, variableName);
  32.         }
  33.         if (!flowchart) { Debug.LogError ("Could not find flowchart with variable \""+variableName+"\""); }
  34.         return flowchart;
  35.     }
  36.  
  37.     [System.Serializable]
  38.     public class SetStringVariable {
  39.         public string variableName, variableValue;
  40.         [Tooltip("will automagically find a Fungus flowchart if none is given")]
  41.         public Fungus.Flowchart flowchart;
  42.         public void Trigger() {
  43.             Fungus.Variable v = null;
  44.             if (!flowchart) { flowchart = FindFlowchartWithVariable (variableName, out v); } else { v = FindVariable (flowchart, variableName); }
  45.             if (v.GetType() == typeof(Fungus.StringVariable)) { flowchart.SetStringVariable (variableName, variableValue); }
  46.             if (v.GetType() == typeof(Fungus.FloatVariable)) { flowchart.SetFloatVariable (variableName, System.Single.Parse(variableValue)); }
  47.             if (v.GetType() == typeof(Fungus.IntegerVariable)) { flowchart.SetIntegerVariable (variableName, System.Int32.Parse(variableValue)); }
  48.             if (v.GetType() == typeof(Fungus.BooleanVariable)) { flowchart.SetBooleanVariable (variableName, (variableValue != "" && variableValue != "0" && variableValue.ToLower() != "false")); }
  49.         }
  50.     }
  51.     public SetStringVariable[] setVariable;
  52.  
  53.     [System.Serializable]
  54.     public class AddToVariable {
  55.         public string variableName, variableValue;
  56.         [Tooltip("will automagically find a Fungus flowchart if none is given")]
  57.         public Fungus.Flowchart flowchart = null;
  58.         public void Trigger() {
  59.             Fungus.Variable v = null;
  60.             if (!flowchart) { flowchart = FindFlowchartWithVariable (variableName, out v); } else { v = FindVariable (flowchart, variableName); }
  61.             if (v.GetType() == typeof(Fungus.StringVariable)) { flowchart.SetStringVariable (variableName, flowchart.GetStringVariable(variableName)+variableValue); }
  62.             if (v.GetType() == typeof(Fungus.FloatVariable)) { flowchart.SetFloatVariable (variableName, flowchart.GetFloatVariable(variableName)+System.Single.Parse(variableValue)); }
  63.             if (v.GetType() == typeof(Fungus.IntegerVariable)) { flowchart.SetIntegerVariable (variableName, flowchart.GetIntegerVariable(variableName)+System.Int32.Parse(variableValue)); }
  64.             if (v.GetType() == typeof(Fungus.BooleanVariable)) { flowchart.SetBooleanVariable (variableName, !flowchart.GetBooleanVariable(variableName)); }
  65.         }
  66.     }
  67.     public AddToVariable[] addToVariable;
  68.  
  69.     [Tooltip("Only triggered by an object with one of these tags, or any object if left blank")]
  70.     public string[] tagsThatCanTrigger;
  71.  
  72.     [Tooltip("Disables after triggering once")]
  73.     public bool onlyTriggerOnce = false;
  74.  
  75.     /// <summary>Trigger this instance</summary>
  76.     public void Trigger() {
  77.         if (this.enabled) {
  78.             if (setVariable!=null)  { System.Array.ForEach (setVariable,   (i) => { i.Trigger(); }); }
  79.             if (addToVariable!=null){ System.Array.ForEach (addToVariable, (i) => { i.Trigger(); }); }
  80.             if (broadcast!=null)    { System.Array.ForEach (broadcast,     (i) => { i.Trigger(); }); }
  81.             if (onlyTriggerOnce) { this.enabled = false; }
  82.         }
  83.     }
  84.  
  85.     private bool IsTaggedCorrectly(GameObject go) {
  86.         return tagsThatCanTrigger.Length == 0 || System.Array.IndexOf(tagsThatCanTrigger, go.tag) >= 0;
  87.     }
  88.  
  89.     void OnTriggerEnter (Collider other) {
  90.         if(IsTaggedCorrectly(other.gameObject)) { Trigger(); }
  91.     }
  92.  
  93.     void OnCollisionEnter(Collision collision) {
  94.         if(IsTaggedCorrectly(collision.gameObject)) { Trigger(); }
  95.     }
  96.  
  97.     void OnControllerColliderHit(ControllerColliderHit hit) {
  98.         if(IsTaggedCorrectly(hit.collider.gameObject)) { Trigger(); }
  99.     }
  100.  
  101.     /// <summary>list of rigid bodies stopped in time</summary>
  102.     private static Rigidbody[] bodies_s = null;
  103.     private static List<MonoBehaviour> temporarilyDisabled_s = null;
  104.  
  105.     /// <summary>keep track of an object's physics info</summary>
  106.     private struct Freeze {
  107.         public Vector3 v;
  108.         public Freeze(Rigidbody rb){
  109.             v=rb.velocity;
  110.             if(!rb.GetComponent<CharacterController>())
  111.                 rb.isKinematic=true;
  112.         }
  113.         public void Unfreeze(Rigidbody rb) {
  114.             rb.velocity=v;
  115.             if(!rb.GetComponent<CharacterController>())
  116.                 rb.isKinematic=false;
  117.         }
  118.     }
  119.  
  120.     /// <summary>list of velocities, saved before the objects are halted.</summary>
  121.     private static Freeze[] snapshot_s = null;
  122.  
  123.     /// <returns><c>true</c> if the physics is frozen; otherwise, <c>false</c>.</returns>
  124.     public static bool IsStopped() { return snapshot_s != null; }
  125.  
  126.     public void TogglePause() { TogglePause_s (); }
  127.     public void PauseEverything() { PauseEverything_s (); }
  128.     public void UnpauseEverything() { UnpauseEverything_s (); }
  129.  
  130.     public static void PauseEverything_s() {
  131.         if (IsStopped ()) return;
  132.         bodies_s = GameObject.FindObjectsOfType<Rigidbody> ();
  133.         snapshot_s = new Freeze[bodies_s.Length];
  134.         for (int i = 0; i < bodies_s.Length; ++i) {
  135.             snapshot_s [i] = new Freeze(bodies_s [i]);
  136.         }
  137.         // disable all active components on CharacterControllers
  138.         CharacterController[] charControllers_s = GameObject.FindObjectsOfType<CharacterController> ();
  139.         temporarilyDisabled_s = new List<MonoBehaviour> ();
  140.         for (int i = 0; i < charControllers_s.Length; ++i) {
  141.             if (charControllers_s [i].enabled) {
  142.                 MonoBehaviour[] list = charControllers_s [i].GetComponents<MonoBehaviour> ();
  143.                 for(int c=0;c<list.Length;++c) {
  144.                     if (list [c].enabled) {
  145.                         list [c].enabled = false;
  146.                         temporarilyDisabled_s.Add (list [c]);
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.     }
  152.  
  153.     public static void UnpauseEverything_s() {
  154.         if (!IsStopped ()) return;
  155.         for (int i = 0; i < bodies_s.Length; ++i) {
  156.             if (bodies_s [i] != null) {
  157.                 snapshot_s [i].Unfreeze(bodies_s [i]);
  158.             }
  159.         }
  160.         // re-enable components on CharacterControllers
  161.         for (int i = 0; i < temporarilyDisabled_s.Count; ++i) {
  162.             temporarilyDisabled_s [i].enabled = true;
  163.         }
  164.         snapshot_s = null;
  165.         bodies_s = null;
  166.         temporarilyDisabled_s = null;
  167.     }
  168.  
  169.     /// <summary>Toggles the rigibdody physics and character controllers.</summary>
  170.     public static void TogglePause_s() {
  171.         if(IsStopped()) {
  172.             UnpauseEverything_s ();
  173.         } else {
  174.             PauseEverything_s ();
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement