honeylisp/game/tiles.fnl

44 lines
1.4 KiB
Fennel

(local util (require :lib.util))
(local lume (require :lib.lume))
(local flags [:walkable :neutable :debris])
(local flag-to-bit {})
(each [iflag flag (ipairs flags)]
(tset flag-to-bit flag (bit.lshift 1 (- iflag 1))))
(fn deserialize [tile]
(match (type tile)
:string {:gfx (tile:fromhex) :flags {}}
:table (doto tile (tset :gfx (tile.gfx:fromhex)))))
(fn serialize [tile] (doto (lume.clone tile) (tset :gfx (tile.gfx:tohex))))
(fn loadtiles [] (lume.map (util.readjson "game/tiles.json") deserialize))
(fn savetiles [tiles] (util.writejson "game/tiles.json" (lume.map tiles serialize)))
(fn loadportraits [] (lume.map (util.readjson "game/portraits.json") deserialize))
(fn saveportraits [p] (util.writejson "game/portraits.json" (lume.map p serialize)))
(fn appendtiles [org tiles]
(each [_ tile (ipairs tiles)]
(when tile.label (org:append tile.label))
(org:append [:bytes tile.gfx])))
(fn append-gfx [org]
(local tiles (loadtiles))
(appendtiles org tiles)
(appendtiles org (loadportraits))
(org:append :tileflags)
(each [_ tile (ipairs tiles)]
(var flags 0)
(each [flag _ (pairs tile.flags)]
(set flags (bit.bor flags (. flag-to-bit flag))))
(org:append [:db flags])))
(fn appendmaps [org]
(local map (util.readjson "game/map00001.json"))
(org:append :map [:bytes (map.map:fromhex)]))
{: loadtiles : savetiles : appendtiles : append-gfx : appendmaps : flags : flag-to-bit : loadportraits : saveportraits}