Skip to content

Commit

Permalink
CSSGraphics game instances will now adjust zoom scale responsively
Browse files Browse the repository at this point in the history
Adds `CameraConfig.adaptiveZoom`
Adds `CameraConfig.scaleMultiplier`
  • Loading branch information
Marak committed Feb 29, 2024
1 parent c3f9270 commit d647918
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mantra-game/lib/Game/construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export default function construct(game, plugins = []) {
fieldOfView: game.config.fieldOfView, // global for game, not camera specific
camera: {
mode: null,
scaleMultiplier: 2.5,
adaptiveZoom: true, // will auto-zoom the viewport to fit the game size
follow: game.config.camera.follow,
currentZoom: game.config.camera.startingZoom,
position: { x: 0, y: 0 },
Expand Down
25 changes: 25 additions & 0 deletions mantra-game/plugins/graphics-css/lib/camera/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@ export default function update() {
let windowHeight = window.innerHeight;
let windowWidth = window.innerWidth;

// TODO: allow overrides if user has started to zoom
if (game.data.camera.adaptiveZoom) {
const baseWidth = game.config.width;
const baseHeight = game.config.height;

// Calculate the scale ratio
let scaleX = windowWidth / baseWidth;
let scaleY = windowHeight / baseHeight;

// Use the smaller scale ratio to ensure the entire game is visible
let scale = Math.min(scaleX, scaleY);

// Set the game zoom level based on the scale
// You might want to adjust the multiplier (e.g., 1, 1.5, etc.) based on your game's specific needs

let scaleMultiplier = 2.5;

if (typeof game.data.camera.scaleMultiplier === 'number') {
scaleMultiplier = game.data.camera.scaleMultiplier;
}

game.setZoom(scale * scaleMultiplier); // The multiplier can be adjusted to suit the game's design
}


let zoomFactor = this.game.data.camera.currentZoom;
// console.log('zoomFactor', zoomFactor)

Expand Down
1 change: 1 addition & 0 deletions mantra-worlds/GravityGardens/GravityGardens.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GravityGardens {
// Actions with left click
game.config.mouseActionButton = 'LEFT';

game.data.camera.scaleMultiplier = 0.9;
// enables the default top-down mouse movements
game.config.defaultMouseMovement = true;

Expand Down

0 comments on commit d647918

Please sign in to comment.