using CinemaDirector.Helpers; using System.Collections.Generic; using UnityEngine; namespace CinemaDirector { /// /// An event to toggle the gravity of a given rigidbody. /// [CutsceneItemAttribute("Physics", "Toggle Gravity", CutsceneItemGenre.ActorItem)] public class ToggleGravityEvent : CinemaActorEvent, IRevertable { // Options for reverting in editor. [SerializeField] private RevertMode editorRevertMode = RevertMode.Revert; // Options for reverting during runtime. [SerializeField] private RevertMode runtimeRevertMode = RevertMode.Revert; /// /// Cache the state of all actors related to this event. /// /// public RevertInfo[] CacheState() { List actors = new List(GetActors()); List reverts = new List(); foreach (Transform go in actors) { if (go != null) { Rigidbody rb = go.GetComponent(); if (rb != null) { reverts.Add(new RevertInfo(this, rb, "useGravity", rb.useGravity)); } } } return reverts.ToArray(); } /// /// Toggle gravity for the given actor's rigidbody component. /// /// The actor with a ridigbody to toggle gravity for. public override void Trigger(GameObject actor) { Rigidbody affectedObjectRigidBody = actor.GetComponent(); if (affectedObjectRigidBody != null) { affectedObjectRigidBody.useGravity = !affectedObjectRigidBody.useGravity; } } /// /// Reverse the toggle of gravity for the given actor's rigidbody component. /// /// The actor with a ridigbody to toggle gravity for. public override void Reverse(GameObject actor) { Trigger(actor); } /// /// Option for choosing when this Event will Revert to initial state in Editor. /// public RevertMode EditorRevertMode { get { return editorRevertMode; } set { editorRevertMode = value; } } /// /// Option for choosing when this Event will Revert to initial state in Runtime. /// public RevertMode RuntimeRevertMode { get { return runtimeRevertMode; } set { runtimeRevertMode = value; } } } }