Game Development Reference
In-Depth Information
Lua to the Rescue
This type of system is a perfect place for a scripting language like Lua.
Features like tables and dynamic typing will save a huge amount of work
when compared to attempting the same implementation in C++. Most of the
examples you
ll see in this chapter are written in Lua using the systems
described in Chapter 12,
'
Scripting with Lua.
If you need a refresher, now
would be a good time.
'
Let
s take a look at a basic state machine implementation:
TeapotStateMachine = class(nil,
{
_teapot = nil,
_currentState = nil,
_brain = nil,
});
function TeapotStateMachine:Destroy()
self._currentState = nil;
self._brain = nil;
end
function TeapotStateMachine:SetState(newState)
if (self._currentState == nil or
not self._currentState:IsInstance(newState)) then
self:_InternalSetState(newState);
end
end
function TeapotStateMachine:ChooseBestState()
if (self._brain) then
local newState = self._brain:Think();
self:SetState(newState);
end
end
function TeapotStateMachine:Update(deltaMs)
if (self._currentState) then
self._currentState:Update(deltaMs);
end
end
function TeapotStateMachine:_InternalSetState(newState)
self._currentState = newState:Create({_teapot = self._teapot});
self._currentState:Init();
end
 
Search WWH ::




Custom Search