honeylisp/game/files.fnl

61 lines
1.7 KiB
Plaintext
Raw Normal View History

(local util (require :lib.util))
(local lume (require :lib.lume))
(local tile (require :game.tiles))
(local tiledraw (require :editor.tiledraw))
(local files (util.hot-table ...))
(local filename "game/game.json")
(fn deserialize [key value]
(match key
(where (or :tiles :portraits :font)) (tile.deserialize value)
:levels (do (set value.map (value.map:fromhex)) value)
_ value))
(fn serialize [key value]
(match key
(where (or :tiles :portraits :font)) (tile.serialize value)
:levels (do (set value.map (value.map:tohex)) value)
_ value))
(fn files.load []
(set files.game
(if (util.file-exists filename)
(let [game (util.readjson filename)]
(each [k v (pairs game)]
(tset game k (lume.map v #(deserialize k (lume.clone $1)))))
game)
{:tiles [] :portraits [] :font [] :levels []}))
files.game)
(fn files.save []
(let [game {}]
(each [k v (pairs files.game)]
(tset game k (lume.map v #(serialize k (lume.clone $1)))))
(util.writejson filename game)))
(fn new-cache [game key]
(let [spritegen (match key
:font tiledraw.char-to-sprite
:portraits tiledraw.portrait-to-sprite
_ tiledraw.tile-to-sprite)
gfx (. game key)]
(tiledraw.TileCache gfx spritegen)))
(fn files.cache [key]
(when (= (?. files :tilecaches key) nil)
(util.nested-tset files [:tilecaches key] (new-cache files.game key)))
(. files.tilecaches key))
(fn files.reload []
(files.load)
(when files.tilecaches
(each [key cache (pairs files.tilecaches)]
(cache:load (. files.game key)))))
(when (= files.game nil)
(files.load))
files.hot