// 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
        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 #3
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 #4
0
        public ColliderFogRect(Collider c, FogOfWar fow)
        {
            Bounds b = c.bounds;

            position = fow.WorldPositionToFogPosition(b.min);
            size     = fow.WorldPositionToFogPosition(b.max) - position;
        }
Exemple #5
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.GetFogValue(_transform.position) < minFogStrength;

            if (_renderer != null)
            {
                _renderer.enabled = visible;
            }
            if (_graphic != null)
            {
                _graphic.enabled = visible;
            }
            if (_canvas != null)
            {
                _canvas.enabled = visible;
            }
        }
        void Awake()
        {
            current = this;
            if (_ids == null)
            {
                _ids = new FoWIDs();
                _ids.Initialise();
            }

            Reinitialize();
        }
Exemple #7
0
        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 #8
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 #9
0
        public FogOfWarShape GetShape(FogOfWar fow, FogOfWarPhysics physics, FogOfWarPlane plane)
        {
            FogOfWarShape shape = CreateShape(fow);

            if (shape == null)
            {
                return(null);
            }
            shape.lineOfSight  = CalculateLineOfSight(physics, shape.eyePosition, plane);
            shape.visibleCells = null;
            return(shape);
        }
Exemple #10
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);
        }
        void Start()
        {
            _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;
                return;
            }

            _mapResolution = _fogOfWar.mapResolution.x;
            _fogOfWar.onRenderFogTexture.AddListener(OnRenderFog);

            ForceLoad();
        }
Exemple #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.foward = FogOfWarConversion.TransformFogPlaneForward(_transform, fow.plane);
     shape.offset = offset;
     shape.radius = radius;
 }
        void Update()
        {
            bool visible = !FogOfWar.GetFogOfWarTeam(team).IsInFog(_transform.position, minFogStrength);

            if (_renderer != null)
            {
                _renderer.enabled = visible;
            }
            if (_graphic != null)
            {
                _graphic.enabled = visible;
            }
            if (_canvas != null)
            {
                _canvas.enabled = visible;
            }
        }
        void Update()
        {
            if (_isInFog == FogOfWar.GetFogOfWarTeam(team).IsInFog(_transform.position, minFogStrength))
            {
                return;
            }

            _isInFog = !_isInFog;

            if (_isInFog)
            {
                onFogEnter.Invoke();
            }
            else
            {
                onFogExit.Invoke();
            }
        }
        void Update()
        {
            _transform.position = Vector2.MoveTowards(_transform.position, _targetPosition, Time.deltaTime * movementSpeed);
            if (cameraTransform != null)
            {
                cameraTransform.position = _transform.position + Vector3.back * 10;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                FogOfWar.GetFogOfWarTeam(0).SetAll(255);
            }

            if (Vector2.Distance(_transform.position, _targetPosition) < 0.01f)
            {
                Vector2 dir = Vector2.zero;
                if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
                {
                    ++dir.y;
                }
                else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
                {
                    ++dir.x;
                }
                else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
                {
                    --dir.y;
                }
                else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
                {
                    --dir.x;
                }

                if (dir.sqrMagnitude > 0.1f)
                {
                    RaycastHit2D hit = Physics2D.Raycast(_targetPosition, dir, 1);
                    if (hit.collider == null)
                    {
                        _targetPosition += dir;
                    }
                }
            }
        }
Exemple #16
0
        void Update()
        {
            bool isinfog = FogOfWar.GetFogOfWarTeam(team).GetFogValue(_transform.position) < (byte)(minFogStrength * 255);

            if (_isInFog == isinfog)
            {
                return;
            }

            _isInFog = !_isInFog;

            if (_isInFog)
            {
                onFogEnter.Invoke();
            }
            else
            {
                onFogExit.Invoke();
            }
        }
Exemple #17
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, shape.CalculateMaxLineOfSightDistance());
            }
            else
            {
                shape.lineOfSight  = CalculateLineOfSight(physics, shape.eyePosition, plane, shape.CalculateMaxLineOfSightDistance());
                shape.visibleCells = null;
            }
            return(shape);
        }
Exemple #18
0
 void OnRenderImage(RenderTexture source, RenderTexture destination)
 {
     FogOfWar.GetFogOfWarTeam(team).RenderFog(source, destination, _camera);
 }
 void Start()
 {
     _transform = transform;
     FogOfWar.RegisterUnit(this);
 }
 void Start()
 {
     _fog             = GetComponent <FogOfWar>();
     _camera          = GetComponent <Camera>();
     _cameraTransform = transform;
 }
Exemple #21
0
 public ColliderFogRectList(FogOfWar fow)
 {
     fogOfWar = fow;
 }
Exemple #22
0
 public FogOfWarMap(FogOfWar fow)
 {
     Set(fow);
 }