public void DistributeResources(TimeSpan elapsedTime) { currentResources += elapsedTime.NumericValue * totalResourcesProvided; var resourceOut = elapsedTime.NumericValue * totalResourcesRequested; if (resourceOut <= currentResources) { foreach (var consumer in resourceConsumers) { var consumerGrantedResources = elapsedTime.NumericValue * consumer.RatePerS; var grant = new ResourceGrant( Math.Min(consumer.Maximum, consumerGrantedResources), consumerGrantedResources >= consumer.Maximum); currentResources -= grant.Amount; consumer.ConsumeResources(grant); } resetForFrame(); return; } var sortedConsumers = resourceConsumers.OrderBy((consumer) => (consumer.Maximum / consumer.RatePerS)); var resourceRatio = currentResources / resourceOut; foreach (var consumer in sortedConsumers) { var consumerGrantedResources = resourceRatio * elapsedTime.NumericValue * consumer.RatePerS; if (consumer.Maximum <= consumerGrantedResources) { consumer.ConsumeResources(new ResourceGrant(consumer.Maximum, true)); resourceOut -= consumer.Maximum; currentResources -= consumer.Maximum; resourceRatio = currentResources / resourceOut; } else { consumer.ConsumeResources(new ResourceGrant(consumerGrantedResources, false)); currentResources -= consumerGrantedResources; } } resetForFrame(); }
public void ConsumeResources(ResourceGrant grant) => processManager.Progress(grant);