Пример #1
0
    private IEnumerator DoLerpCameraToTransform(Transform transformPoint)
    {
        bool posReached = false;
        bool rotReached = false;

        while (!posReached || !rotReached)
        {
            yield return(null);

            mainCam.transform.position = Vector3.Lerp(mainCam.transform.position, transformPoint.position, camLerpSpeed);
            mainCam.transform.rotation = Quaternion.Slerp(mainCam.transform.rotation, transformPoint.rotation, camLerpSpeed);

            posReached = EEMath.Approximately(mainCam.transform.position, transformPoint.position, 0.01f);
            rotReached = EEMath.Approximately(mainCam.transform.rotation.eulerAngles, transformPoint.transform.rotation.eulerAngles, 0.01f);
        }
    }
Пример #2
0
    // Bob the arrows up and down for aesthetic
    private IEnumerator TweenArrow()
    {
        // Bobb Backwards
        while (bobbing)
        {
            yield return(null);

            bool backPosReached  = EEMath.Approximately(transform.position, backPos, 0.01f);
            bool frontPosReached = EEMath.Approximately(transform.position, frontPos, 0.01f);

            if (backPosReached)
            {
                animatingForward = true;
            }

            if (frontPosReached)
            {
                animatingForward = false;
            }

            if (!animatingForward)
            {
                transform.position = Vector3.Lerp(transform.position, backPos, bobbingSpeed);
            }
            else
            {
                transform.position = Vector3.Lerp(transform.position, frontPos, bobbingSpeed);
            }
        }

        if (!bobbing)
        {
            transform.position = frontPos;
            yield break;
        }
    }
Пример #3
0
    public IEnumerator DoProcessMidAirState_Energy()
    {
        Debug.Log("Show transfer of Elastic Potential Energy to Kinetic Energy that Launches the Cannon Ball and Observe Forces while in air");

        if (failLaunch)
        {
            popup.Show();
            yield break;
        }

        CanProcessNextStep = false;
        sliderLockOut      = true; // Lockout physics config sliders

        midAirStep = MidAirStep.Launching;

        energyWidget.Show();

        // Show Kinetic Energy Info
        float currentKineticEnergy = 0;

        energyWidget.UpdateKineticEnergy(currentKineticEnergy);

        // Show Draining of Elastic Potential Energy To Kinetic Energy (Kinetic Energy will equal the Elastic Potential Energy)
        float kineticEnergy = Kinetic_Energy_At_Launch();
        float elasticEnergy = Kinetic_Energy_At_Launch();

        const float drainSpeed = 0.05f;

        // Animate the depletion of Elastic Potential Energy and Gain of Kinetic Energy prior to Launch
        while (!EEMath.Approximately(elasticEnergy, 0, 0.001f) || !EEMath.Approximately(currentKineticEnergy, kineticEnergy, 0.001f))
        {
            yield return(null);

            elasticEnergy = Mathf.Lerp(elasticEnergy, 0, drainSpeed);
            energyWidget.UpdateElasticEnergy(elasticEnergy);

            currentKineticEnergy = Mathf.Lerp(currentKineticEnergy, kineticEnergy, drainSpeed);
            energyWidget.UpdateKineticEnergy(currentKineticEnergy);
        }

        yield return(StartCoroutine(DoLerpCameraToTransform(catapult.step3CamTransform)));

        float velocity = Velocity_At_Time_Of_Launch();
        float ratio    = ratio_Of_DEPE_Over_DGPE();

        catapult.ThrowBall(catapult.launchVector.up, velocity);

        while (!cannonBall.paused)
        {
            yield return(null);

            Transform riseMarker = Utils.CreateMarker(null, 0.1f, Color.red);
            riseMarker.position = cannonBall.transform.position;
            markers.Add(riseMarker.gameObject);

            // Catch when the ball has left max arc height
            if (cannonBall.rigidBody.velocity.y < 0)
            {
                if (!cannonBall.maxHeightReached)
                {
                    cannonBall.maxHeightReached = true;

                    Transform peakMarker = Utils.CreateMarker(null, 0.2f, Color.blue);
                    peakMarker.position = cannonBall.prevPosition;
                    markers.Add(peakMarker.gameObject);

                    cannonBall.PauseInAir();
                    yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step4CamTransform)));
                }
            }
        }

        midAirStep  = MidAirStep.Maxheight;
        weightPoint = cannonBall.LeftWeightPoint;
        resultPoint = catapult.springVector;

        ArrowIndicator maxHeightVelocityArrow = Instantiate(arrowBasicFrontPivotPrefab).GetComponent <ArrowIndicator>();

        maxHeightVelocityArrow.SetArrowTransform(cannonBall.PeakHightVelocityMarker);
        maxHeightVelocityArrow.bobbing = true;
        maxHeightVelocityArrow.Show();
        vectorArrows.Add(maxHeightVelocityArrow);
        maxHeightVelocityArrow.infoWidget.UpdateInfoWidget(null, (float)Math.Round(cannonBall.currentVelocity.magnitude, 2), "Velocity", true);

        energyWidget.AttachToTransform(cannonBall.EnergyWidgetMarker_LeftOffset);
        energyWidget.infoWidget.ToggleElasticInfo(false);
        energyWidget.infoWidget.ToggleKineticInfo(false);
        energyWidget.UpdateGravEnergy(cannonBall.Gravitational_Potential_Energy());

        CanProcessNextStep = true;
        yield return(new WaitUntil(() => { return (midAirStep == MidAirStep.SlopeDown); }));  // wait for camera to complete lerping to Ball mid air position

        CanProcessNextStep = false;

        HideGizmoAndWidgets();  // Hide all force arrows in preperation for next view step
        distanceGizmo.Show();

        yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step5CamTransform)));

        cannonBall.Resume();

        // Cannonball falling downward
        while (!cannonBall.paused)
        {
            Transform downMarker = Utils.CreateMarker(null, 0.1f, Color.green);
            downMarker.position = cannonBall.transform.position;
            markers.Add(downMarker.gameObject);

            yield return(null);

            RaycastHit hitResult;
            if (RaycastFromPos(cannonBall.transform.position, -cannonBall.transform.up, Mathf.Infinity, out hitResult, true))
            {
                float diff = cannonBall.transform.position.y - hitResult.point.y;
                if (diff < DistanceFromGround_At_TimeOFLaunch)
                {
                    cannonBall.PauseInAir();
                }
            }
        }

        yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step4CamTransform)));

        ArrowIndicator slopeDownVelocityArrow = Instantiate(arrowBasicBackPivotPrefab).GetComponent <ArrowIndicator>();

        slopeDownVelocityArrow.bobbing = true;
        vectorArrows.Add(slopeDownVelocityArrow);
        OrientTransformToVector(ref cannonBall.ResultPoint_Centered, cannonBall.currentVelocity);
        slopeDownVelocityArrow.SetArrowTransform(cannonBall.ResultPoint_Centered);
        slopeDownVelocityArrow.Show();
        slopeDownVelocityArrow.infoWidget.UpdateInfoWidget(null, (float)Math.Round(cannonBall.currentVelocity.magnitude, 2), "Velocity", true);

        energyWidget.AttachToTransform(cannonBall.EnergyWidgetMarker_RightOffset);
        energyWidget.infoWidget.ToggleElasticInfo(false);
        energyWidget.UpdateGravEnergy(cannonBall.Gravitational_Potential_Energy());
        energyWidget.Show();

        CanProcessNextStep = true;
        yield return(new WaitUntil(() => { return (midAirStep == MidAirStep.CompleteArc); }));  // wait for camera to complete lerping to Ball mid air position

        CanProcessNextStep = false;

        HideGizmoAndWidgets();
        distanceGizmo.Show();

        yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step5CamTransform))); // RE focus camera

        cannonBall.Resume();                                                                 // resume cannon ball trajectory

        // Draw the rest of the arc markers
        while (!cannonBall.paused && cannonBall.inAir)
        {
            Transform downMarker = Utils.CreateMarker(null, 0.1f, Color.green);
            downMarker.position = cannonBall.transform.position;
            markers.Add(downMarker.gameObject);
            yield return(null);
        }

        cannonBall.transform.rotation = Quaternion.identity;
        midAirStep = MidAirStep.Undefined;  // Mid air step is over;

        // Freeze the ball's movement and rotation as soon as it makes contact with the ground;
        cannonBall.transform.rotation    = Quaternion.identity;
        cannonBall.rigidBody.constraints = RigidbodyConstraints.FreezeAll;

        // Cheat to make Cannonball touch ground
        cannonBall.transform.position = new Vector3(cannonBall.transform.position.x, 0.425f, cannonBall.transform.position.z);
        yield return(StartCoroutine(DoProcessStoppedState_Energy()));

        activeCoroutine = null;
    }
