From 6b2901be2880c7552e2947a2f9709bae90822a5d Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 25 Feb 2025 22:20:35 +0300 Subject: [PATCH] /apps/demos/crawl: cleanup, document --- apps/demos/crawl/data/scripts/game.lua | 16 +++-- apps/demos/crawl/data/scripts/level.lua | 20 ++++--- apps/demos/crawl/data/scripts/render.lua | 74 ++++++++++++------------ 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/apps/demos/crawl/data/scripts/game.lua b/apps/demos/crawl/data/scripts/game.lua index 8727f2b..cd6ad62 100644 --- a/apps/demos/crawl/data/scripts/game.lua +++ b/apps/demos/crawl/data/scripts/game.lua @@ -28,19 +28,23 @@ function game_tick() input_action { control = "S", name = "walk_backward" } if input_action_just_released { name = "turn_left" } then - ctx.udata.player.direction = { x = ctx.udata.player.direction.z, y = ctx.udata.player.direction.y, z = -ctx.udata.player.direction.x } + ctx.udata.player.direction = { x = ctx.udata.player.direction.z, + y = ctx.udata.player.direction.y, + z =-ctx.udata.player.direction.x } end if input_action_just_released { name = "turn_right" } then - ctx.udata.player.direction = { x = -ctx.udata.player.direction.z, y = ctx.udata.player.direction.y, z = ctx.udata.player.direction.x } + ctx.udata.player.direction = { x =-ctx.udata.player.direction.z, + y = ctx.udata.player.direction.y, + z = ctx.udata.player.direction.x } end local move = { x = 0, y = 0 } if input_action_just_released { name = "walk_forward" } then - move = { x = move.x + ctx.udata.player.direction.z, y = move.y + ctx.udata.player.direction.x } + move = { x = move.x + ctx.udata.player.direction.x, y = move.y + ctx.udata.player.direction.z } end if input_action_just_released { name = "walk_backward" } then - move = { x = move.x - ctx.udata.player.direction.z, y = move.y - ctx.udata.player.direction.x } + move = { x = move.x - ctx.udata.player.direction.x, y = move.y - ctx.udata.player.direction.z } end if ctx.udata.level.grid[ctx.udata.player.position.y + move.y][ctx.udata.player.position.x + move.x].solid ~= nil then @@ -56,9 +60,9 @@ function game_tick() draw_camera { position = { - x = ctx.udata.player.position_lerp.y + 0.5 - ctx.udata.player.direction.x / 2, + x = ctx.udata.player.position_lerp.x + 0.5 - ctx.udata.player.direction.x / 2, y = 0.5, - z = ctx.udata.player.position_lerp.x + 0.5 - ctx.udata.player.direction.z / 2, + z = ctx.udata.player.position_lerp.y + 0.5 - ctx.udata.player.direction.z / 2, }, direction = ctx.udata.player.direction_lerp, } diff --git a/apps/demos/crawl/data/scripts/level.lua b/apps/demos/crawl/data/scripts/level.lua index e05046d..31baf61 100644 --- a/apps/demos/crawl/data/scripts/level.lua +++ b/apps/demos/crawl/data/scripts/level.lua @@ -3,14 +3,20 @@ function load_level(file) local f = file_read { file = file } local result = { + -- templates to fill the grid with classes = { + -- predefined empty tile void = { }, }, + -- symbol to class lookup table glossary = { [" "] = "void", }, + -- grid consists of expanded classes, of size dimensions grid = {}, + -- map consists of original rows of symbols that the grid is constructed from map = {}, + -- maximum extends of the map, unspecified tiles are filled with "void" size = { x = 0, y = 0 }, } @@ -28,14 +34,10 @@ function load_level(file) 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 + result.map[#result.map + 1] = line -- templates to expand elseif section == "classes" then -- properties @@ -61,14 +63,16 @@ function load_level(file) from = limit + 1 start, limit = string.find(f, "\n", from) end - -- post process + -- post process, expand map to grid for y = 1, #result.map do result.grid[y] = {} 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] + if x > #result.map[y] then + symbol = " " + else + symbol = result.map[y]:sub(x,x) end local class = result.classes[result.glossary[symbol]] if class["unique"] ~= nil then diff --git a/apps/demos/crawl/data/scripts/render.lua b/apps/demos/crawl/data/scripts/render.lua index c59be03..2e35469 100644 --- a/apps/demos/crawl/data/scripts/render.lua +++ b/apps/demos/crawl/data/scripts/render.lua @@ -1,56 +1,57 @@ - +-- if this is too wasteful, one could check nerby tiles to see whether faces could be visible +-- more robust solution would be to travel the level from observer point of view function render_dungeon(dungeon) for y = 1, dungeon.size.y do for x = 1, dungeon.size.x do if dungeon.grid[y][x].wall_texture ~= nil then draw_quad { texture = dungeon.grid[y][x].wall_texture, - v3 = { x = y, y = 1, z = x }, - v2 = { x = y, y = 0, z = x }, - v1 = { x = y + 1, y = 0, z = x }, - v0 = { x = y + 1, y = 1, z = x }, + v3 = { x = x, y = 1, z = y }, + v2 = { x = x, y = 0, z = y }, + v1 = { x = x + 1, y = 0, z = y }, + v0 = { x = x + 1, y = 1, z = y }, texture_region = { w = 128, h = 128 }, } draw_quad { texture = dungeon.grid[y][x].wall_texture, - v3 = { x = y + 1, y = 1, z = x }, - v2 = { x = y + 1, y = 0, z = x }, - v1 = { x = y + 1, y = 0, z = x + 1 }, - v0 = { x = y + 1, y = 1, z = x + 1 }, + v3 = { x = x + 1, y = 1, z = y }, + v2 = { x = x + 1, y = 0, z = y }, + v1 = { x = x + 1, y = 0, z = y + 1 }, + v0 = { x = x + 1, y = 1, z = y + 1 }, texture_region = { w = 128, h = 128 }, } draw_quad { texture = dungeon.grid[y][x].wall_texture, - v3 = { x = y + 1, y = 1, z = x + 1 }, - v2 = { x = y + 1, y = 0, z = x + 1 }, - v1 = { x = y, y = 0, z = x + 1 }, - v0 = { x = y, y = 1, z = x + 1 }, + v3 = { x = x + 1, y = 1, z = y + 1 }, + v2 = { x = x + 1, y = 0, z = y + 1 }, + v1 = { x = x, y = 0, z = y + 1 }, + v0 = { x = x, y = 1, z = y + 1 }, texture_region = { w = 128, h = 128 }, } draw_quad { texture = dungeon.grid[y][x].wall_texture, - v3 = { x = y, y = 1, z = x + 1 }, - v2 = { x = y, y = 0, z = x + 1 }, - v1 = { x = y, y = 0, z = x }, - v0 = { x = y, y = 1, z = x }, + v3 = { x = x, y = 1, z = y + 1 }, + v2 = { x = x, y = 0, z = y + 1 }, + v1 = { x = x, y = 0, z = y }, + v0 = { x = x, y = 1, z = y }, texture_region = { w = 128, h = 128 }, } elseif dungeon.grid[y][x].tile_texture ~= nil then draw_quad { texture = dungeon.grid[y][x].tile_texture, - v0 = { x = y + 1, y = 0, z = x }, - v1 = { x = y, y = 0, z = x }, - v2 = { x = y, y = 0, z = x + 1 }, - v3 = { x = y + 1, y = 0, z = x + 1}, + v0 = { x = x + 1, y = 0, z = y }, + v1 = { x = x, y = 0, z = y }, + v2 = { x = x, y = 0, z = y + 1 }, + v3 = { x = x + 1, y = 0, z = y + 1}, texture_region = { w = 128, h = 128 }, } draw_quad { texture = dungeon.grid[y][x].tile_texture, - v3 = { x = y + 1, y = 1, z = x }, - v2 = { x = y, y = 1, z = x }, - v1 = { x = y, y = 1, z = x + 1 }, - v0 = { x = y + 1, y = 1, z = x + 1}, + v3 = { x = x + 1, y = 1, z = y }, + v2 = { x = x, y = 1, z = y }, + v1 = { x = x, y = 1, z = y + 1 }, + v0 = { x = x + 1, y = 1, z = y + 1}, texture_region = { w = 128, h = 128 }, } end @@ -59,24 +60,25 @@ function render_dungeon(dungeon) if dungeon.grid[y][x].face == "horizon" then draw_quad { texture = dungeon.grid[y][x].face_texture, - v3 = { x = y, y = 1, z = x + 1 }, - v2 = { x = y, y = 0, z = x + 1 }, - v1 = { x = y + 1, y = 0, z = x + 1 }, - v0 = { x = y + 1, y = 1, z = x + 1 }, - texture_region = { w = 64, h = 96 }, + v3 = { x = x + 1, y = 1, z = y }, + v2 = { x = x + 1, y = 0, z = y }, + v1 = { x = x + 1, y = 0, z = y + 1 }, + v0 = { x = x + 1, y = 1, z = y + 1 }, + texture_region = { w = 64, h = 64 }, } draw_quad { texture = dungeon.grid[y][x].face_texture, - v3 = { x = y + 1, y = 1, z = x + 1 }, - v2 = { x = y + 1, y = 0, z = x + 1 }, - v1 = { x = y, y = 0, z = x + 1 }, - v0 = { x = y, y = 1, z = x + 1 }, - texture_region = { w = 64, h = 96 }, + v3 = { x = x, y = 1, z = y + 1 }, + v2 = { x = x, y = 0, z = y + 1 }, + v1 = { x = x, y = 0, z = y }, + v0 = { x = x, y = 1, z = y }, + texture_region = { w = 64, h = 64 }, } + elseif dungeon.grid[y][x].face == "observer" then draw_billboard { texture = dungeon.grid[y][x].face_texture, - position = { x = y + 0.5, y = 0.5, z = x + 0.5 }, + position = { x = x + 0.5, y = 0.5, z = y + 0.5 }, size = { x = 0.5, y = 0.5 }, } end