using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Revitalize.Framework.Energy { public class EnergyManager { /// /// The remaining energy left in this system. /// public int remainingEnergy; /// /// The maximum amount of energy this system can store. /// public int maxEnergy; public bool requiresUpdate; /// /// How does this energy manager interface energy systems. /// public Enums.EnergyInteractionType energyInteractionType; /// /// Checks to see if this energy manager consumes energy. /// public bool consumesEnergy { get { return this.energyInteractionType == Enums.EnergyInteractionType.Consumes; } } /// /// Checks to see if this energy manager produces energy. /// public bool producesEnergy { get { return this.energyInteractionType == Enums.EnergyInteractionType.Produces; } } /// /// Checks to see if this energy manager transfers energy. /// public bool transfersEnergy { get { return this.energyInteractionType == Enums.EnergyInteractionType.Transfers; } } /// /// Does this energy system have energy. /// public bool hasEnergy { get { return this.remainingEnergy > 0; } } /// /// Checks to see if this energy system has any energy left. /// public bool hasMaxEnergy { get { return this.remainingEnergy == this.maxEnergy; } } /// /// Checks to see if this system can receive any energy externally. /// public bool canReceieveEnergy { get { return !this.hasMaxEnergy; } } public int capacityRemaining { get { return this.maxEnergy - this.remainingEnergy; } } /// /// Returns the energy remaining as a percent value. /// public double energyPercentRemaining { get { return (double)this.remainingEnergy / (double)this.maxEnergy; } } public string energyDisplayString { get { StringBuilder b = new StringBuilder(); b.Append(this.remainingEnergy); b.Append("/"); b.Append(this.maxEnergy); return b.ToString(); } } public EnergyManager() { } public EnergyManager(int Capacity, Enums.EnergyInteractionType EnergyType) : this(0, Capacity, EnergyType) { } public EnergyManager(int CurrentEnergy, int MaxEnergy, Enums.EnergyInteractionType EnergyType) { this.remainingEnergy = CurrentEnergy; this.maxEnergy = MaxEnergy; this.energyInteractionType = EnergyType; } /// /// Checks to see if this energy source has enough energy remaining. /// /// /// public bool hasEnoughEnergy(int Required) { return this.remainingEnergy >= Required; } public void consumeEnergy(int amount) { int amountBeforeConsumption = this.remainingEnergy; this.remainingEnergy = Math.Max(0, this.remainingEnergy - amount); this.requiresUpdate = true; } public void produceEnergy(int amount) { int amountBeforeProduction = this.remainingEnergy; this.remainingEnergy = Math.Min(this.maxEnergy, this.remainingEnergy + amount); this.requiresUpdate = true; } public void transferEnergyFromAnother(EnergyManager other, int amount) { if (this.canReceieveEnergy) { int actualAmount = Math.Min(amount, other.remainingEnergy); int selfCapacity = this.capacityRemaining; this.produceEnergy(Math.Min(actualAmount, selfCapacity)); other.consumeEnergy(Math.Min(actualAmount, selfCapacity)); } else { return; } } public void transferEnergyToAnother(EnergyManager other, int amount) { if (other.canReceieveEnergy) { int actualAmount = Math.Min(amount, this.remainingEnergy); int selfCapacity = other.capacityRemaining; other.produceEnergy(Math.Min(actualAmount, selfCapacity)); this.consumeEnergy(Math.Min(actualAmount, selfCapacity)); } else { return; } } public EnergyManager Copy() { return new EnergyManager(this.maxEnergy, this.energyInteractionType); } } }