public DrawInfo(FogOfWarMap map, FogOfWarShape shape, float xradius, float yradius)
            {
                // convert size to fog space
                fogForward   = shape.foward;
                forwardAngle = FogOfWarUtils.ClockwiseAngle(Vector2.up, fogForward) * Mathf.Deg2Rad;
                float   sin            = Mathf.Sin(-forwardAngle);
                float   cos            = Mathf.Cos(-forwardAngle);
                Vector2 relativeoffset = new Vector2(shape.offset.x * cos - shape.offset.y * sin, shape.offset.x * sin + shape.offset.y * cos);

                fogCenterPos = FogOfWarConversion.WorldToFog(FogOfWarConversion.WorldToFogPlane(shape.eyePosition, map.plane) + relativeoffset, map.offset, map.resolution, map.size);
                fogEyePos    = new Vector2i(FogOfWarConversion.WorldToFog(shape.eyePosition, map.plane, map.offset, map.resolution, map.size));

                // find ranges
                if (shape.visibleCells == null)
                {
                    xMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.x - xradius));
                    xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(fogCenterPos.x + xradius));
                    yMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.y - yradius));
                    yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(fogCenterPos.y + yradius));
                }
                else
                {
                    fogCenterPos = FogOfWarConversion.SnapToNearestFogPixel(fogCenterPos);
                    fogEyePos    = new Vector2i(FogOfWarConversion.SnapToNearestFogPixel(FogOfWarConversion.WorldToFog(shape.eyePosition, map.offset, map.resolution, map.size)));

                    Vector2i pos = new Vector2i(Mathf.RoundToInt(fogCenterPos.x), Mathf.RoundToInt(fogCenterPos.y));
                    Vector2i rad = new Vector2i(Mathf.RoundToInt(xradius), Mathf.RoundToInt(yradius));
                    xMin = Mathf.Max(0, Mathf.RoundToInt(pos.x - rad.x));
                    xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(pos.x + rad.x));
                    yMin = Mathf.Max(0, Mathf.RoundToInt(pos.y - rad.y));
                    yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(pos.y + rad.y));
                }
            }
        void DrawOnMap(string text, Vector3 position, int panelwidth)
        {
            Vector2  normalizedfogpos = (FogOfWarConversion.WorldToFogPlane(position, _fog.plane) - _fog.mapOffset) / _fog.mapSize + new Vector2(0.5f, 0.5f);
            Vector2i mappos           = new Vector2i(normalizedfogpos * (panelwidth - 20));

            GUI.Label(new Rect(10 + mappos.x - 10, Screen.height - mappos.y - 10, 20, 20), text);
        }
Exemplo n.º 3
0
        // Converts a world position to a fog pixel position. Values will be between 0 and mapResolution.
        public Vector2i WorldPositionToFogPosition(Vector3 position)
        {
            Vector2 fogplanepos = FogOfWarConversion.WorldToFogPlane(position, plane);
            Vector2 mappos      = FogOfWarConversion.WorldToFog(fogplanepos, mapOffset, mapResolution, mapSize);

            return(new Vector2i(mappos));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Set the fog for a square area of the map. Positions are all in world coordinates. 0 is fully unfogged and 255 if fully fogged.
        /// </summary>
        /// <param name="bounds"></param>
        /// <param name="value"></param>
        public void SetFog(Bounds bounds, byte value)
        {
            Rect rect = new Rect();

            rect.min = FogOfWarConversion.WorldToFog(bounds.min, plane, mapOffset, mapResolution, mapSize);
            rect.max = FogOfWarConversion.WorldToFog(bounds.max, plane, mapOffset, mapResolution, mapSize);

            int xmin = (int)Mathf.Max(rect.xMin, 0);
            int xmax = (int)Mathf.Min(rect.xMax, mapResolution.x);
            int ymin = (int)Mathf.Max(rect.yMin, 0);
            int ymax = (int)Mathf.Min(rect.yMax, mapResolution.y);

            // if it is not visible on the map
            if (xmin >= mapResolution.x || xmax < 0 || ymin >= mapResolution.y || ymax < 0)
            {
                return;
            }

            for (int y = ymin; y < ymax; ++y)
            {
                for (int x = xmin; x < xmax; ++x)
                {
                    _fogValuesTotal[y * mapResolution.x + x] = value;
                }
            }
        }
Exemplo n.º 5
0
        public void Unfog(Bounds bounds)
        {
            Rect rect = new Rect();

            rect.min = FogOfWarConversion.WorldToFog(bounds.min, plane, mapOffset, mapResolution, mapSize);
            rect.max = FogOfWarConversion.WorldToFog(bounds.max, plane, mapOffset, mapResolution, mapSize);
            Unfog(rect);
        }
Exemplo n.º 6
0
        // Set the fog for a square area of the map. 0 is fully unfogged and 1 if fully fogged.
        public void SetFog(Bounds bounds, byte value)
        {
            Rect rect = new Rect();

            rect.min = FogOfWarConversion.WorldToFog(bounds.min, plane, mapOffset, mapResolution, mapSize);
            rect.max = FogOfWarConversion.WorldToFog(bounds.max, plane, mapOffset, mapResolution, mapSize);
            _drawer.SetFog(rect, value);
        }
Exemplo n.º 7
0
        //void OnDrawGizmosSelected()
        void OnDrawGizmos()
        {
            Gizmos.color = Color.red;
            Vector3 offset = FogOfWarConversion.FogPlaneToWorld(mapOffset.x, mapOffset.y, 0, plane);
            Vector3 size   = FogOfWarConversion.FogPlaneToWorld(mapSize, mapSize, 0, plane);

            Gizmos.DrawWireCube(offset, size);

            Gizmos.color = new Color(1, 0, 0, 0.2f);
            Gizmos.DrawCube(offset, size);
        }
Exemplo n.º 8
0
        public Vector2i WorldPositionToFogPosition(Vector3 position)
        {
            Vector2 mappos = FogOfWarConversion.WorldToFogPlane(position, plane) - mapOffset;

            mappos.Scale(mapResolution.vector2 / mapSize);

            Vector2i mapposi = new Vector2i(mappos);

            mapposi += new Vector2i(mapResolution.x >> 1, mapResolution.y >> 1);
            return(mapposi);
        }
Exemplo n.º 9
0
        public bool[] CalculateLineOfSightCells(FogOfWar fow, FogOfWarPhysics physicsmode, Vector3 eyepos, float distance)
        {
            if (physicsmode == FogOfWarPhysics.Physics3D)
            {
                Debug.LogWarning("Physics3D is not supported with cells!", this);
                return(null);
            }

            int rad   = Mathf.RoundToInt(distance);
            int width = rad + rad + 1;

            if (_visibleCells == null || _visibleCells.Length != width * width)
            {
                _visibleCells = new bool[width * width];
            }

            Vector2 cellsize  = (fow.mapResolution.vector2 * 1.1f) / fow.mapSize; // do 1.1 to bring it away from the collider a bit so the raycast won't hit it
            Vector2 playerpos = FogOfWarConversion.SnapWorldPositionToNearestFogPixel(fow, _transform.position, fow.mapOffset, fow.mapResolution, fow.mapSize);

            //playerpos.y += 5;
            for (int y = -rad; y <= rad; ++y)
            {
                for (int x = -rad; x <= rad; ++x)
                {
                    Vector2i offset = new Vector2i(x, y);

                    // find the nearest point in the cell to the player and raycast to that point
                    Vector2 fogoffset = offset.vector2 - new Vector2(Sign(offset.x) * cellsize.x, Sign(offset.y) * cellsize.y) * 0.5f;
                    //fogoffset.y -= 5;
                    Vector2 worldoffset = FogOfWarConversion.FogToWorldSize(fogoffset, fow.mapResolution, fow.mapSize);
                    Vector2 worldpos    = playerpos + worldoffset;

                    Debug.DrawLine(playerpos, worldpos);

                    int idx = (y + rad) * width + x + rad;

                    // if it is out of range
                    if (worldoffset.magnitude > distance)
                    {
                        _visibleCells[idx] = false;
                    }
                    else
                    {
                        _visibleCells[idx] = true;
                        RaycastHit2D hit = Physics2D.Raycast(playerpos, worldoffset.normalized, Mathf.Max(worldoffset.magnitude - lineOfSightPenetration, 0.00001f), lineOfSightMask);
                        _visibleCells[idx] = hit.collider == null;
                    }
                }
            }

            return(_visibleCells);
        }
Exemplo n.º 10
0
 void FillShape(FogOfWarShape shape)
 {
     if (antiFlicker)
     {
         // snap to nearest fog pixel
         shape.eyePosition = FogOfWarConversion.SnapWorldPositionToNearestFogPixel(FogOfWarConversion.WorldToFogPlane(_transform.position, FogOfWar.current.plane), FogOfWar.current.mapOffset, FogOfWar.current.mapResolution, FogOfWar.current.mapSize);
         shape.eyePosition = FogOfWarConversion.FogPlaneToWorld(shape.eyePosition.x, shape.eyePosition.y, _transform.position.y, FogOfWar.current.plane);
     }
     else
     {
         shape.eyePosition = _transform.position;
     }
     shape.foward = _transform.forward;
     shape.offset = offset;
     shape.radius = radius;
 }
Exemplo n.º 11
0
            public DrawInfo(FogOfWarMap map, FogOfWarShape shape)
            {
                // convert size to fog space
                Vector2 radius = shape.CalculateRadius() * map.pixelSize;

                fogForward = shape.foward;
                Vector2 relativeoffset;

                if (shape.absoluteOffset)
                {
                    forwardAngle   = 0;
                    relativeoffset = shape.offset;
                }
                else
                {
                    forwardAngle = FogOfWarUtils.ClockwiseAngle(Vector2.up, fogForward) * Mathf.Deg2Rad;
                    float sin = Mathf.Sin(-forwardAngle);
                    float cos = Mathf.Cos(-forwardAngle);
                    relativeoffset = new Vector2(shape.offset.x * cos - shape.offset.y * sin, shape.offset.x * sin + shape.offset.y * cos);
                }

                fogCenterPos = FogOfWarConversion.WorldToFog(FogOfWarConversion.WorldToFogPlane(shape.eyePosition, map.plane) + relativeoffset, map.offset, map.resolution, map.size);
                fogEyePos    = FogOfWarConversion.WorldToFog(shape.eyePosition, map.plane, map.offset, map.resolution, map.size).ToInt();

                // find ranges
                if (shape.visibleCells == null)
                {
                    xMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.x - radius.x));
                    xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(fogCenterPos.x + radius.x));
                    yMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.y - radius.y));
                    yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(fogCenterPos.y + radius.y));
                }
                else
                {
                    fogCenterPos = FogOfWarConversion.SnapToNearestFogPixel(fogCenterPos);
                    fogEyePos    = FogOfWarConversion.SnapToNearestFogPixel(FogOfWarConversion.WorldToFog(shape.eyePosition, map.offset, map.resolution, map.size)).ToInt();

                    Vector2Int pos = fogCenterPos.ToInt();
                    Vector2Int rad = radius.ToInt();
                    xMin = Mathf.Max(0, Mathf.RoundToInt(pos.x - rad.x));
                    xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(pos.x + rad.x));
                    yMin = Mathf.Max(0, Mathf.RoundToInt(pos.y - rad.y));
                    yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(pos.y + rad.y));
                }
            }
