Hi, This block of code is sampling 1 cell in a 2d array and checking whether or not it borders the x1, y1, x2, and y2 coordinates. It’s in a for loop and if you need more context, I’m working off of this and I was trying to remake it so it can suit my needs. Looking at the block code, it feels like there are way too many if statements and feels like it could be improved on.
Improvements I already know of:
- Use “elseif x==x1 or y==y1 …” to stop repetition of table.insert()
if x~=x1 and y~=y1 and x~=x2 and y~=y2 then
cell:DestroyWall("N")
cell:DestroyWall("S")
cell:DestroyWall("E")
cell:DestroyWall("W")
elseif x==x1 and y==y1 then
cell:DestroyWall("N")
cell:DestroyWall("E")
table.insert(self.BorderingCells, cell)
elseif x==x2 and y==y1 then
cell:DestroyWall("N")
cell:DestroyWall("W")
table.insert(self.BorderingCells, cell)
elseif x==x1 and y==y2 then
cell:DestroyWall("S")
cell:DestroyWall("E")
table.insert(self.BorderingCells, cell)
elseif x==x2 and y==y2 then
cell:DestroyWall("S")
cell:DestroyWall("W")
table.insert(self.BorderingCells, cell)
elseif x==x1 or x==x2 then
cell:DestroyWall("S")
cell:DestroyWall("N")
table.insert(self.BorderingCells, cell)
elseif y==y1 or y==y2 then
cell:DestroyWall("E")
cell:DestroyWall("W")
table.insert(self.BorderingCells, cell)
end