Code: Select all
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t = {1,0,0,0}
> for i = 1, #t do
>> print(t[i])
>> end
1
0
0
0
> t = {
>> {0,0,1},
>> {0,0,0}
>> }
> for i = 1, #t do
>> for j = 1, #t[i] do
>> print(t[i][j])
>> end
>> end
0
0
1
0
0
0
> t = {
>> {0,1,1}
>> }
>
> for i = 1, #t do
>> for j = 1, #t[i] do
>> if t[i][j] == 1 then
>> return true
>> end
>> end
>> end
true —-should be true true but it breaks
>
Code: Select all
if fun_bool(1) or fun_bool(2) or fun_bool(3) then
--do stuff
end
Why?
So basically I have a 2d matrix that represent a map, like this
Code: Select all
map = {
{1,1,0,0,1,1},
{1,0,0,0,0,1},
{1,0,0,0,0,1},
{1,0,0,0,0,1},
{1,1,1,1,1,1}
}
Code: Select all
--tile (multiple tables can be created with a loop)
local tir = 0 --table iterator
local map_check = 1 --integer in map to check
local cx = u*3 --x constant to add
local cy = u*4 --y constant to add
t = {} --tile objects go in this table
for i = 1, #map do
for j = 1, #map[i] do
if map[i][j] == map_check then
tir = tir + 1 --increment the iterator
t[tir] = {} --create a new table
t[tir].x = (i-1)*u + cx --set x position
t[tir].y = (j-1)*u + cy --set y position
t[tir].w = u --set width by units u (32)
t[tir].h = u --set height by units u (32)
end
end
end
--collision function from (https://love2d.org/wiki/BoundingBox.lua)
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and x2 < x1+w1 and y1 < y2+h2 and y2 < y1+h1
end
Code: Select all
if love.keyboard.isDown('up') then
p.y = p.y - p.s*dt
if (p.x < t[1].x+t[1].w and t[1].x < p.x+p.w and p.y < t[1].y+t[1].h and t[1].y < p.y+p.h) or
(p.x < t[2].x+t[2].w and t[2].x < p.x+p.w and p.y < t[2].y+t[2].h and t[2].y < p.y+p.h) or
(p.x < t[3].x+t[3].w and t[3].x < p.x+p.w and p.y < t[3].y+t[3].h and t[3].y < p.y+p.h) or
(p.x < t[4].x+t[4].w and t[4].x < p.x+p.w and p.y < t[4].y+t[4].h and t[4].y < p.y+p.h) or
(p.x < t[5].x+t[5].w and t[5].x < p.x+p.w and p.y < t[5].y+t[5].h and t[5].y < p.y+p.h) or
(p.x < t[6].x+t[6].w and t[6].x < p.x+p.w and p.y < t[6].y+t[6].h and t[6].y < p.y+p.h) or
(p.x < t[7].x+t[7].w and t[7].x < p.x+p.w and p.y < t[7].y+t[7].h and t[7].y < p.y+p.h) or
(p.x < t[8].x+t[8].w and t[8].x < p.x+p.w and p.y < t[8].y+t[8].h and t[8].y < p.y+p.h) or
(p.x < t[9].x+t[9].w and t[9].x < p.x+p.w and p.y < t[9].y+t[9].h and t[9].y < p.y+p.h) or
(p.x < t[10].x+t[10].w and t[10].x < p.x+p.w and p.y < t[10].y+t[10].h and t[10].y < p.y+p.h) or
(p.x < t[11].x+t[11].w and t[11].x < p.x+p.w and p.y < t[11].y+t[11].h and t[11].y < p.y+p.h) or
(p.x < t[12].x+t[12].w and t[12].x < p.x+p.w and p.y < t[12].y+t[12].h and t[12].y < p.y+p.h) or
(p.x < t[13].x+t[13].w and t[13].x < p.x+p.w and p.y < t[13].y+t[13].h and t[13].y < p.y+p.h) or
(p.x < t[14].x+t[14].w and t[14].x < p.x+p.w and p.y < t[14].y+t[14].h and t[14].y < p.y+p.h) or
(p.x < t[15].x+t[15].w and t[15].x < p.x+p.w and p.y < t[15].y+t[15].h and t[15].y < p.y+p.h) or
(p.x < t[16].x+t[16].w and t[16].x < p.x+p.w and p.y < t[16].y+t[16].h and t[16].y < p.y+p.h)
then
p.y = math.ceil(p.y) + 1
end
end
if love.keyboard.isDown('down') then
p.y = p.y + p.s*dt
if (p.x < t[1].x+t[1].w and t[1].x < p.x+p.w and p.y < t[1].y+t[1].h and t[1].y < p.y+p.h) or
(p.x < t[2].x+t[2].w and t[2].x < p.x+p.w and p.y < t[2].y+t[2].h and t[2].y < p.y+p.h) or
(p.x < t[3].x+t[3].w and t[3].x < p.x+p.w and p.y < t[3].y+t[3].h and t[3].y < p.y+p.h) or
(p.x < t[4].x+t[4].w and t[4].x < p.x+p.w and p.y < t[4].y+t[4].h and t[4].y < p.y+p.h) or
(p.x < t[5].x+t[5].w and t[5].x < p.x+p.w and p.y < t[5].y+t[5].h and t[5].y < p.y+p.h) or
(p.x < t[6].x+t[6].w and t[6].x < p.x+p.w and p.y < t[6].y+t[6].h and t[6].y < p.y+p.h) or
(p.x < t[7].x+t[7].w and t[7].x < p.x+p.w and p.y < t[7].y+t[7].h and t[7].y < p.y+p.h) or
(p.x < t[8].x+t[8].w and t[8].x < p.x+p.w and p.y < t[8].y+t[8].h and t[8].y < p.y+p.h) or
(p.x < t[9].x+t[9].w and t[9].x < p.x+p.w and p.y < t[9].y+t[9].h and t[9].y < p.y+p.h) or
(p.x < t[10].x+t[10].w and t[10].x < p.x+p.w and p.y < t[10].y+t[10].h and t[10].y < p.y+p.h) or
(p.x < t[11].x+t[11].w and t[11].x < p.x+p.w and p.y < t[11].y+t[11].h and t[11].y < p.y+p.h) or
(p.x < t[12].x+t[12].w and t[12].x < p.x+p.w and p.y < t[12].y+t[12].h and t[12].y < p.y+p.h) or
(p.x < t[13].x+t[13].w and t[13].x < p.x+p.w and p.y < t[13].y+t[13].h and t[13].y < p.y+p.h) or
(p.x < t[14].x+t[14].w and t[14].x < p.x+p.w and p.y < t[14].y+t[14].h and t[14].y < p.y+p.h) or
(p.x < t[15].x+t[15].w and t[15].x < p.x+p.w and p.y < t[15].y+t[15].h and t[15].y < p.y+p.h) or
(p.x < t[16].x+t[16].w and t[16].x < p.x+p.w and p.y < t[16].y+t[16].h and t[16].y < p.y+p.h)
then
p.y = math.floor(p.y) - 1
end
end
if love.keyboard.isDown('left') then
p.x = p.x - p.s*dt
if (p.x < t[1].x+t[1].w and t[1].x < p.x+p.w and p.y < t[1].y+t[1].h and t[1].y < p.y+p.h) or
(p.x < t[2].x+t[2].w and t[2].x < p.x+p.w and p.y < t[2].y+t[2].h and t[2].y < p.y+p.h) or
(p.x < t[3].x+t[3].w and t[3].x < p.x+p.w and p.y < t[3].y+t[3].h and t[3].y < p.y+p.h) or
(p.x < t[4].x+t[4].w and t[4].x < p.x+p.w and p.y < t[4].y+t[4].h and t[4].y < p.y+p.h) or
(p.x < t[5].x+t[5].w and t[5].x < p.x+p.w and p.y < t[5].y+t[5].h and t[5].y < p.y+p.h) or
(p.x < t[6].x+t[6].w and t[6].x < p.x+p.w and p.y < t[6].y+t[6].h and t[6].y < p.y+p.h) or
(p.x < t[7].x+t[7].w and t[7].x < p.x+p.w and p.y < t[7].y+t[7].h and t[7].y < p.y+p.h) or
(p.x < t[8].x+t[8].w and t[8].x < p.x+p.w and p.y < t[8].y+t[8].h and t[8].y < p.y+p.h) or
(p.x < t[9].x+t[9].w and t[9].x < p.x+p.w and p.y < t[9].y+t[9].h and t[9].y < p.y+p.h) or
(p.x < t[10].x+t[10].w and t[10].x < p.x+p.w and p.y < t[10].y+t[10].h and t[10].y < p.y+p.h) or
(p.x < t[11].x+t[11].w and t[11].x < p.x+p.w and p.y < t[11].y+t[11].h and t[11].y < p.y+p.h) or
(p.x < t[12].x+t[12].w and t[12].x < p.x+p.w and p.y < t[12].y+t[12].h and t[12].y < p.y+p.h) or
(p.x < t[13].x+t[13].w and t[13].x < p.x+p.w and p.y < t[13].y+t[13].h and t[13].y < p.y+p.h) or
(p.x < t[14].x+t[14].w and t[14].x < p.x+p.w and p.y < t[14].y+t[14].h and t[14].y < p.y+p.h) or
(p.x < t[15].x+t[15].w and t[15].x < p.x+p.w and p.y < t[15].y+t[15].h and t[15].y < p.y+p.h) or
(p.x < t[16].x+t[16].w and t[16].x < p.x+p.w and p.y < t[16].y+t[16].h and t[16].y < p.y+p.h)
then
p.x = math.ceil(p.x) + 1
end
end
if love.keyboard.isDown('right') then
p.x = p.x + p.s*dt
if (p.x < t[1].x+t[1].w and t[1].x < p.x+p.w and p.y < t[1].y+t[1].h and t[1].y < p.y+p.h) or
(p.x < t[2].x+t[2].w and t[2].x < p.x+p.w and p.y < t[2].y+t[2].h and t[2].y < p.y+p.h) or
(p.x < t[3].x+t[3].w and t[3].x < p.x+p.w and p.y < t[3].y+t[3].h and t[3].y < p.y+p.h) or
(p.x < t[4].x+t[4].w and t[4].x < p.x+p.w and p.y < t[4].y+t[4].h and t[4].y < p.y+p.h) or
(p.x < t[5].x+t[5].w and t[5].x < p.x+p.w and p.y < t[5].y+t[5].h and t[5].y < p.y+p.h) or
(p.x < t[6].x+t[6].w and t[6].x < p.x+p.w and p.y < t[6].y+t[6].h and t[6].y < p.y+p.h) or
(p.x < t[7].x+t[7].w and t[7].x < p.x+p.w and p.y < t[7].y+t[7].h and t[7].y < p.y+p.h) or
(p.x < t[8].x+t[8].w and t[8].x < p.x+p.w and p.y < t[8].y+t[8].h and t[8].y < p.y+p.h) or
(p.x < t[9].x+t[9].w and t[9].x < p.x+p.w and p.y < t[9].y+t[9].h and t[9].y < p.y+p.h) or
(p.x < t[10].x+t[10].w and t[10].x < p.x+p.w and p.y < t[10].y+t[10].h and t[10].y < p.y+p.h) or
(p.x < t[11].x+t[11].w and t[11].x < p.x+p.w and p.y < t[11].y+t[11].h and t[11].y < p.y+p.h) or
(p.x < t[12].x+t[12].w and t[12].x < p.x+p.w and p.y < t[12].y+t[12].h and t[12].y < p.y+p.h) or
(p.x < t[13].x+t[13].w and t[13].x < p.x+p.w and p.y < t[13].y+t[13].h and t[13].y < p.y+p.h) or
(p.x < t[14].x+t[14].w and t[14].x < p.x+p.w and p.y < t[14].y+t[14].h and t[14].y < p.y+p.h) or
(p.x < t[15].x+t[15].w and t[15].x < p.x+p.w and p.y < t[15].y+t[15].h and t[15].y < p.y+p.h) or
(p.x < t[16].x+t[16].w and t[16].x < p.x+p.w and p.y < t[16].y+t[16].h and t[16].y < p.y+p.h)
then
p.x = math.floor(p.x) - 1
end
end
Code: Select all
--hard coded values
function chk(i)
return (p.x < t[i].x+t[i].w and t[i].x < p.x+p.w and p.y < t[i].y+t[i].h and t[i].y < p.y+p.h)
end
Code: Select all
if love.keyboard.isDown('up') then
p.y = p.y - p.s*dt
if chk(1) or chk(2) or chk(3) or chk(4) or
chk(5) or chk(6) or chk(7) or chk(8) or
chk(9) or chk(10) or chk(11) or chk(12) or
chk(13) or chk(14) or chk(15) or chk(16)
then
p.y = math.ceil(p.y) + 1
end
end
if love.keyboard.isDown('down') then
p.y = p.y + p.s*dt
if chk(1) or chk(2) or chk(3) or chk(4) or
chk(5) or chk(6) or chk(7) or chk(8) or
chk(9) or chk(10) or chk(11) or chk(12) or
chk(13) or chk(14) or chk(15) or chk(16)
then
p.y = math.floor(p.y) - 1
end
end
if love.keyboard.isDown('left') then
p.x = p.x - p.s*dt
if chk(1) or chk(2) or chk(3) or chk(4) or
chk(5) or chk(6) or chk(7) or chk(8) or
chk(9) or chk(10) or chk(11) or chk(12) or
chk(13) or chk(14) or chk(15) or chk(16)
then
p.x = math.ceil(p.x) + 1
end
end
if love.keyboard.isDown('right') then
p.x = p.x + p.s*dt
if chk(1) or chk(2) or chk(3) or chk(4) or
chk(5) or chk(6) or chk(7) or chk(8) or
chk(9) or chk(10) or chk(11) or chk(12) or
chk(13) or chk(14) or chk(15) or chk(16)
then
p.x = math.floor(p.x) - 1
end
end
Code: Select all
--tile (multiple tables can be created with a loop)
local tir = 0 --table iterator
local map_check = 1 --integer in map to check
local cx = u*3 --x constant
local cy = u*4 --y constant
t = {} --tile objects go in here
for i = 1, #map do
for j = 1, #map[i] do
if map[i][j] == map_check then
tir = tir + 1
t[tir] = {}
t[tir].x = (i-1)*u + cx
t[tir].y = (j-1)*u + cy
t[tir].w = u
t[tir].h = u
function t[tir]:tilecollision() --LINE 41 problem with syntax?
CheckCollision(p.x,p.y,p.w,p.h,self.x,self.y,self.w,self.h)
end
end
end
end
(Unrelated: I can't seem to attach .love anymore, Is it my Mac settings?)