Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update level spatial map when tiles are moved #539

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

mgmarlow
Copy link
Contributor

@mgmarlow mgmarlow commented Nov 24, 2024

Description

  • Expose removeFromSpatialMap and insertIntoSpatialMap from level so they can be called from the tile component.
  • When calling a move function from tile, remove the current component from the spatial map and add them back with the new position.
  • Contribute a Sokoban example that showcases using tile movement to create a tile-based puzzle game.

Here's what the example looks like:

image

Related issues

@mgmarlow mgmarlow changed the title Update level spatial map when tiles are moved fix:Update level spatial map when tiles are moved Nov 24, 2024
@mgmarlow mgmarlow changed the title fix:Update level spatial map when tiles are moved fix: Update level spatial map when tiles are moved Nov 24, 2024
Copy link

pkg-pr-new bot commented Nov 24, 2024

Open in Stackblitz

npm i https://pkg.pr.new/kaplayjs/kaplay@539

commit: 71da333

@mflerackers
Copy link
Member

While this fixes one particular use case, it breaks others (for example navigation) and is very costly when the map is large. What should happen is

  • level.removeFromSpatialMap(this); // This removes the object from the spatial map at the old tilePos
  • update tilePos
  • level.insertIntoSpatialMap(this); // This places the object at the correct position in the spatial map at the new tilePos
  • level.trigger("spatialMapChanged"); // This makes sure navigation and games that make use of it get notified that the spatial map has changed

@mgmarlow
Copy link
Contributor Author

While this fixes one particular use case, it breaks others (for example navigation) and is very costly when the map is large.

Ah, fair enough. I wasn't sure whether you preferred exposing both add/remove from the level object as opposed to an invalidate, similar to the navigation API. But I follow the reasoning. Will update it accordingly.

@mflerackers
Copy link
Member

Best would be to have tile a friend of level, but JavaScript/TypeScript doesn't have any granularity in what's private/public.

- Expose removeFromSpatialMap and insertIntoSpatialMap from
  level so they can be called from the tile component.
- When calling a move function from tile, remove the current
  component from the spatial map and add them back with the
  new position.
- Contribute a Sokoban example that showcases using tile
  movement to create a tile-based puzzle game.
@mgmarlow
Copy link
Contributor Author

One thing that's a little ugly in this case is the type cast from TileComp to GameObj to satisfy the arguments for spatialMap insert/remove:

level.removeFromSpatialMap(this as unknown as GameObj<TileComp>);

I saw the sprite component doing similar so I assumed this is the way to go.

@lajbel lajbel merged commit 5f3a3a4 into kaplayjs:master Nov 28, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: CHERRY-PICK-3001
Development

Successfully merging this pull request may close these issues.

bug: level spatialMap ignores changes to component tilePos
3 participants