Example #1
0
        /// <summary>
        /// Start is only called once in the lifetime of the behaviour.
        /// The difference between Awake and Start is that Start is only called if the script instance is enabled.
        /// This allows you to delay any initialization code, until it is really needed.
        /// Awake is always called before any Start functions.
        /// This allows you to order initialization of scripts
        /// </summary>
        public void Start()
        {
            // If we are using the old GUI, deactivate the new UI canvas
            if (useOldUI == true)
            {
                EnemyDispenser ed = GetComponent <EnemyDispenser>();
                // If we have an enemy canvas assigned, deactivate it
                if (ed.enemyCanvas)
                {
                    ed.enemyCanvas.gameObject.SetActive(false);
                }
            }

            // Go through all the enemy types, reset their delays and add them to the total enemies count
            for (int enemyIndex = 0; enemyIndex < enemies.Length; enemyIndex++)
            {
                // Add to total enemies
                enemiesTotal += enemies[enemyIndex].enemyCounts;
            }

            // If we have a boss, count it too
            if (enemyBoss)
            {
                enemiesTotal++;
            }

            // Check how many Last Line of Defense objects are available
            LLODAvailable = GameObject.FindGameObjectsWithTag("LLOD").Length;
        }
Example #2
0
        /// <summary>
        /// Resumes gameplay, and reactivates in-game menu items.
        /// </summary>
        public void Resume()
        {
            isPaused = false;

            if (gameSpeeds.Length > 0)
            {
                Time.timeScale = gameSpeeds[currentSpeed];
            }
            else
            {
                Time.timeScale = 1;
            }

            BuildController bc = GetComponent <BuildController>();
            EnemyDispenser  ed = GetComponent <EnemyDispenser>();
            GameSpeed       gs = GetComponent <GameSpeed>();
            GameMenu        gm = GetComponent <GameMenu>();

            // Enable all other components in this game controller object
            if (bc)
            {
                bc.enabled = true;
            }

            if (ed)
            {
                ed.enabled = true;
            }

            if (gs)
            {
                gs.enabled = true;
            }

            // Disable the in-game menu
            if (gm)
            {
                gm.enabled = false;
            }

            // If we have a game menu canvas for the new UI (4.6+), deactivate it
            if (useOldUI == false)
            {
                if (gameMenuCanvas)
                {
                    gameMenuCanvas.gameObject.SetActive(false);
                }

                if (speedMenuCanvas)
                {
                    speedMenuCanvas.gameObject.SetActive(true);
                }

                if (buildMenuCanvas)
                {
                    buildMenuCanvas.gameObject.SetActive(true);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Pauses gameplay, and deactivates in-game menu items.
        /// </summary>
        public void Pause()
        {
            isPaused = true;

            Time.timeScale = 0;

            BuildController bc = GetComponent <BuildController>();
            EnemyDispenser  ed = GetComponent <EnemyDispenser>();
            GameSpeed       gs = GetComponent <GameSpeed>();
            GameMenu        gm = GetComponent <GameMenu>();

            // Enable all other components in this game controller object
            if (bc)
            {
                bc.enabled = false;
            }

            if (ed)
            {
                ed.enabled = false;
            }

            if (gs)
            {
                gs.enabled = false;
            }

            // If we are using the old UI, activate the OnGUI game menu
            if (gm && useOldUI == true)
            {
                gm.enabled = true;
            }

            // If we have a game menu canvas for the new UI (4.6+), activate it
            if (useOldUI == false)
            {
                if (gameMenuCanvas)
                {
                    gameMenuCanvas.gameObject.SetActive(true);
                }

                if (speedMenuCanvas)
                {
                    speedMenuCanvas.gameObject.SetActive(false);
                }

                if (buildMenuCanvas)
                {
                    buildMenuCanvas.gameObject.SetActive(false);
                }
            }
        }
Example #4
0
        /// <summary>
        /// OnTriggerEnter is called when the Collider other enters the trigger.
        /// This message is sent to the trigger collider and the rigidbody (if any) that the trigger collider belongs to,
        /// and to the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger
        /// events are only sent if one of the colliders also has a rigidbody attached.
        /// </summary>
        /// <param name='other'>
        /// Other objects collider.
        /// </param>
        public void OnTriggerEnter(Collider other)
        {
            // If this enemy collides with the Defeat Line, it's game over
            if (other.name == defeatLine.name)
            {
                EnemyDispenser ed = gameController.GetComponent <EnemyDispenser>();

                if (ed && ed.lose == false)
                {
                    ed.Lose();
                }
            }
        }
Example #5
0
        /// <summary>
        /// Update is called every frame, if the MonoBehaviour is enabled.
        /// </summary>
        public void Update()
        {
            //If we are using the new GUI (4.6+), update it while the game runs
            if (useOldUI == false)
            {
                EnemyDispenser ed = GetComponent <EnemyDispenser>();

                if (ed.enemyCanvas)
                {
                    // Display how many enemies are left to be spawned
                    Vector2 deltaSize = new Vector2(ed.enemyCanvas.Find("Image").GetComponent <RectTransform>().sizeDelta.x *(1 - enemiesCreated / enemiesTotal), ed.enemyCanvas.Find("Image").GetComponent <RectTransform>().sizeDelta.y);

                    ed.enemyCanvas.Find("Image/Fill").GetComponent <RectTransform>().sizeDelta = deltaSize;
                    ed.enemyCanvas.Find("Image/Text").GetComponent <Text>().text = (enemiesTotal - enemiesCreated).ToString() + "/" + enemiesTotal.ToString();
                }
            }

            // Count up the spawn delay
            spawnDelayCount += Time.deltaTime;

            // Choose a random spawn point from the list
            int spawnIndex = Random.Range(0, spawnPoints.Length);

            if (spawnDelayCount >= spawnDelay)
            {
                // Go through each enemy type and create an enemy at a random spawn point
                if (index < enemies.Length)
                {
                    // If we still have enemies of this type, proceed
                    if (enemies[index].enemyCounts > 0)
                    {
                        // enemies[enemyIndex].enemyDelaysCount = 0;
                        spawnDelayCount = 0;

                        // Count down the enemy amount
                        enemies[index].enemyCounts--;

                        // Count up the number of enemies created
                        enemiesCreated++;

                        // Create the enemy at the chosen spawn point
                        Transform _newEnemy = (Transform)Instantiate(enemies[index].enemyTypes, spawnPoints[spawnIndex].position, spawnPoints[spawnIndex].rotation);

                        // Scale objects for 2D presentation
                        _newEnemy.eulerAngles = new Vector3(_newEnemy.eulerAngles.x, _newEnemy.eulerAngles.y, 180);
                        _newEnemy.localScale  = new Vector3(_newEnemy.localScale.x, _newEnemy.localScale.y * -1, _newEnemy.localScale.z);

                        // Set the lane of this enemy
                        Enemy e = _newEnemy.GetComponent <Enemy>();

                        if (e)
                        {
                            e.lane = spawnPoints[spawnIndex];
                        }
                        //_newEnemy.GetComponent<Enemy>().lane = spawnPoints[spawnIndex];
                    }

                    index++;
                }
                else
                {
                    index = 0;
                }
            }

            if (battleIsOver == false)
            {
                // If we have an enemy boss set, create it
                if (enemyBoss != null)
                {
                    if (enemiesCreated >= enemiesTotal - 1)
                    {
                        if (bossState == 0)
                        {
                            bossState = 1;                             // Set boss state to "created"

                            // Create a boss enemy and place it in the middle spwan point
                            currentBoss = (Transform)Instantiate(enemyBoss, spawnPoints[(int)Mathf.Floor(spawnPoints.Length * 0.5f)].position, spawnPoints[spawnIndex].rotation);

                            // Scale objects for 2D presentation
                            currentBoss.eulerAngles = new Vector3(currentBoss.eulerAngles.x, currentBoss.eulerAngles.y, 180);
                            currentBoss.localScale  = new Vector3(currentBoss.localScale.x, currentBoss.localScale.y * -1, currentBoss.localScale.z);

                            // Set the lane of this enemy
                            currentBoss.GetComponent <Enemy>().lane = spawnPoints[spawnIndex];
                        }
                    }

                    // If all created enemies were killed and we don't have a boss, then we win
                    if (enemiesKilled >= enemiesTotal && currentBoss == null)
                    {
                        Win();
                    }
                }
                else if (enemiesKilled >= enemiesTotal)                  // If all created enemies were killed and we don't have a boss, then we win
                {
                    Win();
                }
            }
        }
Example #6
0
        /// <summary>
        /// Start is only called once in the lifetime of the behaviour.
        /// The difference between Awake and Start is that Start is only called if the script instance is enabled.
        /// This allows you to delay any initialization code, until it is really needed.
        /// Awake is always called before any Start functions.
        /// This allows you to order initialization of scripts
        /// </summary>
        public void Start()
        {
            GameSpeed       gs = GetComponent <GameSpeed>();
            BuildController bc = GetComponent <BuildController>();
            EnemyDispenser  ed = GetComponent <EnemyDispenser>();
            GameMenu        gm = GetComponent <GameMenu>();

            // Go through all the resource types the player has and deactivate their new UI (4.6+) icons
            for (int resourceIndex = 0; resourceIndex < bc.moneyTypes.Length; resourceIndex++)
            {
                // Disable the icon of the resource type
                if (bc.moneyTypes[resourceIndex].icon)
                {
                    bc.moneyTypes[resourceIndex].icon.gameObject.SetActive(false);
                }
            }

            // If we are using the old GUI, deactivate the new UI canvas
            if (useOldUI == true && unitSelectionCanvas)
            {
                unitSelectionCanvas.gameObject.SetActive(false);
            }

            // Deactivate the speed menu canvas, because we need to select units for the battle first
            if (gs.speedMenuCanvas)
            {
                gs.speedMenuCanvas.gameObject.SetActive(false);
            }

            // Deactivate the build menu canvas, because we need to select units for the battle first
            if (bc.buildMenuCanvas)
            {
                bc.buildMenuCanvas.gameObject.SetActive(false);
            }

            // Deactivate the enemy canvas, because we need to select units for the battle first
            if (ed.enemyCanvas)
            {
                ed.enemyCanvas.gameObject.SetActive(false);
            }

            // If we are using the old UI, limit the value of Max Units based on the number of slots in the build list ( new UI )
            if (useOldUI == false)
            {
                if (maxUnits > buildListButtons.Length)
                {
                    maxUnits = buildListButtons.Length;
                }

                // Remove any extra slots in the build list
                for (int index = maxUnits; index < buildListButtons.Length; index++)
                {
                    // Make the button of the building in the build list unclickable
                    //buildListButtons[index].GetComponent(Button).interactable = false;

                    // Disable the whole building slot
                    //buildListButtons[index].Find("Icon").gameObject.SetActive(false);

                    //Disable the whole building slot
                    buildListButtons[index].gameObject.SetActive(false);
                }
            }

            // Pause the game
            Time.timeScale = 0;

            // Calculate the size of a single tile within the grid of units
            tileSize = new Vector2(positionAndSize.width / grid.x, positionAndSize.height / grid.y);

            // Disable all other components in this game controller object
            if (bc)
            {
                bc.enabled = false;
            }

            if (ed)
            {
                ed.enabled = false;
            }

            if (gs)
            {
                gs.enabled = false;
            }

            if (gm)
            {
                gm.enabled = false;
            }

            // Set the build list in the BuildController script to be the same length as maxUnits
            bc.buildList = new Transform[maxUnits];

            // Register the build list transform array for easier access
            buildList = bc.buildList;

            // Set the units which are essential to this battle
            SetEssential();

            // Set the icons of the units in the grid
            SetGrid();
        }
Example #7
0
        /// <summary>
        /// Ends the unit selection phase, and starts the game.
        /// </summary>
        public void StartGame()
        {
            // If we have at least one selected unit, start the game
            if (selectedUnits > 0)
            {
                GameSpeed       gs = GetComponent <GameSpeed>();
                BuildController bc = GetComponent <BuildController>();
                EnemyDispenser  ed = GetComponent <EnemyDispenser>();

                // Unpause the game
                Time.timeScale = 1;

                if (GetComponent <AudioSource>())
                {
                    GetComponent <AudioSource>().Play();
                }

                // Enable all other components in this game controller object
                if (bc)
                {
                    bc.enabled = true;
                }

                if (ed)
                {
                    ed.enabled = true;
                }

                if (gs)
                {
                    gs.enabled = true;
                }

                // Assign the correct cooldown for each unit in the list
                bc.GetCooldowns();

                // Deactivate the unit selection canvas
                if (unitSelectionCanvas)
                {
                    unitSelectionCanvas.gameObject.SetActive(false);
                }

                // If we are using the new GUI (4.6+), activate the build menu canvas and set the build list for it
                if (useOldUI == false)
                {
                    // If we have a speed menu canvas assigned, activate it
                    if (gs.speedMenuCanvas)
                    {
                        gs.speedMenuCanvas.gameObject.SetActive(true);
                    }

                    // If we have a build menu canvas assigned, activate it
                    if (bc.buildMenuCanvas)
                    {
                        bc.buildMenuCanvas.gameObject.SetActive(true);
                    }

                    // If we have an enemy canvas assigned, activate it
                    if (ed.enemyCanvas)
                    {
                        ed.enemyCanvas.gameObject.SetActive(true);
                    }

                    // Set the build list icons in the build controller
                    bc.SetBuildList();

                    // If we have a game speed canvas assigned, activate it
                    if (gs)
                    {
                        if (gs.speedMenuCanvas)
                        {
                            gs.speedMenuCanvas.gameObject.SetActive(true);
                        }
                    }

                    // Go through all the resource types the player has and activate their new UI (4.6+) icons
                    for (int resourceIndex = 0; resourceIndex < bc.moneyTypes.Length; resourceIndex++)
                    {
                        // Activate the icon of the resource type
                        bc.moneyTypes[resourceIndex].icon.gameObject.SetActive(true);
                    }
                }

                // Disable this script
                this.enabled = false;
            }
        }