/// <summary> /// Plays the CinematicAnimation, if it isn't paused /// </summary> /// <param name="time_delta">Amount of time to progress the animation</param> public void Play(float time_delta) { if (Paused) { return; } if (Looping && RemainingActions.Count == 0 && AllActions.Count > 0) { Reset(); } for (int i = 0; i < RemainingActions.Count; i++) { CinematicAction ca = RemainingActions[i]; // are we doing this now!? if (ca.startTime <= CurrentTime) { // yes! PerformAction(ca, time_delta); // remove this action? if (ca.endTime <= CurrentTime) { if (ca.relativeStartArgument) { ca.argument0 = null; } RemainingActions.RemoveAt(i); i--; } } } CurrentTime += time_delta; }
/// <summary> /// Sets the animation back to the start for replaying /// </summary> public void Reset() { CurrentTime = 0f; RemainingActions.Clear(); for (int i = 0; i < AllActions.Count; i++) { CinematicAction ca = AllActions[i]; if (ca.relativeStartArgument) { ca.argument0 = null; } RemainingActions.Add(ca); } }
/// <summary> /// Add a method to be run at the given time. ActionInfo is constructed, set and passed as an argument to the method /// </summary> /// <param name="method">Action to be executed</param> /// <param name="startTime">When to execute the action</param> /// <param name="endTime">Keep running the action until this time, default is 0 which means always just run this once</param> public void AddMethod(Action <ActionInfo> method, float startTime, float endTime = 0f) { CinematicAction ca = new CinematicAction() { Type = ACTION_TYPE.DELEGATE, method = method, startTime = startTime, endTime = endTime }; AllActions.Add(ca); if (CurrentTime <= startTime) { RemainingActions.Add(ca); } }
/// <summary> /// Make an action for this CinematicAnimation at the given time. /// </summary> /// <param name="type">What kind of action is this?</param> /// <param name="target">The TransformComponent to act on</param> /// <param name="endArgument">Primary argument for the action, like Vector3 for SET_POSITION</param> /// <param name="startTime">What time to do this action?</param> /// <param name="startArgument">Where should this action start from? null if use whatever we are currently at</param> /// <param name="endTime">When to end this action?</param> public void AddAction(ACTION_TYPE type, TransformComponent target, object endArgument, float startTime, object startArgument = null, float endTime = 0f) { CinematicAction ca = new CinematicAction() { Type = type, target = target, argument0 = startArgument, argument1 = endArgument, startTime = startTime, endTime = endTime, relativeStartArgument = startArgument == null }; AllActions.Add(ca); if (CurrentTime <= startTime) { RemainingActions.Add(ca); } }
private void PerformAction(CinematicAction ca, float delta_time) { // wait, are we just an action to call here? float totalTimeOfAction = ca.endTime - ca.startTime; float positionInAction = totalTimeOfAction > 0f ? (CurrentTime - ca.startTime) / totalTimeOfAction : 1f; if (positionInAction > 1f) { positionInAction = 1f; } switch (ca.Type) { case ACTION_TYPE.DELEGATE: tempArguments.AnimationTimeElapsed = delta_time; tempArguments.PercentProgress = positionInAction; ca.method(tempArguments); break; case ACTION_TYPE.SET_POSITION: ca.target.Position = (Vector3)ca.argument1; break; case ACTION_TYPE.SET_ROTATION: ca.target.Rotation = (Quaternion)ca.argument1; break; case ACTION_TYPE.SET_SCALE: ca.target.Scale = (Vector3)ca.argument1; break; case ACTION_TYPE.LINEAR_MOVE: if (ca.argument0 == null) { ca.argument0 = ca.target.Position; } ca.target.Position = Vector3.Lerp((Vector3)ca.argument0, (Vector3)ca.argument1, positionInAction); break; case ACTION_TYPE.LINEAR_ROTATE: if (ca.argument0 == null) { ca.argument0 = ca.target.Rotation; } ca.target.Rotation = Quaternion.Lerp((Quaternion)ca.argument0, (Quaternion)ca.argument1, positionInAction); break; case ACTION_TYPE.LINEAR_SCALE: if (ca.argument0 == null) { ca.argument0 = ca.target.Scale; } ca.target.Scale = Vector3.Lerp((Vector3)ca.argument0, (Vector3)ca.argument1, positionInAction); break; case ACTION_TYPE.APPLY_VELOCITY: ca.target.Position += (Vector3)(ca.argument1) * delta_time; break; case ACTION_TYPE.APPLY_SPIN: ca.target.Rotation *= Quaternion.Lerp(Quaternion.Identity, (Quaternion)ca.argument1, delta_time); break; case ACTION_TYPE.SMOOTH_MOVE: if (ca.argument0 == null) { ca.argument0 = ca.target.Position; } ca.target.Position = positionInAction < 1f ? Vector3.Lerp((Vector3)ca.argument0, (Vector3)ca.argument1, Sigmoid(positionInAction * 12.0 - 6.0)) : (Vector3)ca.argument1; break; case ACTION_TYPE.SMOOTH_ROTATE: if (ca.argument0 == null) { ca.argument0 = ca.target.Rotation; } ca.target.Rotation = positionInAction < 1f ? Quaternion.Lerp((Quaternion)ca.argument0, (Quaternion)ca.argument1, Sigmoid(positionInAction * 12.0 - 6.0)) : (Quaternion)ca.argument1; break; case ACTION_TYPE.SMOOTH_SCALE: if (ca.argument0 == null) { ca.argument0 = ca.target.Scale; } ca.target.Scale = positionInAction < 1f ? Vector3.Lerp((Vector3)ca.argument0, (Vector3)ca.argument1, Sigmoid(positionInAction * 12.0 - 6.0)) : (Vector3)ca.argument1; break; } }