Exemplo n.º 12
0
 void FillShape(FogOfWar fow, FogOfWarShape shape)
 {
     if (antiFlicker)
     {
         // snap to nearest fog pixel
         shape.eyePosition = FogOfWarConversion.SnapWorldPositionToNearestFogPixel(fow, FogOfWarConversion.WorldToFogPlane(_transform.position, fow.plane), fow.mapOffset, fow.mapResolution, fow.mapSize);
         shape.eyePosition = FogOfWarConversion.FogPlaneToWorld(shape.eyePosition.x, shape.eyePosition.y, _transform.position.y, fow.plane);
     }
     else
     {
         shape.eyePosition = _transform.position;
     }
     shape.brightness     = brightness;
     shape.foward         = FogOfWarConversion.TransformFogPlaneForward(_transform, fow.plane);
     shape.absoluteOffset = absoluteOffset;
     shape.offset         = offset;
     shape.radius         = circleRadius;
     shape.size           = boxSize;
 }
Exemplo n.º 13
0
        // Checks the visibility of an area, where a value of 0 is fully unfogged and 1 if fully fogged
        public float VisibilityOfArea(Bounds worldbounds)
        {
            Vector2 min = FogOfWarConversion.WorldToFog(worldbounds.min, plane, mapOffset, mapResolution, mapSize);
            Vector2 max = FogOfWarConversion.WorldToFog(worldbounds.max, plane, mapOffset, mapResolution, mapSize);

            int xmin = Mathf.Clamp(Mathf.RoundToInt(min.x), 0, mapResolution.x);
            int xmax = Mathf.Clamp(Mathf.RoundToInt(max.x), 0, mapResolution.x);
            int ymin = Mathf.Clamp(Mathf.RoundToInt(min.y), 0, mapResolution.y);
            int ymax = Mathf.Clamp(Mathf.RoundToInt(max.y), 0, mapResolution.y);

            float total = 0;
            int   count = 0;

            for (int y = ymin; y < ymax; ++y)
            {
                for (int x = xmin; x < xmax; ++x)
                {
                    ++count;
                    total += _fogValuesCopy[y * mapResolution.x + x] / 255.0f;
                }
            }

            return(total / count);
        }