Exemplo n.º 1
0
 public void AddBuildingWaitingForConstruction(BuildingInConstruction building)
 {
     buildingsToConstruct.Add(building);
 }
Exemplo n.º 2
0
 public void RemoveBuildingWaitingForConstruction(BuildingInConstruction building)
 {
     buildingsToConstruct.Remove(building);
 }
Exemplo n.º 3
0
    protected override void Update()
    {
        switch (state)
        {
        case WorkerState.Idle:

            if (Time.time > nextIdleMovementTime)
            {
                nextIdleMovementTime = Time.time + idleMovementInterval;
                Vector3 wanderPoint = UnityEngine.Random.insideUnitSphere * 4;
                wanderPoint.y = entity.transform.position.y;

                wanderPoint += entity.transform.forward * 4 + entity.transform.position;

                Vector3 basePosition = BuildingSystem.Instance.playersBaseLocation.position;
                //if he would stray off to far, bring him back to base
                if (Vector3.Distance(basePosition, wanderPoint) > maxDistanceToBaseForWander)
                {
                    wanderPoint += (basePosition - wanderPoint) / 4;
                }

                movement.MoveTo(wanderPoint);
            }

            break;

        case WorkerState.Construction:

            switch (constructionState)
            {
            case ConstructionState.Idle:

                //if idle /first get nearest Building  which needs construction, if there is no we just chill beside the base
                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    if (BuildingSystem.Instance.AreThereBuildingsToConstruct())
                    {
                        standingBesideBaseAndWaiting = false;

                        BuildingInConstruction nearestBuildingToConstruct = null;
                        float nearestDistance = Mathf.Infinity;

                        foreach (BuildingInConstruction building in BuildingSystem.Instance.GetBuildingsWaitingForConstruction())
                        {
                            float currentDistance = (building.transform.position - entity.transform.position).sqrMagnitude;

                            if (currentDistance < nearestDistance)
                            {
                                nearestDistance            = currentDistance;
                                nearestBuildingToConstruct = building;
                            }
                        }

                        assignedBuildingToBuild = nearestBuildingToConstruct;
                        //we move to a position from where we can build, not to a position inside the building
                        Vector3 targetPosition = nearestBuildingToConstruct.transform.position + (entity.transform.position - nearestBuildingToConstruct.transform.position).normalized * nearestBuildingToConstruct.width / 2;
                        movement.MoveTo(targetPosition);
                        constructionState = ConstructionState.MovingToBuilding;
                    }
                    else if (!standingBesideBaseAndWaiting)
                    {
                        Vector3 positonToMoveTo = UnityEngine.Random.insideUnitSphere * 8;
                        positonToMoveTo += BuildingSystem.Instance.playersBaseLocation.position;
                        standingBesideBaseAndWaiting = true;
                        movement.MoveTo(positonToMoveTo);
                    }
                }

                break;

            case ConstructionState.MovingToBuilding:

                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    if (assignedBuildingToBuild != null)
                    {
                        if (Vector3.Distance(entity.transform.position, assignedBuildingToBuild.transform.position) < assignedBuildingToBuild.width + constructionRange)
                        {
                            movement.Stop();
                            constructionState = ConstructionState.Constructing;
                            constructionTool.StartAnimation();
                            nextConstructionTime = Time.time + constructionInterval;
                        }
                    }
                    else
                    {
                        constructionState = ConstructionState.Idle;
                    }
                }

                break;

            case ConstructionState.Constructing:

                if (Time.time > nextConstructionTime)
                {
                    //check if it istn completed yet
                    if (assignedBuildingToBuild != null)
                    {
                        nextConstructionTime = Time.time + constructionInterval;
                        assignedBuildingToBuild.Construct(constructionPoints);
                    }
                    else
                    {
                        constructionState = ConstructionState.Idle;
                        constructionTool.StopAnimation();
                    }
                }

                break;
            }



            break;

        case WorkerState.Harvesting:

            switch (harvestingState)
            {
            case HarvestingState.Idle:

                //check if there are some ressources in the area - should it check with physics check or get the nearest from the ressourcesmanager?
                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    HashSet <Ressource> ressources = RessourcesManager.Instance.GetRessources(assignedHarvesterType);

                    if (ressources.Count > 0)
                    {
                        standingBesideHarvesterAndWaiting = false;


                        Ressource nearestRessource = null;
                        float     nearestDistance  = Mathf.Infinity;

                        foreach (Ressource ressource in ressources)
                        {
                            float currentDistance = (ressource.transform.position - assignedHarvester.transform.position).sqrMagnitude;

                            if (currentDistance < nearestDistance)
                            {
                                nearestDistance  = currentDistance;
                                nearestRessource = ressource;
                            }
                        }

                        currentSelectedRessource = nearestRessource;
                        Vector3 targetPosition = currentSelectedRessource.transform.position + (entity.transform.position - currentSelectedRessource.transform.position).normalized * currentSelectedRessource.width / 2;
                        movement.MoveTo(targetPosition);
                        harvestingState = HarvestingState.MovingToRessource;
                    }
                    else if (!standingBesideHarvesterAndWaiting)
                    {
                        Vector3 positonToMoveTo = UnityEngine.Random.insideUnitSphere * 5;
                        positonToMoveTo += assignedHarvester.transform.position;
                        standingBesideHarvesterAndWaiting = true;
                        movement.MoveTo(positonToMoveTo);
                    }
                }

                break;

            case HarvestingState.MovingToRessource:

                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;


                    if (currentSelectedRessource != null)
                    {
                        if (Vector3.Distance(entity.transform.position, currentSelectedRessource.transform.position) < currentSelectedRessource.width)
                        {
                            movement.Stop();
                            harvestingState = HarvestingState.GatheringRessource;

                            //activate the tool
                            if (assignedHarvesterType == RessourceType.fer)
                            {
                                ferHarvestingTool.StartAnimation();
                            }
                            else if (assignedHarvesterType == RessourceType.mer)
                            {
                                merHarvestingTool.StartAnimation();
                            }

                            nextRessourceGatheringTime = Time.time + ressourceGatherInterval;
                        }
                    }
                    else
                    {
                        harvestingState = HarvestingState.Idle;
                        ferHarvestingTool.StopAnimation();
                        merHarvestingTool.StopAnimation();
                    }
                }

                break;

            case HarvestingState.GatheringRessource:

                if (Time.time > nextRessourceGatheringTime)
                {
                    //check if it istn completed yet
                    if (currentSelectedRessource != null)
                    {
                        nextRessourceGatheringTime = Time.time + ressourceGatherInterval;
                        //gather but check how much will fit
                        if (ressourceInventory.currentRessourceTransportLoad + ressourceGatheringPower > ressourceInventory.ressourceTransportLoad)
                        {
                            ressourceInventory.AddRessource(currentSelectedRessource.type, currentSelectedRessource.TakeRessource(ressourceInventory.ressourceTransportLoad - ressourceInventory.currentRessourceTransportLoad));
                        }
                        else
                        {
                            ressourceInventory.AddRessource(currentSelectedRessource.type, currentSelectedRessource.TakeRessource(ressourceGatheringPower));
                        }

                        //if the sack is full, go back
                        if (ressourceInventory.currentRessourceTransportLoad == ressourceInventory.ressourceTransportLoad)
                        {
                            Vector3 targetPosition = assignedHarvester.transform.position + (entity.transform.position - assignedHarvester.transform.position).normalized * assignedHarvester.width / 2;
                            movement.MoveTo(targetPosition);
                            // movement.MoveTo(assignedHarvester.transform.position);
                            harvestingState = HarvestingState.TransportingRessourceToHarvester;
                            ferHarvestingTool.StopAnimation();
                            merHarvestingTool.StopAnimation();
                        }
                    }
                    else
                    {
                        ferHarvestingTool.StopAnimation();
                        merHarvestingTool.StopAnimation();

                        if (ressourceInventory.currentRessourceTransportLoad > 0)
                        {
                            Vector3 targetPosition = assignedHarvester.transform.position + (entity.transform.position - assignedHarvester.transform.position).normalized * assignedHarvester.width / 2;
                            movement.MoveTo(targetPosition);
                            //movement.MoveTo(assignedHarvester.transform.position);
                            harvestingState = HarvestingState.TransportingRessourceToHarvester;
                        }
                        else
                        {
                            harvestingState = HarvestingState.Idle;
                        }
                    }
                }

                break;

            case HarvestingState.TransportingRessourceToHarvester:

                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    if (Vector3.Distance(entity.transform.position, assignedHarvester.transform.position) < assignedHarvester.width)
                    {
                        movement.Stop();
                        assignedHarvester.DepotRessource(ressourceInventory.currentRessourceTransportLoad);
                        ressourceInventory.Clear();

                        if (currentSelectedRessource != null)
                        {
                            harvestingState = HarvestingState.MovingToRessource;
                            movement.MoveTo(currentSelectedRessource.transform.position);
                        }
                        else
                        {
                            harvestingState = HarvestingState.Idle;
                        }
                    }
                }
                break;
            }

            break;

        case WorkerState.Depositing:

            if (Time.time > nextScanTime)
            {
                nextScanTime = Time.time + scanInterval;

                if (despositionBuilding != null)
                {
                    if (Vector3.Distance(entity.transform.position, despositionBuilding.transform.position) < despositionBuilding.width)
                    {
                        despositionBuilding.GetComponent <IDepositioneable <B_Worker> >().DepositionWorker(this);
                    }
                }
                else
                {
                    AssignToIdle();
                }
            }


            break;
        }
    }