Esempio n. 1
0
    public IActorState CheckHurts()
    {
        Collider2D hurtBox     = null;
        Collider2D hitCollider = null;

        foreach (var box in GetComponentsInChildren <Collider2D>()
                 .Where(col => col.CompareTag(Tags.Hurtbox) && col.enabled))
        {
            var hitCount = box.OverlapCollider(_hurtContactFilter2D, _colliderBuffer);
            if (hitCount > 0)
            {
                hitCollider = _colliderBuffer[0];
                hurtBox     = box;
                break;
            }
        }

        if (hitCollider != null)
        {
            var harmfull = hitCollider.GetInterfaceComponentInParent <IHarmfull>();
            var source   = harmfull.GameObject;

            Health.AccountDamages(harmfull.Damage, source);

            if (harmfull.SkipHurtState)
            {
                if (!Health.IsAlive)
                {
                    return(new DeathState(this));
                }

                if (harmfull.TeleportToLastCheckpoint)
                {
                    CheckpointManager.TeleportPlayerToLastCheckpoint();
                    return(new PauseState());
                }
            }
            else
            {
                var distance2D = Physics2D.Distance(hurtBox, hitCollider);
                return(new HurtState(this, harmfull, -distance2D.normal));
            }
        }

        return(null);
    }