Ticket #1853: Foundation.patch
File Foundation.patch, 4.5 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Foundation.js
12 12 // its obstruction once there's nothing in the way. 13 13 this.committed = false; 14 14 15 this.buildProgress = 0.0; // 0 <= progress <= 116 17 15 // Set up a timer so we can count the number of builders in a 1-second period. 18 16 // (We assume each builder only builds once per second, which is what UnitAI 19 17 // implements.) … … 37 35 38 36 Foundation.prototype.InitialiseConstruction = function(owner, template) 39 37 { 40 var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);41 this.addedHitpoints = cmpHealth.GetHitpoints();42 43 38 this.finalTemplateName = template; 44 39 45 40 // We need to know the owner in OnDestroy, but at that point the entity has already been … … 54 49 this.initialised = true; 55 50 }; 56 51 52 /** 53 * Moving the revelation logic from Build to here makes the building sink if 54 * it is attacked. 55 */ 56 Foundation.prototype.OnHealthChanged = function(msg) 57 { 58 // Gradually reveal the final building preview 59 var cmpPreviewVisual = Engine.QueryInterface(this.previewEntity, IID_Visual); 60 if (cmpPreviewVisual) 61 cmpPreviewVisual.SetConstructionProgress(this.GetBuildProgress()); 62 63 Engine.PostMessage(this.entity, MT_FoundationProgressChanged, { "to": this.GetBuildPercentage() }); 64 }; 65 66 /** 67 * Returns the current build progress in a [0,1] range. 68 */ 69 Foundation.prototype.GetBuildProgress = function() 70 { 71 var cmpHealth = Engine.QueryInterface(this.entity, IID_Health) 72 var hitpoints = cmpHealth.GetHitpoints(); 73 var maxHitpoints = cmpHealth.GetMaxHitpoints(); 74 75 return (hitpoints / maxHitpoints); 76 }; 77 57 78 Foundation.prototype.GetBuildPercentage = function() 58 79 { 59 return Math.floor(this. buildProgress* 100);80 return Math.floor(this.GetBuildProgress() * 100); 60 81 }; 61 82 62 83 Foundation.prototype.IsFinished = function() 63 84 { 64 return (this. buildProgress >= 1.0);85 return (this.GetBuildProgress() == 1.0); 65 86 }; 66 87 67 88 Foundation.prototype.OnDestroy = function() … … 77 98 this.previewEntity = INVALID_ENTITY; 78 99 } 79 100 80 if (this. buildProgress == 1.0)101 if (this.IsFinished()) 81 102 return; 82 103 83 104 var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); … … 85 106 86 107 for (var r in this.costs) 87 108 { 88 var scaled = Math.floor(this.costs[r] * (1.0 - this. buildProgress));109 var scaled = Math.floor(this.costs[r] * (1.0 - this.GetBuildProgress())); 89 110 if (scaled) 90 111 { 91 112 cmpPlayer.AddResource(r, scaled); … … 115 136 this.numRecentBuilders = this.recentBuilders.length; 116 137 this.SetBuildMultiplier(); 117 138 } 118 } 139 }; 119 140 120 141 /** 121 142 * Sets the build rate multiplier, which is applied to all builders. … … 124 145 { 125 146 // Yields a total rate of construction equal to numRecentBuilders^0.7 126 147 this.buildMultiplier = Math.pow(this.numRecentBuilders, 0.7) / this.numRecentBuilders; 127 } 148 }; 128 149 129 150 /** 130 151 * Perform some number of seconds of construction work. … … 135 156 // Do nothing if we've already finished building 136 157 // (The entity will be destroyed soon after completion so 137 158 // this won't happen much) 138 if (this. buildProgress== 1.0)159 if (this.GetBuildProgress() == 1.0) 139 160 return; 140 161 141 162 // Handle the initial 'committing' of the foundation 142 163 if (!this.committed) 143 164 { … … 223 244 // Record this builder so we can count the total number 224 245 this.AddBuilder(builderEnt); 225 246 226 this.buildProgress += amount * this.buildMultiplier;227 if (this.buildProgress > 1.0)228 this.buildProgress = 1.0;229 230 Engine.PostMessage(this.entity, MT_FoundationProgressChanged, { "to": this.GetBuildPercentage() });231 232 // Gradually reveal the final building preview233 var cmpPreviewVisual = Engine.QueryInterface(this.previewEntity, IID_Visual);234 if (cmpPreviewVisual)235 cmpPreviewVisual.SetConstructionProgress(this.buildProgress);236 237 247 // Add an appropriate proportion of hitpoints 238 248 var cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 239 249 var maxHealth = cmpHealth.GetMaxHitpoints(); 240 var targetHP = Math.max(0, Math.min(maxHealth, Math.floor(maxHealth * this.buildProgress))); 241 var deltaHP = targetHP - this.addedHitpoints; 250 var deltaHP = Math.max(0, Math.min(maxHealth, Math.floor(maxHealth * amount))); 242 251 if (deltaHP > 0) 243 252 { 244 253 cmpHealth.Increase(deltaHP); 245 this.addedHitpoints += deltaHP;246 254 } 247 255 248 if (this. buildProgress>= 1.0)256 if (this.GetBuildProgress() >= 1.0) 249 257 { 250 258 // Finished construction 251 259