-
Notifications
You must be signed in to change notification settings - Fork 362
Agent Behaviours: Player Input
We'll use the SimpleMove script from 'Agent Behaviours: Simple Move' as well as the agent set up in 'Agents Tutorial' to implement player input.
ActiveAbility, an extension of Ability, has the capability of handling deterministic player input. Let's change SimpleMove to inherit from ActiveAbility.
public class SimpleMove : ActiveAbility
Now SimpleMove has access to virtual function OnExecute (Command com) and it will show up in Database/Abilities. RTSInterfacingHelper is hardcoded to work with Move and Scan so set ListenInputCode and InformationGather to the same as Move's.
Now when TutorialAgent is selected and the player right clicks, a command will be sent with a position.
Notice how Move processes the command:
protected override void OnExecute(Command com)
{
LastCommand = com;
if (com.ContainsData<Vector2d> ())
{
StartFormalMove(com.GetData<Vector2d>());
}
}
Command.GetData() accesses the position generated by RTSInterfacingHelper. Let's do something with it. Shift 'destination' out of the method so it can be persistent and set it based on the command's position.
Vector2d destination;
protected override void OnSimulate()
{
long speed = FixedMath.Create(5);
this.MoveTowards(destination, speed / LockstepManager.FrameRate);
}
protected override void OnExecute(Command com)
{
destination = com.GetData<Vector2d>();
}
Now that 'destination' is prone to get changed through the object's current life, let's add something to reset it in OnInitialize.
protected override void OnInitialize()
{
destination = Agent.Body.Position;
}
Play ExampleScene. Left click drag the TutorialAgents and right click to set their new destination.