Game Development Reference
In-Depth Information
'
trigonometry than AI, but if you
re curious, they all exist in the Game Coding Com-
plete source code at Dev\Assets\Scripts\TeapotStates.lua.
The transitional logic is all encapsulated in the teapot brain, which is owned by the
state machine. The interface for the teapot brain is as follows:
TeapotBrain = class(nil,
{
_teapot = nil,
});
function TeapotBrain:Init()
return true;
end
function TeapotBrain:Think()
error(
Calling unimplemented base class version of TeapotBrain.Think()
);
end
This interface is extremely simple because it just defines an Init() function and a
Think() function. Init() gives the brain a chance to do some initialization. Think()
is called when a new decision needs to be made. It goes through whatever decision-
making processes it uses and returns the most appropriate state. Here ' sahard-coded
brainthatimplementsthetransitionallogicinFigure18.1:
HardCodedBrain = class(TeapotBrain,
{
--
});
function HardCodedBrain:Think()
local playerPos = Vec3:Create(g_actorMgr:GetPlayer():GetPos());
local pos = Vec3:Create(self._teapot:GetPos());
local diff = playerPos - pos;
-- player close
if (diff:Length() < 20) then
-- hit points low, run
if (self._teapot.hitPoints <= 1) then
return RunAwayState;
-- hit points not low, attack
else
return AttackState;
end
-- player not close, resume patrol
Search WWH ::




Custom Search