From b553d3f66492f6027a93fa98e84c39805e6658ec Mon Sep 17 00:00:00 2001 From: 0x81c Date: Sun, 15 Mar 2015 05:26:47 -0400 Subject: [PATCH] some changes, idr, hehe sorry --- _debug_helpers.lua | 5 ++ entity.lua | 8 ++- main.lua | 5 ++ room.lua | 129 ++++++++++++++++++++++++++++----------------- switch.lua | 17 ++++++ 5 files changed, 116 insertions(+), 48 deletions(-) create mode 100644 switch.lua diff --git a/_debug_helpers.lua b/_debug_helpers.lua index 6aa47f8..6005198 100644 --- a/_debug_helpers.lua +++ b/_debug_helpers.lua @@ -11,4 +11,9 @@ function printMatrix(matrix) end io.write("\n") end +end + +function randomPosition() + print("hfhfhhdh") + return math.random(1, gridWidth), math.random(1, gridHeight) end \ No newline at end of file diff --git a/entity.lua b/entity.lua index bf35d22..d4ce582 100644 --- a/entity.lua +++ b/entity.lua @@ -8,6 +8,12 @@ function Entity:initialize(t) self.collision = t.collision --"none", "moveable", "unmoveable" self.sprite = t.sprite self.shaAmount = t.sha or 1 + + self.drawOffset = {x = 0, y = 0} + if self.size.w < 1 or self.size.h < 1 then + self.drawOffset.x = (drawScale * 16 * self.size.w) / 2 + self.drawOffset.y = (drawScale * 16 * self.size.h) / 2 + end end function Entity:getCollision() @@ -70,7 +76,7 @@ end function Entity:draw() love.graphics.setColor(self.color:set()) local p = self:getDrawPos() - love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale) + love.graphics.draw(self.sprite, p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale) end function Entity:canMove() diff --git a/main.lua b/main.lua index 077d9c1..a362ae0 100644 --- a/main.lua +++ b/main.lua @@ -133,6 +133,11 @@ function loadSprites() green = love.graphics.newImage("art/wall_g.png"), blue = love.graphics.newImage("art/wall_b.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"), diff --git a/room.lua b/room.lua index 3bff1b1..da42c82 100644 --- a/room.lua +++ b/room.lua @@ -7,16 +7,14 @@ require "artifact" require "npc" require "art" require "assorted" +require "switch" function Room:initialize(t) self.exits = t.exits self.name = t.name self.player = Player:new(t.player.x, t.player.y, t.player.activeColors) - self.collideables = { - red = {}, - green = {}, - blue = {} - } + self.switches = { red = {}, green = {}, blue = {} } + self.collideables = { red = {}, green = {}, blue = {} } self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...} self.activeColors = { @@ -44,6 +42,13 @@ function Room:initialize(t) end end + for i = 0, 5 do + local x, y = randomPosition() + print(x, y) + local c = randomColor() + table.insert(self.switches[c], Switch:new(x, y, c)) + end + for color, active in pairs(self.activeColors) do local x, y = 6, 3 -- if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "animals/processed/", "me_rach") end @@ -77,6 +82,11 @@ function Room:draw() entity:draw() end end + for color, switchArray in pairs(self.switches) do + for _, switch in pairs(switchArray) do + switch:draw() + end + end end function Room:createCollideablesMatrix(collideables) @@ -128,7 +138,7 @@ function Room:movePlayer(key) dir.x, axis = 1, "x" end - local move + local move = true local entitiesToPush = { red = {}, green = {}, @@ -146,55 +156,58 @@ function Room:movePlayer(key) x = pos.x + dir.x, y = pos.y + dir.y } -- this is for reference for each color as we change the targetCells table - local move = true - local targetCells + if not self:isInBounds(initialTargetCell) then + move = true + else + local targetCells - --so the way this worx is.... - for _, color in pairs(activeColors) do - --for each color... - targetCells = {initialTargetCell} -- ...we start with our attempted movement... - local doneSearching - while not doneSearching do - nextTargetCells = {} -- ...then we create this array to save our next group of cells - local emptyCells = {} - for _, cell in pairs(targetCells) do - if self:isInBounds(cell) then - local entity = self:getCellInMatrix(self.collidableMatrices[color], cell) - if entity then - --something is here!! - if not entitiesToPush[color][entity] then --if the entity hasnt been found already - if entity:canMove() then - local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here - entitiesToPush[color][entity] = entity - for _, projectedCell in ipairs(projectedCells) do - table.insert(nextTargetCells, projectedCell) + --so the way this worx is.... + for _, color in pairs(activeColors) do + --for each color... + targetCells = {initialTargetCell} -- ...we start with our attempted movement... + local doneSearching + while not doneSearching do + nextTargetCells = {} -- ...then we create this array to save our next group of cells + local emptyCells = {} + for _, cell in pairs(targetCells) do + if self:isInBounds(cell) then + local entity = self:getCellInMatrix(self.collidableMatrices[color], cell) + if entity then + --something is here!! + if not entitiesToPush[color][entity] then --if the entity hasnt been found already + if entity:canMove() then + local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here + entitiesToPush[color][entity] = entity + for _, projectedCell in ipairs(projectedCells) do + table.insert(nextTargetCells, projectedCell) + end + else + move = false + doneSearching = true end - else - move = false - doneSearching = true end + else + --there's nothin there!! + table.insert(emptyCells, cell) end - else - --there's nothin there!! - table.insert(emptyCells, cell) + else + --we are out of bounds now + move = false + doneSearching = true end - else - --we are out of bounds now - move = false + end + + --if every projected cell is empty then we can do the move + if #emptyCells == #targetCells then + move = true doneSearching = true end - end - --if every projected cell is empty then we can do the move - if #emptyCells == #targetCells then - move = true - doneSearching = true + targetCells = nextTargetCells end - - targetCells = nextTargetCells + --let's get out of this loop b/c if we can't move one color we can't move at all + if move == false then break end end - --let's get out of this loop b/c if we can't move one color we can't move at all - if move == false then break end end @@ -309,9 +322,31 @@ function Room:isInBounds(cell) end function Room:nextRoomCheck() - + if not self:isInBounds(self.player:getGridPos()) then + nextRoom(self.player:getGridPos()) + end end -function nextRoom() +function nextRoom(pos) + local exit + local newPos = {x = pos.x, y = pos.y} + if pos.x > gridWidth then + exit = "right" + newPos.x = 1 + elseif pos.x < 1 then + exit = "left" + newPos.x = gridWidth + elseif pos.y > gridHeight then + exit = "bottom" + newPos.y = 1 + elseif pos.y < 1 then + exit = "top" + newPos.y = gridHeight + end + currentRoom = Room:new{ + player = { + x = newPos.x, y = newPos.y, activeColors = currentRoom.player:getActiveColors() + } + } end \ No newline at end of file diff --git a/switch.lua b/switch.lua new file mode 100644 index 0000000..1bb19ae --- /dev/null +++ b/switch.lua @@ -0,0 +1,17 @@ +require "entity" + +Switch = class("Switch", Entity) + +function Switch:initialize(x, y, color) + print("hey!!", x, y, color) + print(color) + Entity.initialize(self, { + x = x, + y = y, + sizeX = 1, + sizeY = 1, + color = color, + collision = "none", --switches are floor elements that can't be pushed or bloc movement!! + sprite = switchSprite[color] + }) +end \ No newline at end of file