Advertisement
Guest User

Untitled

a guest
Jan 17th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.47 KB | None | 0 0
  1.  
  2. -- This makes sure the zombie is in our new state
  3. function ZombieUpdate(zombie)
  4.  
  5.     -- check if the zombie's current state is lua state
  6.     if zombie:getCurrentState() ~= LuaState:instance() then
  7.         -- if not, then set it to that state. Zombie will therefore not be processing wander / pathfind code any more and
  8.         -- we can override the behaviour.
  9.         zombie:changeState(LuaState:instance());
  10.         -- lock the zombie's state machine so the game code can't change it.
  11.         zombie:setLockStates(true);
  12.     end
  13.    
  14.     local data = zombie:getModData();
  15.     -- If we have no lua state selected then set the default to spin...
  16.     if data.state == nil then
  17.         data.state = "WalkInCircles";
  18.         data.spinTimer = ZombRand(30); -- set timer randomly so zombies are all offset.
  19.     end
  20. end
  21.  
  22. -- Function to make zombie turn anticlockwise one compass direction per second, and walk forward.
  23. function DoWalkInCircles(zombie, data)
  24.    
  25.     data.spinTimer = data.spinTimer - 1;
  26.    
  27.     -- if timer ticks down to zero (one second, non frame comp)
  28.     if data.spinTimer <= 0 then
  29.         data.spinTimer = 30;       
  30.         local dirIndex = zombie:getDir():index(); -- get the # index for the current direction.    
  31.         zombie:setDir(dirIndex+1); -- set the current direction to the index + 1 (N -> NW, E -> NE etc)
  32.     end
  33.  
  34.     local dirToMove = zombie:getVectorFromDirection();
  35.     dirToMove:setLength(zombie:getPathSpeed());
  36.     zombie:Move(dirToMove);
  37.  
  38. end
  39.  
  40. -- Called once per frame when zombie is in LuaState. Here we can call the relevant behaviour.
  41. function AIStateExecute(zombie)
  42.     local data = zombie:getModData();
  43.  
  44.     -- Check all possible states and call relevant function to deal with them.
  45.     if data.state == "WalkInCircles" then
  46.         DoWalkInCircles(zombie, data);
  47.     elseif data.state == "SomethingElse" then
  48.         --DoZombieSomethingElse(zombie);
  49.     end
  50.  
  51. end
  52.  
  53. -- Since we're locking the state to force the zombie to stay in our state at all times,
  54. -- we need to undo this if the zombie is knocked back or killed.
  55. -- Here we check what state the zombie is in and unlock the state machine if we need to.
  56. function ZombieStateChange(owner, newState, oldState)
  57.    
  58.     if newState == StaggerBackDieState:instance() or newState == StaggerBackState:instance() or newState == DieState:instance() or newState == BurntToDeath:instance() then
  59.         if owner:getObjectName() == "Zombie" then
  60.             owner:setLockStates(false);    
  61.         end
  62.     end
  63.  
  64. end
  65.  
  66.  
  67. Events.OnAIStateChange.Add(ZombieStateChange);
  68. Events.OnZombieUpdate.Add(ZombieUpdate);
  69. Events.OnAIStateExecute.Add(AIStateExecute);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement