Example #1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        var attack = collision.gameObject.GetComponent <Attack>();

        if (attack != null && collision.gameObject.tag != gameObject.tag)
        {
            var damage = Mathf.Max(attack.Damage - Defence, 1f);

            health = Mathf.Clamp(health - damage, 0, HealthMax);
            RefreshHealth();

            if (Popup != null && gameObject.tag != "Player")
            {
                Popup.CreatePopup(damage, Color.red);
            }

            if (health <= 0f)
            {
                if (gameObject.tag == "Player")
                {
                    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
                }

                if (Drop != null)
                {
                    Drop.DropStuff();
                }

                Destroy(gameObject);
            }
        }

        var powerup = collision.gameObject.GetComponent <Powerup>();

        if (powerup != null)
        {
            HealthMax += powerup.MaxHealth;
            health     = Mathf.Clamp(health + powerup.Health, 0, HealthMax);

            RefreshHealth();

            if (powerup.MaxHealth > 0f)
            {
                Popup.CreatePopup(powerup.MaxHealth, powerup);
            }
            else if (powerup.Health > 0f)
            {
                Popup.CreatePopup(powerup.Health, powerup);
            }

            if (powerup.Defence > 0f)
            {
                Popup.CreatePopup(powerup.Defence, powerup);
            }

            Destroy(powerup.gameObject);
        }
    }