dcturner [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=10350 FiniteStateMachine boilerplate <p> <table><tr><td> <a href="/bbs/?pid=98757#p"> <img src="/bbs/thumbs/pico8_fsm_boiler-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=98757#p"> fsm_boiler</a><br><br> by <a href="/bbs/?uid=10350"> dcturner</a> <br><br><br> <a href="/bbs/?pid=98757#p"> [Click to Play]</a> </td></tr></table> <br /> --core<br /> -- finite state machine (game state)<br /> fsm={states={}}</p> <p>-- make a state for the fsm system<br /> function mk_state(state_name, f_enter, f_exec, f_exit)<br /> s = {}<br /> s.enter = f_enter<br /> s.exec = f_exec<br /> s.exit = f_exit<br /> s.age=0<br /> fsm.states[state_name] = s<br /> return s<br /> end</p> <p>function set_state(new_state_name)</p> <p>if(fsm.current ~= nil) then<br /> -- exit old state<br /> fsm.current.exit()<br /> fsm.current.age=0<br /> end</p> <p>-- setup new state<br /> fsm.current = fsm.states[new_state_name]<br /> fsm.current.age=0<br /> fsm.current.enter()<br /> end</p> <p>function _init()<br /> -- set initial state to s1<br /> fsm.current = s1<br /> end</p> <p>function _update60()<br /> fsm.current.age+=1<br /> fsm.current.exec()<br /> end</p> <p>--&gt;8<br /> --state 1</p> <p>function s1_enter()<br /> end</p> <p>function s1_execute()</p> <p>cls()<br /> print(&quot;state_1: &quot; .. s1.age, 10,10,9)<br /> -- leave when s1 is older than 60 frames<br /> if(s1.age&gt;60) then<br /> set_state(&quot;s2&quot;)<br /> end<br /> end</p> <p>function s1_exit()<br /> end</p> <p>s1 = mk_state(&quot;s1&quot;, s1_enter, s1_execute, s1_exit)<br /> --&gt;8<br /> --state 2</p> <p>function s2_enter()<br /> end</p> <p>function s2_execute()</p> <p>cls()<br /> print(&quot;state_2: &quot; .. s2.age, 40,10,8)<br /> -- leave when s1 is older than 120 frames<br /> if(s2.age&gt;120) then<br /> set_state(&quot;s1&quot;)<br /> end<br /> end</p> <p>function s2_exit()<br /> end<br /> s2 = mk_state(&quot;s2&quot;, s2_enter, s2_execute, s2_exit)</p> https://www.lexaloffle.com/bbs/?tid=45006 https://www.lexaloffle.com/bbs/?tid=45006 Sat, 16 Oct 2021 21:51:44 UTC ((LOOPHOLE)) <p> <table><tr><td> <a href="/bbs/?pid=23993#p"> <img src="/bbs/thumbs/pico23992.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=23993#p"> ((LOOPHOLE)) 1</a><br><br> by <a href="/bbs/?uid=10350"> dcturner</a> <br><br><br> <a href="/bbs/?pid=23993#p"> [Click to Play]</a> </td></tr></table> </p> <p>Still a WIP, needs sound and a difficulty curve, but let me know what you think :)</p> <p>Had loads of fun making it!</p> <p>dt</p> https://www.lexaloffle.com/bbs/?tid=3733 https://www.lexaloffle.com/bbs/?tid=3733 Thu, 30 Jun 2016 16:07:56 UTC