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

Add multiplayer camera #9

Closed
manuq opened this issue Nov 8, 2024 · 3 comments
Closed

Add multiplayer camera #9

manuq opened this issue Nov 8, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@manuq
Copy link
Contributor

manuq commented Nov 8, 2024

In the Moddable Platformer, we would like the ability to show multiple players on screen at once. We can probably get away with a camera which zooms and pans to contain all of the players. Ideally this should happen "for free": the camera we have built in to the game should understand how to follow however many players exist in the scene.

@manuq manuq added the enhancement New feature or request label Nov 8, 2024
@manuq manuq self-assigned this Nov 8, 2024
@manuq manuq mentioned this issue Nov 8, 2024
manuq added a commit that referenced this issue Nov 12, 2024
Add a new camera that inherits from Camera2D. It can frame the player
characters by positioning and zooming accordingly.

It adds a margin of 128 px (one tile size) to the player positions.

Add a new node for this camera to the main scene. It is disabled by
default. So by enabling this one and disabling or removing the current
camera for the character, this one takes precedence. The new multiplayer
camera has the same limits and position smoothing that the current
camera.

This is based on the Godot recipe [1] but adapted to Godot 4 and to this
project, with variable names improved.

[1] https://kidscancode.org/godot_recipes/3.x/2d/multi_target_camera/

#9
manuq added a commit that referenced this issue Nov 12, 2024
The background didn't work when zooming out either the existing camera
or the new multiplayer camera because it doesn't repeat vertically.
Adapt it to work:
- Enlarge the farther layer using a ColorRect so it can cover the screen
 vertically at 0.4 zoom (the whole default level).
- Also set the background to not follow the camera zoom.

#9
manuq added a commit that referenced this issue Nov 12, 2024
@wjt
Copy link
Member

wjt commented Nov 12, 2024

Implemented in #12.

@wjt wjt closed this as completed Nov 12, 2024
@wjt wjt reopened this Nov 12, 2024
@wjt
Copy link
Member

wjt commented Nov 12, 2024

As of commit a1d098c, starting from a clean working copy, I did the following, following the steps described in MODS.md:

Click on the Player node. In the inspector, try changing the Player
dropdown to "Two" or "Both".

I think this property is confusingly-named. Selecting "Two" works fine, modulo Dvorak Problems that I'll open a PR for.

Duplicate the Player node.

image

Move it next to the existing player.

image

Then
change the Player to "Two" in the duplicated one. Now the game is
multiplayer.

I get this crazy flickering effect:

Screencast.from.2024-11-12.09-50-12.webm

I think what's happening is that the players are intersecting so they're doing some weird collision. I guess the instruction above didn't mean “move it next to the existing player in the scene tree” but “move it in the canvas so that it doesn't overlap the first player”.

You will notice that the camera is following one of the player characters. So
try disabling the Camera2D node that's a child of the Player node.

Both players have a camera because duplicating the player duplicates its children too.

And then
enable the MultiplayerCamera node that's at the bottom of the Scene Dock.

Very nice!

@wjt
Copy link
Member

wjt commented Nov 12, 2024

I had a quick poke around to see if there's some way to make the players overlapping automatically fix itself but it got pretty nasty pretty fast. Maybe it's a fun thing for learners to find for themselves. I tweaked the documentation in #13.

@wjt wjt closed this as completed Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants