honeylisp/game/tiles.fnl

36 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-10-12 15:48:14 +00:00
(local util (require :lib.util))
(local lume (require :lib.lume))
(local flags [:walkable :neutable :debris])
2020-11-16 16:09:14 +00:00
(local flag-to-bit {})
(each [iflag flag (ipairs flags)]
(tset flag-to-bit flag (bit.lshift 1 (- iflag 1))))
2020-10-12 15:48:14 +00:00
(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)))
2020-10-12 15:48:14 +00:00
(fn appendtiles [tiles org]
(each [_ tile (ipairs tiles)]
(when tile.label (org:append tile.label))
2020-11-16 16:09:14 +00:00
(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])))
2020-10-12 15:48:14 +00:00
2020-10-19 00:13:26 +00:00
(fn appendmaps [org]
2020-10-29 17:10:57 +00:00
(local map (util.readjson "game/map00001.json"))
(org:append :map [:bytes (map.map:fromhex)]))
2020-10-19 00:13:26 +00:00
2020-11-16 16:09:14 +00:00
{: loadtiles : savetiles : appendtiles : appendmaps : flags : flag-to-bit}
2020-10-12 15:48:14 +00:00