Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Foundation.js
Show First 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | Foundation.prototype.GetBuildProgress = function() | ||||
return hitpoints / maxHitpoints; | return hitpoints / maxHitpoints; | ||||
}; | }; | ||||
Foundation.prototype.GetBuildPercentage = function() | Foundation.prototype.GetBuildPercentage = function() | ||||
{ | { | ||||
return Math.floor(this.GetBuildProgress() * 100); | return Math.floor(this.GetBuildProgress() * 100); | ||||
}; | }; | ||||
/** | |||||
* Returns the current builders. | |||||
* | |||||
* @return {number[]} - An array containing the entity IDs of assigned builders. | |||||
*/ | |||||
Foundation.prototype.GetBuilders = function() | |||||
{ | |||||
return Array.from(this.builders.keys()); | |||||
}; | |||||
Foundation.prototype.GetNumBuilders = function() | Foundation.prototype.GetNumBuilders = function() | ||||
{ | { | ||||
return this.builders.size; | return this.builders.size; | ||||
}; | }; | ||||
Foundation.prototype.IsFinished = function() | Foundation.prototype.IsFinished = function() | ||||
{ | { | ||||
return (this.GetBuildProgress() == 1.0); | return (this.GetBuildProgress() == 1.0); | ||||
Show All 26 Lines | if (scaled) | ||||
var cmpStatisticsTracker = QueryPlayerIDInterface(this.owner, IID_StatisticsTracker); | var cmpStatisticsTracker = QueryPlayerIDInterface(this.owner, IID_StatisticsTracker); | ||||
if (cmpStatisticsTracker) | if (cmpStatisticsTracker) | ||||
cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -scaled); | cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -scaled); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Adds a builder to the counter. | * Adds an array of builders. | ||||
* | |||||
* @param {number[]} builders - An array containing the entity IDs of builders to assign. | |||||
*/ | */ | ||||
Foundation.prototype.AddBuilder = function(builderEnt) | Foundation.prototype.AddBuilders = function(builders) | ||||
{ | |||||
let changed = false; | |||||
for (let builder of builders) | |||||
if (this.AddSingleBuilder(builder)) | |||||
Silier: just wondering if `changed = changed && this.AddSingleBuilder(builder)` would not be better… | |||||
FreagarachAuthorUnsubmitted Done Inline ActionsIt won't ever become true then, right? Freagarach: It won't ever become `true` then, right? | |||||
SilierUnsubmitted Done Inline Actionsyeah, my bad :) || is correct Silier: yeah, my bad :) || is correct | |||||
FreagarachAuthorUnsubmitted Done Inline ActionsOh, sorry my comment was not supposed to be placed ;) Freagarach: Oh, sorry my comment was not supposed to be placed ;) | |||||
changed = true; | |||||
if (changed) | |||||
this.HandleBuildersChanged(); | |||||
}; | |||||
/** | |||||
Done Inline Actionsremove one but Silier: remove one but | |||||
* Adds a single builder to this entity. | |||||
* Split because adding multiple builders can reuse the functionality | |||||
SilierUnsubmitted Done Inline Actionsideally this should not be here, but instead inform that function calling this needs to send message, or something in way that this one is not sending the message Silier: ideally this should not be here, but instead inform that function calling this needs to send… | |||||
FreagarachAuthorUnsubmitted Done Inline ActionsThe message wasn't sent here anymore anyway :) Freagarach: The message wasn't sent here anymore anyway :) | |||||
* but still send one message. | |||||
* | |||||
* @param {number} builderEnt - The entity to add. | |||||
* @return {boolean} - Whether the addition was successful. | |||||
*/ | |||||
Foundation.prototype.AddSingleBuilder = function(builderEnt) | |||||
SilierUnsubmitted Done Inline Actionsjust thinking maybe AddBuilderHelper ? Silier: just thinking maybe AddBuilderHelper ? | |||||
FreagarachAuthorUnsubmitted Done Inline ActionsSounds wonky but still better than AddSingleBuilder ;) Freagarach: Sounds wonky but still better than `AddSingleBuilder` ;) | |||||
{ | { | ||||
if (this.builders.has(builderEnt)) | if (this.builders.has(builderEnt)) | ||||
return; | return false; | ||||
this.builders.set(builderEnt, Engine.QueryInterface(builderEnt, IID_Builder).GetRate()); | let buildRate = Engine.QueryInterface(builderEnt, IID_Builder).GetRate(); | ||||
Done Inline Actionswhile here, reuse Engine.QueryInterface(builderEnt, IID_Builder).GetRate() Silier: while here, reuse Engine.QueryInterface(builderEnt, IID_Builder).GetRate() | |||||
this.totalBuilderRate += this.builders.get(builderEnt); | this.builders.set(builderEnt, buildRate); | ||||
this.SetBuildMultiplier(); | this.totalBuilderRate += buildRate; | ||||
Done Inline Actionsthis can be computed once per inserting too, move it to functions calling this one pls Silier: this can be computed once per inserting too, move it to functions calling this one pls | |||||
let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | return true; | ||||
if (cmpVisual) | } | ||||
cmpVisual.SetVariable("numbuilders", this.builders.size); | |||||
Engine.PostMessage(this.entity, MT_FoundationBuildersChanged, { "to": Array.from(this.builders.keys()) }); | /** | ||||
* Adds a builder to the counter. | |||||
* | |||||
* @param {number} builderEnt - The entity to add. | |||||
Done Inline ActionsDocumentation for the param? Stan: Documentation for the param? | |||||
*/ | |||||
Foundation.prototype.AddBuilder = function(builderEnt) | |||||
Done Inline Actionsbetter to return true/false and send message below only when true Silier: better to return true/false and send message below only when true | |||||
{ | |||||
if (this.AddSingleBuilder(builderEnt)) | |||||
this.HandleBuildersChanged(); | |||||
}; | }; | ||||
Foundation.prototype.RemoveBuilder = function(builderEnt) | Foundation.prototype.RemoveBuilder = function(builderEnt) | ||||
{ | { | ||||
if (!this.builders.has(builderEnt)) | if (!this.builders.has(builderEnt)) | ||||
return; | return; | ||||
this.totalBuilderRate -= this.builders.get(builderEnt); | this.totalBuilderRate -= this.builders.get(builderEnt); | ||||
this.builders.delete(builderEnt); | this.builders.delete(builderEnt); | ||||
this.HandleBuildersChanged(); | |||||
}; | |||||
/** | |||||
* This has to be called whenever the number of builders change. | |||||
*/ | |||||
Foundation.prototype.HandleBuildersChanged = function() | |||||
{ | |||||
this.SetBuildMultiplier(); | this.SetBuildMultiplier(); | ||||
let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | ||||
if (cmpVisual) | if (cmpVisual) | ||||
cmpVisual.SetVariable("numbuilders", this.builders.size); | cmpVisual.SetVariable("numbuilders", this.builders.size); | ||||
Engine.PostMessage(this.entity, MT_FoundationBuildersChanged, { "to": Array.from(this.builders.keys()) }); | Engine.PostMessage(this.entity, MT_FoundationBuildersChanged, { "to": this.GetBuilders() }); | ||||
}; | } | ||||
/** | /** | ||||
* The build multiplier is a penalty that is applied to each builder. | * The build multiplier is a penalty that is applied to each builder. | ||||
* For example, ten women build at a combined rate of 10^0.7 = 5.01 instead of 10. | * For example, ten women build at a combined rate of 10^0.7 = 5.01 instead of 10. | ||||
*/ | */ | ||||
Foundation.prototype.CalculateBuildMultiplier = function(num) | Foundation.prototype.CalculateBuildMultiplier = function(num) | ||||
{ | { | ||||
// Avoid division by zero, in particular 0/0 = NaN which isn't reliably serialized | // Avoid division by zero, in particular 0/0 = NaN which isn't reliably serialized | ||||
▲ Show 20 Lines • Show All 265 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
just wondering if changed = changed && this.AddSingleBuilder(builder) would not be better than if here