Esempio n. 1
0
        public void ReturnSpawner(Node oldSpawner)
        {
            SpawnAmount--;
            Vector3 enemyHeightOffset = new Vector3(0,2,0);
            Enemy enemy = ObjectPoolManager.ActivateObject("Enemy", oldSpawner.transform.position + enemyHeightOffset, Quaternion.identity).GetComponent<Enemy>();
            enemy.Init();
            enemy.OnDeath += OnEnemyDeath;
            DeployedEnemies.Add(enemy);

            oldSpawner.SetAsNormal();
            MobSpawners.Remove(oldSpawner);
            spawnersDeployed--;

            GetNewSpawner();

            if (SpawnAmount == 0)
            {
                if (OnSpawnComplete != null) OnSpawnComplete();
                MobSpawners.Clear();
                spawnersDeployed = 0;
            }

            if (DeployedEnemies.Count >= MaxInField)
                foreach (Node mobSpawner in MobSpawners.Where(spawner => !spawner.HaltSpawning))
                    mobSpawner.HaltSpawning = true;
            else
                foreach (Node mobSpawner in MobSpawners.Where(spawner => spawner.HaltSpawning))
                    mobSpawner.HaltSpawning = false;
        }
Esempio n. 2
0
        public void Generate()
        {
            System.Random rng = new System.Random();
            if (RandomGeneration)
            {
                GridSeed = Random.Range(1, int.MaxValue);
                MaxObstacleHeight = Random.Range(1.5f, 4);
                ObstacleColorA = new Color((float)rng.NextDouble(), (float)rng.NextDouble(), (float)rng.NextDouble());
            }
            ClearNodes();
            allNodeCoords = new List<Coord>();
            NavMeshObject.localScale = new Vector3(MaxGridSize.x * NodeSize,MaxGridSize.y * NodeSize,1);

            for (int x = 0; x < GridSizeX; x++)
            {
                for (int y = 0; y < GridSizeY; y++)
                {
                    Vector3 nodePos = CoordToVector(x, y);
                    Node node = (Node)Instantiate(Node, nodePos, Quaternion.identity);
                    node.transform.parent = transform;
                    node.GridPosition = new Vector2(x, y);
                    Nodes.Add(node);
                    allNodeCoords.Add(new Coord(x,y));

                    if (GridSizeX/2 == x && GridSizeY/2 == y) CenterNode = node;
                }
            }
            Quaternion teleporterRot = Quaternion.Euler(-90, 0, 0);
            currentTeleporter = (Teleporter)Instantiate(Teleporter, CenterNode.transform.position, teleporterRot);
            currentTeleporter.transform.parent = transform;
            gridCoordCenter = new Coord(GridSizeX/2,GridSizeY/2);

            shuffledNodeCoords = new Queue<Coord>(Functions.ShuffleArray(allNodeCoords.ToArray(), GridSeed));
            bool[,] obstacleMap = new bool[GridSizeX,GridSizeY];
            int currentObstacleCount = 0;

            for (int i = 0; i < Nodes.Count * ObstaclePercent; i++)
            {
                Coord randomCoord = GetRandomCoord();
                Vector3 coordPos = CoordToVector(randomCoord.X, randomCoord.Y);
                obstacleMap[randomCoord.X, randomCoord.Y] = true;
                currentObstacleCount++;

                if (coordPos != GridCenter && GridIsFullyAccessible(obstacleMap, currentObstacleCount))
                {
                    Transform obstacle = (Transform) Instantiate(Obstacle, coordPos, Quaternion.identity);
                    obstacle.parent = transform;

                    float obstacleHeight = Mathf.Lerp(MinObstacleHeight,MaxObstacleHeight , (float)rng.NextDouble());
                    obstacle.localScale = new Vector3(obstacle.localScale.x, obstacleHeight, obstacle.localScale.z);
                    obstacle.position += new Vector3(0,obstacle.localScale.y/2,0);

                    Renderer obstacleRenderer = obstacle.GetComponent<Renderer>();
                    Material obstacleMaterial = new Material(obstacleRenderer.sharedMaterial);
                    float colorPercent = randomCoord.Y/(float)GridSizeY;
                    obstacleMaterial.color = Color.Lerp(ObstacleColorA, ObstacleColorB, colorPercent);
                    obstacleRenderer.sharedMaterial = obstacleMaterial;

                    Obstacles.Add(obstacle);
                }
                else
                {
                    obstacleMap[randomCoord.X, randomCoord.Y] = false;
                    currentObstacleCount--;
                }
            }

            for (int i = 0; i < Obstacles.Count; i++)
            {
                for (int j = 0; j < Nodes.Count; j++)
                {
                    Vector3 obstaclePos = new Vector3(Obstacles[i].transform.position.x, 0, Obstacles[i].transform.position.z);
                    if (Nodes[j].transform.position != obstaclePos) continue;
                    Nodes[j].Obstacle = true;
                    break;
                }
            }

            foreach (Node node in Nodes) GridCenter += node.transform.position;
            GridCenter /= Nodes.Count;
            NavMeshObject.position = GridCenter;
        }