first draft and cooldown component

This commit is contained in:
Olof Larsson 2013-04-03 09:35:23 +02:00
parent 79d382ee40
commit cae94f01f6
2 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,87 @@
package com.massivecraft.mcore;
public final class HeatData
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final transient double HEAT_MIN = 0D;
public static final transient double HEAT_HIGH = 1D;
public static final transient long MILLIS_CALC_EPSILON = 50;
// -------------------------------------------- //
// FIELDS: RAW
// -------------------------------------------- //
private final double heat;
public double getHeat() { return this.heat; }
private final long lastCalcMillis;
public long getLastCalcMillis() { return this.lastCalcMillis; }
// -------------------------------------------- //
// FIELDS: INSPECT
// -------------------------------------------- //
public double getOverheat()
{
return this.getHeat() - HEAT_HIGH;
}
public boolean isOverheated()
{
return this.getOverheat() > 0;
}
// -------------------------------------------- //
// FIELDS: WITH
// -------------------------------------------- //
public HeatData withHeat(double heat) { return new HeatData(heat, lastCalcMillis); }
public HeatData withLastCalcMillis(long lastCalcMillis) { return new HeatData(heat, lastCalcMillis); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
private HeatData(double heat, long lastCalcMillis)
{
this.heat = Math.max(0, heat);
this.lastCalcMillis = lastCalcMillis;
}
private HeatData()
{
this.heat = 0;
this.lastCalcMillis = 0;
}
// -------------------------------------------- //
// FACTORY: VALUE OF
// -------------------------------------------- //
public static HeatData valueOf(double heat, long lastCalcMillis)
{
return new HeatData(heat, lastCalcMillis);
}
// -------------------------------------------- //
// FACTORY: RECALCULATED
// -------------------------------------------- //
public HeatData recalculated(long now, double heatPerMilli)
{
if (this.lastCalcMillis + MILLIS_CALC_EPSILON >= now) return this;
long deltaMillis = now - this.lastCalcMillis;
double heatDelta = heatPerMilli * deltaMillis;
double heatTarget = this.heat + heatDelta;
heatTarget = Math.max(0, heatTarget);
return valueOf(heatTarget, now);
}
}

View File

@ -0,0 +1,57 @@
package com.massivecraft.mcore;
public abstract class Heatable
{
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
protected abstract HeatData getData();
protected abstract void setData(HeatData data);
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
private HeatData getRecalculatedData(double heatPerMilli)
{
long now = System.currentTimeMillis();
HeatData data = this.getData().recalculated(now, heatPerMilli);
this.setData(data);
return data;
}
public void addHeat(double heatPerMilli, double heat)
{
long now = System.currentTimeMillis();
HeatData data = this.getData().recalculated(now, heatPerMilli);
data = data.withHeat(data.getHeat() + heat);
this.setData(data);
}
public double getHeat(double heatPerMilli)
{
HeatData data = getRecalculatedData(heatPerMilli);
return data.getHeat();
}
public boolean isOverheated(double heatPerMilli)
{
HeatData data = getRecalculatedData(heatPerMilli);
return data.isOverheated();
}
public double getOverheat(double heatPerMilli)
{
HeatData data = getRecalculatedData(heatPerMilli);
return data.getOverheat();
}
public long getCooldownMillisLeft(double heatPerMilli)
{
double overheat = this.getOverheat(heatPerMilli);
return (long) (-overheat / heatPerMilli);
}
}