Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/ResourceGatherer.js
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Used to instantly give resources to unit | * Used to instantly give resources to unit | ||||
* @param resources The same structure as returned form GetCarryingStatus | * @param resources The same structure as returned form GetCarryingStatus | ||||
*/ | */ | ||||
ResourceGatherer.prototype.GiveResources = function(resources) | ResourceGatherer.prototype.GiveResources = function(resources) | ||||
{ | { | ||||
for (let resource of resources) | for (let resource of resources) | ||||
this.carrying[resource.type] = +resource.amount; | this.carrying[resource.type] = +resource.amount; | ||||
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() }); | |||||
}; | }; | ||||
/** | /** | ||||
* Returns the generic type of one particular resource this unit is | * Returns the generic type of one particular resource this unit is | ||||
* currently carrying, or undefined if none. | * currently carrying, or undefined if none. | ||||
*/ | */ | ||||
ResourceGatherer.prototype.GetMainCarryingType = function() | ResourceGatherer.prototype.GetMainCarryingType = function() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | ResourceGatherer.prototype.PerformGather = function(target) | ||||
// Update stats of how much the player collected. | // Update stats of how much the player collected. | ||||
// (We have to do it here rather than at the dropsite, because we | // (We have to do it here rather than at the dropsite, because we | ||||
// need to know what subtype it was) | // need to know what subtype it was) | ||||
let cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); | let cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); | ||||
if (cmpStatisticsTracker) | if (cmpStatisticsTracker) | ||||
cmpStatisticsTracker.IncreaseResourceGatheredCounter(type.generic, status.amount, type.specific); | cmpStatisticsTracker.IncreaseResourceGatheredCounter(type.generic, status.amount, type.specific); | ||||
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() }); | |||||
return { | return { | ||||
"amount": status.amount, | "amount": status.amount, | ||||
"exhausted": status.exhausted, | "exhausted": status.exhausted, | ||||
"filled": this.carrying[type.generic] >= this.GetCapacity(type.generic) | "filled": this.carrying[type.generic] >= this.GetCapacity(type.generic) | ||||
}; | }; | ||||
}; | }; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
ResourceGatherer.prototype.CommitResources = function(target) | ResourceGatherer.prototype.CommitResources = function(target) | ||||
{ | { | ||||
let cmpResourceDropsite = Engine.QueryInterface(target, IID_ResourceDropsite); | let cmpResourceDropsite = Engine.QueryInterface(target, IID_ResourceDropsite); | ||||
if (!cmpResourceDropsite) | if (!cmpResourceDropsite) | ||||
return; | return; | ||||
let change = cmpResourceDropsite.ReceiveResources(this.carrying, this.entity); | let change = cmpResourceDropsite.ReceiveResources(this.carrying, this.entity); | ||||
let changed = false; | |||||
for (let type in change) | for (let type in change) | ||||
{ | { | ||||
this.carrying[type] -= change[type]; | this.carrying[type] -= change[type]; | ||||
if (this.carrying[type] == 0) | if (this.carrying[type] == 0) | ||||
delete this.carrying[type]; | delete this.carrying[type]; | ||||
changed = true; | |||||
} | } | ||||
if (changed) | |||||
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() }); | |||||
}; | }; | ||||
/** | /** | ||||
* Drop all currently-carried resources. | * Drop all currently-carried resources. | ||||
* (Currently they just vanish after being dropped - we don't bother depositing | * (Currently they just vanish after being dropped - we don't bother depositing | ||||
* them onto the ground.) | * them onto the ground.) | ||||
*/ | */ | ||||
ResourceGatherer.prototype.DropResources = function() | ResourceGatherer.prototype.DropResources = function() | ||||
{ | { | ||||
this.carrying = {}; | this.carrying = {}; | ||||
Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() }); | |||||
}; | }; | ||||
/** | /** | ||||
* @param {string} type - A generic resource type. | * @param {string} type - A generic resource type. | ||||
*/ | */ | ||||
ResourceGatherer.prototype.AddToPlayerCounter = function(type) | ResourceGatherer.prototype.AddToPlayerCounter = function(type) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator