public void FieldOfViewUpdate() { UInt16 range = this.StatSightRange; //Map currentMap = this.InhabitedMap; if (range < 0) { return; } //REMOVE REDUNDANCY HERE BitArray[] update = new BitArray[_inhabitedMap.BoundX]; for (int i = 0; i < _inhabitedMap.BoundX; ++i) { update[i] = new BitArray(_inhabitedMap.BoundY); } for (Int32 i = -range + 1; i < range; i++) { for (Int32 j = -range + 1; j < range; j++) { Coords current = new Coords(CoordsType.Tile, _positionTile.X + i, _positionTile.Y + j); if ( !_myCollider.CheckInBounds(current) || (StaticMathFunctions.DistanceBetweenTwoCoordsEucledean(this._positionTile, current) > range) ) { continue; } bool val = _myVisibilityTracker.RayTracerVisibilityCheckTile(this._positionTile, current, false); update[current.X][current.Y] = val; } } // determine values that were changed for (int i = 0; i < _inhabitedMap.BoundX; ++i) { update[i] = update[i].Xor(_fieldOfView[i]); } // update changes for (int i = 0; i < _inhabitedMap.BoundX; ++i) { for (int j = 0; j < _inhabitedMap.BoundY; ++j) { if (update[i][j]) { bool val = _fieldOfView[i][j]; _fieldOfView[i][j] = !val; //_inhabitedMap.GetTile(i, j).VisibilityUpdate(this, !val); _myVisibilityTracker.VisibilityUpdate(new Coords(CoordsType.Tile, i, j), this, !val); } } } }
/// <summary> /// Returns the Bresenham line between p0 and p1; Borrowed the code /// from some dude whose name I don't have, who in turn borrowed from Wikipedia. /// </summary> private List <Coords> BresenhamLine(Coords p0, Coords p1) { List <Coords> returnList = new List <Coords>(); Boolean steep = Math.Abs(p1.Y - p0.Y) > Math.Abs(p1.X - p0.X); if (steep == true) { Coords tmpPoint = new Coords(CoordsType.Tile, p0.X, p0.Y); p0 = new Coords(CoordsType.Tile, tmpPoint.Y, tmpPoint.X); tmpPoint = p1; p1 = new Coords(CoordsType.Tile, tmpPoint.Y, tmpPoint.X); } Int32 deltaX = Math.Abs(p1.X - p0.X); Int32 deltaY = Math.Abs(p1.Y - p0.Y); Int32 error = 0; Int32 deltaError = deltaY; Int32 yStep = 0; Int32 xStep = 0; Int32 y = p0.Y; Int32 x = p0.X; if (p0.Y < p1.Y) { yStep = 1; } else { yStep = -1; } if (p0.X < p1.X) { xStep = 1; } else { xStep = -1; } Int32 tmpX = 0; Int32 tmpY = 0; while (x != p1.X) { x += xStep; error += deltaError; //if the error exceeds the X delta then //move one along on the Y axis if ((2 * error) > deltaX) { y += yStep; error -= deltaX; } //flip the coords if they're steep if (steep) { tmpX = y; tmpY = x; } else { tmpX = x; tmpY = y; } //check the point generated is legal //and if it is add it to the list if (_myCollider.CheckInBounds(new Coords(CoordsType.Tile, tmpX, tmpY)) == true) { returnList.Add(new Coords(CoordsType.Tile, tmpX, tmpY)); } else { //a bad point has been found, so return the list thus far return(returnList); } } return(returnList); }