function ResourceSupply() {}
ResourceSupply.prototype.Schema =
"Provides a supply of one particular type of resource." +
"" +
"1000" +
"food.meat" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"wood.tree" +
"wood.ruins" +
"stone.rock" +
"stone.ruins" +
"metal.ore" +
"food.fish" +
"food.fruit" +
"food.grain" +
"food.meat" +
"food.milk" +
"treasure.wood" +
"treasure.stone" +
"treasure.metal" +
"treasure.food" +
"" +
"";
ResourceSupply.prototype.Init = function()
{
// Current resource amount (non-negative; can be a fractional amount)
this.amount = this.GetMaxAmount();
};
ResourceSupply.prototype.GetKillBeforeGather = function()
{
return (this.template.KillBeforeGather == "true");
};
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);
Engine.PostMessage(this.entity, MT_ResourceSupplyChanged, { "from": old, "to": this.amount });
return { "amount": change, "exhausted": (old == 0) };
};
ResourceSupply.prototype.GetType = function()
{
// All resources must have both type and subtype
var [type, subtype] = this.template.Type.split('.');
return { "generic": type, "specific": subtype };
};
Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply);