Game Development Reference
In-Depth Information
This was done so you could easily experiment with the other brains you saw in
Chapter 18. You can also create your own brain if you feel so inclined.
All teapots are controlled by a state machine where their current state defines their
behavior. The next two lines create that state machine and set the initial state to
PatrolState .
Next, there
s a check to see if the enemy processes have been created for periodically
healing, running AI, and allowing the state to run its update logic. If these processes
have been created, they are created here. Finally, the UI is updated since a new teapot
has just arrived.
'
Sending and Receiving Events
Events and processes are extremely important in Lua. Events are your main method
of communication to and from the C++ code. Without events, your scripts are deaf,
blind, and mute. Nothing special was added to make events work with Teapot Wars;
it uses the same mechanisms you saw in Chapter 11,
Game Event Management,
and Chapter 12. Let
s take a look at all works in Teapot Wars by examining the
UpdateUi() function you saw at the bottom of AddEnemy() . This function pro-
vides a good example of sending an event from the gameplay code out to C++.
'
function ActorManager:UpdateUi()
-- Build up the UI text string for the human view
local uiText = “”;
if (self._enemies
= nil) then
for id, teapot in pairs(self._enemies) do
uiText = uiText .. “Teapot ” .. id .. “ HP: ” ..
teapot.hitPoints ..
˜
\n
;
end
end
QueueEvent(EventType.EvtData_Gameplay_UI_Update, uiText);
end
This function loops through all enemies and builds a string that lists the actor
sID
along with its current hit points. After that, it calls the exported C++ QueueEvent()
function to send it out. This event will be caught by the TeapotWarsHumanView
class in C++. Here
'
'
s the delegate registered to listen for this event:
void TeapotWarsHumanView::GameplayUiUpdateDelegate(IEventDataPtr pEventData)
{
shared_ptr<EvtData_Gameplay_UI_Update> pCastEventData =
static_pointer_cast<EvtData_Gameplay_UI_Update>(pEventData);
 
 
Search WWH ::




Custom Search