using System; using UnityEngine; namespace Framework.Cinematics { /// Contains helper methods for instructions in cinematic tracks public static class CinematicHelper { /// Reverses a list of instructions /// Instruction list that will be reversed /// The reversed instructions /// /// This is a non-trivial operation since in addition to reversing the order of /// the instructions in the list, the targets have to be adjusted. /// public static Instruction[] Reverse(Instruction[] instructions) { Vector3[] positions = new Vector3[instructions.Length]; // Cache the intermediate positions because the position field is not used // in all instruction types. Vector3 position = Vector3.zero; for(int index = 0; index < instructions.Length; ++index) { if(IsMovingInstruction(instructions[index])) { position = instructions[index].Position; } positions[index] = position; } // Create a reversed list of instructions, using the next position as // the goal position until the last instruction, which is transform into a wait Instruction[] reversed = new Instruction[instructions.Length]; for(int index = 0; index < instructions.Length; ++index) { int reverseIndex = instructions.Length - index - 1; Instruction instruction = instructions[reverseIndex]; reversed[index] = new Instruction() { Action = instruction.Action, Speed = instruction.Speed, Duration = instruction.Duration, SpecialAnimationState = instruction.SpecialAnimationState, }; if(reverseIndex > 0) { reversed[index].Position = positions[reverseIndex - 1]; } else { reversed[index].Action = Action.Wait; // Can't turn without direction! } } return reversed; } /// /// Checks whether the specified instruction changes the agent's position /// /// Instruction that will be checked /// True if the instruction declares a movement instruction public static bool IsMovingInstruction(Instruction instruction) { return (instruction.Action == Action.Walk) || (instruction.Action == Action.Jump); } } } // namespace Framework.Cinematics