Line data Source code
1 : function Population() {} 2 : 3 1 : Population.prototype.Schema = 4 : "<a:help>Specifies the Population cap increase generated by this entity.</a:help>" + 5 : "<a:example>" + 6 : "<Bonus>15</Bonus>" + 7 : "</a:example>" + 8 : "<element name='Bonus' a:help='Population cap increase while this entity exists.'>" + 9 : "<data type='nonNegativeInteger'/>" + 10 : "</element>"; 11 : 12 1 : Population.prototype.Init = function() 13 : { 14 1 : this.bonus = +this.template.Bonus; 15 : }; 16 : 17 : /** 18 : * @return {number} - The population space provided by this entity. 19 : */ 20 1 : Population.prototype.GetPopBonus = function() 21 : { 22 0 : return this.bonus; 23 : }; 24 : 25 1 : Population.prototype.RecalculateValues = function() 26 : { 27 6 : this.bonus = Math.round(ApplyValueModificationsToEntity("Population/Bonus", +this.template.Bonus, this.entity)); 28 : }; 29 : 30 1 : Population.prototype.OnOwnershipChanged = function(msg) 31 : { 32 3 : if (msg.from != INVALID_PLAYER) 33 : { 34 1 : let cmpPlayer = QueryPlayerIDInterface(msg.from); 35 1 : if (cmpPlayer) 36 1 : cmpPlayer.AddPopulationBonuses(-this.bonus); 37 : } 38 3 : if (msg.to != INVALID_PLAYER) 39 : { 40 2 : this.RecalculateValues(); 41 2 : let cmpPlayer = QueryPlayerIDInterface(msg.to); 42 2 : if (cmpPlayer) 43 2 : cmpPlayer.AddPopulationBonuses(this.bonus); 44 : } 45 : }; 46 : 47 1 : Population.prototype.OnValueModification = function(msg) 48 : { 49 6 : if (msg.component != "Population") 50 1 : return; 51 : 52 : // Foundations shouldn't give a pop bonus. 53 5 : if (Engine.QueryInterface(this.entity, IID_Foundation)) 54 1 : return; 55 : 56 4 : let oldPopBonus = this.bonus; 57 4 : this.RecalculateValues(); 58 4 : let popDifference = this.bonus - oldPopBonus; 59 : 60 4 : if (!popDifference) 61 1 : return; 62 3 : let cmpPlayer = QueryOwnerInterface(this.entity); 63 3 : if (cmpPlayer) 64 3 : cmpPlayer.AddPopulationBonuses(popDifference); 65 : }; 66 : 67 1 : Engine.RegisterComponentType(IID_Population, "Population", Population);