Beispiel #1
0
        void Update()
        {
            FogOfWarTeam fow = FogOfWarTeam.GetTeam(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 * 255;

            if (_renderer != null)
            {
                _renderer.enabled = visible;
            }
            if (_graphic != null)
            {
                _graphic.enabled = visible;
            }
            if (_canvas != null)
            {
                _canvas.enabled = visible;
            }
        }
        // Snaps a world point to a fog pixel. It returns the position
        public static Vector3 SnapWorldPositionToNearestFogPixel(FogOfWarTeam fow, Vector2 worldpos)
        {
            Vector2 fogpos = WorldToFog(worldpos, fow.mapOffset, fow.mapResolution, fow.mapSize);

            fogpos = SnapToNearestFogPixel(fogpos);
            return(FogToWorld(fogpos, fow.mapOffset, fow.mapResolution, fow.mapSize));
        }
        public ColliderFogRect(Collider2D c, FogOfWarTeam fow)
        {
            Bounds b = c.bounds;

            position = fow.WorldPositionToFogPosition(b.min);
            size     = fow.WorldPositionToFogPosition(b.max) - position;
        }
 public void Set(FogOfWarTeam 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;
 }
        void Start()
        {
            _fogOfWar = GetComponent <FogOfWarTeam>();
            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();
        }
Beispiel #6
0
        void UpdatePlayerMovement()
        {
            Vector2 playerpos = Vector2.MoveTowards(playerTransform.position, _targetPosition, Time.deltaTime * movementSpeed);;

            playerTransform.position = playerpos;
            if (cameraTransform != null)
            {
                cameraTransform.position = new Vector3(playerpos.x, playerpos.y, -10);
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                FogOfWarTeam.GetTeam(0).SetAll(255);
            }

            if (Vector2.Distance(playerpos, _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;
                    }
                }
            }

            playerAnimator.movement = (_targetPosition - playerpos).normalized;
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

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

            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: {1}", cm.loadedChunkCount, EditorUtility.FormatBytes(cm.loadedChunkCount * fow.mapResolution.x * fow.mapResolution.y)), MessageType.None);
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Clear Memory"))
                {
                    cm.Clear();
                }
                if (GUILayout.Button("Save"))
                {
                    string path = EditorUtility.SaveFilePanel("Save FogOfWar Chunks", null, "chunks", null);
                    if (!string.IsNullOrEmpty(path))
                    {
                        System.IO.File.WriteAllBytes(path, cm.Save());
                    }
                }
                if (GUILayout.Button("Load"))
                {
                    string path = EditorUtility.OpenFilePanel("Load FogOfWar Chunks", null, null);
                    if (!string.IsNullOrEmpty(path))
                    {
                        cm.Load(System.IO.File.ReadAllBytes(path));
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        void Update()
        {
            bool isinfog = FogOfWarTeam.GetTeam(team).GetFogValue(_transform.position) < (byte)(minFogStrength * 255);

            if (_isInFog == isinfog)
            {
                return;
            }

            _isInFog = !_isInFog;

            if (_isInFog)
            {
                onFogEnter.Invoke();
            }
            else
            {
                onFogExit.Invoke();
            }
        }
        void DrawIconOnMap(FogOfWarTeam fow, Vector3 worldpos, Color color, byte maxfogamount = 255)
        {
            Vector2Int fogpos = fow.WorldPositionToFogPosition(worldpos);

            if (fogpos.x < 0 || fogpos.x >= fow.mapResolution.x ||
                fogpos.y < 0 || fogpos.y >= fow.mapResolution.y)
            {
                return;
            }

            if (maxfogamount < 255 && _fogValues[fow.mapResolution.y * fogpos.y + fogpos.x] > maxfogamount)
            {
                return;
            }

            int offset = (iconSize / 2) - 1;
            int xmin   = fogpos.x - offset;
            int xmax   = fogpos.x + offset;
            int ymin   = fogpos.y - offset;
            int ymax   = fogpos.y + offset;

            for (int y = ymin; y <= ymax; ++y)
            {
                if (y < 0 || y >= fow.mapResolution.y)
                {
                    continue;
                }

                for (int x = xmin; x <= xmax; ++x)
                {
                    if (x < 0 || x >= fow.mapResolution.x)
                    {
                        continue;
                    }

                    _pixels[fow.mapResolution.y * y + x] = color;
                }
            }
        }
        public void Render()
        {
            FogOfWarTeam fow = FogOfWarTeam.GetTeam(team);

            if (fow == null)
            {
                Debug.LogWarning("No FogOfWar team found: " + team.ToString());
                return;
            }

            if (fow.finalFogTexture == null)
            {
                return;
            }

            if (_ids == null)
            {
                _ids = new FoWIDs();
            }

#if UNITY_2019_3_OR_NEWER
            FogOfWarClearFog clearfog = null;
            camera.TryGetComponent(out clearfog);
#else
            FogOfWarClearFog clearfog = camera.GetComponent <FogOfWarClearFog>();
#endif
            if (clearfog != null && clearfog.targetCamera?.targetTexture != null)
            {
                fogColorTexture       = clearfog.targetCamera.targetTexture;
                fogTextureScreenSpace = true;
            }

            if ((camera.depthTextureMode & DepthTextureMode.Depth) == 0)
            {
                camera.depthTextureMode |= DepthTextureMode.Depth;
            }

            fow.finalFogTexture.filterMode = pointFiltering ? FilterMode.Point : FilterMode.Bilinear;
            SetTexture(_ids.fogTex, fow.finalFogTexture);
            SetVector(_ids.fogTextureSize, fow.mapResolution.ToFloat());
            SetFloat(_ids.mapSize, fow.mapSize);
            SetVector(_ids.mapOffset, fow.mapOffset);
            SetColor(_ids.mainFogColor, fogColor);
            SetMatrix(_ids.inverseView, camera.cameraToWorldMatrix);
            SetMatrix(_ids.inverseProj, camera.projectionMatrix.inverse);
            SetFloat(_ids.outsideFogStrength, outsideFogStrength);
            SetVector(_ids.cameraWorldPosition, camera.transform.position);
            SetFloat(_ids.stereoSeparation, camera.stereoSeparation);

            // orthographic is treated very differently in the shader, so we have to make sure it executes the right code
            SetKeyword("CAMERA_PERSPECTIVE", !camera.orthographic);
            SetKeyword("CAMERA_ORTHOGRAPHIC", camera.orthographic);

            // which plane will the fog be rendered to?
            SetKeyword("PLANE_XY", fow.plane == FogOfWarPlane.XY);
            SetKeyword("PLANE_YZ", fow.plane == FogOfWarPlane.YZ);
            SetKeyword("PLANE_XZ", fow.plane == FogOfWarPlane.XZ);

            SetKeyword("FOG_COLORED", fogColorTexture == null);
            SetKeyword("FOG_TEXTURED_WORLD", fogColorTexture != null && !fogTextureScreenSpace);
            SetKeyword("FOG_TEXTURED_SCREEN", fogColorTexture != null && fogTextureScreenSpace);
            if (fogColorTexture != null)
            {
                SetTexture(_ids.fogColorTex, fogColorTexture);
                SetVector(_ids.fogColorTexScale, new Vector2(fogColorTextureScale, fogColorTextureHeight));
            }

            SetKeyword("FOGFARPLANE", fogFarPlane);

            BlitToScreen();
        }
        void LateUpdate()
        {
            FogOfWarTeam fow = FogOfWarTeam.GetTeam(team);

            if (fow == null)
            {
                return;
            }

            // setup texture
            if (_texture == null || _texture.width != fow.mapResolution.x || _texture.height != fow.mapResolution.y)
            {
                if (_texture != null)
                {
                    Destroy(_texture);
                }

                _texture      = new Texture2D(fow.mapResolution.x, fow.mapResolution.y, TextureFormat.ARGB32, false, false);
                _texture.name = "FogOfWarMinimap";
                _fogValues    = new byte[fow.mapResolution.x * fow.mapResolution.y];
                _pixels       = new Color32[fow.mapResolution.x * fow.mapResolution.y];

                GetComponent <RawImage>().texture = _texture;
                if (aspectRatioFitter != null)
                {
                    aspectRatioFitter.aspectRatio = (float)fow.mapResolution.x / fow.mapResolution.y;
                }
            }

            // fog
            fow.GetTotalFogValues(ref _fogValues);
            for (int i = 0; i < _fogValues.Length; ++i)
            {
                int r = (fogColor.r - nonFogColor.r) * _fogValues[i] / 255 + nonFogColor.r;
                int g = (fogColor.g - nonFogColor.g) * _fogValues[i] / 255 + nonFogColor.g;
                int b = (fogColor.b - nonFogColor.b) * _fogValues[i] / 255 + nonFogColor.b;
                int a = (fogColor.a - nonFogColor.a) * _fogValues[i] / 255 + nonFogColor.a;
                _pixels[i] = new Color32((byte)r, (byte)g, (byte)b, (byte)a);
            }

            // units
            byte opponentminvisibility = (byte)(opponentMinFogStrength * 255);

            for (int i = 0; i < FogOfWarUnit.registeredUnits.Count; ++i)
            {
                FogOfWarUnit unit = FogOfWarUnit.registeredUnits[i];
                if (unit.team == team)
                {
                    DrawIconOnMap(fow, unit.transform.position, unitColor);
                }
                else
                {
                    DrawIconOnMap(fow, unit.transform.position, enemyColor, opponentminvisibility);
                }
            }

            // camera
            if (camera != null)
            {
                DrawIconOnMap(fow, camera.transform.position, cameraColor);
            }

            // apply to texture
            _texture.SetPixels32(_pixels);
            _texture.Apply(false, false);
        }
 public FogOfWarMap(FogOfWarTeam fow)
 {
     Set(fow);
 }
 public ColliderFogRectList(FogOfWarTeam fow)
 {
     fogOfWar = fow;
 }