예제 #1
0
    // Update is called once per frame
    void Update()
    {
        timerToWork += Time.deltaTime;

        if (timerToWork >= 1 && state == States.Available)
        {
            if (isThereUncultivated())
            {
                state = States.NeedsCultivating;
            }
            else if (isThereUncut() && rm.food + 10 < rm.storage)
            {
                state = States.NeedsCutting;
            }
            else if (isThereUnplanted())
            {
                state = States.NeedsPlanting;
            }

            timerToWork = 0;
        }

        switch (state)
        {
        case States.NeedsCultivating:
            currentField = getUncultivatedFields()[0];
            if (currentField == null)
            {
                state = States.CommingBack;
            }
            else
            {
                unit.MoveTo(currentField.transform.position);
                state = States.GoingToCultivate;
            }
            break;

        case States.NeedsPlanting:
            currentField = getUnplantedFields()[0];
            if (currentField == null)
            {
                state = States.CommingBack;
                unit.MoveTo(fsc.InitialPosition.transform.position);
            }
            else
            {
                unit.MoveTo(currentField.transform.position);
                state = States.GoingToPlant;
            }
            break;

        case States.GoingToPlant:
            if (ArrivedAtTarget())
            {
                state = States.Planting;
            }
            break;

        case States.Planting:
            if (currentWorkingTime < plantingTime)
            {
                currentWorkingTime += Time.deltaTime;
            }
            else
            {
                currentField.SetPlanted();
                unit.MoveTo(fsc.InitialPosition.transform.position);
                state = States.CommingBack;
                currentWorkingTime = 0;
            }
            break;

        case States.GoingToCultivate:
            if (ArrivedAtTarget())
            {
                state = States.Cultivating;
            }
            break;

        case States.Cultivating:
            if (currentWorkingTime < cultivatingTime)
            {
                currentWorkingTime += Time.deltaTime;
            }
            else
            {
                currentField.SetCultivated();
                unit.MoveTo(fsc.InitialPosition.transform.position);
                state = States.CommingBack;
                currentWorkingTime = 0;
            }
            break;

        case States.CommingBack:
            unit.MoveTo(fsc.InitialPosition.transform.position);

            if (ArrivedAtTarget())
            {
                state = States.Available;
                rm.IncreaseResources(ResourceManager.Resources.Food, carryingFood);
                carryingFood = 0;
            }
            break;

        case States.NeedsCutting:
            currentField = getUncutFields()[0];
            if (currentField == null)
            {
                state = States.CommingBack;
                unit.MoveTo(fsc.InitialPosition.transform.position);
            }
            else
            {
                unit.MoveTo(currentField.transform.position);
                state = States.GoingToCut;
            }
            break;

        case States.GoingToCut:
            if (ArrivedAtTarget())
            {
                state = States.Cutting;
            }
            break;

        case States.Cutting:
            if (currentWorkingTime < cuttingTime)
            {
                currentWorkingTime += Time.deltaTime;
            }
            else
            {
                currentField.SetCut();
                unit.MoveTo(fsc.InitialPosition.transform.position);
                state = States.CommingBack;
                currentWorkingTime = 0;
                carryingFood      += currentField.foodYield;
            }
            break;

        default:

            break;
        }
    }