Index: binaries/data/mods/public/simulation/components/ResourceTrickle.js =================================================================== --- binaries/data/mods/public/simulation/components/ResourceTrickle.js +++ binaries/data/mods/public/simulation/components/ResourceTrickle.js @@ -11,10 +11,8 @@ ResourceTrickle.prototype.Init = function() { - this.ComputeRates(); - - let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); - cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.GetTimer(), this.GetTimer(), undefined); + if (this.ComputeRates()) + this.StartTimer(); }; ResourceTrickle.prototype.GetTimer = function() @@ -30,8 +28,18 @@ ResourceTrickle.prototype.ComputeRates = function() { this.rates = {}; + let hasTrickle = false; for (let resource in this.template.Rates) - this.rates[resource] = ApplyValueModificationsToEntity("ResourceTrickle/Rates/"+resource, +this.template.Rates[resource], this.entity); + { + let rate = ApplyValueModificationsToEntity("ResourceTrickle/Rates/" + resource, +this.template.Rates[resource], this.entity); + if (rate == 0) + continue; + + this.rates[resource] = rate; + hasTrickle = true; + } + + return hasTrickle; }; ResourceTrickle.prototype.Trickle = function(data, lateness) @@ -44,12 +52,39 @@ cmpPlayer.AddResources(this.rates); }; +ResourceTrickle.prototype.StartTimer = function() +{ + if (this.timer) + return; + + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + let interval = +this.template.Interval; + this.timer = cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", interval, interval, undefined); +}; + +ResourceTrickle.prototype.CancelTimer = function() +{ + if (!this.timer) + return; + + let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + cmpTimer.CancelTimer(this.timer); + delete this.timer; +}; + ResourceTrickle.prototype.OnValueModification = function(msg) { if (msg.component != "ResourceTrickle") return; - this.ComputeRates(); + // No need to have a timer running when there is not trickle. + if (!this.ComputeRates()) + { + this.CancelTimer(); + return; + } + + this.StartTimer(); }; Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle);