Sightlines in a Tile-Based Strategy Game (Fog of War)

Hello. I’ve been working on a side project of mine, tile-based (Easiest thing ever, right?) strategy game. I have given each “unit” a method to “see” tiles, based on a “viewRange” integer. The way that I have it set up right now is that in my “Tile” class, if a tile is not within the view range of any units, it just defaults to a gray “Fog of War” tile. Otherwise, it draws the actual tile image (Grass, Mountain, whatever).

Anyways, I’m having difficulty setting up a loop that will take care of this automatically, and can be applied as-is without hard-coding anything specific to each “unit” type. The unit I’m experimenting with is a “scanProbe”, that can has a 5-tile viewRange. My current method is to have a for loop, starting at -viewRange (To start the value of viewRange to the left/above unit), and adding 1 for every iteration. I’m then setting “tile[unitTileX + i][unitTileY + i].canBeSeen = true;”. Github for more in-depth study is here: https://github.com/RyanMB97/TileBasedStrategy. Classes worth mentioning are: “Level”, “Tile”, “Entity”, and “ScanProbe”.

A quick thanks in advance for whoever can help me solve this. Also, seems worth mentioning, but could anyone tell me how to get either a block-like view area, or a diamond-like view area? Thanks again!

P.S.: Almost forgot that a picture would probably be useful:

Quick update: I figured it out. I was trying to fit it inside of a single for (int i = 0) loop. What I should’ve been doing is a nested loop for both x and y, separately.

public void seeTiles(Tile tiles[][]) {
		for (int x = -viewRange; x <= viewRange; x++) {
			for (int y = -viewRange; y <= viewRange; y++) {
				if(game.getLevel().tiles[tileX + x][tileY + y] != null){
					game.getLevel().tiles[tileX + x][tileY + y].setCanBeSeen(true);
				}
			}
		}
	}

A quick snippet from my “Entity” class. I found a separate algorithm/technique that can give a diamond-shape if I wanted. Anyways, just wanted to say that the thread can be closed.

P.S. Link to the diamond-method can be found here: http://www.java2s.com/Tutorial/Java/0080__Statement-Control/PrintoutaDiamond.htm. Just change the “System.out.print” to the tiles that you want changed.

It depends what you are trying to do , i think you are attempting to do a radius. So for a radius use a = Atan(angle) to plot your directions then add sin(a) to x each time and cos(a) to y each time. then draw the respective tile.