From a843daf4f56f4bfebba9b6741a09ab0ee1fb1934 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Uy Date: Sat, 12 Oct 2024 10:05:15 -0700 Subject: [PATCH] Code now loads grasses on switch to Grass layer PR 4 ("Debug and refactor grass.js update code") handled loading grasses during microtab switching, but failed to take into account layer switching. As a result, switching away from Grass then back again while having either the grass or golden grass microtabs selected would result in empty grass or golden grass fields. We solve this by storing previous state of layer loadedness, then using that to load the appropriate type of grass during the transition into the Grass layer. --- js/grass.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/js/grass.js b/js/grass.js index 0453896..8cc97c9 100644 --- a/js/grass.js +++ b/js/grass.js @@ -6,6 +6,15 @@ startData() { return { unlocked: true, + isLayerLoaded: false, + + // The characters are state and player.g.isLayerLoaded, respectively: + // nn: !s !p + // ny: !s p + // yn: s !p + // yy: s p + lastLayerState: 'nn', + isGrassLoaded: false, grass: new Decimal(0), savedGrass: new Decimal(0), @@ -77,20 +86,51 @@ onGoldGrassMicrotab: player.subtabs.g.stuff === 'Golden Grass', } + const thisLayerState = (state.inGrassLayer ? 'y' : 'n') + + (player.g.isLayerLoaded ? 'y' : 'n') + + // Track whether we had the layer loaded previously; the way + // this is set up, player.g.isLayerLoaded follows the state + // of state.inGrassLayer, with a single tick delay, e.g. + // let s be state.inGrassLayer, let p be player.g.isLayerLoaded: + // State 1: on Grass layer (yields s && p) + // State 2: switch to another layer (yields !s && p) + // State 3: on another layer (yields !s && !p) + // State 4: switch to Grass layer (yields s && !p) + // State 5: on Grass layer (yields s && p) + player.g.isLayerLoaded = state.inGrassLayer + + player.g.lastLayerState = (state.inGrassLayer ? 'y' : 'n') + + (player.g.isLayerLoaded ? 'y' : 'n') + // DEBUGGING OUTPUT //logOnce('updateEnter', `.../js/grass.js:update() ${JSON.stringify(state)}`) + //console.log(`Layer state: ${thisLayerState} (was ${player.g.lastLayerState})`) + + // Handle layer switching + if (player.g.lastLayerState == 'yy' && thisLayerState == 'yn') { + if (state.onGrassMicrotab) { + layers.g.loadGrass() + player.g.isGrassLoaded = true + } else if (state.onGoldGrassMicrotab) { + layers.g.loadGoldGrass() + player.g.isGoldGrassLoaded = true + } + } // Grass isn't loaded if we leave its microtab if (player.g.isGrassLoaded && !state.onGrassMicrotab) { + layers.g.unloadGrass() player.g.isGrassLoaded = false } // Golden grass isn't loaded if we leave its microtab if (player.g.isGoldGrassLoaded && !state.onGoldGrassMicrotab) { + layers.g.unloadGoldGrass() player.g.isGoldGrassLoaded = false } - // We only ever want to load grasses if we're _in_ the grass layer + // Handle microtab switching if (state.inGrassLayer) { if (!player.g.isGrassLoaded && state.onGrassMicrotab) { layers.g.loadGrass()