42 lines
1.3 KiB
Fennel
42 lines
1.3 KiB
Fennel
(fn dir-from-key [keymap]
|
|
(var (dx dy) (values 0 0))
|
|
(when (love.keyboard.isDown keymap.up) (set dy -1))
|
|
(when (love.keyboard.isDown keymap.down) (set dy 1))
|
|
(when (love.keyboard.isDown keymap.left) (set dx -1))
|
|
(when (love.keyboard.isDown keymap.right) (set dx 1))
|
|
[dx dy])
|
|
|
|
(fn vec-scalarop [op]
|
|
(fn [vec scalar]
|
|
(icollect [_ v (ipairs vec)] (op v scalar))))
|
|
|
|
(fn vec-op [op]
|
|
(fn [vec val]
|
|
(icollect [i left (ipairs vec)]
|
|
(let [right (match (type val)
|
|
:table (. val i)
|
|
_ val)]
|
|
(op left right)))))
|
|
|
|
(local vec* (vec-op #(* $1 $2)))
|
|
(local vec+ (vec-op #(+ $1 $2)))
|
|
|
|
(fn coordinate-iterator [state]
|
|
(let [{: x : y} state
|
|
xnext (+ (or x state.w) 1)
|
|
ynext (+ (or y state.h) 1)]
|
|
(if (< xnext state.w) (set state.x xnext)
|
|
(< ynext state.h) (do (set state.x 0) (set state.y ynext))
|
|
(do (set state.x nil) (set state.y nil)))
|
|
(values x y)))
|
|
|
|
(fn all-coordinates [w h] (values coordinate-iterator {: w : h :x 0 :y 0}))
|
|
|
|
(fn edge [c dc neg-offset ?pos-offset]
|
|
(local pos-offset (if (= ?pos-offset nil) neg-offset ?pos-offset))
|
|
(if (= dc 0) c
|
|
(< dc 0) (+ c dc (- neg-offset))
|
|
(+ c dc pos-offset)))
|
|
|
|
{: dir-from-key : vec* : vec+ : vec-op : all-coordinates : edge}
|