public void ConsumeOxygen(ResourceManager manager) { var on = false; if (HasEnergy) { // Can only provide oxygen that is stored and at maximum oxygen covering the refilling rate var oxygenAvailable = ResourceMath.Min(oxygenRefillPerSecond * Time.fixedDeltaTime, manager.OxygenAvailable); if (Mathf.Abs((float)oxygenAvailable) > Mathf.Epsilon) { // Calculate amount of oxygen that player would like to have var maxOxygenRequest = connectedPlayers.Aggregate(Oxygen.Zero, (a, b) => a + b.MaxReceiveOxygen()); // Amount of oxygen that will be provided to the players var oxygenRequest = ResourceMath.Min(oxygenAvailable, maxOxygenRequest); // At least consume a minimal amount of oxygen // Without this, the tower wont work. Excess is lost. oxygenRequest = ResourceMath.Max(oxygenRequest, minOxygenUsagePerSecond * Time.fixedDeltaTime); if (manager.TryConsume(oxygenRequest)) { on = true; ShareOxygenFairly(oxygenRequest); } } } if (animator.GetBool(AnimatorOnFlag) != on) { animator.SetBool(AnimatorOnFlag, on); } }
/// <summary>Provide given amount of oxygen fairly among the connected players.</summary> /// <param name="oxygen">Oxygen to be provided.</param> private void ShareOxygenFairly(Oxygen oxygen) { // Start with the player that needs the least amount. // Give said player their fair share (or less if they'd be already overfilled). // Give the next player their fair share. (This might have increased if the previous player didn't use their full share). // ... var playerCount = connectedPlayers.Count; foreach (var player in connectedPlayers.OrderBy(p => p.MaxReceiveOxygen())) { var providing = ResourceMath.Min(player.MaxReceiveOxygen(), oxygen / playerCount); if (player.Receive(providing)) { oxygen -= providing; } --playerCount; } }
public void ProduceMaterial(ResourceManager manager) { if (HasEnergy) { InternalStorage += constructionMaterialPerSecond * Time.fixedDeltaTime; InternalStorage = ResourceMath.Min(internalStorageSize, InternalStorage); Debug.Assert(internalStorageSize >= minimumDeliveryAmount, "Quarry internal storage is not big enough"); Debug.Assert(minimumDeliveryAmount > ConstructionMaterial.Zero, "Delivery amount has to be bigger than 0"); while (InternalStorage >= minimumDeliveryAmount) { manager.Store(minimumDeliveryAmount); InternalStorage -= minimumDeliveryAmount; var fadeout = Instantiate(fadeoutTextPrefab, transform); fadeout.TextMesh.text = $"+{(float)minimumDeliveryAmount:F0}"; fadeout.TextMesh.color = new Color(1f, 0.5f, 0.15f, 1); audioSource.Play(); } } }