36 lines
1.1 KiB
Fennel
36 lines
1.1 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 appendtiles [tiles org]
|
|
(each [_ tile (ipairs tiles)]
|
|
(when tile.label (org:append tile.label))
|
|
(org:append [:bytes tile.gfx]))
|
|
(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 : appendmaps : flags : flag-to-bit}
|
|
|