Game Development Reference
In-Depth Information
Pursuit
Creating a pursuing agent is very similar to seeking, except that we predict the target posi-
tion of another moving agent. Start by creating a new
PursuingAgent
Lua script and
implement the basic
Agent_Cleanup, Agent_HandleEvent
,
Agent_Initialize
, and
Agent_Update
functions:
Create Lua file as follows:
src/my_sandbox/script/PursuingAgent.lua
As a pursuing agent needs to have an enemy to pursue, we create a persistent Lua variable
enemy
outside the scope of our
Initialize
and
Update
functions. During the initializ-
ation of the agent, we will request all agents that are currently within the sandbox and as-
sign the first agent to be our enemy.
A minor change we make to the pursuing agent compared to the seeking agent is to use an
agent's
PredictFuturePosition
function to calculate its future position. We pass in
the number of seconds we want to predict in order to create the pursuing agent's target posi-
tion.
We can even make our pursuing agents slower than their enemy while still being able to
catch up to a future predicted position when the enemy agent changes directions:
PursuingAgent.lua
:
require "AgentUtilities";
local enemy;
function Agent_Cleanup(agent)
end
function Agent_HandleEvent(agent, event)
end
function Agent_Initialize(agent)
AgentUtilities_CreateAgentRepresentation(
agent, agent:GetHeight(), agent:GetRadius());