Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/ResourceGatherer.js
Show First 20 Lines • Show All 69 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 191 Lines • ▼ Show 20 Lines | ResourceGatherer.prototype.PerformGather = function(data, lateness) | ||||
// 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() }); | |||||
if (!this.CanCarryMore(type.generic)) | if (!this.CanCarryMore(type.generic)) | ||||
this.StopGathering("InventoryFilled"); | this.StopGathering("InventoryFilled"); | ||||
else if (status.exhausted) | else if (status.exhausted) | ||||
this.StopGathering("TargetInvalidated"); | this.StopGathering("TargetInvalidated"); | ||||
}; | }; | ||||
/** | /** | ||||
* Compute the amount of resources collected per second from the target. | * Compute the amount of resources collected per second from the target. | ||||
▲ Show 20 Lines • Show All 97 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() }); | |||||
}; | }; | ||||
/** | /** | ||||
* @return {string} - A generic resource type if we were tasked to gather. | * @return {string} - A generic resource type if we were tasked to gather. | ||||
*/ | */ | ||||
ResourceGatherer.prototype.GetTaskedResourceType = function() | ResourceGatherer.prototype.GetTaskedResourceType = function() | ||||
{ | { | ||||
return this.taskedResourceType; | return this.taskedResourceType; | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator