boxes n walls n movement etcgit status
This commit is contained in:
parent
9763040c99
commit
2772ec2563
10 changed files with 318 additions and 45 deletions
23
box.lua
Normal file
23
box.lua
Normal file
|
|
@ -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
|
||||||
22
color.lua
22
color.lua
|
|
@ -3,18 +3,22 @@
|
||||||
|
|
||||||
Color = class("Color")
|
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)
|
function Color:initialize(inColor)
|
||||||
local r, g, b, a
|
local c = Color.list[inColor]
|
||||||
if inColor == "red" then
|
self.r, self.g, self.b, self.a = c.r, c.g, c.b, c.a
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Color:set()
|
function Color:set()
|
||||||
return self.r, self.g, self.b, self.a
|
return self.r, self.g, self.b, self.a
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function randomColor()
|
||||||
|
local colors = {"red", "green", "blue"}
|
||||||
|
return colors[math.random(1, 3)]
|
||||||
|
end
|
||||||
5
debug_helpers.lua
Normal file
5
debug_helpers.lua
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
function shallowTablePrint(t)
|
||||||
|
for i, v in pairs(t) do
|
||||||
|
print(i, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
15
door.lua
Normal file
15
door.lua
Normal file
|
|
@ -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
|
||||||
38
entity.lua
38
entity.lua
|
|
@ -1,22 +1,23 @@
|
||||||
Entity = class("Entity")
|
Entity = class("Entity")
|
||||||
|
|
||||||
function Entity:initialize(t)
|
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.gridSize = {x = t.sizeX, y = t.sizeY}
|
||||||
self.color = Color:new(t.color)
|
self.color = Color:new(t.color)
|
||||||
self.collision = t.collision --either "none", "moveable", "unmoveable"
|
self.collision = t.collision --either "none", "moveable", "unmoveable"
|
||||||
|
self.sprite = t.sprite
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:atGridPos(x, y)
|
function Entity:atGridPos(x, y)
|
||||||
if x >= self.gridX and x <= self.gridX + self.gridSize.x and
|
if x >= self.x and x <= self.x + self.gridSize.x and
|
||||||
y >= self.gridY and y <= self.gridSize.y then
|
y >= self.y and y <= self.gridSize.y then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:getGridPos()
|
function Entity:getGridPos()
|
||||||
return {x = self.gridX, y = self.gridY}
|
return {x = self.x, y = self.y}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:getDrawPos()
|
function Entity:getDrawPos()
|
||||||
|
|
@ -26,3 +27,32 @@ function Entity:getDrawPos()
|
||||||
y = (gridPos.y - 1) * height / gridHeight
|
y = (gridPos.y - 1) * height / gridHeight
|
||||||
}
|
}
|
||||||
end
|
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
|
||||||
26
input.lua
26
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
|
end
|
||||||
|
|
||||||
function input:moveIsPressed()
|
function Input.isPlayerMove(key)
|
||||||
|
if Input.playermovekeys[key] then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return input
|
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
|
||||||
24
main.lua
24
main.lua
|
|
@ -1,9 +1,10 @@
|
||||||
class = require "libs/middleclass"
|
class = require "libs/middleclass"
|
||||||
Timer = require "libs/hump/timer"
|
Timer = require "libs/hump/timer"
|
||||||
Input = require "input"
|
require "input"
|
||||||
require "color"
|
require "color"
|
||||||
require "room"
|
require "room"
|
||||||
require "player"
|
require "player"
|
||||||
|
require "debug_helpers"
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
_initDefaults()
|
_initDefaults()
|
||||||
|
|
@ -33,17 +34,30 @@ function love.draw()
|
||||||
currentRoom:draw()
|
currentRoom:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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()
|
function _initDefaults()
|
||||||
|
--set filter
|
||||||
love.graphics.setDefaultFilter("linear", "nearest")
|
love.graphics.setDefaultFilter("linear", "nearest")
|
||||||
|
|
||||||
|
--set global width / height variables
|
||||||
width = love.graphics.getWidth()
|
width = love.graphics.getWidth()
|
||||||
height = love.graphics.getHeight()
|
height = love.graphics.getHeight()
|
||||||
|
|
||||||
if width > height then
|
if width > height then
|
||||||
width = height
|
width = height
|
||||||
else
|
else
|
||||||
height = width
|
height = width
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--set randomseed
|
||||||
|
math.randomseed(os.time())
|
||||||
end
|
end
|
||||||
|
|
||||||
function _initGridVars()
|
function _initGridVars()
|
||||||
|
|
@ -57,4 +71,10 @@ end
|
||||||
|
|
||||||
function loadSprites()
|
function loadSprites()
|
||||||
playerSprite = love.graphics.newImage("art/player.png")
|
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
|
end
|
||||||
17
player.lua
17
player.lua
|
|
@ -10,12 +10,10 @@ function Player:initialize(x, y, color)
|
||||||
sizeX = 3,
|
sizeX = 3,
|
||||||
sizeY = 3,
|
sizeY = 3,
|
||||||
color = color,
|
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
|
end
|
||||||
|
|
||||||
function Player:update()
|
function Player:update()
|
||||||
|
|
@ -28,14 +26,3 @@ function Player:draw()
|
||||||
love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale)
|
love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale)
|
||||||
end
|
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
|
|
||||||
164
room.lua
164
room.lua
|
|
@ -1,21 +1,40 @@
|
||||||
Room = class("Room")
|
Room = class("Room")
|
||||||
|
|
||||||
require "player"
|
require "player"
|
||||||
|
require "box"
|
||||||
|
require "wall"
|
||||||
|
|
||||||
function Room:initialize(t)
|
function Room:initialize(t)
|
||||||
self.exits = t.exits
|
self.exits = t.exits
|
||||||
self.name = t.name
|
self.name = t.name
|
||||||
self.player = {}
|
self.player = {}
|
||||||
|
self.collideables = {
|
||||||
|
red = {},
|
||||||
|
green = {},
|
||||||
|
blue = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.activeColors = {
|
||||||
|
red = false, green = false, blue = false
|
||||||
|
}
|
||||||
|
|
||||||
for color, coords in pairs(t.player) do
|
for color, coords in pairs(t.player) do
|
||||||
self.player[color] = Player:new(coords.x, coords.y, color)
|
self.player[color] = Player:new(coords.x, coords.y, color)
|
||||||
|
self.activeColors[color] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
local crate
|
for i=0, 20 do
|
||||||
local box
|
local c = randomColor()
|
||||||
local blox
|
local x, y = math.random(1, gridWidth), math.random(1, gridHeight)
|
||||||
local tile
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,9 +48,143 @@ function Room:draw()
|
||||||
for color, player in pairs(self.player) do
|
for color, player in pairs(self.player) do
|
||||||
player:draw()
|
player:draw()
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function Room:tick()
|
function Room:tick()
|
||||||
|
for color, entityArray in pairs(self.collideables) do
|
||||||
|
for _, entity in pairs(entityArray) do
|
||||||
|
entity:tick()
|
||||||
|
end
|
||||||
|
end
|
||||||
self:nextRoomCheck()
|
self:nextRoomCheck()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -40,8 +193,5 @@ function Room:nextRoomCheck()
|
||||||
end
|
end
|
||||||
|
|
||||||
function nextRoom()
|
function nextRoom()
|
||||||
print("next room!!!")
|
|
||||||
-- currentRoom = Room:new{
|
|
||||||
|
|
||||||
-- }
|
|
||||||
end
|
end
|
||||||
23
wall.lua
Normal file
23
wall.lua
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue