From c4908a825252dd7c9663c977981fe3ee4f168893 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 28 Jun 2024 07:03:56 -0500 Subject: [PATCH] feat: growth factor --- app/ecs-components/plant.js | 1 + app/ecs-systems/plant-growth.js | 4 +++- app/engine.js | 10 +++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/ecs-components/plant.js b/app/ecs-components/plant.js index dd8fe93..7fa96e3 100644 --- a/app/ecs-components/plant.js +++ b/app/ecs-components/plant.js @@ -48,6 +48,7 @@ export default class Plant extends Component { static properties = { growScript: {type: 'string'}, growth: {type: 'uint16'}, + growthFactor: {defaultValue: 127, type: 'uint8'}, mayGrowScript: {type: 'string'}, stage: {type: 'uint8'}, stages: { diff --git a/app/ecs-systems/plant-growth.js b/app/ecs-systems/plant-growth.js index 868b3a4..2d5b98f 100644 --- a/app/ecs-systems/plant-growth.js +++ b/app/ecs-systems/plant-growth.js @@ -9,13 +9,15 @@ export default class PlantGrowth extends System { } 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 + (((Math.random() + 0.5) * elapsed) / Plant.stages[stage]) * 65535, + Plant.growth + ((growthFactor * elapsed) / Plant.stages[stage]) * 65535, 65535, ); if (65535 === Plant.growth) { diff --git a/app/engine.js b/app/engine.js index 66e6324..f6b5ce7 100644 --- a/app/engine.js +++ b/app/engine.js @@ -122,7 +122,7 @@ export default class Engine { Plant: { growScript: '/assets/tomato-plant/grow.js', mayGrowScript: '/assets/tomato-plant/may-grow.js', - stages: [300, 300, 300, 300, 300], + stages: Array(5).fill(60), }, Sprite: { anchor: {x: 0.5, y: 0.75}, @@ -136,10 +136,14 @@ export default class Engine { VisibleAabb: {}, }; const promises = []; - for (let y = 0; y < 10; ++y) { - for (let x = 0; x < 10; ++x) { + for (let y = 0; y < 5; ++y) { + for (let x = 0; x < 5; ++x) { promises.push(ecs.create({ ...plant, + Plant: { + ...plant.Plant, + growthFactor: Math.floor(Math.random() * 256), + }, Position: {x: 8 + x * 16, y: 8 + y * 16}, })); }