public void DoLogic(List <ItemMovable> movableItems, BuildingHQ hq) { switch (_state) { //In Idle state do nothing case RobotTransporterState.Idle: break; //Find a resource nearby case RobotTransporterState.SearchForResource: //If there are any resources on the screen, go after the closest one if (movableItems.Count > 0) { ItemMovable closestItem = Helper.GetClosestItem(Position, movableItems.Where(a => a.IsAvailableToBePickedUp).ToList()); if (closestItem == null) { break; } WhereToGo = closestItem.Position; _state = RobotTransporterState.GoForResource; } break; //Find a resource nearby case RobotTransporterState.GoForResource: //Check if robot arrived at the resource if (Functions.DistanceBetweenTwoPoints(Position, WhereToGo) < destinationRange) { //If it got to the resource, pick the closest one(maybe there are more in within the rage) ItemMovable closestItem = Helper.GetClosestItem(Position, movableItems.Where(a => a.IsAvailableToBePickedUp).ToList()); if (closestItem == null) { break; } //Change the states of the robot and the item so no other robot will pick this one up _pickedUpItem = closestItem; closestItem.IsAvailableToBePickedUp = false; WhereToGo = hq.Position; _state = RobotTransporterState.ReturnResourceToBase; } else { _state = RobotTransporterState.SearchForResource; } break; //After the resource is picked up, bring it back to the base case RobotTransporterState.ReturnResourceToBase: if (Functions.DistanceBetweenTwoPoints(Position, WhereToGo) < destinationRange) { _pickedUpItem = null; _state = RobotTransporterState.SearchForResource; WhereToGo = null; } break; } }
public ItemMovable Mine(int value) { if (!IsAlive) { return(null); } Random random = new Random(); int CollectedAmount = Value - value < 0 ? Value : value; Vector2 itemOffset = new Vector2(1, 0).Rotate(random.NextDouble() * Math.PI * 2) * (_outputRange + random.NextDouble() * _outputRange); ItemMovable itemMovable = new ItemMovable(Position + itemOffset, Value) { Value = CollectedAmount }; Value -= CollectedAmount; if (Value <= 0) { IsAlive = false; } return(itemMovable); }
public ItemMovable DoLogic(List <ItemFixed> fixedItems) { ItemMovable item = null; switch (_state) { //In Idle state do nothing case RobotMinerState.Idle: break; case RobotMinerState.SearchForMiningSite: //If there are any resources on the screen, go after the closest one if (fixedItems.Count > 0) { ItemFixed closestItem = Helper.GetClosestItem(Position, fixedItems); if (closestItem == null) { break; } WhereToGo = closestItem.Position; _state = RobotMinerState.GoToMiningSite; } break; //Go to the mining site case RobotMinerState.GoToMiningSite: //Check if robot arrived at the resource if (Functions.DistanceBetweenTwoPoints(Position, WhereToGo) < destinationRange) { //Change the robot's state to Mine WhereToGo = null; _state = RobotMinerState.Mine; } break; case RobotMinerState.Mine: //Check if it has anything to mine if (_itemToMine == null) { //Get the first resource within the range foreach (ItemFixed i in fixedItems) { if (Functions.DistanceBetweenTwoPoints(Position, i.Position) < destinationRange) { _itemToMine = i; break; } } //If no item in range, change the state to search and go if (_itemToMine == null) { _state = RobotMinerState.SearchForMiningSite; } } else { //Mine the resource every _cycleMaxTick ticks _cycleTickIteration++; if (_cycleTickIteration == _cycleMaxTick) { _cycleTickIteration = 0; item = _itemToMine.Mine(_amountToMinePerCycle); if (!_itemToMine.IsAlive) { _itemToMine = null; } } } break; } return(item); }