Example #1
0
        private void Start()
        {
            healthBar.type = Image.Type.Filled;
            vitality       = gameObject.GetComponent <EnemyLiving>();
            baselineHealth = vitality.Health;
            inAttack       = true;

            if (attacks.Length > 0)
            {
                hasAttacksToExecute = true;
                attackIndex         = 0;
                currentAttack       = attacks[attackIndex].GetComponent <EnemyAttack>();
                currentAttack       = Instantiate(currentAttack);
                // Enable healthbar items for first attack
                healthBar.gameObject.SetActive(true);
                timeRemaining.gameObject.SetActive(true);
                attackNameText.gameObject.SetActive(true);
                UpdateNameTextbox();
            }
        }
Example #2
0
        private void Update()
        {
            // Perform attack-related updates.
            if (!hasAttacksToExecute)
            {
                return;
            }
            // Handle cooldowns between attacks.
            if (!inAttack)
            {
                // If we've passed the cooldown period, initiate the next attack.
                if (timeElapsed > attackSwitchCooldownSeconds)
                {
                    timeElapsed = 0;
                    inAttack    = true;
                    // Enable healthbar items for attack
                    healthBar.gameObject.SetActive(true);
                    timeRemaining.gameObject.SetActive(true);
                    attackNameText.gameObject.SetActive(true);
                    currentAttack = Instantiate(currentAttack);
                    UpdateNameTextbox();
                    healthBar.fillAmount = 1;
                    return;
                }

                timeElapsed += Time.deltaTime;
                return;
            }

            // Handle transitions if the player has timed out the enemy or killed it.
            if (timeElapsed > currentAttack.durationSeconds || damageTaken >= currentAttack.health)
            {
                // Dispose of the current attack's emitter(s).
                currentAttack.CleanUp();

                attackIndex += 1;

                // If we've reached the end of the attack list, handle next actions based on the type of enemy.
                if (attackIndex >= attacks.Length)
                {
                    // If the enemy should loop attacks, return to the beginning of the list.
                    if (shouldLoopAttacks)
                    {
                        attackIndex = 0;
                    }

                    // If the enemy should die at the end of its attack cycle, kill it.
                    if (canBeTimedOut)
                    {
                        vitality.health = 0;
                        vitality.Die();
                        // Disable healthbar items after last attack concludes
                        healthBar.gameObject.SetActive(false);
                        timeRemaining.gameObject.SetActive(false);
                        attackNameText.gameObject.SetActive(false);
                        return;
                    }

                    // The enemy has no more attacks if we've reached the end of the list and shouldn't loop or die.
                    hasAttacksToExecute = false;
                    // Disable healthbar items after last attack concludes
                    healthBar.gameObject.SetActive(false);
                    timeRemaining.gameObject.SetActive(false);
                    attackNameText.gameObject.SetActive(false);
                    return;
                }

                // Prepare for the next attack.
                currentAttack  = attacks[attackIndex].GetComponent <EnemyAttack>();
                baselineHealth = vitality.Health;
                damageTaken    = 0;
                timeElapsed    = 0f;
                inAttack       = false;
                // Disable healthbar items between attacks
                healthBar.gameObject.SetActive(false);
                timeRemaining.gameObject.SetActive(false);
                attackNameText.gameObject.SetActive(false);
                return;
            }

            // Update the player's progress.
            timeElapsed         += Time.deltaTime;
            timeRemaining.text   = $"{(int)(currentAttack.durationSeconds - timeElapsed)}";
            damageTaken          = baselineHealth - vitality.Health;
            healthBar.fillAmount = (currentAttack.health - damageTaken) / (float)currentAttack.health;
        }