Game Development Reference
In-Depth Information
function IsHealthLowNode:Decide()
local hitPointPercentageLeft =
self._teapot.hitPoints / self._teapot.maxHitPoints;
if (hitPointPercentageLeft <= self._lowValuePercentage) then
return self._trueNode:Decide();
else
return self._falseNode:Decide();
end
end
The first node is the IsObjectCloseNode class, and it checks to see if the object ID
stored in _testObjId is
defined by the _closeDistance variable. This
replaces the hard-coded check to see if the player is close with a more generic ver-
sion. This is a great example of how you can parameterize nodes to make them more
reusable. For example, this node could be used to detect how close a health pack is
with no modifications.
The Decide() function is pretty straightforward and very similar to the hard-coded
block you saw earlier. If the object is valid and is found to be within the appropriate
distance, the true node
close,
s
Decide() function is called. This steps down a level in the tree and starts the pro-
cess again. IsHealthLowNode works in the same way. The only difference is the
actual logic.
'
s Decide() function is called. Otherwise, the false node
'
Use Percentages Instead of Absolutes
Notice the usage of the _lowValuePercentage variable. You might be
wondering why I
m using a percentage here instead of an absolute value.
Using a percentage allows the max hit points for
'
the teapot
to change
without having to update this logic at all.
It will always consider anything
less than 34% of the max hit points to be
low.
If I used an absolute value,
I
'
d have to update it whenever the max hit points of the teapot changed.
The only thing left is the brain itself:
DecisionTreeBrain = class(TeapotBrain,
{
_root = nil,
});
function DecisionTreeBrain:Init()
self:_BuildDecisionTree();
return true;
end
 
Search WWH ::




Custom Search