Пример #4
0
    public IEnumerator DoProcessTimeOfLaunch_Forces()
    {
        CanProcessNextStep = false;

        yield return(StartCoroutine(DoLerpCameraToTransform(catapult.step2CamTransform)));

        Debug.Log("Observe Forces of Gravity, Spring and Resultant Force at time of launch");

        catapult.rope.gameObject.SetActive(false);

        catapult.launchAngle = catapult.DEFAULT_LAUNCH_ANGLE;

        tensionPoint = cannonBall.TensionPoint;
        weightPoint  = cannonBall.LeftWeightPoint;
        springPoint  = cannonBall.SpringPoint_LeftOffset;
        resultPoint  = cannonBall.ResultPoint_RightOffset;

        resultArrow.SetArrowTransform(resultPoint);
        resultArrow.Show();

        ResultForce = 0;

        /** Smooth lerp the draining of tensions and adding of resultant force over constant time **/
        const float drainSpeed     = 0.035f;
        float       initialTension = TensionForce;
        float       lerpTension    = TensionForce;

        while (!EEMath.Approximately(lerpTension, 0, 0.1f))
        {
            lerpTension  = Mathf.Lerp(lerpTension, 0, drainSpeed);
            TensionForce = lerpTension;
            ResultForce  = initialTension - TensionForce;
            yield return(null);
        }

        yield return(new WaitForSeconds(2)); // Wait to process the changes of forces that was animated

        if (ResultForce <= 0)
        {
            popup.Show();
            yield break;
        }

        tensionArrow.Hide();
        springArrow.SetArrowTransform(catapult.springVector);
        gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint);
        springArrow.bobbing  = gravArrow.bobbing = resultArrow.bobbing = false;
        catapult.throwCalled = true;

        failLaunch = false;
        float ratio = ratio_Of_DEPE_Over_DGPE();

        if (ratio <= 1f)
        {
            catapult.launchAngle *= ratio;
            failLaunch            = true;
        }

        while (catapult.throwCalled)
        {
            yield return(null);

            // Set all arrow starting positions
            Vector3 SVector = CalculateSVector();

            springArrow.SetArrowTransform(catapult.springVector);
            catapult.springVector.transform.up = SVector.normalized;

            // Update Spring Force using KForce
            SpringK = springK;

            CalculateResultVector();    // calculate the result vector, as Spring

            resultArrow.SetArrowTransform(catapult.resultVector);
            gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint);
        }

        springArrow.bobbing = gravArrow.bobbing = resultArrow.bobbing = true;
        springArrow.Show();
        gravArrow.Show();
        resultArrow.Show();

        CanProcessNextStep = true;

        activeCoroutine = null;
    }