1
0
forked from 0ad/0ad

health regeneration

This was SVN commit r13647.
This commit is contained in:
sanderd17 2013-08-12 17:45:18 +00:00
parent 96cef624e3
commit 1aaca7e6e3
36 changed files with 66 additions and 53 deletions

View File

@ -20,8 +20,8 @@ Health.prototype.Schema =
"<text/>" +
"</element>" +
"</optional>" +
"<element name='RegenRate' a:help='Hitpoint regeneration rate per second. Not yet implemented'>" +
"<ref name='nonNegativeDecimal'/>" +
"<element name='RegenRate' a:help='Hitpoint regeneration rate per second.'>" +
"<data type='decimal'/>" +
"</element>" +
"<element name='DeathType' a:help='Behaviour when the unit dies'>" +
"<choice>" +
@ -44,6 +44,7 @@ Health.prototype.Init = function()
// Default to <Initial>, but use <Max> if it's undefined or zero
// (Allowing 0 initial HP would break our death detection code)
this.hitpoints = +(this.template.Initial || this.GetMaxHitpoints());
this.regenRate = +this.template.RegenRate;
};
//// Interface functions ////
@ -95,6 +96,48 @@ Health.prototype.IsUnhealable = function()
|| this.GetHitpoints() >= this.GetMaxHitpoints());
};
Health.prototype.GetRegenRate = function()
{
return this.regenRate;
};
Health.prototype.ExecuteRegeneration = function()
{
var regen = this.GetRegenRate();
if (regen > 0)
this.Increase(regen);
else
this.Reduce(-regen);
};
/*
* Check if the regeneration timer needs to be started or stopped
*/
Health.prototype.CheckRegenTimer = function()
{
// check if we need a timer
if (this.GetRegenRate() == 0 ||
this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() > 0 ||
this.GetHitpoints() == 0)
{
// we don't need a timer, disable if one exists
if (this.regenTimer)
{
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
cmpTimer.CancelTimer(this.regenTimer);
this.regenTimer = undefined;
}
return;
}
// we need a timer, enable is one doesn't exist
if (this.regenTimer)
return;
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
this.regenTimer = cmpTimer.SetInterval(this.entity, IID_Health, "ExecuteRegeneration", 1000, 1000, null);
};
Health.prototype.Kill = function()
{
this.Reduce(this.hitpoints);
@ -164,6 +207,9 @@ Health.prototype.Reduce = function(amount)
Health.prototype.Increase = function(amount)
{
if (this.hitpoints == this.GetMaxHitpoints())
return {"old": this.hitpoints, "new":this.hitpoints};
// If we're already dead, don't allow resurrection
if (this.hitpoints == 0)
return undefined;
@ -284,19 +330,26 @@ Health.prototype.OnTechnologyModification = function(msg)
{
if (msg.component == "Health")
{
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechnologyManager)
var oldMaxHitpoints = this.GetMaxHitpoints();
var newMaxHitpoints = Math.round(ApplyTechModificationsToEntity("Health/Max", +this.template.Max, this.entity));
if (oldMaxHitpoints != newMaxHitpoints)
{
var oldMaxHitpoints = this.GetMaxHitpoints();
var newMaxHitpoints = Math.round(ApplyTechModificationsToEntity("Health/Max", +this.template.Max, this.entity));
if (oldMaxHitpoints != newMaxHitpoints)
{
var newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);
this.maxHitpoints = newMaxHitpoints;
this.SetHitpoints(newHitpoints);
}
var newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);
this.maxHitpoints = newMaxHitpoints;
this.SetHitpoints(newHitpoints);
}
var oldRegenRate = this.regenRate;
this.regenRate = ApplyTechModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity);
if (this.regenRate != oldRegenRate)
this.CheckRegenTimer();
}
};
Health.prototype.OnHealthChanged = function()
{
this.CheckRegenTimer();
};
Engine.RegisterComponentType(IID_Health, "Health", Health);

View File

@ -6,7 +6,6 @@
</Footprint>
<Health>
<Max>200</Max>
<RegenRate>1</RegenRate>
</Health>
<Identity>
<Civ>gaia</Civ>

View File

@ -39,7 +39,6 @@
</Footprint>
<Health>
<Max>300</Max>
<RegenRate>1</RegenRate>
</Health>
<Identity>
<Civ>gaia</Civ>

View File

@ -39,7 +39,6 @@
</Footprint>
<Health>
<Max>250</Max>
<RegenRate>1</RegenRate>
</Health>
<Identity>
<Civ>gaia</Civ>

View File

@ -26,7 +26,6 @@
</Footprint>
<Health>
<Max>200</Max>
<RegenRate>1</RegenRate>
</Health>
<Identity>
<Civ>gaia</Civ>

View File

@ -6,7 +6,6 @@
</Footprint>
<Health>
<Max>150</Max>
<RegenRate>1</RegenRate>
</Health>
<Identity>
<Civ>gaia</Civ>

View File

@ -26,7 +26,6 @@
</Footprint>
<Health>
<Max>130</Max>
<RegenRate>0.2</RegenRate>
</Health>
<Identity>
<Classes datatype="tokens">Cavalry CitizenSoldier Organic</Classes>

View File

