Changeset View
Standalone View
binaries/data/mods/public/simulation/components/ResourceTrickle.js
function ResourceTrickle() {} | function ResourceTrickle() {} | ||||
ResourceTrickle.prototype.Schema = | ResourceTrickle.prototype.Schema = | ||||
"<a:help>Controls the resource trickle ability of the unit.</a:help>" + | "<a:help>Controls the resource trickle ability of the unit.</a:help>" + | ||||
"<element name='Rates' a:help='Trickle Rates'>" + | "<element name='Rates' a:help='Trickle Rates'>" + | ||||
Resources.BuildSchema("nonNegativeDecimal") + | Resources.BuildSchema("nonNegativeDecimal") + | ||||
"</element>" + | "</element>" + | ||||
"<element name='Interval' a:help='Number of miliseconds must pass for the player to gain the next trickle.'>" + | "<element name='Interval' a:help='Number of miliseconds must pass for the player to gain the next trickle.'>" + | ||||
"<ref name='nonNegativeDecimal'/>" + | "<ref name='nonNegativeDecimal'/>" + | ||||
"</element>"; | "</element>"; | ||||
ResourceTrickle.prototype.Init = function() | ResourceTrickle.prototype.Init = function() | ||||
{ | { | ||||
this.ComputeRates(); | if (this.ComputeRates()) | ||||
Freagarach: Not necessary to declare it on init, I guess? | |||||
this.StartTimer(); | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.GetTimer(), this.GetTimer(), undefined); | |||||
}; | }; | ||||
ResourceTrickle.prototype.GetTimer = function() | ResourceTrickle.prototype.GetTimer = function() | ||||
{ | { | ||||
return +this.template.Interval; | return +this.template.Interval; | ||||
}; | }; | ||||
ResourceTrickle.prototype.GetRates = function() | ResourceTrickle.prototype.GetRates = function() | ||||
{ | { | ||||
return this.rates; | return this.rates; | ||||
}; | }; | ||||
ResourceTrickle.prototype.ComputeRates = function() | ResourceTrickle.prototype.ComputeRates = function() | ||||
{ | { | ||||
Done Inline Actionsat least one non-zero trickle rate.? Freagarach: `at least one non-zero trickle rate.`? | |||||
Done Inline Actionshas bb: has | |||||
this.rates = {}; | this.rates = {}; | ||||
let hasTrickle = false; | |||||
for (let resource in this.template.Rates) | 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); | |||||
Done Inline Actionslet? Freagarach: `let`? | |||||
if (rate == 0) | |||||
Done Inline Actionsif (rate) { foo() } ? bb: ```
if (rate)
{
foo()
}
```
? | |||||
continue; | |||||
this.rates[resource] = rate; | |||||
hasTrickle = true; | |||||
Done Inline ActionsThis check is performed earlier as well? Freagarach: This check is performed earlier as well? | |||||
} | |||||
return hasTrickle; | |||||
}; | }; | ||||
ResourceTrickle.prototype.Trickle = function(data, lateness) | ResourceTrickle.prototype.Trickle = function(data, lateness) | ||||
{ | { | ||||
// The player entity may also have a ResourceTrickle component | // The player entity may also have a ResourceTrickle component | ||||
let cmpPlayer = QueryOwnerInterface(this.entity) || Engine.QueryInterface(this.entity, IID_Player); | let cmpPlayer = QueryOwnerInterface(this.entity) || Engine.QueryInterface(this.entity, IID_Player); | ||||
if (!cmpPlayer) | if (!cmpPlayer) | ||||
return; | return; | ||||
cmpPlayer.AddResources(this.rates); | 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); | |||||
Done Inline Actions_ ._ _ , _ ._ (_ ' ( ` )_ .__) ( ( ( ) `) ) _) (__ (_ (_ . _) _) ,__) `~~`\ ' . /`~~` ; ; / \ _____________/_ __ \_____________ Freagarach: ```
_ ._ _ , _ ._
(_ ' ( ` )_ .__)
( ( ( ) `) ) _)
(__… | |||||
Done Inline Actionsbored? :D Silier: bored? :D | |||||
Done Inline Actionsbtw, cache result of this.GetTimers call Silier: btw, cache result of this.GetTimers call | |||||
Done Inline ActionsNope just me being silly with a useless return :D Stan: Nope just me being silly with a useless return :D | |||||
}; | |||||
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) | ResourceTrickle.prototype.OnValueModification = function(msg) | ||||
{ | { | ||||
if (msg.component != "ResourceTrickle") | if (msg.component != "ResourceTrickle") | ||||
return; | return; | ||||
this.ComputeRates(); | // No need to have a timer running when there is not trickle. | ||||
if (!this.ComputeRates()) | |||||
{ | |||||
this.CancelTimer(); | |||||
return; | |||||
} | |||||
Done Inline ActionsOne could do: if (this.ComputeRates()) // Should return hasTrickle then. this.StartTimer(); else this.CancelTimer(); or (if one doesn't want to call the function unnecessary): if (!this.ComputeRates() && this.timer) // this.ComputeRates() Should return hasTrickle then. this.CancelTimer(); else if (!this.timer) this.StartTimer(); ? Freagarach: One could do:
```
if (this.ComputeRates()) // Should return hasTrickle then.
this. | |||||
Done Inline ActionsWas missing a return anyway. Also if you call a function in a if condition it gets called everytime :D Stan: Was missing a return anyway.
Also if you call a function in a if condition it gets called… | |||||
PolakrityUnsubmitted Done Inline Actionsthis return isn't necessary as said Freagarach. Polakrity: this return isn't necessary as said Freagarach. | |||||
StanAuthorUnsubmitted Done Inline ActionsYes it is else you cancel a timer that shouldn't be there to start a new one :) Here it stops, and only resume on value modification. Stan: Yes it is else you cancel a timer that shouldn't be there to start a new one :) Here it stops… | |||||
PolakrityUnsubmitted Done Inline Actionsyou have early returns when you check if a timer exists in your StartTimer and StopTimer functions. Polakrity: you have early returns when you check if a timer exists in your `StartTimer` and `StopTimer`… | |||||
StanAuthorUnsubmitted Done Inline ActionsIf you cancel it this.timer is undefined, hereby making the starttimer code work :) Stan: If you cancel it this.timer is undefined, hereby making the starttimer code work :) | |||||
this.StartTimer(); | |||||
}; | }; | ||||
Done Inline ActionsIs this necessary? I usually don't see it with components with a timer? Freagarach: Is this necessary? I usually don't see it with components with a timer? | |||||
Done Inline ActionsActually if one does not do that, cmpTimer handles the cancelling. Stan: Actually if one does not do that, cmpTimer handles the cancelling. | |||||
Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle); | Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle); |
Not necessary to declare it on init, I guess?