Exemple #1
0
        private static bool HasCrystal(Vector2 pos, int innerRadius, int outerRadius, IMapData mapData)
        {
            for (int dx = -outerRadius; dx < outerRadius; dx++)
            {
                for (int dy = -outerRadius; dy < outerRadius; dy++)
                {
                    var x = (int)pos.x + dx;
                    var y = (int)pos.y + dy;

                    if (x < 0 || x >= mapData.Width)
                    {
                        continue;
                    }

                    if (y < 0 || y >= mapData.Length)
                    {
                        continue;
                    }

                    if (Mathf.Abs(dx) < innerRadius || Mathf.Abs(dy) < innerRadius)
                    {
                        continue;
                    }

                    if (mapData.GetMapObjectAt(x, y) == MapObject.Crystal)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #2
0
        public static bool GetIsAreaFree(this IMapData data, Vector2 position, Vector2 size)
        {
            var intSize = new Vector2Int(Mathf.CeilToInt(size.x), Mathf.CeilToInt(size.y));
            var intPos  = new Vector2Int(Mathf.FloorToInt(position.x), Mathf.FloorToInt(position.y));

            var toCompare = data.GetHeightAt(intPos.x, intPos.y);

            for (int x = 0; x <= intSize.x; x++)
            {
                for (int y = 0; y <= intSize.y; y++)
                {
                    var localPos = intPos + new Vector2Int(x, y);
                    if (localPos.x < 0 || localPos.x >= data.Width || localPos.y < 0 || localPos.y >= data.Length)
                    {
                        return(false);
                    }

                    if (Math.Abs(toCompare - data.GetHeightAt(localPos.x, localPos.y)) > 0.001)
                    {
                        return(false);
                    }

                    if (data.GetMapObjectAt(localPos.x, localPos.y) != MapObject.None && x != intSize.x && y != intSize.y)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #3
0
        public void LoadMap(IMapData mapData, bool generateNavMesh)
        {
            var meshFilter = GetComponent <MeshFilter>();

            if (meshFilter == null)
            {
                throw new Exception("MeshFilter on map not found.");
            }

            if (meshFilter.mesh == null)
            {
                meshFilter.mesh = new Mesh();
            }

            var fowMeshFilter = FogOfWar.GetComponent <MeshFilter>();

            if (fowMeshFilter.mesh == null)
            {
                fowMeshFilter.mesh = new Mesh();
            }

            var fowVertices = new List <Vector3>();
            var mapVertices = new List <Vector3>();
            var mapUVs      = new List <Vector2>();
            var fowUVs      = new List <Vector2>();
            var i           = 0;
            var mapIndices  = new int[(mapData.Width - 1) * (mapData.Length - 1) * 4];

            for (int x = 0; x < mapData.Width; x++)
            {
                for (int y = 0; y < mapData.Length; y++)
                {
                    fowVertices.Add(GameUtils.GetPosition(new Vector2(x, y), mapData));
                    mapVertices.Add(GameUtils.GetPosition(new Vector2(x, y), mapData));
                    mapUVs.Add(new Vector2(x, y) / 4);
                    fowUVs.Add(new Vector2((float)x / mapData.Width, (float)y / mapData.Length));

                    if (y == mapData.Length - 1 || x == mapData.Width - 1)
                    {
                        continue;
                    }

                    mapIndices[i++] = x * mapData.Length + y;
                    mapIndices[i++] = x * mapData.Length + y + 1;
                    mapIndices[i++] = (x + 1) * mapData.Length + y + 1;
                    mapIndices[i++] = (x + 1) * mapData.Length + y;
                }
            }

            ApplyMesh(meshFilter, mapVertices, mapIndices, mapUVs);
            ApplyMesh(fowMeshFilter, fowVertices, mapIndices, fowUVs);

            var mapCollider = GetComponent <MeshCollider>();

            if (mapCollider != null)
            {
                mapCollider.sharedMesh = meshFilter.mesh;
            }

            var fowCollider = FogOfWar.GetComponent <MeshCollider>();

            if (fowCollider != null)
            {
                fowCollider.sharedMesh = fowMeshFilter.mesh;
            }

            gameObject.isStatic = true;

            for (int x = 0; x < mapData.Width; x++)
            {
                for (int y = 0; y < mapData.Length; y++)
                {
                    var        pos  = GameUtils.GetPosition(new Vector2(x + 0.5f, y + 0.5f), mapData);
                    GameObject inst = null;
                    switch (mapData.GetMapObjectAt(x, y))
                    {
                    case MapObject.Tree:
                        inst = Instantiate(TreePrefab);
                        break;

                    case MapObject.Crystal:
                        inst = Instantiate(CrystalPrefab);
                        break;
                    }

                    if (inst == null)
                    {
                        continue;
                    }

                    inst.transform.parent        = ObjectsContainer.transform;
                    inst.transform.localPosition = pos;
                }
            }

            if (generateNavMesh)
            {
                var settings = NavMesh.CreateSettings();
                settings.agentTypeID = AgentTypeID;
                settings.agentRadius = 0;
                settings.agentSlope  = 60;

                var source = new NavMeshBuildSource();
                source.shape        = NavMeshBuildSourceShape.Mesh;
                source.sourceObject = meshFilter.mesh;
                source.transform    = transform.localToWorldMatrix;

                var navMeshData = NavMeshBuilder.BuildNavMeshData(settings, new List <NavMeshBuildSource> {
                    source
                }, new Bounds(new Vector3((float)mapData.Width / 2, 0, (float)mapData.Length / 2), new Vector3(mapData.Width, 2, mapData.Length)), transform.position, transform.rotation);

                NavMesh.RemoveAllNavMeshData();
                NavMesh.AddNavMeshData(navMeshData);
            }
            mMapData = mapData;
            FogOfWarTexture.width                  = mapData.Width;
            FogOfWarTexture.height                 = mapData.Length;
            FogOfWarCamera.orthographicSize        = Mathf.Max(mapData.Width - 1, mapData.Length - 1) / 2f;
            FogOfWarCamera.transform.localPosition = new Vector3((mapData.Width - 1) / 2f, 10, (mapData.Length - 1) / 2f);
        }