Ticket #3707: t3707_repair_cost2.diff
File t3707_repair_cost2.diff, 2.5 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Repairable.js
1 1 function Repairable() {} 2 2 3 var g_CostMultiplier = 0.5 4 3 5 Repairable.prototype.Schema = 4 6 "<a:help>Deals with repairable structures and units.</a:help>" + 5 7 "<a:example>" + … … 13 15 { 14 16 this.builders = []; // builder entities 15 17 this.buildMultiplier = 1; // Multiplier for the amount of work builders do. 18 this.costsToPay = { "food": 0, "wood": 0, "metal": 0, "stone": 0 }; 16 19 this.repairTimeRatio = +this.template.RepairTimeRatio; 17 20 }; 18 21 … … 50 53 this.buildMultiplier = Math.pow(numBuilders, 0.7) / numBuilders; 51 54 }; 52 55 53 // TODO: should we have resource costs?54 56 Repairable.prototype.Repair = function(builderEnt, rate) 55 57 { 56 58 let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); 57 59 let cmpCost = Engine.QueryInterface(this.entity, IID_Cost); 58 if (!cmpHealth || !cmpCost) 60 let cmpPlayer = QueryOwnerInterface(this.entity); 61 if (!cmpHealth || !cmpCost || !cmpPlayer) 59 62 return; 60 63 let damage = cmpHealth.GetMaxHitpoints() - cmpHealth.GetHitpoints(); 61 64 if (damage <= 0) … … 64 67 // Calculate the amount of hitpoints that will be added (using diminishing rate when several builders) 65 68 let work = rate * this.buildMultiplier * this.GetRepairRate(); 66 69 let amount = Math.min(damage, work); 67 cmpHealth.Increase(amount);68 70 69 // If we repaired all the damage, send a message to entities to stop repairing this building 70 if (amount >= damage) 71 // Calculate resource costs for the added hitpoints 72 let newPrice = cmpCost.GetResourceCosts(); 73 let resourcesToSubtract = {}; 74 for (let type in newPrice) 75 { 76 this.costsToPay[type] += g_CostMultiplier * newPrice[type] * amount / cmpHealth.GetMaxHitpoints(); 77 resourcesToSubtract[type] = Math.floor(this.costsToPay[type]); 78 this.costsToPay[type] -= resourcesToSubtract[type]; 79 } 80 81 // TrySubtractResources should report error to player (if they ran out of resources) 82 let affordable = cmpPlayer.TrySubtractResources(resourcesToSubtract) 83 if (affordable) 84 cmpHealth.Increase(amount); 85 86 // If we repaired all the damage or ran out of resource, send a message to entities to stop repairing this building 87 if (amount >= damage || !affordable) 71 88 Engine.PostMessage(this.entity, MT_ConstructionFinished, { "entity": this.entity, "newentity": this.entity }); 72 89 }; 73 90