Skip to content

Commit

Permalink
Add web build
Browse files Browse the repository at this point in the history
This required removing all 'goto continue' statements. Since the web build was running poorly, the tilemap also got optimized using viewfield culling and SpriteBatch rendering. Removed 'Escape' key for web builds
  • Loading branch information
rbaltrusch committed Mar 10, 2024
1 parent fea6b28 commit 8e77cc3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 68 deletions.
2 changes: 2 additions & 0 deletions conf.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
IS_WEB = false

function love.conf(table_)
table_.window.height = 320
table_.window.width = 480
Expand Down
20 changes: 10 additions & 10 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ local function draw()
--TileMap.render_tiles(tiles["decor"].tiles, tileset, camera, TILE_SIZE, WIDTH, HEIGHT)
--TileMap.render_tiles(tiles["collectibles"].tiles, tileset, camera, TILE_SIZE, WIDTH, HEIGHT, x_offset)
--TileMap.render_tiles(tiles["enemies"].tiles, tileset, camera, TILE_SIZE, WIDTH, HEIGHT)
TileMap.render(tiles["terrain"].tiles, tileset, camera, TILE_SIZE)
TileMap.render(tiles["decor"].tiles, tileset, camera, TILE_SIZE)
TileMap.render(tiles["collectibles"].tiles, tileset, camera, TILE_SIZE, x_offset)
TileMap.render(tiles["terrain"].tiles, tileset, camera, TILE_SIZE, WIDTH, HEIGHT)
TileMap.render(tiles["decor"].tiles, tileset, camera, TILE_SIZE, WIDTH, HEIGHT)
TileMap.render(tiles["collectibles"].tiles, tileset, camera, TILE_SIZE, WIDTH, HEIGHT, x_offset)
player:render(camera)

for _, entity in ipairs(entities) do
Expand Down Expand Up @@ -331,7 +331,7 @@ function love.keypressed(key)
elseif key == "m" then
muted = not muted
love.audio.setVolume(muted and 0 or 1)
elseif key == "escape" then
elseif key == "escape" and not IS_WEB then
love.event.quit(0)
end

Expand Down Expand Up @@ -362,16 +362,16 @@ local function handle_player_stop_walk(key)
["s"] = player.start_move_down,
}
for key_, _ in pairs(keys) do
if key_ ~= key then goto continue end
if key_ == key then

for key__, func in pairs(keys) do
if love.keyboard.isDown(key__) then
func(player)
return
for key__, func in pairs(keys) do
if love.keyboard.isDown(key__) then
func(player)
return
end
end
end
player:stop()
::continue::
end
end

Expand Down
11 changes: 3 additions & 8 deletions src/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,19 @@ function Player.construct(args)
for x_offs = -1, 1 do
for y_offs = -1, 1 do
local tile = tiles:get(x + x_offs, y + y_offs)
if tile == nil then
goto continue
end

local tile_rect = TileMap.get_tile_rect(x + x_offs, y + y_offs, self.TILE_SIZE)
local player_rect = player:get_rect()

-- HACK - open doors
local DOOR = 539
if tile.index - 1 == DOOR and (self.inventory.items["key"] or 0) > 0 then
if tile and tile.index - 1 == DOOR and (self.inventory.items["key"] or 0) > 0 then
tiles.tiles[x + x_offs][y + y_offs] = nil -- remove door
self.unlock_sound:play()
self.inventory.items["key"] = self.inventory.items["key"] - 1
goto continue
tile = nil
end

