Skip to content

Commit

Permalink
Code now loads grasses on switch to Grass layer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tsanth committed Oct 12, 2024
1 parent 80672ed commit a843daf
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion js/grass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a843daf

Please sign in to comment.