honeylisp/game/files.fnl

84 lines
2.2 KiB
Plaintext
Raw Normal View History

(local util (require :lib.util))
(local lume (require :lib.lume))
(local tiledraw (require :editor.tiledraw))
(local files (util.hot-table ...))
(local filename "game/game.json")
(local encoded-tile-fields [:gfx :mask])
(fn convert [tile field method]
(local oldval (. tile field))
(when oldval
(tset tile field (: oldval method)))
tile)
(fn convert-all [tile method]
(each [_ field (ipairs encoded-tile-fields)]
(convert tile field method))
tile)
(fn tile-deserialize [tile]
(match (type tile)
:string {:gfx (tile:fromhex) :flags {}}
:table (convert-all tile :fromhex)))
(fn tile-serialize [tile] (convert-all (lume.clone tile) :tohex))
(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))
2021-05-06 01:09:40 +00:00
(fn clone [v]
(match (type v)
:table (lume.clone v)
_ v))
(fn files.load []
(set files.game
(if (util.file-exists filename)
(let [game (util.readjson filename)]
(each [k v (pairs game)]
2021-05-06 01:09:40 +00:00
(tset game k (lume.map v #(deserialize k (clone $1)))))
game)
{:tiles [] :portraits [] :font [] :levels []}))
files.game)
(fn files.save []
(let [game {}]
(each [k v (pairs files.game)]
2021-05-06 01:09:40 +00:00
(tset game k (lume.map v #(serialize k (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