-
-
Notifications
You must be signed in to change notification settings - Fork 660
Add multi-camera support with minimap example #1310
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/examples/src/examples/platformer/entities/minimap.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { Camera2d, event, game, level } from "melonjs"; | ||
|
|
||
| const MINIMAP_WIDTH = 180; | ||
| const MINIMAP_HEIGHT = 100; | ||
|
|
||
| /** | ||
| * A minimap camera that shows a zoomed-out view of the entire level | ||
| * in the top-right corner of the screen. | ||
| */ | ||
| export class MinimapCamera extends Camera2d { | ||
| private boundOnResize: (w: number) => void; | ||
|
|
||
| constructor() { | ||
| super(0, 0, MINIMAP_WIDTH, MINIMAP_HEIGHT); | ||
| this.name = "minimap"; | ||
| this.screenX = game.viewport.width - MINIMAP_WIDTH - 10; | ||
| this.screenY = 10; | ||
|
|
||
| // prevent canvas resize from resetting this camera's dimensions/bounds | ||
| this.autoResize = false; | ||
|
|
||
| // reposition on canvas resize (keep anchored to top-right) | ||
| this.boundOnResize = (w: number) => { | ||
| this.screenX = w - MINIMAP_WIDTH - 10; | ||
| }; | ||
| event.on(event.CANVAS_ONRESIZE, this.boundOnResize); | ||
|
|
||
| const currentLevel = level.getCurrentLevel(); | ||
| if (currentLevel) { | ||
| const lw = currentLevel.cols * currentLevel.tilewidth; | ||
| const lh = currentLevel.rows * currentLevel.tileheight; | ||
| this.setBounds(0, 0, lw, lh); | ||
| this.zoom = Math.min(MINIMAP_WIDTH / lw, MINIMAP_HEIGHT / lh); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Draw minimap overlays: viewport highlight, player marker, and border. | ||
| */ | ||
| override postDraw(renderer: any): void { | ||
| const viewport = game.viewport; | ||
| const screenPx = 1 / this.zoom; // 1 screen pixel in world units | ||
| const savedLineWidth = renderer.lineWidth; | ||
|
|
||
| // main camera's visible area in world space | ||
| const view = viewport.worldView; | ||
| renderer.setGlobalAlpha(0.9); | ||
| renderer.setColor("#ffffff"); | ||
| renderer.lineWidth = 1.5 * screenPx; | ||
| renderer.strokeRect(view.left, view.top, view.width, view.height); | ||
|
|
||
| // player position marker | ||
| const players = game.world.getChildByProp("name", "mainPlayer"); | ||
| if (players.length > 0) { | ||
| const player = players[0]; | ||
| const markerSize = 4 * screenPx; | ||
| renderer.setGlobalAlpha(1.0); | ||
| renderer.setColor("#00ff00"); | ||
| renderer.fillEllipse( | ||
| player.pos.x + player.width / 2, | ||
| player.pos.y + player.height / 2, | ||
| markerSize, | ||
| markerSize, | ||
| ); | ||
| } | ||
|
|
||
| // minimap border | ||
| renderer.setGlobalAlpha(0.8); | ||
| renderer.setColor("#ffffff"); | ||
| renderer.lineWidth = 2 * screenPx; | ||
| renderer.strokeRect( | ||
| 0, | ||
| 0, | ||
| MINIMAP_WIDTH / this.zoom, | ||
| MINIMAP_HEIGHT / this.zoom, | ||
| ); | ||
|
|
||
| // restore lineWidth | ||
| renderer.lineWidth = savedLineWidth; | ||
|
|
||
| // restore the camera context | ||
| super.postDraw(renderer); | ||
| } | ||
|
|
||
| /** | ||
| * Cleanup event listeners. | ||
| */ | ||
| override destroy(): void { | ||
| event.off(event.CANVAS_ONRESIZE, this.boundOnResize); | ||
| super.destroy(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renderer.lineWidthis modified here, but in WebGL the renderer save/restore stack does not tracklineWidth, so this value can leak into subsequent draws even aftersuper.postDraw()restores. Consider saving the previouslineWidthand restoring it before returning (or explicitly resetting it after drawing the minimap overlays).