silphius/app/ecs-systems/plant-growth.js

32 lines
725 B
JavaScript
Raw Normal View History

2024-06-27 15:08:30 -05:00
import {System} from '@/ecs/index.js';
export default class PlantGrowth extends System {
2024-06-30 15:54:23 -05:00
frequency = 1;
2024-06-27 15:08:30 -05:00
static queries() {
return {
default: ['Plant'],
};
}
tick(elapsed) {
2024-06-28 07:03:56 -05:00
const variance = Math.random() * 0.4 + 0.8
2024-06-27 15:08:30 -05:00
for (const {Plant} of this.select('default')) {
if (65535 === Plant.growth || !Plant.mayGrow()) {
continue;
}
const stage = Math.floor(Plant.stage);
2024-06-28 07:03:56 -05:00
const growthFactor = (((Plant.growthFactor + 1) / 256) * 0.4 + 0.8) * variance
2024-06-27 15:08:30 -05:00
Plant.growth = Math.min(
2024-06-28 07:03:56 -05:00
Plant.growth + ((growthFactor * elapsed) / Plant.stages[stage]) * 65535,
2024-06-27 15:08:30 -05:00
65535,
);
if (65535 === Plant.growth) {
Plant.grow();
}
}
}
}