some changes, idr, hehe sorry

This commit is contained in:
0x81c 2015-03-15 05:26:47 -04:00
parent c668dc513b
commit b553d3f664
5 changed files with 116 additions and 48 deletions

View file

@ -11,4 +11,9 @@ function printMatrix(matrix)
end end
io.write("\n") io.write("\n")
end end
end
function randomPosition()
print("hfhfhhdh")
return math.random(1, gridWidth), math.random(1, gridHeight)
end end

View file

@ -8,6 +8,12 @@ function Entity:initialize(t)
self.collision = t.collision --"none", "moveable", "unmoveable" self.collision = t.collision --"none", "moveable", "unmoveable"
self.sprite = t.sprite self.sprite = t.sprite
self.shaAmount = t.sha or 1 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 end
function Entity:getCollision() function Entity:getCollision()
@ -70,7 +76,7 @@ end
function Entity:draw() function Entity:draw()
love.graphics.setColor(self.color:set()) love.graphics.setColor(self.color:set())
local p = self:getDrawPos() 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 end
function Entity:canMove() function Entity:canMove()

View file

@ -133,6 +133,11 @@ function loadSprites()
green = love.graphics.newImage("art/wall_g.png"), green = love.graphics.newImage("art/wall_g.png"),
blue = love.graphics.newImage("art/wall_b.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 = { waterSprite = {
red = love.graphics.newImage("art/nature/processed/water_r.png"), red = love.graphics.newImage("art/nature/processed/water_r.png"),
green = love.graphics.newImage("art/nature/processed/water_g.png"), green = love.graphics.newImage("art/nature/processed/water_g.png"),

129
room.lua
View file

@ -7,16 +7,14 @@ require "artifact"
require "npc" require "npc"
require "art" require "art"
require "assorted" require "assorted"
require "switch"
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 = Player:new(t.player.x, t.player.y, t.player.activeColors) self.player = Player:new(t.player.x, t.player.y, t.player.activeColors)
self.collideables = { self.switches = { red = {}, green = {}, blue = {} }
red = {}, self.collideables = { red = {}, green = {}, blue = {} }
green = {},
blue = {}
}
self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...} self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...}
self.activeColors = { self.activeColors = {
@ -44,6 +42,13 @@ function Room:initialize(t)
end end
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 for color, active in pairs(self.activeColors) do
local x, y = 6, 3 local x, y = 6, 3
-- if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "animals/processed/", "me_rach") end -- 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() entity:draw()
end end
end end
for color, switchArray in pairs(self.switches) do
for _, switch in pairs(switchArray) do
switch:draw()
end
end
end end
function Room:createCollideablesMatrix(collideables) function Room:createCollideablesMatrix(collideables)
@ -128,7 +138,7 @@ function Room:movePlayer(key)
dir.x, axis = 1, "x" dir.x, axis = 1, "x"
end end
local move local move = true
local entitiesToPush = { local entitiesToPush = {
red = {}, red = {},
green = {}, green = {},
@ -146,55 +156,58 @@ function Room:movePlayer(key)
x = pos.x + dir.x, y = pos.y + dir.y x = pos.x + dir.x, y = pos.y + dir.y
} -- this is for reference for each color as we change the targetCells table } -- this is for reference for each color as we change the targetCells table
local move = true if not self:isInBounds(initialTargetCell) then
local targetCells move = true
else
local targetCells
--so the way this worx is.... --so the way this worx is....
for _, color in pairs(activeColors) do for _, color in pairs(activeColors) do
--for each color... --for each color...
targetCells = {initialTargetCell} -- ...we start with our attempted movement... targetCells = {initialTargetCell} -- ...we start with our attempted movement...
local doneSearching local doneSearching
while not doneSearching do while not doneSearching do
nextTargetCells = {} -- ...then we create this array to save our next group of cells nextTargetCells = {} -- ...then we create this array to save our next group of cells
local emptyCells = {} local emptyCells = {}
for _, cell in pairs(targetCells) do for _, cell in pairs(targetCells) do
if self:isInBounds(cell) then if self:isInBounds(cell) then
local entity = self:getCellInMatrix(self.collidableMatrices[color], cell) local entity = self:getCellInMatrix(self.collidableMatrices[color], cell)
if entity then if entity then
--something is here!! --something is here!!
if not entitiesToPush[color][entity] then --if the entity hasnt been found already if not entitiesToPush[color][entity] then --if the entity hasnt been found already
if entity:canMove() then if entity:canMove() then
local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here
entitiesToPush[color][entity] = entity entitiesToPush[color][entity] = entity
for _, projectedCell in ipairs(projectedCells) do for _, projectedCell in ipairs(projectedCells) do
table.insert(nextTargetCells, projectedCell) table.insert(nextTargetCells, projectedCell)
end
else
move = false
doneSearching = true
end end
else
move = false
doneSearching = true
end end
else
--there's nothin there!!
table.insert(emptyCells, cell)
end end
else else
--there's nothin there!! --we are out of bounds now
table.insert(emptyCells, cell) move = false
doneSearching = true
end end
else end
--we are out of bounds now
move = false --if every projected cell is empty then we can do the move
if #emptyCells == #targetCells then
move = true
doneSearching = true doneSearching = true
end end
end
--if every projected cell is empty then we can do the move targetCells = nextTargetCells
if #emptyCells == #targetCells then
move = true
doneSearching = true
end end
--let's get out of this loop b/c if we can't move one color we can't move at all
targetCells = nextTargetCells if move == false then break end
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 end
@ -309,9 +322,31 @@ function Room:isInBounds(cell)
end end
function Room:nextRoomCheck() function Room:nextRoomCheck()
if not self:isInBounds(self.player:getGridPos()) then
nextRoom(self.player:getGridPos())
end
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 end

17
switch.lua Normal file
View file

@ -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