diff --git a/_debug_helpers.lua b/_debug_helpers.lua index 6005198..3ecde16 100644 --- a/_debug_helpers.lua +++ b/_debug_helpers.lua @@ -14,6 +14,5 @@ function printMatrix(matrix) end function randomPosition() - print("hfhfhhdh") return math.random(1, gridWidth), math.random(1, gridHeight) end \ No newline at end of file diff --git a/art/test_object.png b/art/test_object.png new file mode 100644 index 0000000..3f31bc2 Binary files /dev/null and b/art/test_object.png differ diff --git a/artifact.lua b/artifact.lua index 75692fc..c8df85b 100644 --- a/artifact.lua +++ b/artifact.lua @@ -8,6 +8,6 @@ function Artifact:initialize(x, y, color) y = y, color = color, collision = "unmoveable", - sprite = artifactSprite + sprite = sprites.artifact }) end \ No newline at end of file diff --git a/assorted.lua b/assorted.lua index e160d5f..83b790f 100644 --- a/assorted.lua +++ b/assorted.lua @@ -8,6 +8,7 @@ function Ass:initialize(x, y, color, folder, name) x = x, y = y, color = color, + cellShape = spriteCells["mac_smaller"], collision = "moveable", --player can't push other player sprite = spr }) diff --git a/box.lua b/box.lua index f3b9b5d..0b53454 100644 --- a/box.lua +++ b/box.lua @@ -6,11 +6,12 @@ function Box:initialize(x, y, color) Entity.initialize(self, { x = x, y = y, + name = "box", sizeX = 1, sizeY = 1, color = color, collision = "moveable", - sprite = boxSprite[color] + sprite = sprites.box[color] }) end diff --git a/door.lua b/door.lua index 0e2dabb..a2b0a5c 100644 --- a/door.lua +++ b/door.lua @@ -8,7 +8,7 @@ function Door:initialize(x, y, colors) y = y, color = "white", collision = "unmoveable", - sprite = doorSprite["white"] --doesn't matter for now which we put here b/c, we override the draw function neway + sprite = sprites.door["green"] --doesn't matter for now which we put here b/c, we override the draw function neway }) self.locked = true @@ -24,17 +24,17 @@ function Door:draw() if active then love.graphics.setColor(gColor[color]:set()) local p = self:getDrawPos() - love.graphics.draw(doorSprite[color], p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) + love.graphics.draw(sprites.door[color], p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) end end love.graphics.setColor(gColor.white:set()) local p = self:getDrawPos() - love.graphics.draw(doorSprite["white"], p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) + love.graphics.draw(sprites.door["white"], p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) else for color, active in pairs(self.colors) do love.graphics.setColor(gColor[color]:set()) local p = self:getDrawPos() - love.graphics.draw(doorSprite["unlocked"], p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) + love.graphics.draw(sprites.door["unlocked"], p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) end end end diff --git a/entity.lua b/entity.lua index 4e90f8a..b5033f6 100644 --- a/entity.lua +++ b/entity.lua @@ -2,6 +2,7 @@ Entity = class("Entity") function Entity:initialize(t) self.x, self.y = t.x, t.y + self.name = t.name self.spriteSize = {w = t.sprite:getWidth(), h = t.sprite:getHeight()} self.size = {w = self.spriteSize.w / 16, h = self.spriteSize.h / 16} self.cellShape = t.cellShape or self:cellShapeFromBox(self.size) @@ -52,10 +53,8 @@ function Entity:cellShapeFromBox(box) for i = 1, box.w do for j = 1, box.h do table.insert(cells, {x = i, y = j}) - io.write(" boop: " .. i .. ", " .. j) end end - io.write("\n") return cells end diff --git a/main.lua b/main.lua index 3bba00f..aec1818 100644 --- a/main.lua +++ b/main.lua @@ -11,13 +11,14 @@ require "input" require "color" require "room" require "shaders" +require "sprite_manager" function love.load() _initDefaults() _initGridVars() _initSounds() _initGlobalColors() - loadSprites() + processSprites() gameCanvas = love.graphics.newCanvas() finalCanvas = love.graphics.newCanvas() @@ -130,59 +131,4 @@ function _initSounds() static:setVolume(.15) static:play() end -function createCanvases() -end - -function loadSprites() - playerSprite = love.graphics.newImage("art/player.png") - wallSprite = love.graphics.newImage("art/static_wall.png") - boxSprite = { - red = love.graphics.newImage("art/wall_r.png"), - green = love.graphics.newImage("art/wall_g.png"), - blue = love.graphics.newImage("art/wall_b.png") - } - doorSprite = { - white = love.graphics.newImage("art/door_locked_w.png"), - red = love.graphics.newImage("art/door_locked_r.png"), - green = love.graphics.newImage("art/door_locked_g.png"), - blue = love.graphics.newImage("art/door_locked_b.png"), - unlocked = love.graphics.newImage("art/door_unlocked.png") - } - switchSprite = { - red = love.graphics.newImage("art/switch_r.png"), - green = love.graphics.newImage("art/switch_g.png"), - blue = love.graphics.newImage("art/switch_b.png") - } - waterSprite = { - red = love.graphics.newImage("art/nature/processed/water_r.png"), - green = love.graphics.newImage("art/nature/processed/water_g.png"), - blue = love.graphics.newImage("art/nature/processed/water_b.png") - } - npcSprites = { - - } - - -- local artPieceJsonString - -- for line in io.lines("art/museum/to_process/art.json") do - -- artPieceJsonString = line - -- end - - -- artPieceSpritesTable = json.decode(artPieceJsonString) - -- print(artPieceSpritesTable["saturn_son"]["red"]) - -- print(artSpriteJson) - -- -- sets the default input file as test.lua - -- io.input(artSpriteJson) - - -- -- prints the first line of the file - -- artSpriteJsonString = io.read() - - -- -- closes the open file - -- io.close(artSpriteJson) - - -- artSpriteTable = json.decode(artSpriteJsonString) - - -- print(artSpriteTable) - - artifactSprite = love.graphics.newImage("art/process/cone.png") -end \ No newline at end of file diff --git a/npc.lua b/npc.lua index 4d9f972..3b723bb 100644 --- a/npc.lua +++ b/npc.lua @@ -6,6 +6,6 @@ function npc:initialize(x, y, color, name) y = y, color = color, collision = "none", --player can't push other player - sprite = npcSprites[name][color] + sprite = sprites.npc[name][color] }) end \ No newline at end of file diff --git a/player.lua b/player.lua index 412ab29..0ae0233 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 = playerSprite + sprite = sprites.player }) end diff --git a/room.lua b/room.lua index ee4cff2..c786319 100644 --- a/room.lua +++ b/room.lua @@ -33,15 +33,15 @@ function Room:initialize(t) -- self.activeColors[color] = true -- end - -- for i=1, 30 do - -- local x, y = randomPosition() - -- local door = Door:new(x, y, self.colorsPlayerHas) - -- table.insert(self.doors, door) + for i=1, 1 do + local x, y = randomPosition() + -- local door = Door:new(x, y, self.colorsPlayerHas) + -- table.insert(self.doors, door) - -- for color, active in pairs(self.activeColors) do - -- self.collideables[color]["w: " .. x .. ", " .. y] = door - -- end - -- end + for color, active in pairs(self.activeColors) do + self.collideables[color]["w: " .. x .. ", " .. y] = Wall:new(x, y, color) + end + end for i=0, 5 do local x, y = randomPosition() @@ -226,7 +226,7 @@ function Room:movePlayer(key) local targetCells - local pull = {} + local pull --for each color... targetCells = {initialTargetCell} -- ...we start with our attempted movement... local doneSearching @@ -246,7 +246,7 @@ function Room:movePlayer(key) table.insert(nextTargetCells, projectedCell) end else - pull[color] = false + pull = false doneSearching = true end end @@ -262,6 +262,7 @@ function Room:movePlayer(key) --if every projected cell is empty then we can do the move if #emptyCells == #targetCells then + pull = true doneSearching = true end @@ -272,8 +273,10 @@ function Room:movePlayer(key) for _, entity in pairs(entitiesToPush) do entity:move(axis, dir[axis]) end - for _, entity in pairs(entitiesToPull) do - entity:move(axis, dir[axis]) + if pull == true then + for _, entity in pairs(entitiesToPull) do + entity:move(axis, dir[axis]) + end end self.player[color]:move(axis, dir[axis]) moved = true diff --git a/sprite_manager.lua b/sprite_manager.lua new file mode 100644 index 0000000..1f59cd8 --- /dev/null +++ b/sprite_manager.lua @@ -0,0 +1,112 @@ +sprites = { + player = love.graphics.newImage("art/player.png"), + wall = love.graphics.newImage("art/static_wall.png"), + box = { + red = love.graphics.newImage("art/wall_r.png"), + green = love.graphics.newImage("art/wall_g.png"), + blue = love.graphics.newImage("art/wall_b.png") + }, + door = { + white = love.graphics.newImage("art/door_locked_w.png"), + red = love.graphics.newImage("art/door_locked_r.png"), + green = love.graphics.newImage("art/door_locked_g.png"), + blue = love.graphics.newImage("art/door_locked_b.png"), + unlocked = love.graphics.newImage("art/door_unlocked.png") + }, + switch = { + red = love.graphics.newImage("art/switch_r.png"), + green = love.graphics.newImage("art/switch_g.png"), + blue = love.graphics.newImage("art/switch_b.png") + }, + water = { + red = love.graphics.newImage("art/nature/processed/water_r.png"), + green = love.graphics.newImage("art/nature/processed/water_g.png"), + blue = love.graphics.newImage("art/nature/processed/water_b.png") + }, + mac_smaller = love.graphics.newImage("art/tech/processed/mac_smaller_r.png"), + test_object = love.graphics.newImage("art/test_object.png"), + npcs = { + + }, + artifact = love.graphics.newImage("art/process/cone.png"), +} + +spriteCells = {} + + +function processSprites() + getCellsFromSprite(sprites.player) + + for name, spriteObj in pairs(sprites) do + print(name) + if type(spriteObj) == "table" then + spriteCells[name] = {} + --this is multicolored so... + for color, sprite in pairs(spriteObj) do + spriteCells[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 + print("empty @ ", cell.x, cell.y) + 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 e8a73b3..3788df5 100644 --- a/switch.lua +++ b/switch.lua @@ -15,7 +15,7 @@ function Switch:initialize(x, y, color) y = y, color = color, collision = "none", --switches are floor elements that can't be pushed or bloc movement!! - sprite = Switch.sprites[color] + sprite = sprites.switch[color] }) self.activated = false diff --git a/wall.lua b/wall.lua index fa309a0..a9451d9 100644 --- a/wall.lua +++ b/wall.lua @@ -9,8 +9,9 @@ function Wall:initialize(x, y, color) sizeX = 1, sizeY = 1, color = color, + cellShape = spriteCells.test_object, collision = "unmoveable", - sprite = wallSprite + sprite = sprites.test_object }) end