Esempio n. 1
0
    public virtual void TakeHit(Bullet bullet)
    {
        if (GameManager.instance.paused)
        {
            return;
        }
        var shieldActive = this.hasShield && this.shieldHealth > 0;

        if (!shieldActive)
        {
            DisplayUtil.FlashWhite(this, this.sprite, bullet.power / 40);
            if (this.playHitSound)
            {
                SfxManager.instance.PlaySound(SoundType.ENEMY_HURT);
            }
        }


        StartCoroutine(GameManager.instance.PauseForSeconds(shieldActive ? 0.005f : 0.01f * hitStopMultiplier, () => {
            Camera.main.Kick(bullet.velocity * (bullet.power / 4));

            if (hasShield && shieldHealth >= 0)
            {
                if (bullet.color == shieldColor)
                {
                    float tempShieldHealth = this.shieldHealth;
                    this.shieldHealth     -= bullet.power;
                    bullet.power          -= tempShieldHealth;

                    if (this.shieldHealth <= 0)
                    {
                        this.outline.Clear();
                        this.outline.enabled = false;
                        this.hasShield       = false;
                        Camera.main.Shake(0.6f);
                        GameObject explosionCopy = Instantiate(explosion);

                        SfxManager.instance.PlaySound(SoundType.SHIELD_DESTROYED, 1.2f);
                        explosionCopy.transform.localScale -= Vector3.one * 0.25f;
                        explosionCopy.transform.position    = transform.position + (Vector3)offset;
                    }
                    else
                    {
                        DisplayUtil.FlashOutlineWhite(this, this.outline, bullet.power);
                    }
                }
                else
                {
                    SfxManager.instance.PlaySound(SoundType.SHIELD_DEFLECT);
                    var offsetVector = (bullet.transform.position - transform.position).normalized * bullet.velocity.magnitude;
                    bullet.velocity  = offsetVector;
                }
            }
            else
            {
                float tempHealth = this.health;
                this.health     -= bullet.power;
                bullet.power    -= tempHealth;
            }

            Signals.Get <HitByBulletSignal>().Dispatch(new AttackData(this.type, bullet.owner, this != null && this.health <= 0));

            if (bullet != null && bullet.power <= 0)
            {
                bullet.Die();
            }
            if (this != null && this.health <= 0)
            {
                this.Die(true);
            }
        }));
    }