2021-04-24 02:54:28 +00:00
|
|
|
(local util (require :lib.util))
|
|
|
|
(local lume (require :lib.lume))
|
|
|
|
|
|
|
|
(local files (util.hot-table ...))
|
|
|
|
|
2021-08-02 23:40:31 +00:00
|
|
|
(local default-filename "neutgs/game.json")
|
2021-04-25 03:39:50 +00:00
|
|
|
|
|
|
|
(local encoded-tile-fields [:gfx :mask])
|
|
|
|
(fn convert [tile field method]
|
|
|
|
(local oldval (. tile field))
|
|
|
|
(when oldval
|
|
|
|
(tset tile field (: oldval method)))
|
|
|
|
tile)
|
2021-06-26 02:30:15 +00:00
|
|
|
(fn convert-all [tile method root]
|
|
|
|
(let [encoded-tile-fields [:mask]]
|
|
|
|
(each [_ key (pairs (or root.tilesets {:tileset :gfx}))]
|
|
|
|
(table.insert encoded-tile-fields key))
|
|
|
|
(each [_ field (ipairs encoded-tile-fields)]
|
|
|
|
(convert tile field method))
|
|
|
|
tile))
|
|
|
|
|
|
|
|
(fn tile-deserialize [tile root]
|
2021-04-25 03:39:50 +00:00
|
|
|
(match (type tile)
|
|
|
|
:string {:gfx (tile:fromhex) :flags {}}
|
2021-06-26 02:30:15 +00:00
|
|
|
:table (convert-all tile :fromhex root)))
|
2021-04-25 03:39:50 +00:00
|
|
|
|
2021-06-26 02:30:15 +00:00
|
|
|
(fn tile-serialize [tile root] (convert-all (lume.clone tile) :tohex root))
|
2021-04-25 03:39:50 +00:00
|
|
|
|
2021-06-26 02:30:15 +00:00
|
|
|
(fn deserialize [key value root]
|
2021-04-24 02:54:28 +00:00
|
|
|
(match key
|
2021-07-02 19:51:39 +00:00
|
|
|
(where (or :tiles :portraits :font :brushes)) (tile-deserialize value root)
|
2021-11-14 19:55:41 +00:00
|
|
|
:levels (do (set value.map (value.map:fromhex))
|
|
|
|
(set value.layers (icollect [_ layer (ipairs (or value.layers []))] (layer:fromhex)))
|
|
|
|
value)
|
2021-04-24 02:54:28 +00:00
|
|
|
_ value))
|
|
|
|
|
2021-06-26 02:30:15 +00:00
|
|
|
(fn serialize [key value root]
|
2021-04-24 02:54:28 +00:00
|
|
|
(match key
|
2021-07-02 19:51:39 +00:00
|
|
|
(where (or :tiles :portraits :font :brushes)) (tile-serialize value root)
|
2021-11-14 19:55:41 +00:00
|
|
|
:levels (do (set value.map (value.map:tohex))
|
|
|
|
(set value.layers (icollect [_ layer (ipairs (or value.layers []))] (layer:tohex)))
|
|
|
|
value)
|
2021-04-24 02:54:28 +00:00
|
|
|
_ value))
|
|
|
|
|
2021-05-06 01:09:40 +00:00
|
|
|
(fn clone [v]
|
|
|
|
(match (type v)
|
|
|
|
:table (lume.clone v)
|
|
|
|
_ v))
|
|
|
|
|
2021-06-22 02:59:34 +00:00
|
|
|
(fn filename [] (or files.filename default-filename))
|
|
|
|
(fn files.load [?filename]
|
|
|
|
(when ?filename (set files.filename ?filename))
|
2021-04-24 02:54:28 +00:00
|
|
|
(set files.game
|
2021-06-22 02:59:34 +00:00
|
|
|
(if (util.file-exists (filename))
|
|
|
|
(let [game (util.readjson (filename))]
|
2021-04-24 02:54:28 +00:00
|
|
|
(each [k v (pairs game)]
|
2021-08-29 02:04:54 +00:00
|
|
|
(when (= (type v) :table)
|
|
|
|
(tset game k (lume.map v #(deserialize k (clone $1) game)))))
|
2021-04-24 02:54:28 +00:00
|
|
|
game)
|
|
|
|
{:tiles [] :portraits [] :font [] :levels []}))
|
|
|
|
files.game)
|
|
|
|
|
2021-06-22 02:59:34 +00:00
|
|
|
(fn files.save [?filename]
|
|
|
|
(when ?filename (set files.filename ?filename))
|
2021-04-24 02:54:28 +00:00
|
|
|
(let [game {}]
|
|
|
|
(each [k v (pairs files.game)]
|
2021-09-02 02:59:55 +00:00
|
|
|
(tset game k (if (= (type v) :table) (lume.map v #(serialize k (clone $1) files.game)) v)))
|
2021-06-22 02:59:34 +00:00
|
|
|
(util.writejson (filename) game)))
|
2021-04-24 02:54:28 +00:00
|
|
|
|
|
|
|
(fn new-cache [game key]
|
2021-08-29 02:04:54 +00:00
|
|
|
(let [tiledraw (require :editor.tiledraw)
|
2021-11-14 19:55:41 +00:00
|
|
|
tiles (require :game.tiles)
|
|
|
|
spritegen (tiledraw.spritegen-for-style key)
|
2021-04-24 02:54:28 +00:00
|
|
|
gfx (. game key)]
|
|
|
|
(tiledraw.TileCache gfx spritegen)))
|
|
|
|
|
|
|
|
(fn files.cache [key]
|
2021-08-29 02:04:54 +00:00
|
|
|
(when (= (. files.game key) nil)
|
|
|
|
(tset files.game key []))
|
2021-04-24 02:54:28 +00:00
|
|
|
(when (= (?. files :tilecaches key) nil)
|
|
|
|
(util.nested-tset files [:tilecaches key] (new-cache files.game key)))
|
|
|
|
(. files.tilecaches key))
|
|
|
|
|
2021-06-26 01:55:15 +00:00
|
|
|
(fn files.reload [?filename]
|
|
|
|
(files.load ?filename)
|
2021-04-24 02:54:28 +00:00
|
|
|
(when files.tilecaches
|
|
|
|
(each [key cache (pairs files.tilecaches)]
|
|
|
|
(cache:load (. files.game key)))))
|
|
|
|
|
2021-06-22 02:59:34 +00:00
|
|
|
(fn files.module []
|
|
|
|
(or files.game.module (: (filename) :match "^[^/]+")))
|
|
|
|
|
2021-08-25 01:24:06 +00:00
|
|
|
(fn files.platform [] (or files.game.platform :ii))
|
2021-09-06 03:29:16 +00:00
|
|
|
(fn files.default-platform-method [cls module-prefix method default]
|
|
|
|
(tset cls method (fn [...] (let [f (. (require (.. module-prefix :. (files.platform))) method)] (if f (f ...) (default ...))))))
|
2021-08-25 01:24:06 +00:00
|
|
|
(fn files.platform-methods [cls module-prefix ...]
|
2021-09-06 03:29:16 +00:00
|
|
|
(each [_ key (ipairs [...])] (files.default-platform-method cls module-prefix key #nil)))
|
2021-08-25 01:24:06 +00:00
|
|
|
|
2021-04-24 02:54:28 +00:00
|
|
|
(when (= files.game nil)
|
|
|
|
(files.load))
|
|
|
|
|
|
|
|
files.hot
|