mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-29 07:13:10 +00:00
8b1e78ae1e
Fixes: #6285
64 lines
1.1 KiB
JavaScript
64 lines
1.1 KiB
JavaScript
Resources = new Resources();
|
|
|
|
export class ResourcesManager
|
|
{
|
|
constructor(amounts = {}, population = 0)
|
|
{
|
|
for (const key of Resources.GetCodes())
|
|
this[key] = amounts[key] || 0;
|
|
|
|
this.population = population > 0 ? population : 0;
|
|
}
|
|
|
|
reset()
|
|
{
|
|
for (const key of Resources.GetCodes())
|
|
this[key] = 0;
|
|
this.population = 0;
|
|
}
|
|
|
|
canAfford(that)
|
|
{
|
|
for (const key of Resources.GetCodes())
|
|
if (this[key] < that[key])
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
add(that)
|
|
{
|
|
for (const key of Resources.GetCodes())
|
|
this[key] += that[key];
|
|
this.population += that.population;
|
|
}
|
|
|
|
subtract(that)
|
|
{
|
|
for (const key of Resources.GetCodes())
|
|
this[key] -= that[key];
|
|
this.population += that.population;
|
|
}
|
|
|
|
multiply(n)
|
|
{
|
|
for (const key of Resources.GetCodes())
|
|
this[key] *= n;
|
|
this.population *= n;
|
|
}
|
|
|
|
Serialize()
|
|
{
|
|
const amounts = {};
|
|
for (const key of Resources.GetCodes())
|
|
amounts[key] = this[key];
|
|
return { "amounts": amounts, "population": this.population };
|
|
}
|
|
|
|
Deserialize(data)
|
|
{
|
|
for (const key in data.amounts)
|
|
this[key] = data.amounts[key];
|
|
this.population = data.population;
|
|
}
|
|
}
|