Beispiel #1
0
    public bool UseItem(Vector2 worldPos)
    {
        // if item is a seed it adds a tile based on the editor
        if (is_seed)
        {
            Vector3Int cellPos = WorldData.diggableLayer.WorldToCell(worldPos);//PlayerData.player.transform.position);
            if (actionTile == null)
            {
                Debug.Log("Error: actionTile is not set for given seed");
            }

            if ((WorldData.diggableLayer.GetTile(cellPos) == null) && (WorldData.plantableLayer.GetTile(cellPos) != null) && (WorldData.CheckPlantedLocation(cellPos)))
            {
                // Place item in middle of cell, track planted location
                Instantiate(actionPrefab, WorldData.plantableLayer.GetCellCenterWorld(cellPos), Quaternion.identity);
                WorldData.AddPlantedLocation(cellPos);

                return(true);
            }
        }

        // For items that require specific functions
        else
        {
            switch (itemName)
            {
            case "Shovel":
                // Shovel removes a tile off the top layer of the grid, tile should be flagged as diggable; for example, a shovel shouldn't be allowd to dig through concrete
                // This can be changed so that it adds a dirt tile on top instead, or it replaces a grass tile with a dirt one with relative ease

                Vector3Int tileCoordinate = WorldData.diggableLayer.WorldToCell(worldPos);    //PlayerData.player.transform.position);
                if (PlayerData.userArea.OverlapPoint(worldPos))
                {
                    if (WorldData.diggableLayer.GetTile(tileCoordinate) != null)
                    {
                        WorldData.diggableLayer.SetTile(tileCoordinate, null);
                        return(true);
                    }
                }

                break;
            }

            //Vegetables work differently
            if (itemName.Substring(0, 8) == "Sellable")
            {
                if (PlayerData.inBinRange)
                {
                    Sellable sellComp = GetComponent <Sellable>();
                    sellComp.SellPlant();
                    return(true);
                }
            }
        }

        return(false);
    }