Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Loot.js
function Loot() {} | function Loot() {} | ||||
Loot.prototype.Schema = | Loot.prototype.Schema = | ||||
"<a:help>Specifies the loot credited when this entity is killed.</a:help>" + | "<a:help>Specifies the loot credited when this entity is killed.</a:help>" + | ||||
"<a:example>" + | "<a:example>" + | ||||
"<xp>35</xp>" + | "<xp>35</xp>" + | ||||
"<metal>10</metal>" + | "<metal>10</metal>" + | ||||
"</a:example>" + | "</a:example>" + | ||||
Resources.BuildSchema("nonNegativeInteger", ["xp"]); | Resources.BuildSchema("nonNegativeInteger", ["xp"]); | ||||
Loot.prototype.Serialize = null; // we have no dynamic state to save | Loot.prototype.Init = function() | ||||
{ | |||||
this.UpdateProperties(); | |||||
}; | |||||
// Cache some properties that are expensive to compute. | |||||
Loot.prototype.UpdateProperties = function() | |||||
{ | |||||
this.xp = Math.floor(ApplyValueModificationsToEntity("Loot/xp", +(this.template.xp || 0), this.entity)) | |||||
this.resources = {}; | |||||
for (let res of Resources.GetCodes()) | |||||
this.resources[res] = Math.floor(ApplyValueModificationsToEntity("Loot/" + res, +(this.template[res] || 0), this.entity)); | |||||
}; | |||||
Loot.prototype.Serialize = function() | |||||
{ | |||||
return { | |||||
"xp": this.xp, | |||||
"resources": this.resources | |||||
}; | |||||
}; | |||||
Loot.prototype.Deserialize = function(data) | |||||
{ | |||||
this.xp = data.xp; | |||||
this.resources = data.resources; | |||||
}; | |||||
Loot.prototype.GetXp = function() | Loot.prototype.GetXp = function() | ||||
{ | { | ||||
return Math.floor(ApplyValueModificationsToEntity("Loot/xp", +(this.template.xp || 0), this.entity)); | return this.xp; | ||||
}; | }; | ||||
Loot.prototype.GetResources = function() | Loot.prototype.GetResources = function() | ||||
{ | { | ||||
let ret = {}; | return this.resources; | ||||
for (let res of Resources.GetCodes()) | }; | ||||
ret[res] = Math.floor(ApplyValueModificationsToEntity("Loot/" + res, +(this.template[res] || 0), this.entity)); | |||||
return ret; | Loot.prototype.OnValueModification = function(msg) | ||||
{ | |||||
if (msg.component == "Loot") | |||||
this.UpdateProperties(); | |||||
}; | |||||
Loot.prototype.OnOwnershipChanged = function(msg) | |||||
{ | |||||
if (msg.to == INVALID_PLAYER) | |||||
return; | |||||
this.UpdateProperties(); | |||||
}; | |||||
Loot.prototype.OnGlobalInitGame = function(msg) | |||||
{ | |||||
this.UpdateProperties(); | |||||
}; | |||||
Loot.prototype.OnMultiplierChanged = function(msg) | |||||
{ | |||||
if (msg.player == QueryOwnerInterface(this.entity, IID_Player).GetPlayerID()) | |||||
this.UpdateProperties(); | |||||
}; | }; | ||||
Engine.RegisterComponentType(IID_Loot, "Loot", Loot); | Engine.RegisterComponentType(IID_Loot, "Loot", Loot); |
Wildfire Games · Phabricator