diff --git a/box.lua b/box.lua new file mode 100644 index 0000000..f3b9b5d --- /dev/null +++ b/box.lua @@ -0,0 +1,23 @@ +require "entity" + +Box = class("Box", Entity) + +function Box:initialize(x, y, color) + Entity.initialize(self, { + x = x, + y = y, + sizeX = 1, + sizeY = 1, + color = color, + collision = "moveable", + sprite = boxSprite[color] + }) +end + +function Box:update() + +end + +function Box:tick() + +end \ No newline at end of file diff --git a/color.lua b/color.lua index 4de1d62..0ff3136 100644 --- a/color.lua +++ b/color.lua @@ -3,18 +3,22 @@ Color = class("Color") +Color.static.list = { + red = {r = 255, g = 0, b = 0, a = 255}, + green = {r = 0, g = 255, b = 0, a = 255}, + blue = {r = 0, g = 0, b = 255, a = 255} +} + function Color:initialize(inColor) - local r, g, b, a - if inColor == "red" then - r, g, b, a = 255, 0, 0, 255 - elseif inColor == "green" then - r, g, b, a = 0, 255, 0, 255 - elseif inColor == "blue" then - r, g, b, a = 0, 0, 255, 255 - end - self.r, self.g, self.b, self.a = r, g, b, a + local c = Color.list[inColor] + self.r, self.g, self.b, self.a = c.r, c.g, c.b, c.a end function Color:set() return self.r, self.g, self.b, self.a +end + +function randomColor() + local colors = {"red", "green", "blue"} + return colors[math.random(1, 3)] end \ No newline at end of file diff --git a/debug_helpers.lua b/debug_helpers.lua new file mode 100644 index 0000000..da170e2 --- /dev/null +++ b/debug_helpers.lua @@ -0,0 +1,5 @@ +function shallowTablePrint(t) + for i, v in pairs(t) do + print(i, v) + end +end \ No newline at end of file diff --git a/door.lua b/door.lua new file mode 100644 index 0000000..44252c2 --- /dev/null +++ b/door.lua @@ -0,0 +1,15 @@ +require "entity" + +Door = class("Door", Entity) + +function Door:initialize() + +end + +function Door:unloc() + self.collision = "none" +end + +function Door:loc() + self.collision = "unmoveable" +end \ No newline at end of file diff --git a/entity.lua b/entity.lua index 9027811..ee433e2 100644 --- a/entity.lua +++ b/entity.lua @@ -1,22 +1,23 @@ Entity = class("Entity") function Entity:initialize(t) - self.gridX, self.gridY = t.x, t.y + self.x, self.y = t.x, t.y self.gridSize = {x = t.sizeX, y = t.sizeY} self.color = Color:new(t.color) self.collision = t.collision --either "none", "moveable", "unmoveable" + self.sprite = t.sprite end function Entity:atGridPos(x, y) - if x >= self.gridX and x <= self.gridX + self.gridSize.x and - y >= self.gridY and y <= self.gridSize.y then + if x >= self.x and x <= self.x + self.gridSize.x and + y >= self.y and y <= self.gridSize.y then return true end return false end function Entity:getGridPos() - return {x = self.gridX, y = self.gridY} + return {x = self.x, y = self.y} end function Entity:getDrawPos() @@ -25,4 +26,33 @@ function Entity:getDrawPos() x = (gridPos.x - 1) * width / gridWidth, y = (gridPos.y - 1) * height / gridHeight } +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) +end + +function Entity:isAlignedWithPlayerMovement(axis, pos) + if self:getGridPos()[axis] == pos then + return true + end + return false +end + +function Entity:canMove() + if self.collision == "unmoveable" then + return false + end + return true +end + + +function Entity:move(axis, direction) + self[axis] = self[axis] + direction +end + +function Entity:push(direction) + end \ No newline at end of file diff --git a/input.lua b/input.lua index 94f1309..1b003c4 100644 --- a/input.lua +++ b/input.lua @@ -1,11 +1,27 @@ -local input = {} +Input = {} -function input:handler() +Input.playermovekeys = { up = "up", down = "down", left = "left", right = "right" } +Input.playerswitchkeys = { ["1"] = "1", ["2"] = "2", ["3"] = "3" } +Input.colorfromkey = {"red", "green", "blue"} + +function Input:handler() end -function input:moveIsPressed() - +function Input.isPlayerMove(key) + if Input.playermovekeys[key] then + return true + end + return false end -return input \ No newline at end of file +function Input.isPlayerSwitch(key) + if Input.playerswitchkeys[key] then + return true + end + return false +end + +function Input.playerColor(key) + return Input.colorfromkey[tonumber(key)] +end \ No newline at end of file diff --git a/main.lua b/main.lua index e814c48..1b67658 100644 --- a/main.lua +++ b/main.lua @@ -1,9 +1,10 @@ class = require "libs/middleclass" Timer = require "libs/hump/timer" -Input = require "input" +require "input" require "color" require "room" require "player" +require "debug_helpers" function love.load() _initDefaults() @@ -33,17 +34,30 @@ function love.draw() currentRoom:draw() end -function _initDefaults() - love.graphics.setDefaultFilter("linear", "nearest") +function love.keypressed(key) + if Input.isPlayerMove(key) then + currentRoom:movePlayer(key) + elseif Input.isPlayerSwitch(key) then + local c = Input.playerColor(key) + currentRoom:switchPlayer(c) + end +end +function _initDefaults() + --set filter + love.graphics.setDefaultFilter("linear", "nearest") + + --set global width / height variables width = love.graphics.getWidth() height = love.graphics.getHeight() - if width > height then width = height else height = width end + + --set randomseed + math.randomseed(os.time()) end function _initGridVars() @@ -57,4 +71,10 @@ 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") + } end \ No newline at end of file diff --git a/player.lua b/player.lua index f32718e..6563b8a 100644 --- a/player.lua +++ b/player.lua @@ -10,12 +10,10 @@ function Player:initialize(x, y, color) sizeX = 3, sizeY = 3, color = color, - collision = "none" --player can't push other player + collision = "none", --player can't push other player + sprite = playerSprite }) - self.sprite = playerSprite - - print("i exist!!") end function Player:update() @@ -28,14 +26,3 @@ function Player:draw() love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale) end -function Player:move(direction) - if direction == "up" then - self.gridY = self.gridY - 1 - elseif direction == "down" then - self.gridY = self.gridY + 1 - elseif direction == "left" then - self.gridX = self.gridX - 1 - elseif direction == "right" then - self.gridX = self.gridX + 1 - end -end \ No newline at end of file diff --git a/room.lua b/room.lua index d70e050..8727f74 100644 --- a/room.lua +++ b/room.lua @@ -1,21 +1,40 @@ Room = class("Room") require "player" +require "box" +require "wall" function Room:initialize(t) self.exits = t.exits self.name = t.name self.player = {} + self.collideables = { + red = {}, + green = {}, + blue = {} + } + + self.activeColors = { + red = false, green = false, blue = false + } for color, coords in pairs(t.player) do self.player[color] = Player:new(coords.x, coords.y, color) + self.activeColors[color] = true end - local crate - local box - local blox - local tile + for i=0, 20 do + local c = randomColor() + local x, y = math.random(1, gridWidth), math.random(1, gridHeight) + self.collideables[c]["b: " .. x .. ", " .. y] = Box:new(x, y, c) + end + + for i=0, 20 do + local c = randomColor() + local x, y = math.random(1, gridWidth), math.random(1, gridHeight) + self.collideables[c]["w: " .. x .. ", " .. y] = Wall:new(x, y, c) + end end @@ -29,9 +48,143 @@ function Room: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() + end + end +end + +function Room:movePlayer(key) + + local moveAxis + local direction + + if key == "up" then + moveAxis, direction = "y", -1 + elseif key == "down" then + moveAxis, direction = "y", 1 + elseif key == "left" then + moveAxis, direction = "x", -1 + elseif key == "right" then + moveAxis, direction = "x", 1 + else + error("wtf did u do") + end + + for color, active in pairs(self.activeColors) do + if active then + local playerPos = self.player[color]:getGridPos() + local entities = self:getAdjacentEntities(self.collideables[color], playerPos, moveAxis, direction) + local canMove = self:tryPlayerMove(entities, playerPos, moveAxis, direction) + if canMove then + self:checkAndPull(self.collideables[color], playerPos, moveAxis, direction) + self.player[color]:move(moveAxis, direction) + self:tick() + end + end + end +end + +function Room:switchPlayer(colorToActivate) + for color, active in pairs(self.activeColors) do + if color == colorToActivate then + self:setActiveColor(color, true) + else + self:setActiveColor(color, false) + end + end +end + +function Room:setActiveColor(color, bool) + self.activeColors[color] = bool +end + +function Room:getAdjacentEntities(entities, pos, axis, direction) + local t = {} + + --first we gather all entities in direction of movement + for _, entity in pairs(entities) do + if axis == "x" then + if entity:isAlignedWithPlayerMovement("y", pos.y) then + if direction > 0 and entity:getGridPos()[axis] > pos[axis] then + table.insert(t, entity) + elseif direction < 0 and entity:getGridPos()[axis] < pos[axis] then + table.insert(t, entity) + end + end + elseif axis == "y" then + if entity:isAlignedWithPlayerMovement("x", pos.x) then + if direction > 0 and entity:getGridPos()[axis] > pos[axis] then + table.insert(t, entity) + elseif direction < 0 and entity:getGridPos()[axis] < pos[axis] then + table.insert(t, entity) + end + end + end + end + + --then we put them in order + table.sort(t, function(a, b) + if direction > 0 then + return a:getGridPos()[axis] < b:getGridPos()[axis] + else + return a:getGridPos()[axis] > b:getGridPos()[axis] + end + end) + + return t +end + +function Room:checkAndPull(entities, pos, axis, direction) + + for _, entity in pairs(entities) do + local entPos = entity:getGridPos() + print(pos.x, pos.y, entPos.x, entPos.y) + if axis == "x" and entPos.x == pos.x - direction and entPos.y == pos.y then + if entity:canMove() then + entity:move(axis, direction) + return + end + elseif axis == "y" and entPos.x == pos.x and entPos.y == pos.y - direction then + if entity:canMove() then + entity:move(axis, direction) + return + end + end + end +end + +function Room:tryPlayerMove(entities, pos, axis, direction) + local counter = direction + local entitiesToMove = {} + + for _, entity in ipairs(entities) do + if entity:getGridPos()[axis] == pos[axis] + counter then + if entity:canMove() then + table.insert(entitiesToMove, entity) + else + return false + end + else + break + end + counter = counter + direction + end + + for _, entity in ipairs(entitiesToMove) do + entity:move(axis, direction) + end + + return true end function Room:tick() + for color, entityArray in pairs(self.collideables) do + for _, entity in pairs(entityArray) do + entity:tick() + end + end self:nextRoomCheck() end @@ -40,8 +193,5 @@ function Room:nextRoomCheck() end function nextRoom() - print("next room!!!") - -- currentRoom = Room:new{ - - -- } + end \ No newline at end of file diff --git a/wall.lua b/wall.lua new file mode 100644 index 0000000..fa309a0 --- /dev/null +++ b/wall.lua @@ -0,0 +1,23 @@ +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, + collision = "unmoveable", + sprite = wallSprite + }) +end + +function Wall:update(dt) + +end + +function Wall:tick() + +end \ No newline at end of file