public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) { var playable = ScriptPlayable <AICommandBehaviour> .Create(graph, template); AICommandBehaviour clone = playable.GetBehaviour(); clone.commandType = commandType; clone.targetPosition = targetPosition; clone.targetUnit = targetUnit.Resolve(graph.GetResolver()); return(playable); }
//Happens every frame in Edit mode. //Uses transform.position of the units to approximate what they would do in Play mode with the NavMeshAgent private void ProcessEditModeFrame(Playable playable) { previousInputFinalPositions = defaultPositions; int inputCount = playable.GetInputCount(); int unitCount = trackBinding.units.Count; for (int i = 0; i < inputCount; i++) { float inputWeight = playable.GetInputWeight(i); ScriptPlayable <AICommandBehaviour> inputPlayable = (ScriptPlayable <AICommandBehaviour>)playable.GetInput(i); AICommandBehaviour input = inputPlayable.GetBehaviour(); //Some actionTypes have special needs switch (input.commandType) { case AICommand.CommandType.Die: case AICommand.CommandType.Stop: //Do nothing if it's a Die or Stop action continue; //Will skip to the next input clip in the for loop above case AICommand.CommandType.AttackTarget: //Force the finalPosition to the attack target in case of an Attack action if (input.targetUnit != null) { input.targetPosition = input.targetUnit.transform.position; } break; } //Create an array of final positions for the entire Platoon finalPositions = trackBinding.GetFormationPositions(input.targetPosition); if (inputWeight > 0f) { double progress = inputPlayable.GetTime() / inputPlayable.GetDuration(); newPositions = new Vector3[unitCount]; for (int j = 0; j < unitCount; j++) { newPositions[j] = Vector3.Lerp(previousInputFinalPositions[j], finalPositions[j], (float)progress); } trackBinding.SetPositions(newPositions); continue; } else { previousInputFinalPositions = finalPositions; //cached to act as initial position for the next input } } }
//Happens in Play mode //Uses the NavMeshAgent to control the units, delegating their movement and animations to the AI private void ProcessPlayModeFrame(Playable playable) { int inputCount = playable.GetInputCount(); for (int i = 0; i < inputCount; i++) { float inputWeight = playable.GetInputWeight(i); ScriptPlayable <AICommandBehaviour> inputPlayable = (ScriptPlayable <AICommandBehaviour>)playable.GetInput(i); AICommandBehaviour input = inputPlayable.GetBehaviour(); //Make the Unit script execute the command if (inputWeight > 0f) { if (!input.commandExecuted) { AICommand c = new AICommand(input.commandType, input.targetPosition, input.targetUnit); trackBinding.ExecuteCommand(c); input.commandExecuted = true; //this prevents the command to be executed every frame of this clip } } } }