47 lines
1.4 KiB
Fennel
47 lines
1.4 KiB
Fennel
(local util (require :lib.util))
|
|
(local state (require :game.state))
|
|
(local map (require :game.tilemap))
|
|
(local rules (require :game.rules))
|
|
(local tiles (require :game.tiles))
|
|
(local dim (require :game.dim))
|
|
(local {: draw : update} (util.require :game.entity))
|
|
|
|
(fn exception-update []
|
|
(when (love.keyboard.isDown :f2)
|
|
(set state.update-exception nil)))
|
|
|
|
(fn wrap-call [thunk]
|
|
(if state.update-exception
|
|
(exception-update)
|
|
(xpcall thunk (fn [msg]
|
|
(set state.update-exception (.. msg "\n" (debug.traceback)))))))
|
|
|
|
(fn update [dt] (wrap-call #(rules.update dt)))
|
|
|
|
(fn game-draw []
|
|
(let [xnext (/ dim.tilesize 2)
|
|
xwell (* dim.tilesize 5)
|
|
current (rules.current-tetromino)]
|
|
(map.draw-tilemap xwell 0 state.well tiles.tetristile)
|
|
(map.draw-tilemap xnext dim.tilesize (rules.tetromino state.next-piece 1) tiles.tetristile)
|
|
(when current
|
|
(map.draw-tilemap (+ xwell (* state.current.x dim.tilesize)) (* state.current.y dim.tilesize) current tiles.tetristile))))
|
|
|
|
(fn exception-draw []
|
|
(love.graphics.setColor 1 0 0 1)
|
|
(love.graphics.setNewFont 14)
|
|
(love.graphics.printf (.. "Press F2 when resolved\n" state.update-exception) 20 20 (- (love.graphics.getWidth) 40)))
|
|
|
|
(fn draw []
|
|
(if state.update-exception
|
|
(exception-draw)
|
|
(game-draw)))
|
|
|
|
(fn handler [ev ...]
|
|
(let [f (. rules ev)
|
|
args [...]]
|
|
(when f (wrap-call #(f (table.unpack args))))))
|
|
|
|
{: update : draw : handler}
|
|
|