I have an issue where my tile based lighting code is not functioning properly going up and left. It works properly going right and downward (see image), but only goes one tile to the left/up and doesn’t continue like it should. http://i.imgur.com/DAM1pg2.png
My code seems correct; I have placed print statements to check values (removed in sample) and it seems alright.
public void light(){
for(int i = 0; i < limity; i++){
for(int x = 0; x < limitx; x++){
light[i][x] = 0; //darken to current time of day
}
}
int ty = (p.desiredy/32)-16;
int tx = (p.desiredx/32)-16;
if(ty > limity){
ty = limity - 1;
}
if(ty < 0){
ty = 0;
}
if(tx > limitx){
tx = limitx - 1;
}
if(tx < 0){
tx = 0;
}
int my = ty + 32;
int mx = tx + 32;
if(my > limity){
my = limity - 1;
}
if(my < 0){
my = 0;
}
if(mx > limitx){
mx = limitx - 1;
}
if(mx < 0){
mx = 0;
}
for(int by = ty; by < my; by++){
for(int bx = tx; bx < mx; bx++){//for each block in the area
if(layer2[by][bx].lightlevel > 0){ //for each LIGHT SOURCE
light[by][bx] = layer2[by][bx].lightlevel;
}else light[by][bx] = 0;
}
}
for(int by = ty; by < my; by++){
for(int bx = tx; bx < mx; bx++){//for each block in the area, do adjacent check
if(light[by][bx] > 1){
//now do blocks adjacent
//left, right, up, down
//must do a check for blocks, otherwise exception
//then check if block is DARKER than itself
if(bx - 1 != -1){
if(light[by][bx - 1] < light[by][bx]){
light[by][bx - 1] = light[by][bx] - 1;
}
}
if(by - 1 != -1){
if(light[by - 1][bx] < light[by][bx]){
light[by - 1][bx] = light[by][bx] - 1;
}
}
if(bx + 1 != limitx){
if(light[by][bx + 1] < light[by][bx]){
light[by][bx + 1] = light[by][bx] - 1;
}
}
if(by + 1 != limity){
if(light[by + 1][bx] < light[by][bx]){
light[by + 1][bx] = light[by][bx] - 1;
}
}
}
}
//System.out.println("end of y loop, " + ty + " " + my);
}
}