From 7fa9171ba11275fdba9c3af8760898e7c83f2370 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 7 Aug 2026 01:05:08 -0400 Subject: [PATCH] kill dead code --- _helpers.lua | 64 ++++++++++++++++++++++++++ box.lua | 21 --------- door.lua | 10 ++-- main.lua | 4 -- player.lua | 2 +- room.lua | 13 +----- sprite_manager.lua | 112 --------------------------------------------- switch.lua | 9 +--- wall.lua | 24 ---------- 9 files changed, 72 insertions(+), 187 deletions(-) delete mode 100644 box.lua delete mode 100644 sprite_manager.lua delete mode 100644 wall.lua diff --git a/_helpers.lua b/_helpers.lua index 9f45d97..ccf5465 100644 --- a/_helpers.lua +++ b/_helpers.lua @@ -45,3 +45,67 @@ function readFromSource(relPath) end return love.filesystem.read(relPath) end + +-- Cached image loader. The filesystem is the source of truth for art, so every +-- sprite is loaded on demand by path (deduped here) rather than from a +-- hand-maintained central table. +local spriteCache = {} +function loadSprite(path) + if not spriteCache[path] then + spriteCache[path] = love.graphics.newImage(path) + end + return spriteCache[path] +end + +function cellShapeFromBox(box) + local cells = {} + for i = 1, box.w do + for j = 1, box.h do + table.insert(cells, {x = i, y = j}) + end + end + return cells +end + +-- Which 16x16 cells of a sprite are non-empty, for multi-cell collision shapes. +function getCellsFromSprite(sprite) + local w, h = sprite:getWidth(), sprite:getHeight() + local box = {w = w / 16, h = h / 16} + local boxCells = cellShapeFromBox(box) + if box.w < 1 or box.h < 1 then + return boxCells + end + + local img = sprite:getData() + local cellsToRemove = {} + + for _, cell in pairs(boxCells) do + local startPixel = {x = (cell.x - 1) * 16, y = (cell.y - 1) * 16} + local endPixel = {x = (cell.x * 16) - 1, y = (cell.y * 16) - 1} + local empty = true + + for i = startPixel.x, endPixel.x do + for j = startPixel.y, endPixel.y do + local r, g, b, a = img:getPixel(i, j) + if a > 0 and (r > 0 or g > 0 or b > 0) then + empty = false + end + end + end + + if empty then + cellsToRemove[cell] = cell + end + end + + local i = 1 + while i <= #boxCells do + if boxCells[i] == cellsToRemove[boxCells[i]] then + table.remove(boxCells, i) + else + i = i + 1 + end + end + + return boxCells +end diff --git a/box.lua b/box.lua deleted file mode 100644 index a782f68..0000000 --- a/box.lua +++ /dev/null @@ -1,21 +0,0 @@ -require "entity" - -Box = class("Box", Entity) - -function Box:initialize(x, y, color) - Entity.initialize(self, { - x = x, - y = y, - color = color, - collision = "moveable", - sprite = sprites.box[color] - }) -end - -function Box:update() - -end - -function Box:tick() - -end \ No newline at end of file diff --git a/door.lua b/door.lua index 3eb96a6..978e5d4 100644 --- a/door.lua +++ b/door.lua @@ -9,9 +9,9 @@ function Door:initialize(x, y) color = "white", collision = "unmoveable", sprites = { - red = sprites.door.red, - green = sprites.door.green, - blue = sprites.door.blue, + red = loadSprite("art/game/door_r.png"), + green = loadSprite("art/game/door_g.png"), + blue = loadSprite("art/game/door_b.png"), }, }) self.locked = true @@ -29,9 +29,9 @@ function Door:refreshSprites() self.sprites = {} for _, c in ipairs({"red", "green", "blue"}) do if self.locked then - if self.colors[c] then self.sprites[c] = sprites.door[c] end + if self.colors[c] then self.sprites[c] = loadSprite("art/game/door_" .. c:sub(1, 1) .. ".png") end else - self.sprites[c] = sprites.door.unlocked + self.sprites[c] = loadSprite("art/game/door_unlocked.png") end end end diff --git a/main.lua b/main.lua index 80cc202..24d7dc8 100644 --- a/main.lua +++ b/main.lua @@ -11,7 +11,6 @@ require "input" require "color" require "room" require "shaders" -require "sprite_manager" require "level_editor/editor" function love.load() @@ -23,9 +22,6 @@ function love.load() _initGlobalColors() _initLevelEditor() - - processSprites() - gameCanvas = love.graphics.newCanvas() sideBarCanvas = love.graphics.newCanvas(sideBarBox.w, sideBarBox.h) finalCanvas = love.graphics.newCanvas() diff --git a/player.lua b/player.lua index 0ae0233..85b8686 100644 --- a/player.lua +++ b/player.lua @@ -13,7 +13,7 @@ function Player:initialize(x, y, color) y = y, color = color, collision = "none", --player can't push other player - sprite = sprites.player + sprite = loadSprite("art/game/player.png") }) end diff --git a/room.lua b/room.lua index 0d2ef9d..8a7e695 100644 --- a/room.lua +++ b/room.lua @@ -2,8 +2,6 @@ Room = class("Room") require "libs/TSerial" require "player" -require "box" -require "wall" require "artifact" require "npc" require "art" @@ -14,15 +12,6 @@ require "dialogue_manager" require "generic_moveable" require "generic_unmoveable" --- cache loaded images so repeated placing/loading doesn't re-decode PNGs -local placeImageCache = {} -local function loadPlaceImage(path) - if not placeImageCache[path] then - placeImageCache[path] = love.graphics.newImage(path) - end - return placeImageCache[path] -end - function Room:initialize(t) self.player = {} self.switches = { red = {}, green = {}, blue = {} } @@ -558,7 +547,7 @@ function Room:createEntity(class, color, x, y) self:registerPlayer(entity, color) self:setActiveColor(color, true) elseif props and props.sprites and props.sprites[color] then - local sprite = loadPlaceImage(props.sprites[color]) + local sprite = loadSprite(props.sprites[color]) if behavior == "moveable" then entity = GenericMoveable:new(x, y, color, sprite, class) else diff --git a/sprite_manager.lua b/sprite_manager.lua deleted file mode 100644 index f9c02f8..0000000 --- a/sprite_manager.lua +++ /dev/null @@ -1,112 +0,0 @@ -sprites = { - player = love.graphics.newImage("art/game/player.png"), - wall = love.graphics.newImage("art/game/static_wall.png"), - box = { - red = love.graphics.newImage("art/game/box_r.png"), - green = love.graphics.newImage("art/game/box_g.png"), - blue = love.graphics.newImage("art/game/box_b.png") - }, - door = { - white = love.graphics.newImage("art/game/door_locked_w.png"), - red = love.graphics.newImage("art/game/door_r.png"), - green = love.graphics.newImage("art/game/door_g.png"), - blue = love.graphics.newImage("art/game/door_b.png"), - unlocked = love.graphics.newImage("art/game/door_unlocked.png") - }, - switch = { - red = love.graphics.newImage("art/game/switch_r.png"), - green = love.graphics.newImage("art/game/switch_g.png"), - blue = love.graphics.newImage("art/game/switch_b.png") - }, - water = { - red = love.graphics.newImage("art/nature/water_r.png"), - green = love.graphics.newImage("art/nature/water_g.png"), - blue = love.graphics.newImage("art/nature/water_b.png") - }, - mac_smaller = love.graphics.newImage("art/tech/mac_smaller_r.png"), - tree = { - red = love.graphics.newImage("art/nature/palm_scaled_r.png"), - green = love.graphics.newImage("art/nature/palm_scaled_g.png"), - blue = love.graphics.newImage("art/nature/palm_scaled_b.png") - }, - npcs = { - - }, -} - -spriteCells = {} - -function processSprites() - getCellsFromSprite(sprites.player) - - for name, spriteObj in pairs(sprites) do - if type(spriteObj) == "table" then - spriteCells[name] = {} - --this is multicolored so... - for color, sprite in pairs(spriteObj) do - spriteCells[name][color] = getCellsFromSprite(sprite) - end - else - --assume it's an image obj - spriteCells[name] = getCellsFromSprite(spriteObj) - end - end -end - -function getCellsFromSprite(sprite) - local w, h = sprite:getWidth(), sprite:getHeight() - local box = {w = w / 16, h = h / 16} - local boxCells = cellShapeFromBox(box) - if box.w < 1 or box.h < 1 then - return boxCells - end - - local img = sprite:getData() - - local cellsToRemove = {} - local stop - - for _, cell in pairs(boxCells) do - - local startPixel = {x = (cell.x - 1) * 16, y = (cell.y - 1) * 16} - local endPixel = {x = (cell.x * 16) - 1, y = (cell.y * 16) - 1} - - local empty = true - - for i = startPixel.x, endPixel.x do - for j = startPixel.y, endPixel.y do - local r, g, b, a = img:getPixel(i, j) - if a > 0 and (r > 0 or g > 0 or b > 0) then - --this cell has color so - empty = false - end - end - end - - if empty then - cellsToRemove[cell] = cell --references boxCells cell so comparison should wor_ - end - end - - local i = 1 - while i <= #boxCells do - if boxCells[i] == cellsToRemove[boxCells[i]] then - table.remove(boxCells, i) - else - i = i + 1 - end - end - - shallowTablePrint(boxCells) - return boxCells -end - -function cellShapeFromBox(box) - local cells = {} - for i = 1, box.w do - for j = 1, box.h do - table.insert(cells, {x = i, y = j}) - end - end - return cells -end diff --git a/switch.lua b/switch.lua index 8213258..f48689c 100644 --- a/switch.lua +++ b/switch.lua @@ -2,13 +2,6 @@ require "entity" Switch = class("Switch", Entity) -Switch.static.sprites = { - red = love.graphics.newImage("art/game/switch_r.png"), - green = love.graphics.newImage("art/game/switch_g.png"), - blue = love.graphics.newImage("art/game/switch_b.png"), - pressed = love.graphics.newImage("art/game/switch_pressed.png") -} - function Switch:initialize(x, y, color) Entity.initialize(self, { x = x, @@ -16,7 +9,7 @@ function Switch:initialize(x, y, color) color = color, collision = "none", --switches are floor elements that can't be pushed or bloc movement!! cellShape = {{x = 1, y = 1}}, - sprite = sprites.switch[color] + sprite = loadSprite("art/game/switch_" .. color:sub(1, 1) .. ".png") }) self.activated = false diff --git a/wall.lua b/wall.lua deleted file mode 100644 index ee809d3..0000000 --- a/wall.lua +++ /dev/null @@ -1,24 +0,0 @@ -require "entity" - -Wall = class("Wall", Entity) - -function Wall:initialize(x, y, color) - Entity.initialize(self, { - x = x, - y = y, - sizeX = 1, - sizeY = 1, - color = color, - cellShape = spriteCells.wall, - collision = "unmoveable", - sprite = sprites.wall - }) -end - -function Wall:update(dt) - -end - -function Wall:tick() - -end \ No newline at end of file