コード例 #1
0
    /// <summary>
    /// Damages the player.
    /// </summary>
    /// <param name="damage">Amount of damage done to the player.</param>
    /// <param name="hitFront">If the player was hit in the front.</param>
    /// <returns>If the damage was applied.</returns>
    public bool Damage(int damage, bool hitFront)
    {
        if (invulnerable)
        {
            return(false);
        }

        damageDone += damage;

        // If max damage was reached, it will heal
        if (damageDone < maxDamage)
        {
            return(true);
        }

        // Vulnerability is set to true here
        // Has to be set to false at the end of the Heal animation
        invulnerable = true;
        powerUpController.DisableAll();

        // Disable collisions between player and enemies
        // Has to be reenabled at the end of the Heal animation
        Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), LayerMask.NameToLayer("Enemy"), true);

        onHeal?.Invoke();

        // No more paper towels, player has lost
        if (numPaperTowels <= 0)
        {
            animator.SetTrigger("Lose");
            gameOverUI.SetActive(true);
            Time_Display.Stop();
        }

        if (hitFront)
        {
            animator.SetTrigger("HitFront");
        }
        else
        {
            animator.SetTrigger("HitBack");
        }

        // Damage sound is randomly picked
        switch (Random.Range(0, 2))
        {
        case 0:
            audioManager.PlaySound("Damage1");
            break;

        default:
            audioManager.PlaySound("Damage2");
            break;
        }

        damageDone = 0;

        // Paper towel has been used, decrease by one
        numPaperTowels     = Mathf.Max(numPaperTowels - 1, 0);
        healthDisplay.text = numPaperTowels.ToString();

        return(true);
    }