Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MODULES REQUIRED...
- -- input manager module which will be linked to the input handler object
- local user_input = require "modules.input_manager.lua"
- -- STATE MACHINE BEGINS...
- local state_machine = {
- { -- 1
- name = "standing",
- init = function(self)
- print("Entered STANDING state")
- -- play standing animation, etc...
- end,
- update = function(self, dt)
- -- no functionality here beyond changing state according to user input...
- -- jump button begins jumping (with or without directional input)
- if user_input.jump_btn == true then
- set_state(self, "jumping")
- -- input manager sets directional flag to true on left or right input,
- -- which shifts state machine to walking state
- elseif user_input.directional == true then
- set_state(self, "walking")
- end
- end,
- exit = function(self)
- print("Leaving STANDING state")
- end,
- }, -- 1 STANDING
- { -- 2
- name = "walking",
- init = function(self)
- print("Entered WALKING state")
- -- set walking animation, start footsteps sound effect, etc...
- end,
- update = function(self, dt)
- -- handle walking in either direction
- if user_input.left == true then
- -- adjust velocity/position, negative on the x-axis...
- elseif user_input.right == true then
- -- adjust velocity/position, positive on the x-axis...
- elseif user_input.jump_btn == true then
- set_state(self, "jumping") -- jump if btn pressed
- else set_state(self, "standing") -- if no further directional input, switch back to standing state
- end
- end,
- exit = function(self)
- print("Leaving WALKING state")
- -- stop playing footsteps sound effect, etc..
- end,
- }, -- 2 WALKING
- { -- 3
- name = "jumping",
- init = function(self)
- print("Entered JUMPING state")
- -- play jumping animation and start jump sound effect etc...
- state_timer = 0 -- reset state timer to limit maximum jump height
- end,
- update = function(self, dt)
- -- handle horizontal movement in the air...
- if user_input.left == true then
- -- adjust velocity/position, negative on the x-axis...
- elseif user_input.right == true then
- -- adjust velocity/position, positive on the x-axis...
- end
- -- check for collisions and adjust state accordingly... ie. to damaged falling state etc..
- -- increment state_timer to record how long we have been jumping up
- state_timer = state_timer + 1
- -- continue gaining height if not reached max jump frames and jump btn still pressed
- if state_timer <= self.max_jump_frames and user_input.jump_btn == true then
- -- adjust velocity/position, positive on the y-axis..
- else set_state(self, "falling") -- else, switch to falling state
- end
- end,
- exit = function(self)
- print("Leaving JUMPING state")
- end,
- }, -- 3 JUMPING
- { -- 4
- name = "falling",
- init = function(self)
- print("Entered FALLING state")
- -- play falling animation, etc..
- end,
- update = function(self, dt)
- -- handle horizontal movement..
- -- adjust velocity/position negative on the y-axis
- -- check for ground collision..
- if self.is_on_ground == true then
- set_state(self, "landing")
- end
- end,
- exit = function(self)
- print("Leaving FALLING state")
- end,
- }, -- 4 FALLING
- { -- 5
- name = "landing",
- init = function(self)
- print("Entering LANDING state")
- -- play landing animation and sound effect, etc..
- -- reset state_timer
- state_timer = 0
- end,
- update = function(self, dt)
- -- increment state_timer
- state_timer = state_timer + 1
- -- check if landing delay/lag completed, switch to standing state if true
- if state_timer >= self.landing_lag then
- set_state(self, "standing")
- end
- end,
- exit = function(self)
- print("Leaving LANDING state")
- -- play stand up/ready sound effect..
- end,
- }, -- 5 LANDING
- --[[ To add more states copy and edit the following...
- { --
- name = "",
- init = function(self)
- end,
- update = function(self, dt)
- end,
- exit = function(self)
- end,
- }, --
- ]]
- } -- state_machine ENDS
- -- STATE MACHINE VARIABLES/FUNCTIONS --
- local state = 1 -- variable to track the current state
- -- state_machine begins in state 1, i.e. standing state
- local state_timer = 0 -- variable to track time spent in any given state
- function set_state(self, new_state) -- called to change states
- -- run the exit function on the current state
- state_machine[state].exit(self)
- -- determine the new state to set from the "new_state" string passed into this function...
- local state_to_set = 0
- for i, v in ipairs(state_machine) do -- loop through states in state_machine table
- if v.name == new_state then -- if state.name is the requested new state
- state_to_set = i -- record the requested state's index (i)
- break -- stop looping as we have found the next state
- end
- end
- -- adjust the current state variable
- state = state_to_set
- -- call the init() script of the new current state
- state_machine[state].init(self)
- end
- -- CORE FUNCTIONS
- function init(self)
- -- call the init() function of the starting default state
- state_machine[state].init(self,dt)
- end
- function update(self, dt)
- -- call the update funciton of the current state
- state_machine[state].update(self,dt)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement