diff --git a/conf.lua b/conf.lua index ef6f85f..7e535b2 100644 --- a/conf.lua +++ b/conf.lua @@ -1,3 +1,5 @@ +IS_WEB = false + function love.conf(table_) table_.window.height = 320 table_.window.width = 480 diff --git a/main.lua b/main.lua index 227cad4..5ad3fda 100644 --- a/main.lua +++ b/main.lua @@ -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 @@ -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 @@ -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 diff --git a/src/player.lua b/src/player.lua index 7115b8a..7730c8f 100644 --- a/src/player.lua +++ b/src/player.lua @@ -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 @@ -222,7 +218,6 @@ function Player.construct(args) end end end - ::continue:: end end end diff --git a/src/shader.lua b/src/shader.lua index d66ab5d..1319c4c 100644 --- a/src/shader.lua +++ b/src/shader.lua @@ -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 diff --git a/src/sprite_sheet.lua b/src/sprite_sheet.lua index 5129f3b..12edd6d 100644 --- a/src/sprite_sheet.lua +++ b/src/sprite_sheet.lua @@ -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 diff --git a/src/tilemap.lua b/src/tilemap.lua index c78b200..a8107d5 100644 --- a/src/tilemap.lua +++ b/src/tilemap.lua @@ -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 @@ -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