This is the method I used to accomplish this:
for (int y=0;y<mapHeight;y++) { for (int x=0;x<mapWidth;x++) { if(currentMap.getUnit(x, y)!=null) { int sightRange = currentMap.getUnit(x, y).getMovementRange()/2; for(int sightStepY = -sightRange;sightStepY<sightRange;sightStepY++) { for(int sightStepX = -sightRange;sightStepX<sightRange;sightStepX++) { if(x+sightStepX>=0&&x+sightStepX<mapWidth&&y+sightStepY>=0&&y+sightStepY<mapHeight) { if(sightStepY>=0&&sightStepX>=0) { if(sightStepY+sightStepX<=sightRange) { currentMap.fogArray[x+sightStepX][y+sightStepY]=0; } } if(y+sightRange<mapHeight){ currentMap.fogArray[x][y+sightRange]=0;} if(x+sightRange<mapHeight){ currentMap.fogArray[x+sightRange][y]=0;} if(sightStepY<=0&&sightStepX>=0) { if(sightStepX-sightStepY<=sightRange) { currentMap.fogArray[x+sightStepX][y+sightStepY]=0; } } if(sightStepY<=0&&sightStepX<=0) { if(-sightStepX-sightStepY<=sightRange) { currentMap.fogArray[x+sightStepX][y+sightStepY]=0; } } if(sightStepY>=0&&sightStepX<=0) { if(sightStepY-sightStepX<=sightRange) { currentMap.fogArray[x+sightStepX][y+sightStepY]=0; } } }}} }}}
Here is an example of the result when rendered in the game:
I think the above is rather messy and was achieved with a good deal of trial and error. I'm sure there is a more elegant way of achieving this. Any suggestions would be appreciated.
No comments:
Post a Comment