if Collision.colliding(player_rect, tile_rect) then
if tile and Collision.colliding(player_rect, tile_rect) then
local speed_x = self.x - self.previous_x
local speed_y = self.y - self.previous_y
if speed_x ~= 0 then
Expand All @@ -222,7 +218,6 @@ function Player.construct(args)
end
end
end
::continue::
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/shader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ vec4 effect(vec4 color, Image image, vec2 uvs, vec2 texture_coords) {
//float dist = distance(u_light_pos / u_resolution, texture_coords / u_resolution);
vec2 middle = vec2(0.41, 0.51); // approximately where character is
float pulse = 0.03 * sin(u_time * 3);
float pulse = 0.03 * sin(u_time * 3.0);
float dist = distance(middle, texture_coords / u_resolution);
float factor = 1 - pow(dist, u_lighting_dist + pulse);
float total_factor = factor * (1 - u_factor);
float factor = 1.0 - pow(dist, u_lighting_dist + pulse);
float total_factor = factor * (1.0 - u_factor);
//float factor = (MAX_DIST - dist) / MAX_DIST;
// special treatment for red for torch effect
Expand Down
2 changes: 1 addition & 1 deletion src/sprite_sheet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ function SpriteSheet.load_sprite_sheet(filepath, width, height)
table.insert(quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
return {image = image, quads = quads, size = #quads}
return {image = image, quads = quads, size = #quads, sprite_batch = love.graphics.newSpriteBatch(image)}
end
81 changes: 35 additions & 46 deletions src/tilemap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ function TileMap.construct_tiles(tilemap, tileset)
for _, layer in ipairs(tilemap.layers) do
local name = layer.name
for i, tile_index in ipairs(layer.data) do
-- skip empty tiles
if tile_index == 0 then goto continue end
if tile_index ~= 0 then -- skips empty tiles

i = i - 1
local x = i % width
local y = math.floor(i / width)
layers[name].tiles[x][y] = {index=tile_index, quad=tileset.quads[tile_index]}
::continue::
i = i - 1
local x = i % width
local y = math.floor(i / width)
layers[name].tiles[x][y] = {index=tile_index, quad=tileset.quads[tile_index]}
end
end
end

Expand All @@ -60,53 +59,43 @@ function TileMap.construct_collision_map(tilemap, layer_name, passable_getter)
local tiles = {}
local width = tilemap.width
for _, layer in ipairs(tilemap.layers) do
if layer.name ~= layer_name then goto continue end
for i = 0, tilemap.width do
tiles[i] = {}
end
for i, tile_index in ipairs(layer.data) do
local x = i % width
local y = math.floor(i / width)
tiles[x][y] = passable_getter(tile_index)
if layer.name == layer_name then
for i = 0, tilemap.width do
tiles[i] = {}
end
for i, tile_index in ipairs(layer.data) do
local x = i % width
local y = math.floor(i / width)
tiles[x][y] = passable_getter(tile_index)
end
end
::continue::
end
return tiles
end

-- offset is optional
function TileMap.render_tiles(tiles, tileset, camera, tilesize, width, height, y_offset)
y_offset = y_offset or 0
local tiles_min_x = camera.total_x / tilesize - 100
local tiles_min_y = camera.total_y / tilesize - 100
local tiles_max_x = (width + camera.total_x) / tilesize + 100
local tiles_max_y = (height + camera.total_y) / tilesize + 100

for x, col in pairs(tiles) do
if x < tiles_min_x then goto continuex end
if x > tiles_max_x then break end
for y, tile in pairs(col) do
if y < tiles_min_y or tile == nil then goto continuey end
if y > tiles_max_y then break end
local transform = love.math.newTransform(
x * tilesize - camera.total_x, y * tilesize - camera.total_y + y_offset
)
love.graphics.draw(tileset.image, tile.quad, transform)
::continuey::
end
::continuex::
end
end
-- function TileMap.render(tiles, tileset, camera, tilesize, y_offset)
-- y_offset = y_offset or 0
-- for x, col in pairs(tiles) do
-- for y, tile in pairs(col) do
-- local transform = love.math.newTransform(
-- x * tilesize - camera.total_x, y * tilesize - camera.total_y + y_offset
-- )
-- love.graphics.draw(tileset.image, tile.quad, transform)
-- end
-- end
-- end

function TileMap.render(tiles, tileset, camera, tilesize, y_offset)
y_offset = y_offset or 0
function TileMap.render(tiles, tileset, camera, tilesize, width, height, y_offset)
tileset.sprite_batch:clear()
y_offset = (y_offset or 0) - camera.total_y
for x, col in pairs(tiles) do
local x_transform = x * tilesize - camera.total_x
for y, tile in pairs(col) do
local transform = love.math.newTransform(
x * tilesize - camera.total_x, y * tilesize - camera.total_y + y_offset
)
love.graphics.draw(tileset.image, tile.quad, transform)
::continue::
local y_transform = y * tilesize + y_offset
if x_transform < width and y_transform < height and not tile.collected then
tileset.sprite_batch:add(tile.quad, x_transform, y_transform)
end
end
end
love.graphics.draw(tileset.sprite_batch)
end

0 comments on commit 8e77cc3

Please sign in to comment.