Put territory decay in the Capture regen timer to avoid jumpyness of GUI elements

This was SVN commit r16588.
This commit is contained in:
sanderd17 2015-04-27 18:04:58 +00:00
parent 40eb747bc4
commit 04c4e666de
2 changed files with 46 additions and 48 deletions

View File

@ -16,7 +16,7 @@ Capturable.prototype.Init = function()
// Cache this value
this.maxCp = +this.template.CapturePoints;
this.cp = [];
this.startRegenTimer();
this.StartRegenTimer();
};
//// Interface functions ////
@ -34,6 +34,11 @@ Capturable.prototype.GetMaxCapturePoints = function()
return this.maxCp;
};
Capturable.prototype.GetGarrisonRegenRate = function()
{
return ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", +this.template.GarrisonRegenRate, this.entity);
};
/**
* Set the new capture points, used for cloning entities
* The caller should assure that the sum of capture points
@ -86,7 +91,7 @@ Capturable.prototype.Reduce = function(amount, playerID)
var takenCp = this.maxCp - this.cp.reduce(function(a, b) { return a + b; });
this.cp[playerID] += takenCp;
this.startRegenTimer();
this.StartRegenTimer();
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp })
@ -112,7 +117,7 @@ Capturable.prototype.CanCapture = function(playerID)
var cmpPlayerSource = QueryPlayerIDInterface(playerID);
if (!cmpPlayerSource)
warn(source + " has no player component defined on its owner ");
warn(playerID + " has no player component defined on its id");
var cp = this.GetCapturePoints()
var sourceEnemyCp = 0;
for (let i in this.GetCapturePoints())
@ -128,14 +133,19 @@ Capturable.prototype.GetRegenRate = function()
var regenRate = +this.template.RegenRate;
regenRate = ApplyValueModificationsToEntity("Capturable/RegenRate", regenRate, this.entity);
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
if (!cmpGarrisonHolder)
return regenRate;
var cmpTerritoryDecay = Engine.QueryInterface(this.entity, IID_TerritoryDecay);
if (cmpTerritoryDecay && cmpTerritoryDecay.IsDecaying())
var territoryDecayRate = cmpTerritoryDecay.GetDecayRate();
else
var territoryDecayRate = 0;
var garrisonRegenRate = +this.template.GarrisonRegenRate;
garrisonRegenRate = ApplyValueModificationsToEntity("Capturable/GarrisonRegenRate", garrisonRegenRate, this.entity);
var garrisonedUnits = cmpGarrisonHolder.GetEntities().length;
return regenRate + garrisonedUnits * garrisonRegenRate;
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
if (cmpGarrisonHolder)
var garrisonRegenRate = this.GetGarrisonRegenRate() * cmpGarrisonHolder.GetEntities().length;
else
var garrisonRegenRate = 0;
return regenRate + garrisonRegenRate - territoryDecayRate;
};
Capturable.prototype.RegenCapturePoints = function()
@ -144,7 +154,12 @@ Capturable.prototype.RegenCapturePoints = function()
if (!cmpOwnership || cmpOwnership.GetOwner() == -1)
return;
var takenCp = this.Reduce(this.GetRegenRate(), cmpOwnership.GetOwner())
var regenRate = this.GetRegenRate();
if (regenRate < 0)
var takenCp = this.Reduce(-regenRate, 0);
else
var takenCp = this.Reduce(regenRate, cmpOwnership.GetOwner())
if (takenCp > 0)
return;
@ -160,7 +175,7 @@ Capturable.prototype.RegenCapturePoints = function()
* rate is 0, or because it is fully regenerated),
* the timer stops automatically after one execution.
*/
Capturable.prototype.startRegenTimer = function()
Capturable.prototype.StartRegenTimer = function()
{
if (this.regenTimer)
return;
@ -184,17 +199,23 @@ Capturable.prototype.OnValueModification = function(msg)
for (let i in this.cp)
this.cp[i] *= scale;
Engine.PostMessage(this.entity, MT_CapturePointsChanged, { "capturePoints": this.cp });
this.startRegenTimer();
this.StartRegenTimer();
};
Capturable.prototype.OnGarrisonedUnitsChanged = function(msg)
{
this.startRegenTimer();
this.StartRegenTimer();
};
Capturable.prototype.OnTerritoryDecayChanged = function(msg)
{
if (msg.to)
this.StartRegenTimer();
};
Capturable.prototype.OnOwnershipChanged = function(msg)
{
this.startRegenTimer();
this.StartRegenTimer();
// if the new owner has no capture points, it means that either
// * it's being initialised now, or

View File

@ -7,7 +7,6 @@ TerritoryDecay.prototype.Schema =
TerritoryDecay.prototype.Init = function()
{
this.timer = undefined;
this.decaying = false;
};
@ -42,27 +41,20 @@ TerritoryDecay.prototype.IsDecaying = function()
return this.decaying;
};
TerritoryDecay.prototype.GetDecayRate = function()
{
return ApplyValueModificationsToEntity(
"TerritoryDecay/DecayRate",
+this.template.DecayRate,
this.entity);
};
TerritoryDecay.prototype.UpdateDecayState = function()
{
var connected = this.IsConnected();
if (!connected && !this.timer)
{
// Start decaying
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
this.timer = cmpTimer.SetInterval(this.entity, IID_TerritoryDecay, "Decay", 1000, 1000, {});
}
else if (connected && this.timer)
{
// Stop decaying
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
cmpTimer.CancelTimer(this.timer);
this.timer = undefined;
}
if (connected)
if (this.IsConnected())
var decaying = false;
else
var decaying = (Math.round(ApplyValueModificationsToEntity("TerritoryDecay/DecayRate", +this.template.DecayRate, this.entity)) > 0);
var decaying = this.GetDecayRate() != 0;
if (decaying === this.decaying)
return;
this.decaying = decaying;
@ -84,19 +76,4 @@ TerritoryDecay.prototype.OnOwnershipChanged = function(msg)
this.UpdateDecayState();
};
TerritoryDecay.prototype.Decay = function()
{
var cmpCapturable = Engine.QueryInterface(this.entity, IID_Capturable);
if (!cmpCapturable)
return; // error
var decayRate = ApplyValueModificationsToEntity(
"TerritoryDecay/DecayRate",
+this.template.DecayRate,
this.entity);
// Reduce capture points in favour of Gaia
cmpCapturable.Reduce(decayRate, 0);
};
Engine.RegisterComponentType(IID_TerritoryDecay, "TerritoryDecay", TerritoryDecay);