Line data Source code
1 : class AutoBuildable 2 : { 3 : Init() 4 : { 5 3 : this.UpdateRate(); 6 : } 7 : 8 : /** 9 : * @return {number} - The rate with technologies and aura modification applied. 10 : */ 11 : GetRate() 12 : { 13 2 : return this.rate; 14 : } 15 : 16 : UpdateRate() 17 : { 18 3 : this.rate = ApplyValueModificationsToEntity("AutoBuildable/Rate", +this.template.Rate, this.entity); 19 3 : if (this.rate) 20 2 : this.StartTimer(); 21 : } 22 : 23 : StartTimer() 24 : { 25 2 : if (this.timer || !this.rate) 26 0 : return; 27 : 28 2 : let cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation); 29 2 : if (!cmpFoundation) 30 1 : return; 31 : 32 1 : cmpFoundation.AddBuilder(this.entity); 33 1 : let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 34 1 : this.timer = cmpTimer.SetInterval(this.entity, IID_AutoBuildable, "AutoBuild", 0, 1000, undefined); 35 : } 36 : 37 : CancelTimer() 38 : { 39 1 : if (!this.timer) 40 0 : return; 41 : 42 1 : let cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation); 43 1 : if (cmpFoundation) 44 1 : cmpFoundation.RemoveBuilder(this.entity); 45 : 46 1 : let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 47 1 : cmpTimer.CancelTimer(this.timer); 48 1 : delete this.timer; 49 : } 50 : 51 : AutoBuild() 52 : { 53 9 : if (!this.rate) 54 : { 55 0 : this.CancelTimer(); 56 0 : return; 57 : } 58 9 : let cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation); 59 9 : if (!cmpFoundation) 60 : { 61 0 : this.CancelTimer(); 62 0 : return; 63 : } 64 : 65 9 : cmpFoundation.Build(this.entity, this.rate); 66 : } 67 : 68 : OnValueModification(msg) 69 : { 70 0 : if (msg.component != "AutoBuildable") 71 0 : return; 72 : 73 0 : this.UpdateRate(); 74 : } 75 : 76 : OnOwnershipChanged(msg) 77 : { 78 0 : if (msg.to == INVALID_PLAYER) 79 0 : return; 80 : 81 0 : this.UpdateRate(); 82 : } 83 : } 84 : 85 2 : AutoBuildable.prototype.Schema = 86 : "<a:help>Defines whether the entity can be built by itself.</a:help>" + 87 : "<a:example>" + 88 : "<Rate>1.0</Rate>" + 89 : "</a:example>" + 90 : "<element name='Rate' a:help='The rate at which the building autobuilds.'>" + 91 : "<ref name='nonNegativeDecimal'/>" + 92 : "</element>"; 93 : 94 2 : Engine.RegisterComponentType(IID_AutoBuildable, "AutoBuildable", AutoBuildable);