Advertisement
Guest User

Untitled

a guest
May 25th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. -- MODULES REQUIRED...
  2.  
  3. -- input manager module which will be linked to the input handler object
  4. local user_input = require "modules.input_manager.lua"
  5.  
  6.  
  7. -- STATE MACHINE BEGINS...
  8.  
  9. local state_machine = {
  10.  
  11.   { -- 1
  12.     name = "standing",
  13.     init = function(self)
  14.       print("Entered STANDING state")
  15.       -- play standing animation, etc...
  16.     end,
  17.     update = function(self, dt)
  18.       -- no functionality here beyond changing state according to user input...
  19.       -- jump button begins jumping (with or without directional input)
  20.       if user_input.jump_btn == true then
  21.         set_state(self, "jumping")
  22.       -- input manager sets directional flag to true on left or right input,
  23.       -- which shifts state machine to walking state
  24.       elseif user_input.directional == true then
  25.         set_state(self, "walking")
  26.       end
  27.     end,
  28.     exit = function(self)
  29.       print("Leaving STANDING state")
  30.     end,
  31.   }, -- 1 STANDING
  32.  
  33.   { -- 2
  34.     name = "walking",
  35.     init = function(self)
  36.       print("Entered WALKING state")
  37.       -- set walking animation, start footsteps sound effect, etc...
  38.     end,
  39.     update = function(self, dt)
  40.       -- handle walking in either direction
  41.       if user_input.left == true then
  42.         -- adjust velocity/position, negative on the x-axis...
  43.       elseif user_input.right == true then
  44.         -- adjust velocity/position, positive on the x-axis...
  45.       elseif user_input.jump_btn == true then
  46.         set_state(self, "jumping") -- jump if btn pressed
  47.       else set_state(self, "standing") -- if no further directional input, switch back to standing state
  48.       end
  49.     end,
  50.     exit = function(self)
  51.       print("Leaving WALKING state")
  52.       -- stop playing footsteps sound effect, etc..
  53.     end,
  54.   }, -- 2 WALKING
  55.  
  56.   { -- 3
  57.     name = "jumping",
  58.     init = function(self)
  59.       print("Entered JUMPING state")
  60.       -- play jumping animation and start jump sound effect etc...
  61.       state_timer = 0 -- reset state timer to limit maximum jump height
  62.     end,
  63.     update = function(self, dt)
  64.       -- handle horizontal movement in the air...
  65.       if user_input.left == true then
  66.         -- adjust velocity/position, negative on the x-axis...
  67.       elseif user_input.right == true then
  68.         -- adjust velocity/position, positive on the x-axis...
  69.       end
  70.       -- check for collisions and adjust state accordingly... ie. to damaged falling state etc..
  71.       -- increment state_timer to record how long we have been jumping up
  72.       state_timer = state_timer + 1
  73.       -- continue gaining height if not reached max jump frames and jump btn still pressed
  74.       if state_timer <= self.max_jump_frames and user_input.jump_btn == true then
  75.         -- adjust velocity/position, positive on the y-axis..
  76.       else set_state(self, "falling") -- else, switch to falling state
  77.       end
  78.     end,
  79.     exit = function(self)
  80.       print("Leaving JUMPING state")
  81.     end,
  82.   }, -- 3 JUMPING
  83.  
  84.   { -- 4
  85.     name = "falling",
  86.     init = function(self)
  87.       print("Entered FALLING state")
  88.       -- play falling animation, etc..
  89.     end,
  90.     update = function(self, dt)
  91.       -- handle horizontal movement..
  92.       -- adjust velocity/position negative on the y-axis
  93.       -- check for ground collision..
  94.       if self.is_on_ground == true then
  95.         set_state(self, "landing")
  96.       end
  97.     end,
  98.     exit = function(self)
  99.       print("Leaving FALLING state")
  100.     end,
  101.   }, -- 4 FALLING
  102.  
  103.   { -- 5
  104.     name = "landing",
  105.     init = function(self)
  106.       print("Entering LANDING state")
  107.       -- play landing animation and sound effect, etc..
  108.       -- reset state_timer
  109.       state_timer = 0
  110.     end,
  111.     update = function(self, dt)
  112.       -- increment state_timer
  113.       state_timer = state_timer + 1
  114.       -- check if landing delay/lag completed, switch to standing state if true
  115.       if state_timer >= self.landing_lag then
  116.         set_state(self, "standing")
  117.       end
  118.     end,
  119.     exit = function(self)
  120.       print("Leaving LANDING state")
  121.       -- play stand up/ready sound effect..
  122.     end,
  123.   }, -- 5 LANDING
  124.  
  125. --[[ To add more states copy and edit the following...
  126.   { --
  127.     name = "",
  128.     init = function(self)
  129.     end,
  130.     update = function(self, dt)
  131.     end,
  132.     exit = function(self)
  133.     end,
  134.   }, --
  135. ]]
  136.  
  137. } -- state_machine ENDS
  138.  
  139. -- STATE MACHINE VARIABLES/FUNCTIONS --
  140.  
  141. local state = 1 -- variable to track the current state
  142. -- state_machine begins in state 1, i.e. standing state
  143.  
  144. local state_timer = 0 -- variable to track time spent in any given state
  145.  
  146. function set_state(self, new_state) -- called to change states
  147.   -- run the exit function on the current state
  148.   state_machine[state].exit(self)
  149.   -- determine the new state to set from the "new_state" string passed into this function...
  150.   local state_to_set = 0
  151.   for i, v in ipairs(state_machine) do -- loop through states in state_machine table
  152.     if v.name == new_state then -- if state.name is the requested new state
  153.       state_to_set = i -- record the requested state's index (i)
  154.       break -- stop looping as we have found the next state
  155.     end
  156.   end
  157.   -- adjust the current state variable
  158.     state = state_to_set
  159.   -- call the init() script of the new current state
  160.     state_machine[state].init(self)
  161. end
  162.  
  163. -- CORE FUNCTIONS
  164.  
  165. function init(self)
  166.   -- call the init() function of the starting default state
  167.     state_machine[state].init(self,dt)
  168. end
  169.  
  170. function update(self, dt)
  171.   -- call the update funciton of the current state
  172.     state_machine[state].update(self,dt)
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement