0ad/binaries/data/mods/public/simulation/ai/common-api/resources.js
phosit 0a2bd6a5a4 Use proper js-modules for ai common-api
The common-api already used a coding pattern which was called modules.
That is replaced with native js-modules.

Refs: #8081
2025-08-13 14:04:16 +02:00

60 lines
1.3 KiB
JavaScript

Resources = new Resources();
export function ResourcesManager(amounts = {}, population = 0)
{
for (const key of Resources.GetCodes())
this[key] = amounts[key] || 0;
this.population = population > 0 ? population : 0;
}
ResourcesManager.prototype.reset = function()
{
for (const key of Resources.GetCodes())
this[key] = 0;
this.population = 0;
};
ResourcesManager.prototype.canAfford = function(that)
{
for (const key of Resources.GetCodes())
if (this[key] < that[key])
return false;
return true;
};
ResourcesManager.prototype.add = function(that)
{
for (const key of Resources.GetCodes())
this[key] += that[key];
this.population += that.population;
};
ResourcesManager.prototype.subtract = function(that)
{
for (const key of Resources.GetCodes())
this[key] -= that[key];
this.population += that.population;
};
ResourcesManager.prototype.multiply = function(n)
{
for (const key of Resources.GetCodes())
this[key] *= n;
this.population *= n;
};
ResourcesManager.prototype.Serialize = function()
{
const amounts = {};
for (const key of Resources.GetCodes())
amounts[key] = this[key];
return { "amounts": amounts, "population": this.population };
};
ResourcesManager.prototype.Deserialize = function(data)
{
for (const key in data.amounts)
this[key] = data.amounts[key];
this.population = data.population;
};