Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Population.js
function Population() {} | function Population() {} | ||||
Population.prototype.Schema = | Population.prototype.Schema = | ||||
"<a:help>Specifies the Population cap increase generated by this entity.</a:help>" + | "<a:help>Specifies the Population properties of this entity.</a:help>" + | ||||
"<a:example>" + | "<a:example>" + | ||||
"<Cost>1</Cost>" + | |||||
"<Bonus>15</Bonus>" + | "<Bonus>15</Bonus>" + | ||||
"</a:example>" + | "</a:example>" + | ||||
"<oneOrMore>" + | |||||
"<choice>" + | |||||
"<element name='Cost' a:help='Population space this entity takes.'>" + | |||||
"<data type='nonNegativeInteger'/>" + | |||||
"</element>" + | |||||
"<element name='Bonus' a:help='Population cap increase while this entity exists.'>" + | "<element name='Bonus' a:help='Population cap increase while this entity exists.'>" + | ||||
"<data type='nonNegativeInteger'/>" + | "<data type='nonNegativeInteger'/>" + | ||||
"</element>"; | "</element>" + | ||||
"</choice>" + | |||||
"</oneOrMore>"; | |||||
Population.prototype.Init = function() | Population.prototype.Init = function() | ||||
{ | { | ||||
this.bonus = +this.template.Bonus; | |||||
}; | }; | ||||
/** | /** | ||||
* @return {number} - The population space provided by this entity. | * @return {number} - The population space provided by this entity. | ||||
*/ | */ | ||||
Population.prototype.GetPopBonus = function() | Population.prototype.Bonus = function() | ||||
{ | { | ||||
return this.bonus; | return this.bonus; | ||||
}; | }; | ||||
/** | |||||
* @return {number} - The population space taken by this entity. | |||||
*/ | |||||
Population.prototype.Cost = function() | |||||
{ | |||||
return this.cost; | |||||
}; | |||||
Population.prototype.RecalculateValues = function() | Population.prototype.RecalculateValues = function() | ||||
{ | { | ||||
this.bonus = Math.round(ApplyValueModificationsToEntity("Population/Bonus", +this.template.Bonus, this.entity)); | this.bonus = Math.round(ApplyValueModificationsToEntity("Population/Bonus", +(this.template.Bonus || 0), this.entity)); | ||||
this.cost = Math.round(ApplyValueModificationsToEntity("Population/Cost", +(this.template.Cost || 0), this.entity)); | |||||
}; | }; | ||||
Population.prototype.OnOwnershipChanged = function(msg) | Population.prototype.OnOwnershipChanged = function(msg) | ||||
{ | { | ||||
if (msg.from != INVALID_PLAYER) | if (msg.from != INVALID_PLAYER) | ||||
{ | { | ||||
let cmpPlayer = QueryPlayerIDInterface(msg.from); | let cmpPlayer = QueryPlayerIDInterface(msg.from); | ||||
if (cmpPlayer) | if (cmpPlayer) | ||||
{ | |||||
cmpPlayer.AddPopulation(-this.cost); | |||||
cmpPlayer.AddPopulationBonuses(-this.bonus); | cmpPlayer.AddPopulationBonuses(-this.bonus); | ||||
} | } | ||||
} | |||||
if (msg.to != INVALID_PLAYER) | if (msg.to != INVALID_PLAYER) | ||||
{ | { | ||||
this.RecalculateValues(); | this.RecalculateValues(); | ||||
let cmpPlayer = QueryPlayerIDInterface(msg.to); | let cmpPlayer = QueryPlayerIDInterface(msg.to); | ||||
if (cmpPlayer) | if (cmpPlayer) | ||||
{ | |||||
cmpPlayer.AddPopulation(this.cost); | |||||
cmpPlayer.AddPopulationBonuses(this.bonus); | cmpPlayer.AddPopulationBonuses(this.bonus); | ||||
} | } | ||||
} | |||||
}; | }; | ||||
Population.prototype.OnValueModification = function(msg) | Population.prototype.OnValueModification = function(msg) | ||||
{ | { | ||||
if (msg.component != "Population") | if (msg.component != "Population") | ||||
return; | return; | ||||
let cmpPlayer = QueryOwnerInterface(this.entity); | |||||
if (!cmpPlayer) | |||||
return; | |||||
let oldPopCost = this.cost; | |||||
this.cost = Math.round(ApplyValueModificationsToEntity("Population/Cost", +(this.template.Cost || 0), this.entity)); | |||||
let popCostDifference = this.cost - oldPopCost; | |||||
if (popCostDifference) | |||||
cmpPlayer.AddPopulation(popCostDifference); | |||||
// Foundations shouldn't give a pop bonus. | // Foundations shouldn't give a pop bonus. | ||||
if (Engine.QueryInterface(this.entity, IID_Foundation)) | if (Engine.QueryInterface(this.entity, IID_Foundation)) | ||||
return; | return; | ||||
let oldPopBonus = this.bonus; | let oldPopBonus = this.bonus; | ||||
this.RecalculateValues(); | this.bonus = Math.round(ApplyValueModificationsToEntity("Population/Bonus", +(this.template.Bonus || 0), this.entity)); | ||||
let popDifference = this.bonus - oldPopBonus; | |||||
if (!popDifference) | let popBonusDifference = this.bonus - oldPopBonus; | ||||
return; | if (popBonusDifference) | ||||
let cmpPlayer = QueryOwnerInterface(this.entity); | cmpPlayer.AddPopulationBonuses(popBonusDifference); | ||||
if (cmpPlayer) | |||||
cmpPlayer.AddPopulationBonuses(popDifference); | |||||
}; | }; | ||||
Engine.RegisterComponentType(IID_Population, "Population", Population); | Engine.RegisterComponentType(IID_Population, "Population", Population); |
Wildfire Games · Phabricator