Exemplo n.º 1
0
    void FillDirtPatch(int centerX, int centerY)
    {
        int patchWidth  = Random.Range(3, 8);
        int patchHeight = Random.Range(3, 7);

        int startX = centerX - Mathf.RoundToInt(patchWidth * 0.5f);
        int startY = centerY - Mathf.RoundToInt(patchHeight * 0.5f);
        int lastX  = centerX + Mathf.RoundToInt(patchWidth * 0.5f);
        int lastY  = centerY + Mathf.RoundToInt(patchHeight * 0.5f);

        DirtPatch newPatch = new DirtPatch(centerX, centerY, patchWidth, patchHeight);

        if (dirt_patches.Count >= 1)
        {
            // Make sure we are at a safe distance from the last patch
            Dimensions lastPatchDimensions = dirt_patches[dirt_patches.Count - 1].dimensions;
            if (startX >= lastPatchDimensions.startX && lastX <= lastPatchDimensions.endX)
            {
                return;
            }
        }
        dirt_patches.Add(newPatch);

        for (int x = startX; x <= lastX; x++)
        {
            for (int y = startY; y <= lastY; y++)
            {
                Tile tile = active_area.tileGrid[x, y];
                if (tile == null)
                {
                    continue;
                }
                if (tile.tileType != TileType.Grass && tile.tileType != TileType.Dirt)
                {
                    continue;
                }
                if (x > startX && x < lastX && y > startY && y < lastY)
                {
                    tile.SetAs(TileType.Dirt);
                    // Chance of ORE:
                    FillOre(tile);
                }
                else
                {
                    tile.SetAs(TileType.Grass_Dirt);
                }
            }
        }
    }
    private void Update()
    {
        if (toolUsageCooldownTimer > 0f)
        {
            toolUsageCooldownTimer -= Time.deltaTime;
        }

        bool useItemPressed        = Input.GetButton("Fire1");
        bool useItemSpecialPressed = Input.GetButton("SpecialAttack");
        bool waterButtonPressed    = Input.GetButton("Fire2");

        int indexChange = 0;

        indexChange  = Mathf.RoundToInt(10f * Input.GetAxis("Mouse ScrollWheel"));
        indexChange += Input.GetButtonDown("NavLeft")? -1 : 0;
        indexChange += Input.GetButtonDown("NavRight") ? 1 : 0;

        if (indexChange != 0)
        {
            SetSelectedItemIndex(selectedItemIndex += indexChange);
        }

        if (useItemPressed)
        {
            InventorySlot currentItem = inventorySlots[selectedItemIndex];

            // Right now all we can do is use items, so nothing left to do.
            if (currentItem.IsEmpty())
            {
                return;
            }

            // Check to see if we have a variety of things and if we can use them
            Growable  plantableSeed = currentItem.GetGamePrefab().GetComponent <Growable>();
            DirtPatch dirtPatch     = currentItem.GetGamePrefab().GetComponent <DirtPatch>();

            PlantableZone plantableZone = player.GetAvailablePlantableZone();

            // Planting a seed
            if (plantableSeed != null)
            {
                ItemSeed seed = (ItemSeed)currentItem.GetItem();
                if (plantableZone != null && !plantableZone.IsPlanted())
                {
                    plantableZone.PlantSeed(seed);
                    currentItem.Use();
                }
            }
            // Placing dirt on the ground
            else if (dirtPatch != null)
            {
                if (plantableZone == null && player.OnPlantableGround())
                {
                    GameObject dirt = Instantiate(dirtPatch.gameObject);

                    // Assign the dirt patch to be parented to whatever the player is standing on
                    // This allows us to recursively destroy plants after we plant on top of them
                    // (e.g. dirt pile on a leaf platform. Destroy bottom plant, it destroys the rest)
                    Transform parent = player.GetObjectBelow().transform;
                    dirt.transform.parent = parent;

                    // Place this dirt roughly on the ground
                    dirt.transform.position = player.transform.position + Vector3.down * 0.5f;
                    currentItem.Use();
                }
            }
            else
            {
                currentItem.Use();
            }
        }

        if (useItemSpecialPressed)
        {
            InventorySlot currentItem = inventorySlots[selectedItemIndex];

            // Make sure we have something equipped.
            if (currentItem.IsEmpty())
            {
                return;
            }

            currentItem.UseSpecial();
        }

        if (waterButtonPressed)
        {
            PlantableZone plantableZone = player.GetAvailablePlantableZone();
            if (plantableZone != null && plantableZone.CanBeWatered())
            {
                GameObject target = (plantableZone as MonoBehaviour).gameObject;
                if (waterLevel > 0)
                {
                    if (!waterSprite.PlanningToVisit(target))
                    {
                        // Everything that we have that implements interfaces is also a MonoBehavior, so we can
                        // use this as a """safe""" cast in order to find the game object
                        // The water sprite reaching the PlantableZone will handle the watering itself.
                        waterSprite.AddImmediateToTargetList((plantableZone as MonoBehaviour).gameObject);

                        // TODO: Consider implications of this call. It means we can't possibly overwater, but it
                        // also changes the watersprite visual before it actually reaches the PlantableZone
                        ChangeWaterLevel(-1);
                    }
                    else
                    {
                        Debug.LogError("D:");
                    }
                }
                else
                {
                    // lol you've got no water, nerd
                }
            }
        }


        // Quick and dirty keypress check for inventory slots
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            SetSelectedItemIndex(0);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            SetSelectedItemIndex(1);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            SetSelectedItemIndex(2);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            SetSelectedItemIndex(3);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            SetSelectedItemIndex(4);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha6))
        {
            SetSelectedItemIndex(5);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha7))
        {
            SetSelectedItemIndex(6);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha8))
        {
            SetSelectedItemIndex(7);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha9))
        {
            SetSelectedItemIndex(8);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha0))
        {
            SetSelectedItemIndex(9);
        }
        else if (Input.GetKeyDown(KeyCode.Minus))
        {
            SetSelectedItemIndex(10);
        }
        else if (Input.GetKeyDown(KeyCode.Equals))
        {
            SetSelectedItemIndex(11);
        }
    }