/apps/demos/crawl: cleanup, document
This commit is contained in:
parent
9f0d15b9f6
commit
6b2901be28
@ -28,19 +28,23 @@ function game_tick()
|
|||||||
input_action { control = "S", name = "walk_backward" }
|
input_action { control = "S", name = "walk_backward" }
|
||||||
|
|
||||||
if input_action_just_released { name = "turn_left" } then
|
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
|
end
|
||||||
|
|
||||||
if input_action_just_released { name = "turn_right" } then
|
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
|
end
|
||||||
|
|
||||||
local move = { x = 0, y = 0 }
|
local move = { x = 0, y = 0 }
|
||||||
if input_action_just_released { name = "walk_forward" } then
|
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
|
end
|
||||||
if input_action_just_released { name = "walk_backward" } then
|
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
|
end
|
||||||
|
|
||||||
if ctx.udata.level.grid[ctx.udata.player.position.y + move.y][ctx.udata.player.position.x + move.x].solid ~= nil then
|
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 {
|
draw_camera {
|
||||||
position = {
|
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,
|
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,
|
direction = ctx.udata.player.direction_lerp,
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,20 @@ function load_level(file)
|
|||||||
local f = file_read { file = file }
|
local f = file_read { file = file }
|
||||||
|
|
||||||
local result = {
|
local result = {
|
||||||
|
-- templates to fill the grid with
|
||||||
classes = {
|
classes = {
|
||||||
|
-- predefined empty tile
|
||||||
void = { },
|
void = { },
|
||||||
},
|
},
|
||||||
|
-- symbol to class lookup table
|
||||||
glossary = {
|
glossary = {
|
||||||
[" "] = "void",
|
[" "] = "void",
|
||||||
},
|
},
|
||||||
|
-- grid consists of expanded classes, of size dimensions
|
||||||
grid = {},
|
grid = {},
|
||||||
|
-- map consists of original rows of symbols that the grid is constructed from
|
||||||
map = {},
|
map = {},
|
||||||
|
-- maximum extends of the map, unspecified tiles are filled with "void"
|
||||||
size = { x = 0, y = 0 },
|
size = { x = 0, y = 0 },
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,14 +34,10 @@ function load_level(file)
|
|||||||
section = line:sub(2); subsection = "none"
|
section = line:sub(2); subsection = "none"
|
||||||
-- decode map one line at a time
|
-- decode map one line at a time
|
||||||
elseif section == "map" then
|
elseif section == "map" then
|
||||||
local l = #result.map + 1
|
|
||||||
if result.size.x < #line then
|
if result.size.x < #line then
|
||||||
result.size.x = #line
|
result.size.x = #line
|
||||||
end
|
end
|
||||||
result.map[l] = {}
|
result.map[#result.map + 1] = line
|
||||||
for i = 1, #line do
|
|
||||||
result.map[l][i] = line:sub(i,i)
|
|
||||||
end
|
|
||||||
-- templates to expand
|
-- templates to expand
|
||||||
elseif section == "classes" then
|
elseif section == "classes" then
|
||||||
-- properties
|
-- properties
|
||||||
@ -61,14 +63,16 @@ function load_level(file)
|
|||||||
from = limit + 1
|
from = limit + 1
|
||||||
start, limit = string.find(f, "\n", from)
|
start, limit = string.find(f, "\n", from)
|
||||||
end
|
end
|
||||||
-- post process
|
-- post process, expand map to grid
|
||||||
for y = 1, #result.map do
|
for y = 1, #result.map do
|
||||||
result.grid[y] = {}
|
result.grid[y] = {}
|
||||||
for x = 1, result.size.x do
|
for x = 1, result.size.x do
|
||||||
-- past defined for line
|
-- past defined for line
|
||||||
local symbol
|
local symbol
|
||||||
if x > #result.map[y] then symbol = " "
|
if x > #result.map[y] then
|
||||||
else symbol = result.map[y][x]
|
symbol = " "
|
||||||
|
else
|
||||||
|
symbol = result.map[y]:sub(x,x)
|
||||||
end
|
end
|
||||||
local class = result.classes[result.glossary[symbol]]
|
local class = result.classes[result.glossary[symbol]]
|
||||||
if class["unique"] ~= nil then
|
if class["unique"] ~= nil then
|
||||||
|
@ -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)
|
function render_dungeon(dungeon)
|
||||||
for y = 1, dungeon.size.y do
|
for y = 1, dungeon.size.y do
|
||||||
for x = 1, dungeon.size.x do
|
for x = 1, dungeon.size.x do
|
||||||
if dungeon.grid[y][x].wall_texture ~= nil then
|
if dungeon.grid[y][x].wall_texture ~= nil then
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].wall_texture,
|
texture = dungeon.grid[y][x].wall_texture,
|
||||||
v3 = { x = y, y = 1, z = x },
|
v3 = { x = x, y = 1, z = y },
|
||||||
v2 = { x = y, y = 0, z = x },
|
v2 = { x = x, y = 0, z = y },
|
||||||
v1 = { x = y + 1, y = 0, z = x },
|
v1 = { x = x + 1, y = 0, z = y },
|
||||||
v0 = { x = y + 1, y = 1, z = x },
|
v0 = { x = x + 1, y = 1, z = y },
|
||||||
texture_region = { w = 128, h = 128 },
|
texture_region = { w = 128, h = 128 },
|
||||||
}
|
}
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].wall_texture,
|
texture = dungeon.grid[y][x].wall_texture,
|
||||||
v3 = { x = y + 1, y = 1, z = x },
|
v3 = { x = x + 1, y = 1, z = y },
|
||||||
v2 = { x = y + 1, y = 0, z = x },
|
v2 = { x = x + 1, y = 0, z = y },
|
||||||
v1 = { x = y + 1, y = 0, z = x + 1 },
|
v1 = { x = x + 1, y = 0, z = y + 1 },
|
||||||
v0 = { x = y + 1, y = 1, z = x + 1 },
|
v0 = { x = x + 1, y = 1, z = y + 1 },
|
||||||
texture_region = { w = 128, h = 128 },
|
texture_region = { w = 128, h = 128 },
|
||||||
}
|
}
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].wall_texture,
|
texture = dungeon.grid[y][x].wall_texture,
|
||||||
v3 = { x = y + 1, y = 1, z = x + 1 },
|
v3 = { x = x + 1, y = 1, z = y + 1 },
|
||||||
v2 = { x = y + 1, y = 0, z = x + 1 },
|
v2 = { x = x + 1, y = 0, z = y + 1 },
|
||||||
v1 = { x = y, y = 0, z = x + 1 },
|
v1 = { x = x, y = 0, z = y + 1 },
|
||||||
v0 = { x = y, y = 1, z = x + 1 },
|
v0 = { x = x, y = 1, z = y + 1 },
|
||||||
texture_region = { w = 128, h = 128 },
|
texture_region = { w = 128, h = 128 },
|
||||||
}
|
}
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].wall_texture,
|
texture = dungeon.grid[y][x].wall_texture,
|
||||||
v3 = { x = y, y = 1, z = x + 1 },
|
v3 = { x = x, y = 1, z = y + 1 },
|
||||||
v2 = { x = y, y = 0, z = x + 1 },
|
v2 = { x = x, y = 0, z = y + 1 },
|
||||||
v1 = { x = y, y = 0, z = x },
|
v1 = { x = x, y = 0, z = y },
|
||||||
v0 = { x = y, y = 1, z = x },
|
v0 = { x = x, y = 1, z = y },
|
||||||
texture_region = { w = 128, h = 128 },
|
texture_region = { w = 128, h = 128 },
|
||||||
}
|
}
|
||||||
|
|
||||||
elseif dungeon.grid[y][x].tile_texture ~= nil then
|
elseif dungeon.grid[y][x].tile_texture ~= nil then
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].tile_texture,
|
texture = dungeon.grid[y][x].tile_texture,
|
||||||
v0 = { x = y + 1, y = 0, z = x },
|
v0 = { x = x + 1, y = 0, z = y },
|
||||||
v1 = { x = y, y = 0, z = x },
|
v1 = { x = x, y = 0, z = y },
|
||||||
v2 = { x = y, y = 0, z = x + 1 },
|
v2 = { x = x, y = 0, z = y + 1 },
|
||||||
v3 = { x = y + 1, y = 0, z = x + 1},
|
v3 = { x = x + 1, y = 0, z = y + 1},
|
||||||
texture_region = { w = 128, h = 128 },
|
texture_region = { w = 128, h = 128 },
|
||||||
}
|
}
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].tile_texture,
|
texture = dungeon.grid[y][x].tile_texture,
|
||||||
v3 = { x = y + 1, y = 1, z = x },
|
v3 = { x = x + 1, y = 1, z = y },
|
||||||
v2 = { x = y, y = 1, z = x },
|
v2 = { x = x, y = 1, z = y },
|
||||||
v1 = { x = y, y = 1, z = x + 1 },
|
v1 = { x = x, y = 1, z = y + 1 },
|
||||||
v0 = { x = y + 1, y = 1, z = x + 1},
|
v0 = { x = x + 1, y = 1, z = y + 1},
|
||||||
texture_region = { w = 128, h = 128 },
|
texture_region = { w = 128, h = 128 },
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -59,24 +60,25 @@ function render_dungeon(dungeon)
|
|||||||
if dungeon.grid[y][x].face == "horizon" then
|
if dungeon.grid[y][x].face == "horizon" then
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].face_texture,
|
texture = dungeon.grid[y][x].face_texture,
|
||||||
v3 = { x = y, y = 1, z = x + 1 },
|
v3 = { x = x + 1, y = 1, z = y },
|
||||||
v2 = { x = y, y = 0, z = x + 1 },
|
v2 = { x = x + 1, y = 0, z = y },
|
||||||
v1 = { x = y + 1, y = 0, z = x + 1 },
|
v1 = { x = x + 1, y = 0, z = y + 1 },
|
||||||
v0 = { x = y + 1, y = 1, z = x + 1 },
|
v0 = { x = x + 1, y = 1, z = y + 1 },
|
||||||
texture_region = { w = 64, h = 96 },
|
texture_region = { w = 64, h = 64 },
|
||||||
}
|
}
|
||||||
draw_quad {
|
draw_quad {
|
||||||
texture = dungeon.grid[y][x].face_texture,
|
texture = dungeon.grid[y][x].face_texture,
|
||||||
v3 = { x = y + 1, y = 1, z = x + 1 },
|
v3 = { x = x, y = 1, z = y + 1 },
|
||||||
v2 = { x = y + 1, y = 0, z = x + 1 },
|
v2 = { x = x, y = 0, z = y + 1 },
|
||||||
v1 = { x = y, y = 0, z = x + 1 },
|
v1 = { x = x, y = 0, z = y },
|
||||||
v0 = { x = y, y = 1, z = x + 1 },
|
v0 = { x = x, y = 1, z = y },
|
||||||
texture_region = { w = 64, h = 96 },
|
texture_region = { w = 64, h = 64 },
|
||||||
}
|
}
|
||||||
|
|
||||||
elseif dungeon.grid[y][x].face == "observer" then
|
elseif dungeon.grid[y][x].face == "observer" then
|
||||||
draw_billboard {
|
draw_billboard {
|
||||||
texture = dungeon.grid[y][x].face_texture,
|
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 },
|
size = { x = 0.5, y = 0.5 },
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user