コード例 #1
0
    private void Update()
    {
        if (Keyboard.current[Key.F1].wasPressedThisFrame)
        {
            string path = Application.persistentDataPath;
            path += "/Screenshot_";
            path += System.DateTime.Now.Ticks;
            path += ".png";
            ScreenCapture.CaptureScreenshot(path);
        }

        if (isDead || menuUI.paused)
        {
            return;
        }

        if (transform.position.y < deathDepth)
        {
            DoDeath("Delved too deep");
            return;
        }

        if (Mathf.Abs(moveInput.x) > 0.1f)
        {
            spriteRenderer.sprite = moveSprite;
        }
        else
        {
            spriteRenderer.sprite = idleSprite;
        }

        float   curMoveSpeed = submerged ? WaterMoveSpeed : airMoveSpeed;
        Vector2 moveVec      = new Vector2(moveInput.x * curMoveSpeed, (submerged || onLadder) ? moveInput.y * curMoveSpeed : 0);

        controller2D.Move(moveVec, false, (!submerged && !onLadder && moveInput.y > 0.1f));

        bool    usingGamepad = lookInput.magnitude > 0.1f;
        Vector2 lookVec      = lookInput * DigRange;

        if (!usingGamepad)
        {
            Vector2 relativeMouse = mousePosition - transform.position;
            if (relativeMouse.magnitude > DigRange)
            {
                lookVec = relativeMouse.normalized * DigRange;
            }
            else
            {
                lookVec = relativeMouse;
            }

            trueCursor.transform.localPosition = relativeMouse;
        }
        else
        {
            trueCursor.transform.localPosition = lookVec;
        }

        //camControl.offset = lookVec;

        RaycastHit2D hit = Physics2D.Raycast(transform.position, lookVec, DigRange, 8);

        if (hit.collider == null)
        {
            tileIndicator.SetActive(false);
            //cursor.SetActive(true);
            cursor.transform.localPosition = lookVec;
        }
        else
        {
            cursor.transform.position = hit.point + (hit.normal * -0.1f);

            tileIndicator.SetActive(true);
            //cursor.SetActive(false);
            tileIndicator.transform.position = builder.SnapToTile(cursor.transform.position) + new Vector3(0.5f, 0.5f, 0f);
        }

        if (fireHeld)
        {
            Vector3Int currentAim = builder.SnapToGrid(cursor.transform.position);
            if (currentAim != currentlyDigging)
            {
                currentlyDigging   = currentAim;
                currentTileDigTime = 0;
            }

            currentTileDigTime += Time.deltaTime;

            if (currentTileDigTime >= (1 / DigSpeed))
            {
                float fuelCost = builder.Dig(cursor.transform.position) == WorldBuilder.TileType.Sand ? 0.25f : 1.0f;
                drillCurFuel -= fuelCost;
                if (drillCurFuel < 0)
                {
                    drillCurFuel = 0;
                }

                fuelMeter.maxVal   = DrillMaxFuel;
                fuelMeter.Value    = drillCurFuel;
                currentTileDigTime = 0;
            }

            if (builder.TilePresent(cursor.transform.position))
            {
                if (!breakTileIndicator.activeSelf)
                {
                    breakTileIndicator.SetActive(true);
                    if (!drillSource.isPlaying)
                    {
                        if (drillCurFuel > 0)
                        {
                            drillSource.PlayOneShot(drillSounds[Random.Range(0, drillSounds.Length)]);
                        }
                        else
                        {
                            drillSource.PlayOneShot(sadDigSounds[Random.Range(0, sadDigSounds.Length)]);
                        }
                    }
                }

                Meter indicator = breakTileIndicator.GetComponent <Meter>();
                indicator.maxVal = (1 / DigSpeed);
                indicator.Value  = currentTileDigTime;
            }
            else
            {
                breakTileIndicator.SetActive(false);
            }
        }
        else
        {
            breakTileIndicator.SetActive(false);
            currentTileDigTime = 0;
        }


        if (headSubmerged)
        {
            curAir -= Time.deltaTime * airDecayRate;
        }
        else
        {
            curAir += Time.deltaTime * AirRecoveryRate;
        }
        curAir          = Mathf.Clamp(curAir, 0, AirCapacity);
        airMeter.maxVal = AirCapacity;
        airMeter.Value  = curAir;

        if (curAir == 0)
        {
            curHealth -= Time.deltaTime * healthDecayRate;

            if (Time.time - lastDamageSoundTime > damageSoundInterval)
            {
                lastDamageSoundTime = Time.time;
                bubbleSource.PlayOneShot(bubbleSounds[Random.Range(0, bubbleSounds.Length)]);
                oofSource.PlayOneShot(oofSounds[Random.Range(0, oofSounds.Length)]);
            }
        }
        else
        {
            curHealth += Time.deltaTime * healthRecoveryRate;
        }
        curHealth = Mathf.Clamp(curHealth, 0, healthAmount);

        if (curHealth == 0)
        {
            DoDeath("You ran out of air");
            GetComponent <Inventory>()?.DropAll();
        }

        healthMeter.Value = curHealth;
    }