Esempio n. 1
0
    // called when character is entering this collectable
    public void OnTriggerEnter2D(Collider2D otherCollider)
    {
        // do nothing if not ebabled
        if (!enabled)
        {
            return;
        }

        if (m_playing)
        {
            // update playing state
            float fAnimTime = m_anim.GetCurrentAnimatorStateInfo(0).normalizedTime;
            if (fAnimTime >= 1f)
            {
                m_playing = false;
            }
            else
            {
                // do not allow impulse during play
                return;
            }
        }

        APCharacterController character = otherCollider.GetComponent <APCharacterController>();

        if (character != null)
        {
            m_playing = true;
            m_anim.Play(m_animHash, 0, 0f);

            // launch impulse/jump
            if (m_mode == EImpulseMode.Impulse)
            {
                float   fAngle       = -Mathf.Deg2Rad * m_impulseDirection;
                Vector2 v2ImpulseDir = new Vector2(Mathf.Cos(fAngle), -Mathf.Sin(fAngle));
                v2ImpulseDir = transform.TransformDirection(v2ImpulseDir);

                Vector2 charVel         = character.GetVelocity();
                float   velAlongImpulse = Vector2.Dot(charVel, v2ImpulseDir);
                charVel += v2ImpulseDir * (m_impulsePower - velAlongImpulse);
                character.SetVelocity(charVel);
            }
            else
            {
                character.Jump(m_jumpMinHeight, m_jumpMaxHeight);
            }
        }
    }
Esempio n. 2
0
    // called when character motor ray is touching us
    override public void OnCharacterTouch(APCharacterController character, APCharacterMotor.RayType rayType)
    {
        // Do nothing if dead
        if (IsDead())
        {
            return;
        }

        // prevent checking hits too often
        if (Time.time < m_lastHitTime + m_minTimeBetweenTwoReceivedHits)
        {
            return;
        }

        // save current hit time
        m_lastHitTime = Time.time;

        // check if jumping on us
        APSamplePlayer samplePlayer = character.GetComponent <APSamplePlayer>();

        if (rayType == APCharacterMotor.RayType.Ground)
        {
            // make character jump
            float  fRatio      = m_jumpRatioInputReleased;
            string sJumpButton = character.m_jump.m_button;
            if (!string.IsNullOrEmpty(sJumpButton) && Input.GetButton(sJumpButton))
            {
                fRatio = m_jumpRatioInputPushed;
            }

            if (fRatio > 0f)
            {
                character.Jump(fRatio);
            }

            // add hit to us
            AddHitDamage(samplePlayer.m_jumpDamage);
        }
        else
        {
            // notify hit to character controller as Blob makes damage by touching it
            samplePlayer.OnHit(this);
        }
    }
Esempio n. 3
0
    // called when hit by NPC object
    public void OnHit(APSampleBlob blob)
    {
        // do nothing if already dead or no damage
        if (IsDead() || (blob.m_touchDamage <= 0f))
        {
            return;
        }

        // reduce life point
        m_life -= blob.m_touchDamage;

        // handle death & callbacks
        if (m_life <= 0f)
        {
            // die !
            m_life = 0f;
        }
        else
        {
            // hit!
            // enable god mode if not already done
            if (!m_godMode)
            {
                m_godMode     = true;
                m_godModeTime = Time.time;
                if (m_anim)
                {
                    m_anim.SetBool("GodMode", true);
                }

                // disable collisions with npcs for a while
                APCharacterMotor motor = m_player.GetMotor();
                m_prevCollisionLayer = motor.m_rayLayer;
                motor.m_rayLayer     = m_godModeLayerMask;

                // add small impulse in opposite direction
                Vector2 v2Dir = motor.transform.position - blob.transform.position;
                motor.m_velocity.x += (v2Dir.x > 0f ? 1f : -1f) * m_hitImpulse;
                m_player.Jump(0.75f);
            }
        }
    }
Esempio n. 4
0
    // called when character motor ray is touching us
    override public bool OnCharacterTouch(APCharacterController character, APCharacterMotor.RayType rayType, RaycastHit2D hit, float penetration,
                                          APMaterial hitMaterial)
    {
        // Do nothing if dead
        if (IsDead())
        {
            return(false);
        }

        // Do nothing in godmode
        APSamplePlayer samplePlayer = character.GetComponent <APSamplePlayer>();

        if (samplePlayer && samplePlayer.IsGodModeEnabled())
        {
            return(false);
        }

        // check if jumping on us
        bool bHit = false;

        if ((rayType == APCharacterMotor.RayType.Ground) && (penetration <= m_jumpHitPenetrationTolerance))
        {
            // make character jump
            float fRatio = m_jumpRatioInputReleased;
            if (character.m_jump.m_button.IsSpecified() && character.m_jump.m_button.GetButton())
            {
                fRatio = m_jumpRatioInputPushed;
            }

            if (fRatio > 0f)
            {
                character.Jump(character.m_jump.m_minHeight * fRatio, character.m_jump.m_maxHeight * fRatio);
            }

            // add hit to NPC
            if (samplePlayer)
            {
                AddHitDamage(samplePlayer.m_jumpDamage);
            }

            bHit = true;
        }
        else if (penetration <= m_hitPenetrationTolerance)
        {
            // add hit to character
            if (samplePlayer)
            {
                samplePlayer.OnHit(m_touchDamage, transform.position);
            }

            bHit = true;
        }

        // prevent checking hits too often
        if (bHit)
        {
            if (Time.time < m_lastHitTime + m_minTimeBetweenTwoReceivedHits)
            {
                return(false);
            }

            // save current hit time
            m_lastHitTime = Time.time;
        }

        // always ignore contact
        return(false);
    }