Created
November 14, 2012 10:44
-
-
Save NimbusBP1729/4071483 to your computer and use it in GitHub Desktop.
A finite state machine for player.lua that manages poses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--a snippet of how player.lua might function | |
local currentState = sm.wielding.lookup | |
local pose = currentState.pose | |
local keydown_DOWN = controls.isDown( 'DOWN' ) | |
--at keyPress | |
if currentState.key then | |
currentState = currentState.key | |
--process the key(maybe pick an item up) | |
elseif key == 'A' then | |
if keydown_DOWN then | |
currentState = currentState.drop | |
elseif crawlable then --touching a crawlable item | |
currentState = currentState.crawl | |
elseif climbable then --touching a climbable item | |
currentState = currentState.climb | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--================== | |
--DECLARE STATES | |
--================== | |
local SM = | |
{ | |
wielding = { | |
lookup = {pose = 'gaze', action=function1}, | |
lookdown = {pose = 'crouchwalk', action=function1}, | |
walkLeft = {pose = 'wieldwalk', action=function1}, | |
walkRight= {pose = 'wieldwalk', action=function1}, | |
walkUp = {pose = 'gazewalk', action=function1}, | |
walkDown = {pose = 'gazewalk', action=function1}, | |
crouch = {pose = 'crouch', action=function1}, | |
jump = {pose = 'wieldjump', action=function1}, | |
idle = {pose = 'wieldidle', action=function1}, | |
}, | |
holding = { | |
up = {pose = 'gazewalk', action=function1}, | |
down = {pose = 'gazewalk', action=function1}, | |
walk = {pose = 'gazewalk', action=function1}, | |
crouch = {pose = 'gazewalk', action=function1}, | |
jump = {pose = 'gazewalk', action=function1}, | |
idle = nil, | |
}, | |
} | |
--possible transition list: | |
--up,down,left,right,attack,jump,land,pickup,idle,crouch | |
--================== | |
--DECLARE TRANSITIONS | |
--================== | |
SM.wielding.lookup.UP = SM.wielding.lookup | |
SM.wielding.lookup.DOWN = SM.wielding.lookdown | |
SM.wielding.lookup.LEFT = SM.wielding.walkLeft | |
SM.wielding.lookup.RIGHT = SM.wielding.walkRight | |
SM.wielding.lookup.B = SM.wielding.jump | |
SM.wielding.lookup.drop = SM.default.lookup | |
SM.wielding.lookup.pickup = SM.wielding.lookup | |
SM.wielding.lookup.crawl = SM.crawling.lookup | |
SM.wielding.lookup.climb = SM.climbing.lookup | |
SM.wielding.lookup.idle = SM.wielding.idle | |
SM.wielding.lookup.crouch = nil --can't currently crouch and lookup in a room | |
--================== | |
--HELPER FUNCTIONS | |
--================== | |
function isWielding(state) | |
for k,v in pairs(SM.wielding) do | |
if v==state then | |
return true | |
end | |
end | |
return false | |
end | |
function isJumping(state) | |
for k,v in pairs(SM) do | |
for l,u in pairs(v) do | |
if l=='jump' and u==state then | |
return true | |
end | |
end | |
return false | |
end | |
return SM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment