Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //FOV CODE
- bool raycast(int sx, int sy, int sh, int tx, int ty, int th)
- {
- int x = sx;
- int y = sy;
- int dx = tx - sx;
- int dy = ty - sy;
- if (dx == 0 && dy == 0) return false;
- float cdx = 10000.0;
- float cdy = 10000.0;
- if (dy != 0)
- {
- cdx = IntToFloat(dx)/IntToFloat(dy);
- if (cdx < 0.0) cdx = -cdx;
- }
- if (dx != 0)
- {
- cdy = IntToFloat(dy)/IntToFloat(dx);
- if (cdy < 0.0) cdy = -cdy;
- }
- float D = Maths.Sqrt(IntToFloat(dx*dx + dy*dy));
- // distance
- float fx = 0.0;
- float fy = 0.0;
- // initial steps to get from the center of the tile to lines
- float hdx = 0.5*cdx;
- float hdy = 0.5;
- float vdx = 0.5;
- float vdy = 0.5*cdy;
- int xstep = 1, ystep = 1;
- if (dx < 0) xstep = -1;
- if (dy < 0) ystep = -1;
- bool done = false;
- while (!done)
- {
- if (hdx < vdx)
- {
- //HIT H
- fx += hdx;
- fy += hdy;
- vdx -= hdx;
- vdy -= hdy;
- hdx = cdx;
- hdy = 1.0;
- y += ystep;
- }
- else
- {
- //HIT V
- fx += vdx;
- fy += vdy;
- hdx -= vdx;
- hdy -= vdy;
- vdx = 1.0;
- vdy = cdy;
- x += xstep;
- }
- // dcalc
- float d = Maths.Sqrt(fx*fx + fy*fy);
- float h = IntToFloat(sh) + (d/D)*IntToFloat(th - sh);
- int index = Map.Index(x, y, 0);
- int tileType = tiles[index];
- if (!TileInfo[tileType].Walkable)
- {
- return false;
- }
- else if (x == tx && y == ty)
- {
- return true;
- }
- }
- }
- void DoFov(int sX, int sY, float x, float y, int radius)
- {
- int i;
- float ox,oy;
- ox = IntToFloat(sX)+0.5;
- oy = IntToFloat(sY)+0.5;
- while (i < radius){
- int ix = FloatToInt(ox, eRoundDown);
- int iy = FloatToInt(oy, eRoundDown);
- if (!Map.Sane(ix, iy)) return;
- int index = Map.Index(ix, iy, 0);
- //tileMask[tileIndex(FloatToInt(ox, eRoundDown), FloatToInt(oy, eRoundDown))] = 1;//Set the tile to visible.
- //if(tiles[index].Height - tiles[tileIndex(pos.X, pos.Y)].Height > 1) return;
- if (raycast(sX, sY, 1, ix, iy, 1)) {
- tileMask[index] = 1;
- }
- ox+=x;
- oy+=y;
- i++;
- }
- }
- static void Map::SetFOV(int sX, int sY, int radius)
- {
- float x,y;
- int i;
- ClearTileMask();
- while (i < 360){
- x = Maths.Cos(IntToFloat(i) * 0.01745);
- y = Maths.Sin(IntToFloat(i) * 0.01745);
- DoFov(sX, sY, x, y, radius);
- i+=1;
- }
- }
Add Comment
Please, Sign In to add comment