   <                                  330# #  "3  "#0" {     pg ÃÄ 0C 0C !â ! ð          pg ÃÄ 0C 0C !â !                                                                                                                                                                                                                                                                                                                                                                                                                                                                     pg ÃÄ 0C 0C !â ! ð  pg ÃÄ 0C 0C !â !         pg ÃÄ 0C 0C !â !  ð          pg ÃÄ 0C 0C !â ! ð  pg ÃÄ 0C 0C !â !ò         pg ÃÄ 0C 0C !â !  ð                                                                                                                                                                                                                                                                                                                                         pg ÃÄ 0C 0C !â ! ð  pg ÃÄ 0C 0C !â !  ð     pg ÃÄ 0C 0C !â !ò         pg ÃÄ 0C 0C !â !  ð^                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 	>                                                                H0     ÿÿ ÿ ÿ                    ÿÿÿ ÿðÿÿ                                                                                 ÿ ÿ ÿ                    ðÿ ÿÿÿ                                                                                 ÿ ÿ ÿÿ                    ðÿ ÿÿÿ                                                                                 ÿÿ ÿ ÿð                   ÿðÿ ÿÿ                                                                                 ÿ ðÿ ÿ                    ðÿ ðÿ  ÿ                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ð    ÿ               ÿ                                                                                    ðÿ ðÿÿ ðÿÿÿðÿ ðÿ         ÿÿ   ÿÿ ÿ                                                                              ÿ  ÿ ÿ ÿ ð ðÿðð         ÿ ÿðÿ ðÿ                                                                               ÿ  ÿÿ  ÿð ðÿðð         ÿ  ÿ ðÿðÿ                                                                               ÿÿ  ÿÿ ðÿ ÿÿ  ÿðÿÿðð         ÿ  ÿðÿ ÿ                                                                                                                                                                                                                                                                                                                                                                                                                                                      ð  ð                          ðÿ   ÿ ÿ                                                                              ÿÿ ÿ                          ÿð ðððð                                                                              ð  ð                          ÿ  ÿ ÿ                                                                              ð  ð                          ÿ ÿðððð                                                                               ÿ ÿÿ                          ðÿ ÿ ÿ ÿ                                                                                                                                                                                                                                                                                                                                                                                                                                                       ð ð                                                                                                                  ðÿ ÿ                                                                                                                  ð ð                                                                                                                  ð ð                                                                                                                  ðÿ ÿÿ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ð                                                                                                                  ÿ  ÿ                                                                                                                  ÿ  ð                                                                                                                  ðÿ  ð                                                                                                                     ÿÿ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      wf                                                                                                                      1L                                                                                                                     3D                                                                                                                      3D                                                                                                                      "                                                                                                                     "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          330330330330330330330330330330330330330330330330330330330330330330330330330330330330330330# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  "3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3"3  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "  "#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"#0"                              c ;; title:   FSM + LERP movement
;; author:  r0wan
;; desc:    Run and Jump FSM + LERP
;; license: MIT License
;; version: 0.1
;; script:  fennel
;; strict:  true

(local [frame inc-frame] [((fn []
	(var tic 0)
	(values #tic	#(set tic (+ tic 1)))
))])

(macro //% [val d r]
`(-> ,val (// ,d) (% ,r)))

(macro //%. [val d arr]
`(. ,arr (-> ,val (// ,d) (% (length ,arr)) (+ 1))))

(macro ++ [n] `(+ ,n 1))
(macro round [n] `(math.floor (+ ,n 0.5)))
(macro !. [t k]
`(let [v# (. ,t ,k)]
		(if
			(= v# nil)
			(error (string.format "attempt to lookup a nil value (field '%s')" ,k))
			v#)))

(fn lerp [start end t]
	(+ start (* (- end start) t)))

(fn t++ [frames t]
	(/ (++ (round (* frames t))) frames))

(fn ease-out [t]
 (- 1 (* (- 1 t) (- 1 t))))

(fn ease-in [t]
 (* t t))

(local run [])
(fn run.idle []
	(if
  (and (btn 2) (not (btn 3)))
 	(values "speedup" -1 0)
  (and (btn 3) (not (btn 2)))
  (values "speedup" 1 0)
  (values "idle" 0 0)))

(fn run.speedup [dir t]
	(let [t (t++ 25 t)]
		(if
	 	(or
				(and (= dir -1) (btn 2) (not (btn 3)))
				(and (= dir 1) (btn 3) (not (btn 2))))
	  (if (= t 1)
	  	(values "sustain" dir 1)
	   (values "speedup" dir t))
	  (values "slowdown" dir (- 1 t)))))

(fn run.sustain [dir t]
	(if
 	(or
  	(and (= dir -1) (btn 2) (not (btn 3)))
  	(and (= dir 1) (btn 3) (not (btn 2))))
		(values "sustain" dir t)
		(values "slowdown" dir (- 1 t))))

(fn run.slowdown [dir t]
	(let [t (t++ 25 t)]
		(if (= t 1)
			(values "idle" dir 0)
			(values "slowdown" dir t))))

(local jump {})
(fn jump.idle [t]
	(if
		(btnp 4)
		(values "rise" t)
		(values "idle" t)))

(fn jump.rise [t]
	(let
		[t (t++ 25 t)]
		(if (= t 1)
			(values "fall" 0)
			(values "rise" t))))

(fn jump.fall [t]
	(let
		[t (t++ 25 t)]
		(if (= t 1)
			(values "idle" 0)
			(values "fall" t))))

(local plr {
	:x 48
	:y 88
	:velx 0
	:vely 0
	:dir 0
	:spr-id #(case [$1.velx $1.vely]
 		[0 0] (//%. (frame) 18 [256 257])
	 	(where [a 0] (not= a 0))
									(//%. (frame) 6 [272 273 274 275 276 277])
			(where [_ a] (not= a 0))
								 (//%. (frame) 6 [288 289 290 291]))
	:spr-flip #(if (< $1.dir 0) 1 0)
})

(var rs "idle")
(var rd 0)
(var rt 0)

(var js "idle")
(var jt 0)

(fn _G.TIC []
;; update
	(inc-frame)

	(set [rs rd rt] [((!. run rs) rd rt)])

	(set plr.velx
		(case rs
			"speedup" (lerp 0 rd rt)
			"sustain" (lerp 0 rd rt)
			"slowdown" (lerp rd 0 rt)
			"idle" 0))

	(set plr.x (+ plr.x plr.velx))
	(set plr.dir rd)

	(set [js jt] [((!. jump js) jt)])
		
	(set plr.y
		(case js
			"rise" (- 88 (* 30 (ease-out jt)))
			"fall" (+ (- 88 30) (* 30 (ease-in jt)))
			"idle" (+ 88 jt)))

;; draw
 (cls)
 (map)
 (spr (plr.spr-id plr) plr.x plr.y
 	0 1 (plr.spr-flip plr))

	(print "s" 0 8)
	(print "t" 0 16)
	(print "d" 0 24)
	(print "v" 0 32)
	
	(print "RUN" 8 0)
	(print rs 8 8)
	(print rt 8 16)
	(print rd 8 24)
	(print plr.velx 8 32)
	
	(print "JUMP" 64 0)
	(print js 64 8)
	(print jt 64 16)
)
