From 80a4ae3d0e75025b44f348522b0b38662c05c20f Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Thu, 20 Feb 2025 22:20:02 +0300 Subject: [PATCH] progress on /apps/demos/crawl --- apps/demos/crawl/data/levels/00.lvl | 21 +++++++++------- apps/demos/crawl/data/scripts/game.lua | 33 +++++++++++++++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/apps/demos/crawl/data/levels/00.lvl b/apps/demos/crawl/data/levels/00.lvl index 128c5f6..c97c65b 100644 --- a/apps/demos/crawl/data/levels/00.lvl +++ b/apps/demos/crawl/data/levels/00.lvl @@ -1,24 +1,27 @@ @map --- Grid of entities defined by @classes. +-- Grid of symbols defined by @classes section. ##### #.@.# #...# -##.## -#...# -#...# +##.###### +#...X...# +#./.##### ##### @classes --- Defines classes under symbols, which could have properties. +-- Defines classes under symbols, which could have properties attached. # wall -. void - test : this +. floor @ player_spawn unique : face : south hold : torch +X door + open_on : sg_torch0 +/ torch + emit : sg_torch0 @meta --- Arbitrary sections could be defined with values pairs. -description : Test Level! Just two square rooms. +-- Arbitrary sections could be defined with value pairs. +description : Test Level! Just two square rooms and a tunnel opened by lever. diff --git a/apps/demos/crawl/data/scripts/game.lua b/apps/demos/crawl/data/scripts/game.lua index a0ee7ec..6beaf88 100644 --- a/apps/demos/crawl/data/scripts/game.lua +++ b/apps/demos/crawl/data/scripts/game.lua @@ -4,10 +4,15 @@ function load_level(file) local f = file_read { file = file } local result = { - classes = {}, - glossary = {}, + classes = { + void = { }, + }, + glossary = { + [" "] = "void", + }, grid = {}, map = {}, + size = { x = 0, y = 0 }, } -- iterate over lines @@ -20,21 +25,23 @@ function load_level(file) if #line == 0 or line:find("^%-%-%s*") then goto skip -- start new section - elseif line:find("^@%a+") then + elseif line:find("^@%g+") then section = line:sub(2); subsection = "none" -- decode map one line at a time elseif section == "map" then local l = #result.map + 1 + if result.size.x < #line then + result.size.x = #line + end result.map[l] = {} for i = 1, #line do result.map[l][i] = line:sub(i,i) end - assert(#result.map[1] == #result.map[l]) -- templates to expand elseif section == "classes" then -- properties - if line:find("^ %a+") then - local _, _, property, value = line:find("^ (%a+)%s?:%s?(.*)") + if line:find("^ %g+") then + local _, _, property, value = line:find("^ (%g+)%s?:%s?(.*)") result.classes[subsection][property] = value goto skip end @@ -45,7 +52,7 @@ function load_level(file) result.glossary[symbol] = classname subsection = classname elseif section ~= "none" then - local _, _, property, value = line:find("^(%a+)%s?:%s?(.*)") + local _, _, property, value = line:find("^(%g+)%s?:%s?(.*)") if result[section] == nil then result[section] = {} end @@ -58,15 +65,23 @@ function load_level(file) -- post process for y = 1, #result.map do result.grid[y] = {} - for x = 1, #result.map[y] do - local class = result.classes[result.glossary[result.map[y][x]]] + for x = 1, result.size.x do + -- past defined for line + local symbol + if x > #result.map[y] then symbol = " " + else symbol = result.map[y][x] + end + local class = result.classes[result.glossary[symbol]] if class["unique"] ~= nil then class.position = { x = x, y = y } end result.grid[y][x] = class + ::continue:: end end + result.size.y = #result.map + print(result.meta.description) return result