Esempio n. 1
0
    public void Execute()
    {
        if (leavingBase || enterBase)
        {
            return;
        }

        switch (state)
        {
        case State.Idle:
            if (ant.GetAgent().isOnNavMesh)
            {
                ant.GetAgent().isStopped = true;
            }
            TargetResource();
            break;

        case State.Scouting:
            if (!preparingReturn)
            {
                preparingReturn = true;
                ant.StartCoroutine(ReturnToBase());
            }

            if (!scouting)
            {
                var tempTarget = GameWorld.Instance.FindNearestUnknownResource(ant.transform.position, ant.TeamID);
                if (tempTarget != null && Vector3.Distance(ant.transform.position, tempTarget.GetPosition()) < 2f)
                {
                    target = tempTarget;
                    ant.StartCoroutine(EnterBase(ant.GetBaseController().GetPosition()));
                    state = State.MovingToStorage;
                    ant.StartCoroutine(Discover());
                    preparingReturn = false;
                }
                else
                {
                    scouting = true;
                    ant.StartCoroutine(Scout());
                }
            }

            break;

        case State.MovingToResource:
            if (target != null)
            {
                if (Vector3.Distance(ant.transform.position, target.GetPosition()) < 1f)
                {
                    state = State.Gathering;
                }
                if (Vector3.Distance(ant.GetAgent().destination, target.GetPosition()) > 1f)
                {
                    ant.GetAgent().SetDestination(target.GetPosition());
                }
            }
            else
            {
                TargetResource();
            }
            break;

        case State.Gathering:
            if (target == null)
            {
                TargetResource();
                break;
            }
            if (!inventory.ContainsKey(target.resourceType))
            {
                inventory.Add(target.resourceType, 1);
            }
            else
            {
                inventory[target.resourceType]++;
            }
            carryResource(target);
            target.GrabResource();

            //calculate new speed
            var speedPower = (float)Math.Pow(0.75, carryingObjects.Count);
            ant.currentSpeed = ant.baseSpeed * speedPower;

            ant.UpdateSpeed();

            nextHarvest--;
            if (carryingObjects.Count >= carryWeight)
            {
                ant.StartCoroutine(EnterBase(ant.GetBaseController().GetPosition()));
                state = State.MovingToStorage;
            }
            else
            {
                if (nextHarvest > 0)
                {
                    break;
                }
                target = null;
                TargetResource();
            }

            break;

        case State.MovingToStorage:
            if (ant.GetBaseController()?.QueenRoom != null)
            {
                if (ant.AtBase())
                {
                    if (IsScout && target != null && (target.TeamIsKnown & (1 << ant.TeamID)) == 0)
                    {
                        target.Discover(ant.TeamID);
                        ant.StopCoroutine(ReturnToBase());
                    }
                    if (inventory != null)
                    {
                        ant.GetBaseController().GetGameResources().AddResources(inventory);
                        inventory.Clear();
                        foreach (var gameObject in carryingObjects)
                        {
                            Object.Destroy(gameObject);
                        }
                        carryingObjects.Clear();
                        gatheredResources.Clear();
                        ant.currentSpeed = ant.baseSpeed;
                        ant.UpdateSpeed();
                    }

                    state = State.Idle;
                    busy  = false;
                }
            }

            break;
        }
    }