Exemplo n.º 1
0
    void FixedUpdate()
    {
        // I think I will change the Update loop to call single methods such as:
        // LegacyControl();
        // AdvancedControl();
        // RotateTaxi();
        // UprightTaxi();
        // UpdateUI();

        /*
         * // THIS WHOLE SECTION GOES!
         * // This is temporary, using the left trigger to end UI panel. Eventually I will move that to one of the four lettered buttons, on buttontrigger or something
         * turboTrigger = Input.GetAxis(turboButton);
         * // For now, to test, take UI down when you hit the left trigger. This goes away soon.
         * if (turboTrigger > 0.01f)
         * {
         *  Debug.Log("<color=green>****</color> Pulling UI down with Turbo Trigger.");
         *  gm.UiDown();
         * }
         *
         */



        // I guess first, I should check to see if I've crashed, and am dead on the ground somewhere

        // First, have I gone through the ground? This is the extreme case where you fall through or hit the ground. Dead.

        /*
         * if (transform.position.y < 0.0f)
         * {
         *  Debug.Log("<color=red> YOU HAVE BUST THROUGH THE GROUND!</color>");
         *  gm.RestartShift();
         * }
         */


        // Have I crashed and stopped somewhere above ground?
        // IS THIS MY PROBLEM? That I'm doing this for normal crashing, that is, crashing and stopping above-ground
        // but also if you just happen to drop below city ground level (abandonment)
        // Perhaps it was that the ground was wet to 0. Perhaps I need to lower that ground level for simple abandomnent
        if ((isCrashing && rb.velocity == Vector3.zero))
        {
            gm.RestartShift();
        }

        if (transform.position.y < 10.0f)
        {
            gm.cash += gm.crashDeductible / 2.0f;
            gm.RestartShift();
        }



        //
        if (Input.GetAxis("Fire2") >= 0.1f)
        {
            Debug.Log("<color=green>****</color> Pulling UI down with B button.");
            gm.PullUiDown();
        }



        if (gm.uiIsUp)
        {
            isCrashing = false;

            // If the UI is up, don't do anything in the update loop. This should only happen when you're parked at home at the beginning of a shift
            return;
        }

        else
        {
            // Accurate thrust control:
            // - Joystick forward/backward adds a positive or negative force in only the forward direction
            // - Joystick left/right adds and subtracts torque
            // - A button is upward thrust, same as legacy control
            // - B button is downward thrust, in emergencies.

            // Get forwart thrust only
            // !!!! PROBLEM! This really does do only FORWARD thrust, as in not aimed in the direction of the torque
            // SO I have to figure out the torque angle of the object, and perhaps it's simply the Y rotation +90 (since default is -90)
            // And apply thrust in that direction. Probably requires trig to get the X and Y thrust based on angle (ArcTan?)
            //float thrust = Input.GetAxis("Vertical");

            // ACTUALLY, I have to instead get the angle of the joystick which I used to use to point the car in legacy control,
            // and sunglasses on Boxxy, and apply thrust in that direction instead.
            //
            //!!!!!! Something is wrong. It is not taking the angle as a 0-360 I think.
            // I think rb.transform.localRotation is the transform rotation of the rigidbody component specifically where I want
            // the transform of the game object, not the ridigbody attached

            angle = rb.transform.eulerAngles.y - 90.0f; // The 90.0f is added because 0 degrees on a trig circle points left, not
            // forward. Yet my car points forward at 0

            // For this scheme we only want forward/backward thrust in the direction of the vehicle
            // Direction of the vehicle is controlled below with torque.



            if (hasControl)
            {
                moveForward = Input.GetAxis(forwardJoy);
                if (moveForward > joyToleranceMin && moveForward < joyToleranceMax)
                {
                    moveForward = 0.0f;
                }



                movement = Quaternion.AngleAxis(angle + 90.0f,
                                                Vector3.up * moveForward * moveForward * thrustMultiplier * forwardThrustMult * turboAmount)
                           * Vector3.right * moveForward * thrustMultiplier * turboAmount;
                rb.AddForce(movement);

                UseGas(gasUseRateForwardThrust * Math.Abs(moveForward) * turboAmount);
                if (moveForward >= joyToleranceMax)
                {
                    FireEngines(enginesForward, Math.Abs(moveForward * forwardJetScale));
                    if (turboTrigger > 0.01f && hasTurbo) // IF it's being thrust forward AND Turbo is on, fire turbo visuals at scale 1.
                    {
                        FireEngines(enginesTurboFront, 0.5f);
                    }
                    else
                    {
                        StopEngines(enginesTurboFront);
                    }
                }
                if (moveForward <= joyToleranceMin)
                {
                    FireEngines(enginesBackward, Math.Abs(moveForward * backJetScale));
                    if (turboTrigger > 0.01f && hasTurbo) // IF it's being thrust forward AND Turbo is on, fire turbo visuals at scale 1.
                    {
                        FireEngines(enginesTurboRear, 0.5f);
                    }
                    else
                    {
                        StopEngines(enginesTurboRear);
                    }
                }
                if (moveForward > joyToleranceMin && moveForward < joyToleranceMax)
                {
                    StopEngines(enginesForward);
                    StopEngines(enginesBackward);
                    StopEngines(enginesTurboFront);
                    StopEngines(enginesTurboRear);
                }
            }



            if (hasControl)
            {
                turboTrigger = Input.GetAxis(turboButton);

                if (turboTrigger > 0.01f && hasTurbo)
                {
                    turboAmount = turboMultiplier;
                }
                else
                {
                    turboAmount = 1.0f;
                }
            }



            if (hasControl)
            {
                // Now get rotation
                turn = Input.GetAxis(turnJoy);

                if (turn > joyToleranceMin && turn < joyToleranceMax)
                {
                    turn = 0.0f;
                }

                rb.AddTorque(transform.up * torque * turn);
                rb.AddTorque(transform.right * turn * bank * -1.0f);

                UseGas(gasUseRateRotateThrust * Math.Abs(turn));
                if (turn >= joyToleranceMax)
                {
                    FireEngines(enginesRight, Math.Abs(turn * turnJetScale));
                }
                if (turn <= joyToleranceMin)
                {
                    FireEngines(enginesLeft, Math.Abs(turn * turnJetScale));
                }
                if (turn < joyToleranceMax && turn > joyToleranceMin)
                {
                    StopEngines(enginesRight);
                    StopEngines(enginesLeft);
                }
            }



            if (hasControl && hasStrafe)
            {
                moveSideways = Input.GetAxis(sideJoy);
                if (moveSideways > joyToleranceMin && moveSideways < joyToleranceMax)
                {
                    moveSideways = 0.0f;
                }

                movement = Quaternion.AngleAxis(angle + 180.0f,
                                                Vector3.up * Mathf.Abs(moveSideways))
                           * Vector3.right * moveSideways;
                rb.AddForce(movement * thrustMultiplier * sideThrustMult);

                UseGas(gasUseRateSideThrust * Math.Abs(moveSideways));
                if (moveSideways >= joyToleranceMax)
                {
                    FireEngines(enginesSideLeft, Math.Abs(moveSideways * sideJetScale));
                }
                if (moveSideways <= joyToleranceMin)
                {
                    FireEngines(enginesSideRight, Math.Abs(moveSideways * sideJetScale));
                }
                if (moveSideways > joyToleranceMin && moveSideways < joyToleranceMax)
                {
                    StopEngines(enginesSideLeft);
                    StopEngines(enginesSideRight);
                }
            }
        }


        //angleText.text = displayAngle.ToString("0.00");
        gasText.text = gas.ToString("0.00");
        // velocityText.text = rb.velocity.y.ToString("0.00"); // FOR NOW DON'T DO THIS, as I am using this text field to test collisionEffect

        /*
         * // Invert Control toggle
         * if (Input.GetAxis("Fire1") >= 0.1f)
         * {
         *  invertControl = false;
         *  invertControlString = "Flight Control";
         * }
         *
         *
         * // Invert Control toggle
         * if (Input.GetAxis("Jump") >= 0.1f)
         * {
         *  invertControl = true;
         *  invertControlString = "Inverted Control";
         * }
         */



        /*
         * // Switch which joystick side motion turns and which strafes
         * if (Input.GetAxis("Fire2") >= 0.1f)
         * {
         *
         *  turnJoy = "Horizontal";  // Default "Horizontal"
         *  sideJoy = "Mouse Y";     // Default "Mouse Y"
         *  Debug.Log("<color=blue>Fire2 - Strafe on Joy1. Turn on Joy2.</color>");
         *
         *
         * }
         *
         * // Switch which joystick side motion turns and which strafes
         * if (Input.GetAxis("Fire3") >= 0.1f)
         * {
         *
         *  turnJoy = "Mouse Y";  // Default "Horizontal"
         *  sideJoy = "Horizontal";     // Default "Mouse Y"
         *  Debug.Log("<color=green>Fire3 - Turn on Joy1. Strafe on Joy2.</color>");
         *
         *
         * }
         *
         */



        //Oh, and for now, I'm leaving in the A/Y buttons for up/down, but also adding this:
        invertControlText.text = invertControlString;



        // !!!!!!!!!!!!!!!!! NEXT
        //    GET THE JETS WORKING AS INTENDED! INVERSION FAILS THEM
        //
        // CLUE: Right now, the stick itself determines which engines fire, not the actual thrust direction as determined by "invertedControl".
        // RIGHT NOW, after putting in some debug text, it seems DOWN in Flight control think
        //

        upwardThrust = Input.GetAxis(verticalJoy);

        if (upwardThrust > joyToleranceMin && upwardThrust < joyToleranceMax)
        {
            upwardThrust = 0.0f;
        }

        // Thrust Upward. Unless Inverted Control. Then Thrust Downward
        if (upwardThrust >= joyToleranceMax && hasControl)
        {
            if (invertControl)
            {
                ThrustDown(Math.Abs(upwardThrust));
                FireEngines(enginesDown, Math.Abs(upwardThrust) * downJetScale);
            }
            else
            {
                ThrustUp(Math.Abs(upwardThrust));
                FireEngines(enginesUp, Math.Abs(upwardThrust) * upJetScale);
            }
        }


        // Thrust Downward. Unless Inverted Control. Then Thrust Upward
        if (upwardThrust <= joyToleranceMin && hasControl)
        {
            if (invertControl)
            {
                ThrustUp(Math.Abs(upwardThrust));
                FireEngines(enginesUp, Math.Abs(upwardThrust) * upJetScale);
            }
            else
            {
                ThrustDown(Math.Abs(upwardThrust));
                FireEngines(enginesDown, Math.Abs(upwardThrust) * downJetScale);
            }
        }



        // NO JOY. STOP ALL ENGINES
        if (upwardThrust <= joyToleranceMax && upwardThrust > joyToleranceMin)
        {
            StopEngines(enginesDown);
            StopEngines(enginesUp);
        }

        GasUp();
        UprightTaxi();

        // Display damage
        collisionText.text = damage.ToString();



        gm.hasControl = hasControl;


        if (!hasControl)
        {
            StopEngines(enginesForward);
            StopEngines(enginesBackward);
            StopEngines(enginesUp);
            StopEngines(enginesDown);
            StopEngines(enginesLeft);
            StopEngines(enginesRight);
            StopEngines(enginesSideLeft);
            StopEngines(enginesSideRight);
            StopEngines(enginesTurboFront);
            StopEngines(enginesTurboRear);
        }
    }