Ejemplo n.º 1
0
    // INTERNALS

    private void InternalRespawn(TSVector2 i_Position, FP i_Rotation)
    {
        // Pre-Respawn.

        if (m_PreRespawnEvent != null)
        {
            m_PreRespawnEvent();
        }

        // Respawn - Move Rigidbody.

        if (m_Rigidbody2d != null)
        {
            FP mass = m_Rigidbody2d.mass;

            bool kinematic = m_Rigidbody2d.isKinematic;
            m_Rigidbody2d.isKinematic = true;

            m_Rigidbody2d.velocity = TSVector2.zero;
            m_Rigidbody2d.MovePosition(i_Position);
            m_Rigidbody2d.MoveRotation(i_Rotation);

            m_Rigidbody2d.isKinematic = kinematic;
            m_Rigidbody2d.mass        = mass;
        }
        else
        {
            tsTransform2D.position = i_Position;
            tsTransform2D.rotation = i_Rotation;
        }

        // Notify listeners.

        if (m_RespawnOccurredEvent != null)
        {
            m_RespawnOccurredEvent();
        }

        // Post-Respawn.

        if (m_PostRespawnEvent != null)
        {
            m_PostRespawnEvent();
        }
    }
    // INTERNALS

    private void Respawn()
    {
        int tick = TrueSyncManager.ticksMain;

        // If rigidbody, re-config it. Move otherwise.

        if (m_Rigidbody != null)
        {
            m_Rigidbody.isKinematic = m_CachedKinematic;
            m_Rigidbody.mass        = m_CachedMass;

            m_Rigidbody.MovePosition(m_RespawnPosition);
            m_Rigidbody.AddForce(m_OutForce);
        }
        else
        {
            tsTransform2D.position = m_RespawnPosition;
        }

        // Play Effect.

        if (!m_OutEffectTicks.Contains(tick))
        {
            Effect outEffect = m_OutEffect;
            EffectUtils.PlayEffect(outEffect, currentPosition);

            m_OutEffectTicks.Add(tick);
        }

        // Clear class variables.

        m_EnteringHoleIndex = -1;
        m_TeleportTick      = 0;

        m_Teleporting = false;
        m_Timer       = FP.Zero;

        m_Delay           = FP.Zero;
        m_OutForce        = TSVector2.zero;
        m_RespawnPosition = TSVector2.zero;

        m_OutEffect           = null;
        m_TeleportRequestTick = 0;
    }
Ejemplo n.º 3
0
    public override void OnSyncedUpdate()
    {
        base.OnSyncedUpdate();

        if (!m_IsActive || m_HoleIndex < 0)
        {
            return;
        }

        // Get Simulation info.

        int currentTick    = TrueSyncManager.ticksMain;
        int rollbackWindow = TrueSyncManager.rollbackWindowMain;

        // Check current collision

        TSVector2 myPosition = tsTransform2D.position;

        for (int targetIndex = 0; targetIndex < m_Targets.Count; ++targetIndex)
        {
            tnHoleTarget holeTarget = m_Targets[targetIndex];

            if (holeTarget == null)
            {
                continue;
            }

            TSTransform2D otherTransform = holeTarget.GetComponent <TSTransform2D>();

            if (otherTransform == null)
            {
                continue;
            }

            TSVector2 targetPosition = otherTransform.position;

            TSVector2 positionDelta = targetPosition - myPosition;

            FP distance2 = positionDelta.LengthSquared();
            if (distance2 < m_Threshold * m_Threshold)
            {
                // Notify collision.

                holeTarget.CollidingWithHole();

                // Add object to pending list.

                if (holeTarget.canEnterHole && !holeTarget.isTeleporting)
                {
                    Internal_CacheTarget(currentTick, holeTarget);
                }
            }
        }

        // Check pending objects.

        for (int index = 0; index < m_Pending.count; ++index)
        {
            int tick = m_Pending.GetKey(index);

            if (currentTick == tick + rollbackWindow)
            {
                List <tnHoleTarget> holeTargets = m_Pending.GetValue(tick);
                if (holeTargets != null)
                {
                    for (int targetIndex = 0; targetIndex < holeTargets.Count; ++targetIndex)
                    {
                        tnHoleTarget holeTarget = holeTargets[targetIndex];

                        if (holeTarget == null)
                        {
                            continue;
                        }

                        RespawnPoint respawnPoint = GetRandomSpawnPoint();

                        if (respawnPoint == null || respawnPoint.transform == null)
                        {
                            continue;
                        }

                        TSTransform2D targetTransform = holeTarget.tsTransform2D;
                        TSRigidBody2D targetRigidbody = holeTarget.GetComponent <TSRigidBody2D>();

                        // Snap position.

                        if (targetRigidbody != null)
                        {
                            targetRigidbody.MovePosition(tsTransform2D.position);
                        }
                        else
                        {
                            targetTransform.position = tsTransform2D.position;
                        }

                        // Set rigidbody velocity,

                        if (targetRigidbody != null)
                        {
                            targetRigidbody.velocity = TSVector2.zero;
                        }

                        // Eavluate force

                        TSVector2 forceDirection = respawnPoint.forceDirection;
                        forceDirection.Normalize();

                        if (MathFP.Abs(respawnPoint.errorAngle) > FP.Zero)
                        {
                            int random      = TSRandom.Range(0, 101);
                            FP  t           = ((FP)random) / 100;
                            FP  randomError = MathFP.Lerp(-respawnPoint.errorAngle, respawnPoint.errorAngle, t);
                            forceDirection = forceDirection.Rotate(randomError);
                        }

                        TSVector2 outForce = forceDirection * respawnPoint.forceIntensity;

                        // Teleport.

                        holeTarget.Teleport(m_HoleIndex, respawnPoint.respawnPosition, outForce, m_RespawnTime, m_InEffect, m_OutEffect);
                    }
                }
            }
        }

        // Remove old data from dictionary.

        for (int index = 0; index < m_Pending.count; ++index)
        {
            int tick          = m_Pending.GetKey(index);
            int executionTick = tick + rollbackWindow;

            bool isSafeTick = TrueSyncManager.IsTickOutOfRollbackMain(executionTick);
            if (isSafeTick)
            {
                m_Pending.Remove(tick);
                index = -1;
            }
        }
    }