Skip to content

Commit

Permalink
Fix move actions not locking to tiles. Fixes #28 (#29)
Browse files Browse the repository at this point in the history
There were two problems here. The currently selected tile was changing
before the modifier was invoked and it was referencing the wrong tile.
This is a problem for move actions because the a new tile is selected
during modification (the 'to' tile).
  • Loading branch information
kflorence authored Sep 27, 2024
1 parent d68ae0d commit 67aa5e0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/modifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class Modifier extends Stateful {
}

dispatchEvent (event, detail) {
emitEvent(event, Object.assign({}, detail || {}, { modifier: this, tile: this.tile }))
emitEvent(event, Object.assign({ tile: this.tile }, detail || {}, { modifier: this }))
}

equals (other) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/modifiers/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class Move extends Modifier {
items.forEach((item) => item.move(tile))
return {
moved: [Move.data(this.tile, tile, items)],
selectedTile: tile,
tile: this.tile,
tiles: [this.tile, tile]
}
}
Expand All @@ -60,9 +62,6 @@ export class Move extends Modifier {

if (tile) {
const data = this.moveItems(tile)

puzzle.updateSelectedTile(tile)

this.dispatchEvent(Modifier.Events.Invoked, data)
}

Expand Down
2 changes: 2 additions & 0 deletions src/components/modifiers/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class Swap extends Move {

return {
moved: [Move.data(this.tile, tile, fromItems), Move.data(tile, this.tile, toItems)],
selectedTile: tile,
tile: this.tile,
tiles: [this.tile, tile]
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/puzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ export class Puzzle {
.forEach((other) => other.update({ disabled: true }))
}

if (event.detail.selectedTile) {
this.updateSelectedTile(event.detail.selectedTile)
}

this.addMove()
this.updateState()

Expand Down

0 comments on commit 67aa5e0

Please sign in to comment.