1
0
forked from 0ad/0ad
0ad/binaries/data/mods/public/simulation/components/ResourceSupply.js
WhiteTreePaladin 6af3f28ad4 Converted resources:
wood -> wood.tree
 stone -> stone.rock
 metal -> metal.ore
Added resource: metal.treasure
Renamed cursors and added placeholder treasure cursor
Fixed basic templates and Hellene actors to work with new resources
(need to fix Celt actors)
Fixed some warnings in the session scripts.
Added entity "Golden Fleece" placeholder

This was SVN commit r7778.
2010-07-21 18:51:27 +00:00

76 lines
2.1 KiB
JavaScript

function ResourceSupply() {}
ResourceSupply.prototype.Schema =
"<a:help>Provides a supply of one particular type of resource.</a:help>" +
"<a:example>" +
"<Amount>1000</Amount>" +
"<Type>food.meat</Type>" +
"</a:example>" +
"<element name='Amount' a:help='Amount of resources available from this entity'>" +
"<data type='nonNegativeInteger'/>" +
"</element>" +
"<element name='Type' a:help='Type of resources'>" +
"<choice>" +
"<value>wood.tree</value>" +
"<value>stone.rock</value>" +
"<value>metal.ore</value>" +
"<value>metal.treasure</value>" +
"<value>food.fish</value>" +
"<value>food.fruit</value>" +
"<value>food.grain</value>" +
"<value>food.meat</value>" +
"<value>food.milk</value>" +
"</choice>" +
"</element>";
ResourceSupply.prototype.Init = function()
{
// Current resource amount (non-negative; can be a fractional amount)
this.amount = this.GetMaxAmount();
};
ResourceSupply.prototype.GetMaxAmount = function()
{
return +this.template.Amount;
};
ResourceSupply.prototype.GetCurrentAmount = function()
{
return this.amount;
};
ResourceSupply.prototype.TakeResources = function(rate)
{
// Internally we handle fractional resource amounts (to be accurate
// over long periods of time), but want to return integers (so players
// have a nice simple integer amount of resources). So return the
// difference between rounded values:
var old = this.amount;
this.amount = Math.max(0, old - rate);
// (use ceil instead of floor so that we continue returning non-zero values even if 0 < amount < 1)
var change = Math.ceil(old) - Math.ceil(this.amount);
// Remove entities that have been exhausted
if (this.amount == 0)
Engine.DestroyEntity(this.entity);
return { "amount": change, "exhausted": (old == 0) };
};
ResourceSupply.prototype.GetType = function()
{
if (this.template.Type.indexOf('.') == -1)
{
return { "generic": this.template.Type };
}
else
{
var [type, subtype] = this.template.Type.split('.');
return { "generic": type, "specific": subtype };
}
};
Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply);