// MonoBehaviour's INTERFACE

    void Awake()
    {
        m_CharacterInput = GetComponent <tnCharacterInput>();

        m_Respawn  = GetComponent <tnRespawn>();
        m_Kick     = GetComponent <tnKick>();
        m_Kickable = GetComponent <tnKickable>();
    }
    // MonoBehaviour's INTERFACE

    void Awake()
    {
        m_CharacterController = GetComponent <tnCharacterController>();
        m_Respawn             = GetComponent <tnRespawn>();

        m_Kick     = GetComponent <tnKick>();
        m_Kickable = GetComponent <tnKickable>();

        m_AttractContoller = GetComponent <tnAttract>();

        // Set sort order.

        sortOrder = BehaviourSortOrder.s_SortOrder_CharacterStats;
    }
Ejemplo n.º 3
0
    // TrueSyncBehaviour's interface

    public override void OnSyncedTriggerEnter(TSCollision2D i_Collision)
    {
        if (!i_Collision.collider.CompareTag(Tags.s_Ball))
        {
            return;
        }

        if (m_TeamId == Hash.s_NULL)
        {
            return;
        }

        tnGoalEventParams goalEventParams = new tnGoalEventParams();

        goalEventParams.SetTeamId(m_TeamId);

        goalEventParams.SetHasValidScorer(false);

        {
            // Try to assign a valid scorer.

            tnKickable kickable = i_Collision.gameObject.GetComponent <tnKickable>();
            if (kickable != null)
            {
                for (int touchIndex = 0; touchIndex < kickable.touchesCount; ++touchIndex)
                {
                    tnTouch ballTouch = kickable.GetTouch(touchIndex);

                    if (m_TeamId != ballTouch.teamId)
                    {
                        goalEventParams.SetScorerId(ballTouch.characterId);
                        goalEventParams.SetScorerTeamId(ballTouch.teamId);

                        goalEventParams.SetIsHumanScorer(ballTouch.isHuman);
                        goalEventParams.SetIsLocalScorer(ballTouch.isLocal);

                        goalEventParams.SetHasValidScorer(true);

                        break;
                    }
                }
            }
        }

        // Send event.

        Messenger.Broadcast <tnGoalEventParams>("Goal", goalEventParams);
    }
    private void OnKickOccurred(tnKickable i_Target)
    {
        if (i_Target == null)
        {
            return;
        }

        if (m_Params == null)
        {
            return;
        }

        if (i_Target.CompareTag(Tags.s_Ball))
        {
            InternalSetVibrationValues(m_Params.kickBallIntensity, m_Params.kickBallDuration);
        }
        else
        {
            if (i_Target.CompareTag(Tags.s_Character))
            {
                InternalSetVibrationValues(m_Params.kickCharacterIntensity, m_Params.kickCharacterDuration);
            }
        }
    }
    private void OnKickOccurred(tnKickable i_Target)
    {
        if (m_Results == null)
        {
            return;
        }

        if (i_Target == null)
        {
            return;
        }

        if (i_Target.CompareTag(Tags.s_Ball))
        {
            m_Results.shots = m_Results.shots + 1;

            if (m_OpponentGoal != null)
            {
                TSVector2 topPostPosition    = m_OpponentGoal.topPostPosition;
                TSVector2 bottomPostPosition = m_OpponentGoal.bottomPostPosition;

                TSVector2 currentPosition = tsTransform2D.position;

                TSVector2 topPostDirection = topPostPosition - currentPosition;
                topPostDirection.Normalize();

                TSVector2 bottomPostDirection = bottomPostPosition - currentPosition;
                bottomPostDirection.Normalize();

                TSVector2 targetPosition = i_Target.tsTransform2D.position;

                TSVector2 targetDirection = targetPosition - currentPosition;
                targetDirection.Normalize();

                TSVector2 goalPosition = m_OpponentGoal.tsTransform2D.position;

                TSVector2 goalDirection = goalPosition - currentPosition;
                goalDirection.Normalize();

                FP dot = TSVector2.Dot(targetDirection, goalDirection);

                if (dot > 0f)
                {
                    FP angleA = TSVector2.Angle(topPostDirection, TSVector2.up);
                    FP angleB = TSVector2.Angle(bottomPostDirection, TSVector2.up);

                    FP shotAngle = TSVector2.Angle(targetDirection, TSVector2.up);

                    if (shotAngle > angleA && shotAngle < angleB)
                    {
                        m_Results.shotsOnTarget = m_Results.shotsOnTarget + 1;
                    }
                }
            }
        }
        else
        {
            if (i_Target.CompareTag(Tags.s_Character))
            {
                m_Results.tackles = m_Results.tackles + 1;
            }
        }
    }
    // INTERNALS

    private bool Kick(LayerMask i_Mask, FP i_AppliedForce, FP i_DeltaTime, bool i_InvertVelocity = false)
    {
        bool kickedAnything = false;

        TSCollider2D[] colliders = TSPhysics2D.OverlapCircleAll(tsTransform2D.position, m_Radius, i_Mask);

        if (colliders != null)
        {
            for (int colliderIndex = 0; colliderIndex < colliders.Length; ++colliderIndex)
            {
                TSCollider2D currentCollider = colliders[colliderIndex];

                if (currentCollider == null)
                {
                    continue;
                }

                GameObject currentGo = currentCollider.gameObject;

                if (m_CharacterController != null)
                {
                    int currentLayer = m_CharacterController.currentLayer;

                    if (currentLayer == m_ChargingLayer)
                    {
                        continue;
                    }
                }

                tnKickable kickable = currentGo.GetComponent <tnKickable>();

                if (kickable == null)
                {
                    continue;
                }

                TSTransform2D kickableTransform = kickable.tsTransform2D;

                if (kickableTransform == null)
                {
                    continue;
                }

                TSVector2 kickablePosition = kickableTransform.position;

                kickedAnything = true;

                // Evaluate force.

                TSVector2 direction = kickablePosition - tsTransform2D.position;
                direction.Normalize();

                TSVector2 force = direction * i_AppliedForce;

                if (i_InvertVelocity)
                {
                    if (i_DeltaTime > FP.Zero)
                    {
                        FP        otherMass     = kickable.mass;
                        TSVector2 otherVelocity = kickable.currentVelocity;

                        TSVector2 oppositeForce = (otherMass * otherVelocity) / i_DeltaTime;
                        oppositeForce = oppositeForce * -FP.One;
                        force        += oppositeForce;
                    }
                }

                // Kick.

                kickable.Kick(force, gameObject);

                // Notify listeners.

                if (m_KickEvent != null)
                {
                    m_KickEvent(kickable);
                }
            }
        }

        return(kickedAnything);
    }