Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Upkeep.js
- This file was copied from binaries/data/mods/public/simulation/components/ResourceTrickle.js.
function ResourceTrickle() {} | function Upkeep() {} | ||||
ResourceTrickle.prototype.Schema = | Upkeep.prototype.Schema = | ||||
"<a:help>Controls the resource trickle ability of the unit.</a:help>" + | "<a:help>Controls the resource upkeep of an entity.</a:help>" + | ||||
"<element name='Rates' a:help='Trickle Rates'>" + | "<element name='Rates' a:help='Upkeep 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 lose the next upkeep.'>" + | ||||
"<ref name='nonNegativeDecimal'/>" + | "<ref name='nonNegativeDecimal'/>" + | ||||
"</element>"; | "</element>"; | ||||
ResourceTrickle.prototype.Init = function() | Upkeep.prototype.Init = function() | ||||
{ | { | ||||
this.trickleInterval = +this.template.Interval; | this.upkeepInterval = +this.template.Interval; | ||||
this.CheckTimer(); | this.CheckTimer(); | ||||
}; | }; | ||||
ResourceTrickle.prototype.GetInterval = function() | /** | ||||
* @return {number} - The interval between resource subtractions, in ms. | |||||
*/ | |||||
Upkeep.prototype.GetInterval = function() | |||||
{ | { | ||||
return this.trickleInterval; | return this.upkeepInterval; | ||||
}; | }; | ||||
ResourceTrickle.prototype.GetRates = function() | /** | ||||
* @return {Object} - The upkeep rates in the form of { "resourceName": {number} }. | |||||
*/ | |||||
Upkeep.prototype.GetRates = function() | |||||
{ | { | ||||
return this.rates; | return this.rates; | ||||
}; | }; | ||||
/** | /** | ||||
* @return {boolean} - Whether this entity has at least one non-zero trickle rate. | * @return {boolean} - Whether this entity has at least one non-zero trickle rate. | ||||
*/ | */ | ||||
ResourceTrickle.prototype.ComputeRates = function() | Upkeep.prototype.ComputeRates = function() | ||||
{ | { | ||||
this.rates = {}; | this.rates = {}; | ||||
let hasTrickle = false; | let hasUpkeep = false; | ||||
for (let resource in this.template.Rates) | for (let resource in this.template.Rates) | ||||
{ | { | ||||
let rate = ApplyValueModificationsToEntity("ResourceTrickle/Rates/" + resource, +this.template.Rates[resource], this.entity); | let rate = ApplyValueModificationsToEntity("Upkeep/Rates/" + resource, +this.template.Rates[resource], this.entity); | ||||
if (rate) | if (rate) | ||||
{ | { | ||||
this.rates[resource] = rate; | this.rates[resource] = rate; | ||||
hasTrickle = true; | hasUpkeep = true; | ||||
} | } | ||||
} | } | ||||
return hasTrickle; | return hasUpkeep; | ||||
}; | }; | ||||
ResourceTrickle.prototype.Trickle = function(data, lateness) | /** | ||||
* Try to subtract the needed resources. | |||||
* Data and lateness are unused. | |||||
*/ | |||||
Upkeep.prototype.Trickle = function(data, lateness) | |||||
Silier: probably should have different name | |||||
{ | { | ||||
// The player entity may also have a ResourceTrickle component | let cmpPlayer = QueryOwnerInterface(this.entity); | ||||
let cmpPlayer = QueryOwnerInterface(this.entity) || Engine.QueryInterface(this.entity, IID_Player); | |||||
if (!cmpPlayer) | if (!cmpPlayer) | ||||
return; | return; | ||||
cmpPlayer.AddResources(this.rates); | if (!cmpPlayer.TrySubtractResources(this.rates)) | ||||
this.HandleInsufficientUpkeep(); | |||||
Done Inline ActionsCould pass a boolean the two functions are very similar. Stan: Could pass a boolean the two functions are very similar. | |||||
Done Inline ActionsThis is more explicit. Freagarach: This is more explicit. | |||||
}; | }; | ||||
ResourceTrickle.prototype.OnValueModification = function(msg) | /** | ||||
* E.g. take a hitpoint, reduce CP. | |||||
*/ | |||||
Upkeep.prototype.HandleInsufficientUpkeep = function() | |||||
{ | |||||
}; | |||||
Upkeep.prototype.OnValueModification = function(msg) | |||||
{ | { | ||||
if (msg.component != "ResourceTrickle") | if (msg.component != "Upkeep") | ||||
return; | return; | ||||
this.CheckTimer(); | this.CheckTimer(); | ||||
}; | }; | ||||
ResourceTrickle.prototype.CheckTimer = function() | /** | ||||
* Recalculate the interval and update the timer accordingly. | |||||
*/ | |||||
Upkeep.prototype.CheckTimer = function() | |||||
{ | { | ||||
if (!this.ComputeRates()) | if (!this.ComputeRates()) | ||||
{ | { | ||||
if (!this.timer) | if (!this.timer) | ||||
return; | return; | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
cmpTimer.CancelTimer(this.timer); | cmpTimer.CancelTimer(this.timer); | ||||
delete this.timer; | delete this.timer; | ||||
return; | return; | ||||
} | } | ||||
let oldTrickleInterval = this.trickleInterval; | let oldUpkeepInterval = this.upkeepInterval; | ||||
this.trickleInterval = ApplyValueModificationsToEntity("ResourceTrickle/Interval", +this.template.Interval, this.entity); | this.upkeepInterval = ApplyValueModificationsToEntity("Upkeep/Interval", +this.template.Interval, this.entity); | ||||
if (this.trickleInterval < 0) | if (this.upkeepInterval < 0) | ||||
{ | { | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
cmpTimer.CancelTimer(this.timer); | cmpTimer.CancelTimer(this.timer); | ||||
delete this.timer; | delete this.timer; | ||||
return; | return; | ||||
} | } | ||||
if (this.timer) | if (this.timer) | ||||
{ | { | ||||
if (this.trickleInterval == oldTrickleInterval) | if (this.upkeepInterval == oldUpkeepInterval) | ||||
return; | return; | ||||
// If the timer wasn't invalidated before (interval <= 0), just update it. | // If the timer wasn't invalidated before (interval <= 0), just update it. | ||||
if (oldTrickleInterval > 0) | if (oldUpkeepInterval > 0) | ||||
{ | { | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
cmpTimer.UpdateRepeatTime(this.timer, this.trickleInterval); | cmpTimer.UpdateRepeatTime(this.timer, this.upkeepInterval); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
this.timer = cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.trickleInterval, this.trickleInterval, undefined); | this.timer = cmpTimer.SetInterval(this.entity, IID_Upkeep, "Trickle", this.upkeepInterval, this.upkeepInterval, undefined); | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle); | Engine.RegisterComponentType(IID_Upkeep, "Upkeep", Upkeep); |
Wildfire Games · Phabricator
probably should have different name