@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Entity parent="template_unit">
<Health>
<RegenRate>0.4</RegenRate>
</Health>
<Identity>
<GenericName>Champion Unit</GenericName>
<Classes datatype="tokens">Champion Organic</Classes>

View File

@ -3,7 +3,6 @@
<Health>
<Max>100</Max>
<DeathType>remain</DeathType>
<RegenRate>1</RegenRate>
<Unhealable>true</Unhealable>
<Repairable>false</Repairable>
</Health>

View File

@ -31,7 +31,6 @@
</Cost>
<Health>
<Max>600</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<GenericName>Hero</GenericName>

View File

@ -35,7 +35,6 @@
</Footprint>
<Health>
<Max>800</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<GenericName>Hero Cavalry</GenericName>

View File

@ -42,7 +42,6 @@
</Footprint>
<Health>
<Max>800</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Classes datatype="tokens">Hero Bow -Javelin</Classes>

View File

@ -34,7 +34,6 @@
</Footprint>
<Health>
<Max>800</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Classes datatype="tokens">Hero</Classes>

View File

@ -33,7 +33,6 @@
</Footprint>
<Health>
<Max>600</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Classes datatype="tokens">Hero Infantry</Classes>

View File

@ -34,7 +34,6 @@
</Footprint>
<Health>
<Max>600</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<GenericName>Hero Archer</GenericName>

View File

@ -40,7 +40,6 @@
</Footprint>
<Health>
<Max>600</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Classes datatype="tokens">Hero</Classes>

View File

@ -26,7 +26,6 @@
</Cost>
<Health>
<Max>500</Max>
<RegenRate>0.2</RegenRate>
</Health>
<Identity>
<GenericName>Hero</GenericName>

View File

@ -44,7 +44,6 @@
</Footprint>
<Health>
<Max>100</Max>
<RegenRate>0.2</RegenRate>
</Health>
<Identity>
<GenericName>Infantry</GenericName>

View File

@ -25,7 +25,6 @@
</Cost>
<Health>
<Max>100</Max>
<RegenRate>0.2</RegenRate>
</Health>
<Identity>
<GenericName>Melee Infantry</GenericName>

View File

@ -17,7 +17,6 @@
</Attack>
<Health>
<Max>90</Max>
<RegenRate>0.2</RegenRate>
</Health>
<Identity>
<GenericName>Ranged</GenericName>

View File

@ -28,6 +28,7 @@
</Footprint>
<Health>
<Max>500</Max>
<RegenRate>-3</RegenRate>
</Health>
<Identity>
<GenericName>Fire Ship</GenericName>
@ -42,9 +43,6 @@
<BarHeight>0.5</BarHeight>
<HeightOffset>6.0</HeightOffset>
</StatusBars>
<TerritoryDecay>
<HealthDecayRate>3</HealthDecayRate>
</TerritoryDecay>
<ResourceGatherer disable=""/>
<UnitMotion>
<WalkSpeed>16.0</WalkSpeed>

View File

@ -8,9 +8,6 @@
<Cost>
<BuildTime>15</BuildTime>
</Cost>
<Health>
<RegenRate>0.2</RegenRate>
</Health>
<Identity>
<GenericName>Support</GenericName>
<Classes datatype="tokens">Support Organic</Classes>

View File

@ -47,7 +47,6 @@
</Cost>
<Health>
<Max>75</Max>
<RegenRate>0.3</RegenRate>
</Health>
<Identity>
<GenericName>Female Citizen</GenericName>

View File

@ -14,7 +14,6 @@
</Cost>
<Health>
<Max>85</Max>
<RegenRate>0.3</RegenRate>
</Health>
<Identity>
<Classes datatype="tokens">Healer</Classes>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>95</Max>
<RegenRate>0.3</RegenRate>
</Health>
<Identity>
<Rank>Advanced</Rank>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>105</Max>
<RegenRate>0.4</RegenRate>
</Health>
<Identity>
<Rank>Elite</Rank>

View File

@ -7,7 +7,6 @@
</Attack>
<Health>
<Max>75</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Civ>brit</Civ>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>95</Max>
<RegenRate>0.3</RegenRate>
</Health>
<Identity>
<Rank>Advanced</Rank>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>105</Max>
<RegenRate>0.4</RegenRate>
</Health>
<Identity>
<Rank>Elite</Rank>

View File

@ -12,7 +12,6 @@
</Builder>
<Health>
<Max>75</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Civ>celt</Civ>

View File

@ -12,7 +12,6 @@
</Builder>
<Health>
<Max>75</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Civ>gaul</Civ>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>95</Max>
<RegenRate>0.3</RegenRate>
</Health>
<Identity>
<Rank>Advanced</Rank>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>105</Max>
<RegenRate>0.4</RegenRate>
</Health>
<Identity>
<Rank>Elite</Rank>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>95</Max>
<RegenRate>0.3</RegenRate>
</Health>
<Identity>
<Rank>Advanced</Rank>

View File

@ -13,7 +13,6 @@
</Attack>
<Health>
<Max>105</Max>
<RegenRate>0.4</RegenRate>
</Health>
<Identity>
<Rank>Elite</Rank>

View File

@ -22,7 +22,6 @@
</Cost>
<Health>
<Max>600</Max>
<RegenRate>0.5</RegenRate>
</Health>
<Identity>
<Civ>maur</Civ>