//if wr want to remove, we just use negative numbas? public void AddElementToQueue(int unitID, int number) { UnitData unit = units[unitID]; RecruitmentQueueElement elementContainingThisUnit = null; foreach (RecruitmentQueueElement element in unitsQueue) { if (element.unit == unit) { elementContainingThisUnit = element; } } if (elementContainingThisUnit != null) { elementContainingThisUnit.number += number; } else { unitsQueue.Enqueue(new RecruitmentQueueElement(unit, number)); } currentlyNeededPeople += number * unit.populationValue; for (int i = 0; i < number * unit.populationValue; i++) { if (PlayerManager.Instance.GetIdleWorkersNumber() > 0) { PlayerManager.Instance.GetNearestIdleWorker(transform.position).AssignToDeposition(building); } } }
public void RemoveElementFromQueue(int unitID, int number) { UnitData unit = units[unitID]; RecruitmentQueueElement elementContainingThisUnit = null; foreach (RecruitmentQueueElement element in unitsQueue) { if (element.unit == unit) { elementContainingThisUnit = element; } } elementContainingThisUnit.number -= number; if (elementContainingThisUnit.number <= 0) { if (currentRecruitedUnit == elementContainingThisUnit.unit) { StopRecruitment(); unitsQueue.Dequeue(); } } currentlyNeededPeople -= number; //we need to fire the workers - lets check if it enough to fire the ones which are currently on their way int workersToFireLeft = number; HashSet <B_Worker> workersToRemove = new HashSet <B_Worker>(); foreach (B_Worker worker in peopleOnTheirWayToBarracks) { if (workersToFireLeft > 0) { worker.AssignToIdle(); workersToFireLeft--; workersToRemove.Add(worker); } else { break; } } foreach (B_Worker worker in workersToRemove) { peopleOnTheirWayToBarracks.Remove(worker); } //if this is not enough, we need to release some workers which are already in the building //backward for for remove for (int i = workersToFireLeft - 1; i >= 0; i--) { peopleInBarracks[i].AssignToIdle(); peopleInBarracks[i].entity.gameObject.SetActive(true); peopleInBarracks.RemoveAt(i); } }