From 1156564fa26e04320b8bda30f1ee9efec127612e Mon Sep 17 00:00:00 2001 From: 0x81c Date: Sun, 15 Mar 2015 15:34:17 -0400 Subject: [PATCH] player done, old way --- input.lua | 4 +- player.lua | 13 +-- room.lua | 241 ++++++++++++++++++++++++++++++++-------------------- shaders.lua | 29 +++++++ 4 files changed, 184 insertions(+), 103 deletions(-) diff --git a/input.lua b/input.lua index eeb1cf4..b4d5d46 100644 --- a/input.lua +++ b/input.lua @@ -1,8 +1,8 @@ Input = {} Input.playermovekeys = { up = "up", down = "down", left = "left", right = "right" } -Input.playerswitchkeys = { ["1"] = "1", ["2"] = "2", ["3"] = "3", ["4"] = "4" } -Input.colorfromkey = {"red", "green", "blue", "white"} +Input.playerswitchkeys = { ["1"] = "1", ["2"] = "2", ["3"] = "3"} +Input.colorfromkey = {"red", "green", "blue"} function Input:handler() diff --git a/player.lua b/player.lua index 2537b52..412ab29 100644 --- a/player.lua +++ b/player.lua @@ -7,15 +7,14 @@ Player.static.sounds = { switchSuccess = love.audio.newSource("sounds/switch_success.wav") } -function Player:initialize(x, y, activeColors) +function Player:initialize(x, y, color) Entity.initialize(self, { x = x, y = y, - color = "white", + color = color, collision = "none", --player can't push other player sprite = playerSprite }) - self.activeColors = activeColors or {"red", "green", "blue"} end function Player:setActiveColors(colorArray) @@ -40,11 +39,3 @@ function Player:playSwitchSound(success) sound:stop() sound:play() end - -function Player:draw() - for _, color in ipairs(self.activeColors) do - love.graphics.setColor(gColor[color]:set()) - local p = self:getDrawPos() - love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale) - end -end \ No newline at end of file diff --git a/room.lua b/room.lua index 316b2d6..a435af6 100644 --- a/room.lua +++ b/room.lua @@ -14,23 +14,26 @@ function Room:initialize(t) self.colorsPlayerHas = {"red", "green", "blue"} self.exits = t.exits self.name = t.name - self.player = Player:new(t.player.x, t.player.y, t.player.activeColors) + self.player = {} self.switches = { red = {}, green = {}, blue = {} } self.switchMatrices = {} self.collideables = { red = {}, green = {}, blue = {} } self.collidableMatrices = {} --{red = {{...}{...}{...}}, ... + for _, color in ipairs(t.player.activeColors) do + self.player[color] = Player:new(t.player.x, t.player.y, color) + end + self.doors = {} - self.activeColors = { - red = true, green = true, blue = true - } + self.activeColors = {} + for _, color in ipairs(t.player.activeColors) do self.activeColors[color] = true end -- for color, coords in pairs(t.player) do -- self.player[color] = Player:new(coords.x, coords.y, color) -- self.activeColors[color] = true -- end - for i=1, 5 do + for i=1, 30 do local x, y = randomPosition() local door = Door:new(x, y, self.colorsPlayerHas) table.insert(self.doors, door) @@ -65,9 +68,9 @@ function Room:initialize(t) local x, y = 7, 7 if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "food/processed/", "beer_mug") end + self.collideables[color]["fpp"] = Artifact:new(6, 6, color) end - -- self.collideables["green"]["fpp"] = Artifact:new(6, 6, "green") for color, entities in pairs(self.collideables) do self.collidableMatrices[color] = self:createEntityMatrix(entities) @@ -76,12 +79,16 @@ end function Room:update(dt) - self.player:update(dt) + for color, player in pairs(self.player) do + player:update(dt) + end end function Room:draw() - self.player:draw() + for color, player in pairs(self.player) do + player:draw() + end for color, entityArray in pairs(self.collideables) do for _, entity in pairs(entityArray) do entity:draw() @@ -134,50 +141,40 @@ function Room:getCellInMatrix(matrix, cell) end function Room:movePlayer(key) - local activeColors = self.player:getActiveColors() - local pos = self.player:getGridPos() - local x, y = pos.x, pos.y - local dir = {x = 0, y = 0} - local axis - if key == "up" then - dir.y, axis = -1, "y" - elseif key == "down" then - dir.y, axis = 1, "y" - elseif key == "left" then - dir.x, axis = -1, "x" - elseif key == "right" then - dir.x, axis = 1, "x" - end + local moved = false - local move = true - local entitiesToPush = { - red = {}, - green = {}, - blue = {} - } - local entitiesToPull = { - red = {}, - green = {}, - blue = {} - } + for _, color in pairs(self:getActiveColors()) do + local entitiesToPush, entitiesToPull = {}, {} + local pos = self.player[color]:getGridPos() + local x, y = pos.x, pos.y + local dir = {x = 0, y = 0} + local axis - -- PUSHHHHHHHHHHHHHHHHHHHHHH--------------------- + if key == "up" then + dir.y, axis = -1, "y" + elseif key == "down" then + dir.y, axis = 1, "y" + elseif key == "left" then + dir.x, axis = -1, "x" + elseif key == "right" then + dir.x, axis = 1, "x" + end - local initialTargetCell = { - 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 + -- PUSHHHHHHHHHHHHHHHHHHHHHH--------------------- - if not self:isInBounds(initialTargetCell) then - move = true - elseif self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell) and self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell):canPassOver() then - move = true - else - local targetCells + local initialTargetCell = { + x = pos.x + dir.x, y = pos.y + dir.y + } -- this is for reference for each color as we change the targetCells table + + if not self:isInBounds(initialTargetCell) then + move = true + elseif self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell) and self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell):canPassOver() 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 @@ -188,10 +185,10 @@ function Room:movePlayer(key) 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 not entitiesToPush[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 + entitiesToPush[entity] = entity for _, projectedCell in ipairs(projectedCells) do table.insert(nextTargetCells, projectedCell) end @@ -220,20 +217,16 @@ function Room:movePlayer(key) targetCells = nextTargetCells 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 - end - ---PULL CHEC + ---PULL CHEC - local initialTargetCell = {x = pos.x - dir.x, y = pos.y - dir.y} + local initialTargetCell = {x = pos.x - dir.x, y = pos.y - dir.y} - local targetCells + local targetCells - local pull = {} - --so the way this worx is.... - for _, color in pairs(activeColors) do + local pull = {} --for each color... targetCells = {initialTargetCell} -- ...we start with our attempted movement... local doneSearching @@ -245,10 +238,10 @@ function Room:movePlayer(key) local entity = self:getCellInMatrix(self.collidableMatrices[color], cell) if entity then --something is here!! - if not entitiesToPull[color][entity] then --if the entity hasnt been found already + if not entitiesToPull[entity] then --if the entity hasnt been found already if entity:canMove() then local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here - entitiesToPull[color][entity] = entity + entitiesToPull[entity] = entity for _, projectedCell in ipairs(projectedCells) do table.insert(nextTargetCells, projectedCell) end @@ -262,7 +255,6 @@ function Room:movePlayer(key) table.insert(emptyCells, cell) end else - pull[color] = true --we are out of bounds now doneSearching = true end @@ -270,58 +262,96 @@ function Room:movePlayer(key) --if every projected cell is empty then we can do the move if #emptyCells == #targetCells then - pull[color] = true doneSearching = true end targetCells = nextTargetCells 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 - - - if move then - for color, entities in pairs(entitiesToPush) do - for _, entity in pairs(entities) do + if move then + for _, entity in pairs(entitiesToPush) do entity:move(axis, dir[axis]) end - end - for color, entities in pairs(entitiesToPull) do - if pull[color] then - for _, entity in pairs(entities) do - entity:move(axis, dir[axis]) - end + for _, entity in pairs(entitiesToPull) do + entity:move(axis, dir[axis]) end + self.player[color]:move(axis, dir[axis]) + moved = true end - self.player:move(axis, dir[axis]) + + end + + if moved then self:tick() end end function Room:switchPlayerColor(color) - local colors - if color == "white" then colors = {"red", "green", "blue"} else colors = {color} end + local canSwitch - local canSwitch = true - local playerPos = self.player:getGridPos() - - for _, color in ipairs(colors) do - if self:getCellInMatrix(self.collidableMatrices[color], playerPos) then - canSwitch = false + if self:onlyColorIsActive(color) then + canSwitch = false + else + for _, c in pairs(self:getPossibleColors()) do + if c == color then + self:setActiveColor(c, true) + else + self:setActiveColor(c, false) + end end end - if canSwitch then - self.player:setActiveColors(colors) - end - self.player:playSwitchSound(canSwitch) + + self.player[color]:playSwitchSound(canSwitch) end function Room:setActiveColor(color, bool) self.activeColors[color] = bool end +function Room:getActiveColors() + local colors = {} + + for color, active in pairs(self.activeColors) do + if active then + table.insert(colors, color) + end + end + + return colors +end + +function Room:getInactiveColors() + local colors = {} + + for color, active in pairs(self.activeColors) do + if not active then + table.insert(colors, color) + end + end + + return colors +end + +function Room:getPossibleColors() + local colors = {} + + for _, color in ipairs(self.colorsPlayerHas) do + table.insert(colors, color) + print(color) + end + + return colors +end + +function Room:onlyColorIsActive(color) + local activeColors = self:getActiveColors() + if #activeColors == 1 and activeColors[1] == color then + return true + else + return false + end +end + function Room:tick() for color, entityArray in pairs(self.collideables) do for _, entity in pairs(entityArray) do @@ -335,9 +365,37 @@ function Room:tick() print(door) door:setColors(setDoorColors) end + self:playerJoinCheck() self:nextRoomCheck() end +function Room:playerJoinCheck() + local join = {} + + for _, inactiveColor in ipairs(self:getInactiveColors()) do + local overlapping = 0 + local activeColors = self:getActiveColors() + local inactivePos = self:getPlayerPos(inactiveColor) + for _, activeColor in ipairs(activeColors) do + local activePos = self:getPlayerPos(activeColor) + if activePos.x == inactivePos.x and activePos.y == inactivePos.y then + overlapping = overlapping + 1 + end + end + if overlapping == #activeColors then + table.insert(join, inactiveColor) + end + end + + for _, color in ipairs(join) do + self:setActiveColor(color, true) + end +end + +function Room:getPlayerPos(color) + return self.player[color]:getGridPos() +end + function Room:switchCheck() local doorsToUnlock = {} for color, switchArray in pairs(self.switches) do @@ -367,8 +425,11 @@ function Room:isInBounds(cell) end function Room:nextRoomCheck() - if not self:isInBounds(self.player:getGridPos()) then - nextRoom(self.player:getGridPos()) + for _, color in ipairs(self:getActiveColors()) do + if not self:isInBounds(self.player[color]:getGridPos()) then + nextRoom(self.player[color]:getGridPos()) + return + end end end @@ -391,7 +452,7 @@ function nextRoom(pos) currentRoom = Room:new{ player = { - x = newPos.x, y = newPos.y, activeColors = currentRoom.player:getActiveColors() + x = newPos.x, y = newPos.y, activeColors = currentRoom:getActiveColors() } } end \ No newline at end of file diff --git a/shaders.lua b/shaders.lua index 254c9bb..a08f415 100644 --- a/shaders.lua +++ b/shaders.lua @@ -29,3 +29,32 @@ wiggle = love.graphics.newShader[[ */ } ]] + +glow = love.graphics.newShader[[ + vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) { + vec4 pixel_me = Texel(texture, texture_coords ); + if (pixel_me.rgb != vec3(0,0,0)) + return pixel_me; + + int samples = 5; + float intensity = 60; + float radius = .1; + + for (int i = 1; i < samples; i++ ) { + vec4 pixel_left = Texel(texture, texture_coords + vec2(-radius * i, 0)); + vec4 pixel_right = Texel(texture, texture_coords + vec2(radius * i, 0)); + vec4 pixel_top = Texel(texture, texture_coords + vec2(0, radius * i)); + vec4 pixel_bottom = Texel(texture, texture_coords + vec2(0, -radius * i)); + vec4 pixel_top_left = Texel(texture, texture_coords + vec2(-radius * i, radius * i)); + vec4 pixel_top_right = Texel(texture, texture_coords + vec2(radius * i, radius * i)); + vec4 pixel_bottom_left = Texel(texture, texture_coords + vec2(-radius * i, -radius * i)); + vec4 pixel_bottom_right = Texel(texture, texture_coords + vec2(radius * i, -radius * i)); + + pixel_me += (pixel_left + pixel_right + pixel_top + pixel_bottom + pixel_bottom_right + pixel_bottom_left + pixel_top_right + pixel_top_left) / (intensity * i); + + intensity -= 2.5; + + } + return pixel_me; + } + ]] \ No newline at end of file