From d6479186e5e8b2fc832c62713376d421addcdb18 Mon Sep 17 00:00:00 2001 From: Marak Date: Thu, 29 Feb 2024 17:45:00 -0500 Subject: [PATCH] `CSSGraphics` game instances will now adjust zoom scale responsively Adds `CameraConfig.adaptiveZoom` Adds `CameraConfig.scaleMultiplier` --- mantra-game/lib/Game/construct.js | 2 ++ .../plugins/graphics-css/lib/camera/update.js | 25 +++++++++++++++++++ .../GravityGardens/GravityGardens.js | 1 + 3 files changed, 28 insertions(+) diff --git a/mantra-game/lib/Game/construct.js b/mantra-game/lib/Game/construct.js index a472709a..a8e3d733 100644 --- a/mantra-game/lib/Game/construct.js +++ b/mantra-game/lib/Game/construct.js @@ -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 }, diff --git a/mantra-game/plugins/graphics-css/lib/camera/update.js b/mantra-game/plugins/graphics-css/lib/camera/update.js index 1df3064c..84f37673 100644 --- a/mantra-game/plugins/graphics-css/lib/camera/update.js +++ b/mantra-game/plugins/graphics-css/lib/camera/update.js @@ -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) diff --git a/mantra-worlds/GravityGardens/GravityGardens.js b/mantra-worlds/GravityGardens/GravityGardens.js index 0af7c2da..da848784 100644 --- a/mantra-worlds/GravityGardens/GravityGardens.js +++ b/mantra-worlds/GravityGardens/GravityGardens.js @@ -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;