Esempio n. 1
0
        public void Go(Random random)
        {
            Age++;
            BeeState oldState = CurrentState;

            switch (CurrentState)
            {
            case BeeState.Idle:
                if (Age > CareerSpan)
                {
                    CurrentState = BeeState.Retired;
                }
                else if (world.Flowers.Count > 0 &&
                         hive.ConsumeHoney(HoneyConsumed))
                {
                    Flower flower =
                        world.Flowers[random.Next(world.Flowers.Count)];
                    if (flower.Nectar >= MinimumFlowerNectar && flower.Alive)
                    {
                        destinationFlower = flower;
                        CurrentState      = BeeState.FlyingToFlower;
                    }
                }
                break;

            case BeeState.FlyingToFlower:
                if (!world.Flowers.Contains(destinationFlower))
                {
                    CurrentState = BeeState.ReturningToHive;
                }
                else if (InsideHive)
                {
                    if (MoveTowardsLocation(hive.GetLocation("Exit")))
                    {
                        InsideHive = false;
                        location   = hive.GetLocation("Entrance");
                    }
                }
                else
                if (MoveTowardsLocation(destinationFlower.Location))
                {
                    CurrentState = BeeState.GatheringNectar;
                }
                break;

            case BeeState.GatheringNectar:
                double nectar = destinationFlower.HarvestNectar();
                if (nectar > 0)
                {
                    NectarCollected += nectar;
                }
                else
                {
                    CurrentState = BeeState.ReturningToHive;
                }
                break;

            case BeeState.ReturningToHive:
                if (!InsideHive)
                {
                    if (MoveTowardsLocation(hive.GetLocation("Entrance")))
                    {
                        InsideHive = true;
                        location   = hive.GetLocation("Exit");
                    }
                }
                else
                if (MoveTowardsLocation(hive.GetLocation("HoneyFactory")))
                {
                    CurrentState = BeeState.MakingHoney;
                }
                break;

            case BeeState.MakingHoney:
                if (NectarCollected < 0.5)
                {
                    NectarCollected = 0;
                    CurrentState    = BeeState.Idle;
                }
                else
                if (hive.AddHoney(0.5))
                {
                    NectarCollected -= 0.5;
                }
                else
                {
                    NectarCollected = 0;
                }
                break;

            case BeeState.Retired:
                // Do nothing! We’re retired!
                break;
            }
            if (oldState != CurrentState &&
                MessageSender != null)
            {
                MessageSender(ID, CurrentState.ToString());
            }
        }
Esempio n. 2
0
        public void Go(Random random)
        {
            Age++;
            BeeState oldState = CurrentState;

            switch (CurrentState)
            {
            case BeeState.Idle:
                if (Age > CAREERSPAN)
                {
                    CurrentState = BeeState.Retired;
                }
                else if (_world.Flowers.Count > 0 && _hive.ConsumeHoney(HONEYCONSUMED))
                {
                    Flower flower = _world.Flowers[random.Next(_world.Flowers.Count)];
                    if (flower.Nectar >= MINIMUMFLOWERNECTAR && flower.Alive)
                    {
                        _destinationFlower = flower;
                        CurrentState       = BeeState.FlyingToFlower;
                    }
                }
                break;

            case BeeState.FlyingToFlower:
                if (!_world.Flowers.Contains(_destinationFlower))
                {
                    CurrentState = BeeState.ReturningToHive;
                }
                else if (InsideHive)
                {
                    if (MoveTowardsLocation(_hive.GetLocation("Exit")))
                    {
                        InsideHive = false;
                        _location  = _hive.GetLocation("Entrance");
                    }
                }
                else if (MoveTowardsLocation(_destinationFlower.Location))
                {
                    CurrentState = BeeState.GatheringNectar;
                }
                break;

            case BeeState.GatheringNectar:
                double nectar = _destinationFlower.HarvestNectar();
                if (nectar > 0)
                {
                    NectarCollected += nectar;
                }
                else
                {
                    CurrentState = BeeState.ReturningToHive;
                }
                break;

            case BeeState.ReturningToHive:
                if (!InsideHive)
                {
                    if (MoveTowardsLocation(_hive.GetLocation("Entrance")))
                    {
                        InsideHive = true;
                        _location  = _hive.GetLocation("Exit");
                    }
                }
                else
                {
                    if (MoveTowardsLocation(_hive.GetLocation("HoneyFactory")))
                    {
                        CurrentState = BeeState.MakingHoney;
                    }
                }
                break;

            case BeeState.MakingHoney:
                if (NectarCollected < ADDNECTARRATE)
                {
                    NectarCollected = 0;
                    CurrentState    = BeeState.Idle;
                }
                else if (_hive.AddHoney(ADDNECTARRATE))
                {
                    NectarCollected -= ADDNECTARRATE;
                }
                else
                {
                    NectarCollected = 0;
                }
                break;

            case BeeState.Retired:
                // Não faz nada, já que está aposentada.
                break;
            }

            if (oldState != CurrentState && MessageSender != null)
            {
                MessageSender(_id, CurrentState);
            }
        }