Hey guys. This is a method which is called for each tile in a grid of my own construction, and needs to find the value of the tile that is closer to the center. I get the left, right, up, and down checks, and it works wonderfully although it can't draw angles. I would love it to draw angles, but to do that, I need the other four directions.
The code functions as it should in the its current form here. I already set up handlers for the other four directions, but the little brackets there are only testing four directions.
Halp please! I even tried sleeping this time and that didn't fix it either!
kthxbai
The code functions as it should in the its current form here. I already set up handlers for the other four directions, but the little brackets there are only testing four directions.
Halp please! I even tried sleeping this time and that didn't fix it either!
kthxbai
Code:
def closer(x, y)
# checks which tile in the grid is closer to the center and return its value
# first, determine which of the 8 directions to check by offset from center
sx = 0 - (@center - x)
sy = 0 - (@center - y)
return 0 if (sx < 2) & (sy < 2) & (sx > -2) & (sy > -2)
if sy.abs > sx.abs
sy > 0 ? dir = 'up' : dir = 'down'
else
sx > 0 ? dir = 'left' : dir = 'right'
end
case dir
when 'up' then return @tile[x][y - 1] rescue 1
when 'up-right' then return @tile[x + 1][y - 1] rescue 1
when 'right' then return @tile[x + 1][y] rescue 1
when 'down-right' then return @tile[x + 1][y + 1] rescue 1
when 'down' then return @tile[x][y + 1] rescue 1
when 'down-left' then return @tile[x - 1][y + 1] rescue 1
when 'left' then return @tile[x - 1][y] rescue 1
when 'top-left' then return @tile[x - 1][y - 1] rescue 1
end
end