// Snaps a world point to a fog pixel. It returns the position
        public static Vector2 SnapWorldPositionToNearestFogPixel(FogOfWar fow, Vector2 worldpos, Vector2 offset, Vector2i resolution, float size)
        {
            Vector2 fogpos = WorldToFog(worldpos, fow.mapOffset, fow.mapResolution, fow.mapSize);

            fogpos = SnapToNearestFogPixel(fogpos);
            return(FogToWorld(fogpos, fow.mapOffset, fow.mapResolution, fow.mapSize));
        }
Exemple #2
0
        FogOfWarShape CreateShape(FogOfWar fow)
        {
            if (shapeType == FogOfWarShapeType.Circle)
            {
                FogOfWarShapeCircle shape = new FogOfWarShapeCircle();
                FillShape(fow, shape);
                shape.innerRadius = innerRadius;
                shape.angle       = angle;
                return(shape);
            }
            else if (shapeType == FogOfWarShapeType.Box)
            {
                FogOfWarShapeBox shape = new FogOfWarShapeBox();
                FillShape(fow, shape);
                return(shape);
            }
            else if (shapeType == FogOfWarShapeType.Texture)
            {
                if (texture == null)
                {
                    return(null);
                }

                FogOfWarShapeTexture shape = new FogOfWarShapeTexture();
                FillShape(fow, shape);
                shape.texture         = texture;
                shape.rotateToForward = rotateToForward;
                return(shape);
            }
            return(null);
        }
Exemple #3
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            FogOfWarChunkManager cm  = (FogOfWarChunkManager)target;
            FogOfWar             fow = cm.GetComponent <FogOfWar>();

            if (fow.mapResolution.x != fow.mapResolution.y)
            {
                EditorGUILayout.HelpBox("Map Resolution must be square!", MessageType.Error);
            }
            if (fow.mapResolution.x % 2 != 0)
            {
                EditorGUILayout.HelpBox("Map Resolution must be divisible by 2!", MessageType.Error);
            }

            if (Application.isPlaying)
            {
                EditorGUILayout.HelpBox(string.Format("Chunks Loaded: {0}\nMemory Usage {0:0.0}kb", cm.loadedChunkCount, cm.loadedChunkCount * fow.mapResolution.manhattanMagnitude / 1024.0f), MessageType.None);
                if (GUILayout.Button("Clear Memory"))
                {
                    cm.Clear();
                }
            }
        }
Exemple #4
0
        public ColliderFogRect(Collider2D c, FogOfWar fow)
        {
            Bounds b = c.bounds;

            position = fow.WorldPositionToFogPosition(b.min);
            size     = fow.WorldPositionToFogPosition(b.max) - position;
        }
        void Awake()
        {
            _fogOfWar = GetComponent <FogOfWar>();

            if (_fogOfWar.mapResolution.x != _fogOfWar.mapResolution.y)
            {
                Debug.LogError("FogOfWarChunkManager requires FogOfWar Map Resolution to be square and a power of 2!");
                enabled = false;
            }

            _mapResolution = _fogOfWar.mapResolution.x;
        }
Exemple #6
0
 public void Set(FogOfWar fow)
 {
     resolution    = fow.mapResolution;
     size          = fow.mapSize;
     offset        = fow.mapOffset;
     pixelSize     = resolution.x / size;
     pixelCount    = resolution.x * resolution.y;
     plane         = fow.plane;
     physics       = fow.physics;
     filterMode    = fow.filterMode;
     multithreaded = fow.multithreaded;
 }
Exemple #7
0
        public bool[] CalculateLineOfSightCells(FogOfWar fow, FogOfWarPhysics physicsmode, Vector3 eyepos)
        {
            if (physicsmode == FogOfWarPhysics.Physics3D)
            {
                Debug.LogWarning("Physics3D is not supported with cells!", this);
                return(null);
            }

            int rad   = Mathf.RoundToInt(radius);
            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);

            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;
                    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 > radius)
                    {
                        _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);
        }
Exemple #8
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.foward = FogOfWarConversion.TransformFogPlaneForward(_transform, fow.plane);
     shape.offset = offset;
     shape.radius = radius;
 }
        void Update()
        {
            if (_isInFog == FogOfWar.GetFogOfWarTeam(team).IsInFog(_transform.position, minFogStrength))
            {
                return;
            }

            _isInFog = !_isInFog;

            if (_isInFog)
            {
                onFogEnter.Invoke();
            }
            else
            {
                onFogExit.Invoke();
            }
        }
Exemple #10
0
        public FogOfWarShape GetShape(FogOfWar fow, FogOfWarPhysics physics, FogOfWarPlane plane)
        {
            FogOfWarShape shape = CreateShape(fow);

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

            if (cellBased)
            {
                shape.lineOfSight  = null;
                shape.visibleCells = CalculateLineOfSightCells(fow, physics, shape.eyePosition);
            }
            else
            {
                shape.lineOfSight  = CalculateLineOfSight(physics, shape.eyePosition, plane);
                shape.visibleCells = null;
            }
            return(shape);
        }
Exemple #11
0
        void Update()
        {
            FogOfWar fow = FogOfWar.GetFogOfWarTeam(team);

            if (fow == null)
            {
                Debug.LogWarning("There is no Fog Of War team for team #" + team.ToString());
                return;
            }
            bool visible = !fow.IsInFog(_transform.position, minFogStrength);

            if (_renderer != null)
            {
                _renderer.enabled = visible;
            }
            if (_graphic != null)
            {
                _graphic.enabled = visible;
            }
            if (_canvas != null)
            {
                _canvas.enabled = visible;
            }
        }
 void OnRenderImage(RenderTexture source, RenderTexture destination)
 {
     FogOfWar.GetFogOfWarTeam(team).RenderFog(source, destination, _camera, _transform);
 }
Exemple #13
0
 public ColliderFogRectList(FogOfWar fow)
 {
     fogOfWar = fow;
 }
Exemple #14
0
 public FogOfWarMap(FogOfWar fow)
 {
     Set(fow);
 }