-- This makes sure the zombie is in our new state
function ZombieUpdate(zombie)
-- check if the zombie's current state is lua state
if zombie:getCurrentState() ~= LuaState:instance() then
-- if not, then set it to that state. Zombie will therefore not be processing wander / pathfind code any more and
-- we can override the behaviour.
zombie:changeState(LuaState:instance());
-- lock the zombie's state machine so the game code can't change it.
zombie:setLockStates(true);
end
local data = zombie:getModData();
-- If we have no lua state selected then set the default to spin...
if data.state == nil then
data.state = "WalkInCircles";
data.spinTimer = ZombRand(30); -- set timer randomly so zombies are all offset.
end
end
-- Function to make zombie turn anticlockwise one compass direction per second, and walk forward.
function DoWalkInCircles(zombie, data)
data.spinTimer = data.spinTimer - 1;
-- if timer ticks down to zero (one second, non frame comp)
if data.spinTimer <= 0 then
data.spinTimer = 30;
local dirIndex = zombie:getDir():index(); -- get the # index for the current direction.
zombie:setDir(dirIndex+1); -- set the current direction to the index + 1 (N -> NW, E -> NE etc)
end
local dirToMove = zombie:getVectorFromDirection();
dirToMove:setLength(zombie:getPathSpeed());
zombie:Move(dirToMove);
end
-- Called once per frame when zombie is in LuaState. Here we can call the relevant behaviour.
function AIStateExecute(zombie)
local data = zombie:getModData();
-- Check all possible states and call relevant function to deal with them.
if data.state == "WalkInCircles" then
DoWalkInCircles(zombie, data);
elseif data.state == "SomethingElse" then
--DoZombieSomethingElse(zombie);
end
end
-- Since we're locking the state to force the zombie to stay in our state at all times,
-- we need to undo this if the zombie is knocked back or killed.
-- Here we check what state the zombie is in and unlock the state machine if we need to.
function ZombieStateChange(owner, newState, oldState)
if newState == StaggerBackDieState:instance() or newState == StaggerBackState:instance() or newState == DieState:instance() or newState == BurntToDeath:instance() then
if owner:getObjectName() == "Zombie" then
owner:setLockStates(false);
end
end
end
Events.OnAIStateChange.Add(ZombieStateChange);
Events.OnZombieUpdate.Add(ZombieUpdate);
Events.OnAIStateExecute.Add(AIStateExecute);