oh thank you! amazing it's almost done!
I added the two functions, now the last part missing is to call those functions to check if the collision is happening.
I created something like this:
Code: Select all
function pb:getCollisionExit (Agent)
for i, agent in ipairs (self.agents) do
if agent == self.agent then
-- no collision detection with active agent
elseif self:isCollisionAgentToAnyExitArea (agent) then
return true -- collision with exit
end
end
end
Am I in the right direction?
I have an idea, the game is too big with the assets right now so I will make a small demo of the first level to post it here, I will upload it in a few minutes:
So there it is, don't select any other level than the first one because it will crash.
What I think it needs to be done is to call that function and ask if there is collision to the exit or if the two agents are colliding with the exit area.
Code: Select all
function pb:getBlocksToMove (agent, dx, dy)
local blocks = {}
local canMove = self:getCollisionBlocks (agent, blocks, dx, dy)
return blocks, canMove
end
function pb:moveAgent (agent, dx, dy)
self.agent.x = self.agent.x + dx
self.agent.y = self.agent.y + dy
end
function pb:moveBlocks (blocks, dx, dy)
for i, block in ipairs (blocks) do
block.x = block.x + dx
block.y = block.y + dy
if soundon==true then
--scrape6:play() --friction sound
end
end
end
function pb:isCollisionBlockToAllBlocks (blockA, dx, dy)
for i, block in ipairs (self.blocks) do
if not (block == blockA)
and self:isBlockToBlockCollision (blockA, block, dx, dy) then
return true
end
end
return false
end
function pb:isCollisionBlockToAllAgents (blockA, dx, dy)
for i, agent in ipairs (self.agents) do
if self:isBlockToBlockCollision (blockA, agent, dx, dy) then
return agent -- dead agent :(
end
end
return false
end
function pb:areAllAgentsInExitAreas ()
for i, agent in ipairs (self.agents) do
if not self:isCollisionAgentToAnyExitArea (agent) then
return false -- at least one of agents is not on exit, the game is not over
end
return true -- all agents are in exit areas, level is done
end
end
function pb:isCollisionAgentToAnyExitArea (agent)
for i, area in ipairs (self.exitAreas) do
if self:isBlockToBlockCollision (area, agent, 0, 0) then -- dx and dy are 0
return true -- collision one of areas to agent in /this/ position
end
end
return false -- this agent has no collision with any of exit areas
end
function pb:fallBlocks (blocks)
local dx, dy = 0, 1 -- no horizontal speed, but positive (down) vertical
for i = 1, self.gridWidth do
for i, block in ipairs (blocks) do
if self:isBlockToMapCollision (block, dx, dy) then
-- not falling
block.deadly = false
-- table.remove (blocks, i)
elseif self:isCollisionBlockToAllBlocks (block, dx, dy) then
block.deadly = false
-- collision to block: probably not falling
elseif block.deadly and self:isCollisionBlockToAllAgents (block, dx, dy) then
local deadAgent = self:isCollisionBlockToAllAgents (block, dx, dy)
deadAgent.dead = true
block.deadly = false
-- table.remove (blocks, i)
elseif self:isCollisionBlockToAllAgents (block, dx, dy) then
-- the block is on fish
else
-- sure falling
if soundon==true then
--impactmetal:play() --impact sound
end
block.x = block.x + dx -- never changes
block.y = block.y + dy
block.deadly = true
end
end
end
end
function pb:mainMoving (dx, dy)
local agent = self.agent -- active agent
if dx > 0 then
agent.direction = "right"
elseif dx < 0 then
agent.direction = "left"
end
local blocks, canMove = self:getBlocksToMove (agent, dx, dy)
if canMove then
self:moveAgent (agent, dx, dy)
self:moveBlocks (blocks, dx, dy)
self:fallBlocks (self.blocks, dx, dy)
--self:isCollisionAgentToAnyExitArea (agent)
end
end
If I add this function to the pb:mainMoving the game crashed when moving, I though that would be the way to know if the agents are in the exit...
--self:isCollisionAgentToAnyExitArea (agent)
*if the dialogs annoys you please press "c", I did not have time to fix the interface for the demo