silphius/app/ecs/systems/plant-growth.js
2024-07-20 05:07:39 -05:00

32 lines
725 B
JavaScript

import {System} from '@/ecs/index.js';
export default class PlantGrowth extends System {
frequency = 1;
static queries() {
return {
default: ['Plant'],
};
}
tick(elapsed) {
const variance = Math.random() * 0.4 + 0.8
for (const {Plant} of this.select('default')) {
if (65535 === Plant.growth || !Plant.mayGrow()) {
continue;
}
const stage = Math.floor(Plant.stage);
const growthFactor = (((Plant.growthFactor + 1) / 256) * 0.4 + 0.8) * variance
Plant.growth = Math.min(
Plant.growth + ((growthFactor * elapsed) / Plant.stages[stage]) * 65535,
65535,
);
if (65535 === Plant.growth) {
Plant.grow();
}
}
}
}