2021-02-07 21:56:19 +00:00
|
|
|
(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])
|
|
|
|
|
2021-03-07 16:55:50 +00:00
|
|
|
(fn vec-scalarop [op]
|
|
|
|
(fn [vec scalar]
|
|
|
|
(icollect [_ v (ipairs vec)] (op v scalar))))
|
2021-02-07 21:56:19 +00:00
|
|
|
|
2021-03-07 16:55:50 +00:00
|
|
|
(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}))
|
|
|
|
|
|
|
|
{: dir-from-key : vec* : vec+ : vec-op : all-coordinates}
|