2019-09-08 10:04:24 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Revitalize.Framework.Energy
|
|
|
|
{
|
|
|
|
public class EnergyManager
|
|
|
|
{
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The remaining energy left in this system.
|
|
|
|
/// </summary>
|
|
|
|
public int remainingEnergy;
|
|
|
|
/// <summary>
|
|
|
|
/// The maximum amount of energy this system can store.
|
|
|
|
/// </summary>
|
|
|
|
public int maxEnergy;
|
|
|
|
|
|
|
|
public bool requiresUpdate;
|
|
|
|
/// <summary>
|
|
|
|
/// How does this energy manager interface energy systems.
|
|
|
|
/// </summary>
|
|
|
|
public Enums.EnergyInteractionType energyInteractionType;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks to see if this energy manager consumes energy.
|
|
|
|
/// </summary>
|
|
|
|
public bool consumesEnergy
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.energyInteractionType == Enums.EnergyInteractionType.Consumes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Checks to see if this energy manager produces energy.
|
|
|
|
/// </summary>
|
|
|
|
public bool producesEnergy
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.energyInteractionType == Enums.EnergyInteractionType.Produces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks to see if this energy manager transfers energy.
|
|
|
|
/// </summary>
|
|
|
|
public bool transfersEnergy
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.energyInteractionType == Enums.EnergyInteractionType.Transfers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Does this energy system have energy.
|
|
|
|
/// </summary>
|
|
|
|
public bool hasEnergy
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.remainingEnergy > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Checks to see if this energy system has any energy left.
|
|
|
|
/// </summary>
|
|
|
|
public bool hasMaxEnergy
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.remainingEnergy == this.maxEnergy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Checks to see if this system can receive any energy externally.
|
|
|
|
/// </summary>
|
|
|
|
public bool canReceieveEnergy
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return !this.hasMaxEnergy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int capacityRemaining
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.maxEnergy - this.remainingEnergy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 05:40:22 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the energy remaining as a percent value.
|
|
|
|
/// </summary>
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 10:04:24 +08:00
|
|
|
public EnergyManager()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-17 06:48:26 +08:00
|
|
|
public EnergyManager(int Capacity,Enums.EnergyInteractionType EnergyType) : this(0, Capacity,EnergyType)
|
2019-09-08 10:04:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-17 06:48:26 +08:00
|
|
|
public EnergyManager(int CurrentEnergy, int MaxEnergy, Enums.EnergyInteractionType EnergyType)
|
2019-09-08 10:04:24 +08:00
|
|
|
{
|
|
|
|
this.remainingEnergy = CurrentEnergy;
|
|
|
|
this.maxEnergy = MaxEnergy;
|
2019-09-17 06:48:26 +08:00
|
|
|
this.energyInteractionType = EnergyType;
|
2019-09-08 10:04:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks to see if this energy source has enough energy remaining.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="Required"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
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);
|
|
|
|
if (this.remainingEnergy != amountBeforeConsumption)
|
|
|
|
{
|
|
|
|
this.requiresUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void produceEnergy(int amount)
|
|
|
|
{
|
|
|
|
int amountBeforeProduction = this.remainingEnergy;
|
|
|
|
this.remainingEnergy = Math.Min(this.maxEnergy, this.remainingEnergy + amount);
|
|
|
|
if (this.remainingEnergy != amountBeforeProduction)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2019-09-17 06:48:26 +08:00
|
|
|
return new EnergyManager(this.maxEnergy,this.energyInteractionType);
|
2019-09-08 10:04:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|