Ejemplo n.º 1
0
        public void Spawn()
        {
            if (_spawnedObstacles != null)
            {
                foreach (Obstacle obstacle in _spawnedObstacles)
                {
                    Destroy(obstacle.gameObject);
                }
                _spawnedObstacles.Clear();
            }
            else
            {
                _spawnedObstacles = new List <Obstacle>();
            }

            if (_spawnedOpenTiles != null)
            {
                foreach (OpenTile openTile in _spawnedOpenTiles)
                {
                    Destroy(openTile.gameObject);
                }
                _spawnedOpenTiles.Clear();
            }
            else
            {
                _spawnedOpenTiles = new List <OpenTile>();
            }

            for (int i = 0; i < obstacleSpawnTries; i++)
            {
                Vector2Int coord = new Vector2Int(UnityEngine.Random.Range(0, PathFinder.Size), UnityEngine.Random.Range(0, PathFinder.Size));
                if (!Obstacle.CanOccupy(coord))
                {
                    continue;
                }

                Obstacle obstacle = Instantiate(obstaclePrefab, new Vector3(coord.x, 0f, coord.y), Quaternion.identity);
                _spawnedObstacles.Add(obstacle);
            }

            for (int x = 0; x < PathFinder.Size; x++)
            {
                for (int y = 0; y < PathFinder.Size; y++)
                {
                    Vector2Int coord = new Vector2Int(x, y);
                    if (!Obstacle.CanOccupy(coord))
                    {
                        continue;
                    }

                    OpenTile openTile = Instantiate(openTilePrefab, new Vector3(coord.x, 0f, coord.y), Quaternion.identity);
                    _spawnedOpenTiles.Add(openTile);
                }
            }
        }
Ejemplo n.º 2
0
        private void RePath()
        {
            for (int x = 0; x < Size; x++)
            {
                for (int y = 0; y < Size; y++)
                {
                    OpenTile.SetAsPathMember(new Vector2Int(x, y), false);
                }
            }

            Vector2Int[] path = TofuUnity.Vector2IntPathfinder.GetPath(_fromCoord, _toCoord, (Vector2Int coord) =>
            {
                bool toReturn = coord.x >= 0 && coord.x < PathFinder.Size && coord.y >= 0 && coord.y < PathFinder.Size && Obstacle.CanOccupy(coord);
                toReturn     |= coord == _toCoord;
                return(toReturn);
            });

            for (int i = 0; i < path.Length; i++)
            {
                OpenTile.SetAsPathMember(path[i], true);
            }
        }