Beispiel #1
0
    ///////////////////////////////////////////////////////////////////////////

    bool LineOfSightCanSee(FogShape shape, Vector2 offset, float fogradius)
    {
        if (shape.LineOfSight == null)
        {
            return(true);
        }

        float idx = FogTools.ClockwiseAngle(Vector2.up, offset) * shape.LineOfSight.Length / 360.0f;

        if (idx < 0)
        {
            idx += shape.LineOfSight.Length;
        }


        // sampling
        float value;

        if (_Map.FilterMode == FilterMode.Point)
        {
            value = shape.LineOfSight[Mathf.RoundToInt(idx) % shape.LineOfSight.Length];
        }
        else
        {
            int idxlow  = Mathf.FloorToInt(idx);
            int idxhigh = (idxlow + 1) % shape.LineOfSight.Length;
            value = Mathf.LerpUnclamped(shape.LineOfSight[idxlow], shape.LineOfSight[idxhigh], idx % 1);
        }

        float dist = value * fogradius;

        return(offset.sqrMagnitude < dist * dist);
    }
Beispiel #2
0
        //                        FUNCTIONS
        ///////////////////////////////////////////////////////////////////////////

        public DrawInfo(FogMap map, FogShape shape, float xradius, float yradius)
        {
            // convert size to fog space
            fogForward   = shape.Forward;
            forwardAngle = FogTools.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 = FogConversion.WorldToFog(FogConversion.WorldToFogPlane(shape.EyePosition, map.Plane) + relativeoffset, map.Offset, map.Resolution, map.Size);
            fogEyePos    = new Vector2_1(FogConversion.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 = FogConversion.SnapToNearestFogPixel(fogCenterPos);
                fogEyePos    = new Vector2_1(FogConversion.SnapToNearestFogPixel(FogConversion.WorldToFog(shape.EyePosition, map.Offset, map.Resolution, map.Size)));

                Vector2_1 pos = new Vector2_1(Mathf.RoundToInt(fogCenterPos.x), Mathf.RoundToInt(fogCenterPos.y));
                Vector2_1 rad = new Vector2_1(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 ProcessUnits(bool checkstopwatch)
    {
        // remove any invalid units
        FogUnit.RegisteredUnits.RemoveAll(u => u == null);

        double millisecondfrequency = 1000.0 / System.Diagnostics.Stopwatch.Frequency;

        for (; _CurrentUnitProcessing < FogUnit.RegisteredUnits.Count; ++_CurrentUnitProcessing)
        {
            if (!FogUnit.RegisteredUnits[_CurrentUnitProcessing].isActiveAndEnabled)
            {
                continue;
            }

            FogShape shape = FogUnit.RegisteredUnits[_CurrentUnitProcessing].GetShape(this, Plane);
            if (MultiThreaded && UpdateAutomatically)
            {
                _ThreadPool.Run(() => _Drawer.Draw(shape));
            }
            else
            {
                _Drawer.Draw(shape);
            }

            // do the timer check here so that at least one unit will be processed!
            if (checkstopwatch && _StopWatch.ElapsedTicks * millisecondfrequency >= MaxMillisecondsPerFrame)
            {
                ++_CurrentUnitProcessing;
                break;
            }
        }
    }
Beispiel #4
0
 public void Draw(FogShape shape)
 {
     if (shape is FogCircle)
     {
         DrawCircle(shape as FogCircle);
     }
     else if (shape is FogShapeTexture)
     {
         DrawTexture(shape as FogShapeTexture);
     }
 }
Beispiel #5
0
    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Returns fog of war line of sight shape.
    /// </summary>
    /// <param name="fow"></param>
    /// <param name="plane"></param>
    /// <returns></returns>
    public FogShape GetShape(FogManager fow, FogOfWarPlane plane)
    {
        FogShape shape = CreateShape(fow);

        if (shape == null)
        {
            return(null);
        }

        shape.LineOfSight  = CalculateLineOfSight(shape.EyePosition, plane);
        shape.VisibleCells = null;
        return(shape);
    }
Beispiel #6
0
    ///////////////////////////////////////////////////////////////////////////

    void FillShape(FogManager fow, FogShape shape)
    {
        if (AntiFlicker)
        {
            // snap to nearest fog pixel
            shape.EyePosition = FogConversion.SnapWorldPositionToNearestFogPixel(fow, FogConversion.WorldToFogPlane(_Transform.position, fow.Plane), fow.MapOffset, fow.MapResolution, fow.MapSize);
            shape.EyePosition = FogConversion.FogPlaneToWorld(shape.EyePosition.x, shape.EyePosition.y, _Transform.position.y, fow.Plane);
        }
        else
        {
            shape.EyePosition = _Transform.position;
        }
        shape.Forward = FogConversion.TransformFogPlaneForward(_Transform, fow.Plane);
        shape.Offset  = Offset;
        shape.Radius  = Radius;
    }
Beispiel #7
0
    ///////////////////////////////////////////////////////////////////////////

    bool LineOfSightCanSeeCell(FogShape shape, Vector2_1 offset)
    {
        if (shape.VisibleCells == null)
        {
            return(true);
        }

        int radius = Mathf.RoundToInt(shape.Radius);
        int width  = radius + radius + 1;

        offset.x += radius;
        if (offset.x < 0 || offset.x >= width)
        {
            return(true);
        }

        offset.y += radius;
        if (offset.y < 0 || offset.y >= width)
        {
            return(true);
        }

        return(shape.VisibleCells[offset.y * width + offset.x]);
    }