| 1 | function ResourceTrickle() {} |
| 2 | |
| 3 | ResourceTrickle.prototype.Schema = |
| 4 | "<a:help>Controls the resource trickle ability of the unit.</a:help>" + |
| 5 | "<element name='FoodRate' a:help='Food given to the player every interval'>" + |
| 6 | "<ref name='nonNegativeDecimal'/>" + |
| 7 | "</element>" + |
| 8 | "<element name='WoodRate' a:help='Wood given to the player every interval'>" + |
| 9 | "<ref name='nonNegativeDecimal'/>" + |
| 10 | "</element>" + |
| 11 | "<element name='StoneRate' a:help='Stone given to the player every interval'>" + |
| 12 | "<ref name='nonNegativeDecimal'/>" + |
| 13 | "</element>" + |
| 14 | "<element name='MetalRate' a:help='Metal given to the player every interval'>" + |
| 15 | "<ref name='nonNegativeDecimal'/>" + |
| 16 | "</element>" + |
| 17 | "<element name='Interval' a:help='Number of miliseconds must pass for the player to gain the next trickle.'>" + |
| 18 | "<ref name='nonNegativeDecimal'/>" + |
| 19 | "</element>"; |
| 20 | |
| 21 | ResourceTrickle.prototype.Init = function() |
| 22 | { |
| 23 | // We can't add float values to player's resources. So we put the reminder of the float value here. |
| 24 | // For example, if our food rate is 1.5, in the first interval it gives 1 point of food to the player |
| 25 | // and puts the rest (0.5) here. In the next interval, the value here is added to the rate, which means |
| 26 | // 0.5 + 1.5 = 2.0 . So 2 points of food is added to the player and the rest (0.0) is put here and so on. |
| 27 | this.store = { "food": 0.0, "wood": 0.0, "stone": 0.0, "metal": 0.0 }; |
| 28 | |
| 29 | // Call the timer |
| 30 | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); |
| 31 | cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.GetTimer(), this.GetTimer(), undefined) |
| 32 | }; |
| 33 | |
| 34 | ResourceTrickle.prototype.GetTimer = function() |
| 35 | { |
| 36 | var interval = +this.template.Interval; |
| 37 | |
| 38 | interval = ApplyValueModificationsToEntity("ResourceTrickle/Interval", interval, this.entity); |
| 39 | |
| 40 | return interval; |
| 41 | }; |
| 42 | |
| 43 | ResourceTrickle.prototype.GetRates = function() |
| 44 | { |
| 45 | var foodrate = +this.template.FoodRate; |
| 46 | var woodrate = +this.template.WoodRate; |
| 47 | var stonerate = +this.template.StoneRate; |
| 48 | var metalrate = +this.template.MetalRate; |
| 49 | |
| 50 | foodrate = ApplyValueModificationsToEntity("ResourceTrickle/FoodRate", foodrate, this.entity); |
| 51 | woodrate = ApplyValueModificationsToEntity("ResourceTrickle/WoodRate", woodrate, this.entity); |
| 52 | stonerate = ApplyValueModificationsToEntity("ResourceTrickle/StoneRate", stonerate, this.entity); |
| 53 | metalrate = ApplyValueModificationsToEntity("ResourceTrickle/MetalRate", metalrate, this.entity); |
| 54 | |
| 55 | return [ { "type": "food", "rate": foodrate }, |
| 56 | { "type": "wood", "rate": woodrate }, |
| 57 | { "type": "stone", "rate": stonerate }, |
| 58 | { "type": "metal", "rate": metalrate } ]; |
| 59 | }; |
| 60 | |
| 61 | // Do the actual work here |
| 62 | ResourceTrickle.prototype.Trickle = function(data, lateness) |
| 63 | { |
| 64 | // Get the rates |
| 65 | var rates = this.GetRates(); |
| 66 | |
| 67 | // Get the player |
| 68 | var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); |
| 69 | |
| 70 | for each (var resource in rates) |
| 71 | { |
| 72 | // Always add an integer to the player's treasury. Explained fully in the init() function |
| 73 | this.store[resource.type] += resource.rate; |
| 74 | if (this.store[resource.type] >= 1.0) |
| 75 | { |
| 76 | var commit = Math.floor(this.store[resource.type]); // The ammount that is going to be committed to the player |
| 77 | cmpPlayer.AddResource(resource.type, commit); |
| 78 | this.store[resource.type] -= commit; // Store the reminder |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | }; |
| 83 | |
| 84 | Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle); |