Пример #1
0
    private void FixedUpdate()
    {
        // no world input in shop
        if (gctl.ShopOpen || gctl.Victory || gctl.Paused)
        {
            return;
        }

        // explosion effect and stuff
        if (gctl.GameOver)
        {
            exhaust.Stop();

            if (!didDeathAnimation)
            {
                didDeathAnimation = true;
                ScreenShaker.SetShake(0.8f, 0.1f);
                Instantiate(explosionParticles, transform.position, Quaternion.identity);
                gameObject.SetActive(false);
            }

            return;
        }

#if UNITY_EDITOR
        if (Input.GetKeyDown("f6"))
        {
            Money += 100000;
        }
        if (Input.GetKeyDown("f7"))
        {
            Fuel = 1f;
        }
        if (Input.GetKeyDown("f8"))
        {
            Fuel = FuelMax;
        }
#endif

        // animate model rotation
        //Debug.Log(facing);
        if (facing)
        {
            currentAngle += 500f * Time.fixedDeltaTime;
            if (currentAngle > 90f)
            {
                currentAngle = 90f;
            }
        }
        else
        {
            currentAngle -= 500f * Time.fixedDeltaTime;
            if (currentAngle < -90f)
            {
                currentAngle = -90f;
            }
        }
        cachedTf.localRotation = Quaternion.Euler(0f, currentAngle, 0f);

        if (digging)
        {
            // ignore input while digging, and animate position
            accelHor = 0f;
            accelVer = 0f;
            cachedRigid.isKinematic = true;
            cachedRigid.velocity    = Vector3.zero;
            digProgress            += Time.fixedDeltaTime * Tuning.PLAYER_DIG_SPEED;
            Fuel -= Time.fixedDeltaTime * Tuning.FUEL_DIGGING_RATE;

            SetExhaustStrength(1.0f);
            AnimateScale(drillDown, !digHorizontal);
            AnimateScale(drillForward, digHorizontal);

            ScreenShaker.SetShake(0.1f, 0.01f);

            if (digProgress > 0.5f && digTile != null)
            {
                digTile.GetComponent <Tile>().PickUp();
                Destroy(digTile);
                digTile = null;
            }

            if (digProgress >= 1.0f)
            {
                cachedTf.position       = digTarget;
                cachedRigid.isKinematic = false;
                digging = false;
            }
            else
            {
                cachedTf.position = Vector3.Lerp(digStart, digTarget, digProgress) + (digHorizontal
                    ? Vector3.up * Random.Range(-0.025f, 0.025f)
                    : Vector3.right * Random.Range(-0.045f, 0.045f));
            }

            return;
        }

        // standby fuel consumption
        SetExhaustStrength(0.25f);
        Fuel -= Time.fixedDeltaTime * Tuning.FUEL_IDLE_RATE;

        {
            // update flag whether user is using controller
            float ctlKey = Mathf.Abs(Input.GetAxis("KeyX")) + Mathf.Abs(Input.GetAxis("KeyY"));
            float ctlJoy = Mathf.Abs(Input.GetAxis("JoyX")) + Mathf.Abs(Input.GetAxis("JoyY"));
            if (ctlJoy > 0.1f || ctlKey > 0.1f)
            {
                gctl.ControllerMode = ctlJoy > ctlKey;
            }
            //Debug.Log(gctl.ControllerMode);
        }

        float hor = Input.GetAxis("Horizontal");
        float ver = Input.GetAxis("Vertical");

        if (Input.GetButtonDown("Flashlight"))
        {
            Flashlight = !Flashlight;
        }
        if (Flashlight)
        {
            Fuel -= Tuning.FUEL_FLASHLIGHT_RATE * Time.fixedDeltaTime;
            SetExhaustStrength(1f);
        }

        // player position
        int playerMapX, playerMapY;
        GameController.WorldToMap(cachedTf.position, out playerMapX, out playerMapY);
        bool grounded = gctl.GetTileAt(playerMapX, playerMapY + 1) != null;

        // flashlight tutorial when turning it on or sufficiently deep
        if (!flashlightTutorialShown && (Flashlight || playerMapY >= 24))
        {
            flashlightTutorialShown = true;
            TutorialManager.QueueTutorial(2);
        }

        // accelerate / decelerate
        accelHor = Mathf.Abs(hor) > 0.001f
            ? Mathf.Min(accelHor + Time.fixedDeltaTime * Tuning.PLAYER_ACCEL, 1.0f)
            : Mathf.Max(accelHor - Time.fixedDeltaTime * Tuning.PLAYER_DECEL, 0.0f);
        accelVer = Mathf.Abs(ver) > 0.001f
            ? Mathf.Min(accelVer + Time.fixedDeltaTime * Tuning.PLAYER_ACCEL, 1.0f)
            : Mathf.Max(accelVer - Time.fixedDeltaTime * Tuning.PLAYER_DECEL, 0.0f);

        // interact with fuel depot
        if (playerMapY == -1 && playerMapX >= 9 && playerMapX <= 14)
        {
            refuelText.SetActive(true);
            refuelText.GetComponent <TextMesh>().text = gctl.ControllerMode
                ? "Press A to open shop"
                : "Press Enter to open shop";

            if (Input.GetButtonDown("Confirm"))
            {
                //Debug.Log("Do fancy thing");
                //Fuel = FuelMax;
                Input.ResetInputAxes();
                refuelText.SetActive(false);
                gctl.SetShopOpen(true);
            }
        }
        else
        {
            refuelText.SetActive(false);
        }

        // horizontal movement
        Vector3 movedir = lastMoveDir;
        if (Mathf.Abs(hor) > 0.001f)
        {
            // set horizontal speed
            movedir.x = hor * Tuning.PLAYER_MOVE_SPEED;
            Fuel     -= Time.fixedDeltaTime * Tuning.FUEL_DRIVING_RATE;
            SetExhaustStrength(0.75f);

            // set model rotation
            facing = hor < 0f; // left/right

            // try digging maybe
            if (Mathf.Abs(hor) >= 0.85f &&                                                        // deadzone
                grounded && Mathf.Abs(cachedRigid.velocity.y) < 0.1f && cachedRigid.useGravity && // can't dig sideways while flying
                gctl.GetTileAt(playerMapX + Mathf.RoundToInt(hor), playerMapY) != null)           // must be a solid tile in the way

            {
                StartDigging(playerMapX + Mathf.RoundToInt(hor), playerMapY, true);
            }
        }

        AnimateScale(drillDown, false);
        AnimateScale(drillForward, grounded && Mathf.Abs(hor) > 0.1f);

        // vertical movement
        //Debug.Log(ver);
        if (ver > 0.5f)
        {
            movedir.y = ver * Tuning.PLAYER_FLY_SPEED;
            Fuel     -= Time.fixedDeltaTime * Tuning.FUEL_FLYING_RATE;
            SetExhaustStrength(0.75f);
            AnimateScale(propellor, true, 2.5f);
            AnimateScale(drillDown, false);
            AnimateScale(drillForward, false);

            // cancel out gravity
            cachedRigid.useGravity = false;
            Vector3 vel = cachedRigid.velocity;
            vel.y = Mathf.Min(0.0f, vel.y + 4.0f * Time.fixedDeltaTime);
            cachedRigid.velocity = vel;
        }
        else
        {
            // was flying, need to accelerate again
            //if (!cachedRigid.useGravity) {
            //    cachedRigid.velocity = accelVer * Tuning.
            //}
            movedir.y = 0;
            cachedRigid.useGravity = true;
            AnimateScale(propellor, !grounded, 2.5f);

            if (grounded && ver < -0.5f &&                                            // dead zone
                Mathf.Abs(cachedRigid.velocity.y) < 0.1f && cachedRigid.useGravity && // can't dig while flying
                gctl.GetTileAt(playerMapX, playerMapY + 1) != null)                   // must be a solid tile
            {
                // downwards
                StartDigging(playerMapX, playerMapY + 1, false);
            }
        }

        // if moving, save the last speed
        if (Mathf.Abs(hor) > 0.001f || Mathf.Abs(ver) > 0.001f)
        {
            lastMoveDir = movedir;
        }

        // apply velocity
        Vector3 finalMoveDir = movedir;
        finalMoveDir.x *= accelHor;
        finalMoveDir.y *= accelVer;
        Vector3 newpos = cachedRigid.position + finalMoveDir * Time.fixedDeltaTime;
        newpos.x             = Mathf.Clamp(newpos.x, -(Tuning.MAP_WIDTH / 2), Tuning.MAP_WIDTH / 2.0f - 1.0f);
        cachedRigid.position = newpos;
    }