Skip to content
New issue

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

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

Already on GitHub? # to your account

Add option to reset the map #58

Open
ricardodalarme opened this issue Aug 28, 2024 · 1 comment
Open

Add option to reset the map #58

ricardodalarme opened this issue Aug 28, 2024 · 1 comment

Comments

@ricardodalarme
Copy link
Contributor

I would like to have an easy way to reset the map.

My use case is this: when the player dies, he respawns at the starting position and the map SHOULD be reset.

Right now, there is no easy way to do this. So having a .reset() like function inside the LeapMap would help a lot.

@kurtome
Copy link
Owner

kurtome commented Aug 29, 2024

The pattern I've been using for this is to reload the map in the Game class and add:

class MyGame {
  ...

  Future<void> reloadLevel() async {
    await loadWorldAndMap(
      tiledMapPath: 'map.tmx',
      tiledObjectHandlers: tiledObjectHandlers,
    );

    // Don't let the camera move outside the bounds of the map, inset
    // by half the viewport size to the edge of the camera if flush with the
    // edge of the map.
    final inset = camera.viewport.virtualSize;
    camera.setBounds(
      Rectangle.fromLTWH(
        inset.x / 2,
        inset.y / 2,
        leapMap.width - inset.x,
        leapMap.height - inset.y,
      ),
    );
  }


  @override
  void onMapUnload(LeapMap map) {
    player?.removeFromParent();
  }

  @override
  void onMapLoaded(LeapMap map) {
    if (player != null) {
      world.add(player!);
      player!.resetPosition();
    }
  }
}

And make sure to call game.reloadLevel() from the Player when the player dies

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants