Ejemplo n.º 1
0
    private bool PlantAction(Inventory userInventory)
    {
        GridSelector gridSelector      = userInventory.GetComponent <GridSelector>();
        Vector3Int   selectionLocation = gridSelector.GetGridSelectionPosition();

        Vector3 loc = gridSelector.GetGridWorldSelectionPosition();

        RaycastHit2D[] findObjects = Physics2D.BoxCastAll(loc, gridManager.Grid.cellSize * 0.5f, 0, Vector2.zero, 50);

        // In case there is already a crop on this location, return.
        for (int i = 0; i < findObjects.Length; i++)
        {
            if (findObjects[i].transform.CompareTag("Crop"))
            {
                return(false);
            }
        }

        if (gridManager.HasDirtHole(selectionLocation))
        {
            GameObject gameObject = plantablePrefab.Retrieve <GameObject>();
            gameObject.transform.position = gridManager.GetWorldLocation(selectionLocation);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 2
0
    public override IEnumerator ItemUseAction(Inventory userInventory, int itemIndex)
    {
        GridSelector gridSelector = userInventory.GetComponent <GridSelector>();

        if (gridManager == null)
        {
            gridManager = gridManagerReference.Reference?.GetComponent <GridManager>();
        }

        if (gridManager != null)
        {
            if (weatherEvent.HasParameter)
            {
                currentWeather = weatherEvent.LastParameter;
            }
        }

        if (gridSelector != null && gridManager != null)
        {
            Vector3Int selectionLocation = gridSelector.GetGridSelectionPosition();

            userInventory.GetComponent <Aimer>().LookAt(gridManager.Grid.CellToWorld(selectionLocation));

            Mover getMover = userInventory.GetComponent <Mover>();
            getMover?.FreezeMovement(true);

            BodyAnimation[] getEntityAnimator = userInventory.GetComponentsInChildren <BodyAnimation>();

            float animationTime = 0;

            for (int i = 0; i < getEntityAnimator.Length; i++)
            {
                animationTime = getEntityAnimator[i].ApplySmashAnimation(speed, userInventory.GetItem(itemIndex).Data.Icon);
            }

            gridSelector.SetFrozen(true);

            yield return(new WaitForSeconds(animationTime * 0.5f));

            if (!gridManager.HasDirtHole(selectionLocation) && !gridManager.HasWater(selectionLocation) && gridManager.HasDirt(selectionLocation))
            {
                gridManager.SetDirtHoleTile(selectionLocation);

                if (currentWeather == EWeather.Rainy)
                {
                    gridManager.SetWateredDirtTile(selectionLocation);
                }

                onSuccess.Invoke();
            }

            yield return(new WaitForSeconds(animationTime * 0.5f));

            getMover.FreezeMovement(false);
            gridSelector.SetFrozen(false);
        }
    }
Ejemplo n.º 3
0
    public override IEnumerator ItemUseAction(Inventory userInventory, int itemIndex)
    {
        InventoryItem getInventoryItem = userInventory.GetItem(itemIndex);

        GridSelector gridSelector = userInventory.GetComponent <GridSelector>();

        if (gridManager == null)
        {
            gridManager = gridManagerReference.Reference?.GetComponent <GridManager>();
        }

        if (gridSelector != null && gridManager != null)
        {
            Vector3Int location = gridSelector.GetGridSelectionPosition();

            userInventory.GetComponent <Aimer>().LookAt(gridManager.Grid.CellToWorld(location));

            Mover getMover = userInventory.GetComponent <Mover>();
            getMover?.FreezeMovement(true);

            BodyAnimation[] getEntityAnimator = userInventory.GetComponentsInChildren <BodyAnimation>();

            float animationTime = 0;

            for (int i = 0; i < getEntityAnimator.Length; i++)
            {
                animationTime = getEntityAnimator[i].ApplyDropAnimation(speed, userInventory.GetItem(itemIndex).Data.Icon);
            }

            gridSelector.SetFrozen(true);

            yield return(new WaitForSeconds(animationTime * 0.5f));

            if (!gridManager.HasWater(location))
            {
                if (!gridManager.HasWateredDirt(location) && gridManager.HasDirtHole(location))
                {
                    ItemEnergy currentItemEnergy = getInventoryItem.Energy;

                    if (currentItemEnergy.current >= energyCost)
                    {
                        gridManager.SetWateredDirtTile(location);

                        float newEnergy = currentItemEnergy.current - energyCost;
                        if (newEnergy < energyCost)
                        {
                            newEnergy = 0;
                        }

                        getInventoryItem.Energy = new ItemEnergy()
                        {
                            min     = currentItemEnergy.min,
                            max     = currentItemEnergy.max,
                            current = newEnergy
                        };

                        // Also do a sphere cast to ensure that any Day Count Listener is no longer frozen
                        // It becomes frozen when there is no watered ground, meaning the plant growth will stagnate.

                        Vector3 loc = gridSelector.GetGridWorldSelectionPosition();

                        RaycastHit2D[] findObjects = Physics2D.BoxCastAll(loc, gridManager.Grid.cellSize * 0.5f, 0, Vector2.zero, 50);

                        for (int i = 0; i < findObjects.Length; i++)
                        {
                            if (findObjects[i].transform.CompareTag("Crop"))
                            {
                                findObjects[i].transform?.GetComponent <DayCountDownListener>()?.FreezeCountDown(false);
                            }
                        }

                        OnWateredGround.Invoke();

                        userInventory.ReloadItemSlot(itemIndex);
                    }
                }
            }
            else
            {
                ItemEnergy currentItemEnergy = getInventoryItem.Energy;

                getInventoryItem.Energy = new ItemEnergy()
                {
                    min     = currentItemEnergy.min,
                    max     = currentItemEnergy.max,
                    current = Mathf.Clamp(currentItemEnergy.current + waterEnergyRecovery, currentItemEnergy.min, currentItemEnergy.max)
                };

                OnObtainedWater.Invoke();

                userInventory.ReloadItemSlot(itemIndex);
            }

            yield return(new WaitForSeconds(animationTime * 0.5f));

            getMover.FreezeMovement(false);
            gridSelector.SetFrozen(false);
        }
    }