From 853af6fe2ec2d51a59a7769c4400955c225d52b4 Mon Sep 17 00:00:00 2001 From: Marak Date: Tue, 12 Mar 2024 22:02:44 -0400 Subject: [PATCH] `ThreeGraphics` - Smoother camera follows `ThreeGraphics` - Moves camera update logic to render loop `ThreeGraphics` - Reduces console logging for missing textures `Sutra` - Simplifies default movement binding `Game.useSutra()` - Updates helper signature Removes default movement sutras from level code Code comments / Code clean-up --- mantra-game/Game.js | 18 ++++++++++++++++-- .../Game/defaults/defaultPlatformMovement.js | 4 +++- .../plugins/graphics-three/ThreeGraphics.js | 13 ++++++------- .../graphics-three/lib/inflateTexture.js | 4 ++++ .../plugins/graphics-three/lib/render.js | 3 +-- mantra-game/plugins/rbush/RBush.js | 2 ++ mantra-game/plugins/sutra/Sutra.js | 19 +++++++++---------- mantra-worlds/Home/sutras.js | 2 +- mantra-worlds/Music/Music.js | 4 ++-- 9 files changed, 44 insertions(+), 25 deletions(-) diff --git a/mantra-game/Game.js b/mantra-game/Game.js index 1ede2797..9c7ea4cd 100644 --- a/mantra-game/Game.js +++ b/mantra-game/Game.js @@ -581,9 +581,23 @@ class Game { // // Sutra Behavior Tree APIs // - useSutra(subSutra, name) { + useSutra(subSutra, name, options = {}) { if (this.rules) { - this.rules.use(subSutra, name); + let insertAt = 0; + + let defaultOptions = { + shareListeners: true, + shareTree: true, + shareMap: true + }; + + for (let key in defaultOptions) { + if (typeof options[key] === 'undefined') { + options[key] = defaultOptions[key]; + } + } + + this.rules.use(subSutra, name, insertAt, options.shareListeners, options.shareTree, options.shareMap); // TODO: update sutra api to use options object if (this.systems['gui-sutra']) { this.systems['gui-sutra'].setRules(this.rules); } diff --git a/mantra-game/lib/Game/defaults/defaultPlatformMovement.js b/mantra-game/lib/Game/defaults/defaultPlatformMovement.js index 7f9343ef..f09f3e5b 100644 --- a/mantra-game/lib/Game/defaults/defaultPlatformMovement.js +++ b/mantra-game/lib/Game/defaults/defaultPlatformMovement.js @@ -124,7 +124,9 @@ export default function platformMovement(game) { //rules.if('P').then('ZOOM_OUT'); rules.on('JUMP', function (player, node, gameState) { - // console.log("JUMP", gameState.input, gameState.controls) + // console.log("JUMP", gameState.inputTicks.SPACE, maxJumpTicks) + // TODO: fix this, it now seems to be resetting to zero prematurely + // could be input system, keyboard? if (gameState.inputTicks.SPACE >= maxJumpTicks) { return; } diff --git a/mantra-game/plugins/graphics-three/ThreeGraphics.js b/mantra-game/plugins/graphics-three/ThreeGraphics.js index aa0e2aa4..6de96374 100644 --- a/mantra-game/plugins/graphics-three/ThreeGraphics.js +++ b/mantra-game/plugins/graphics-three/ThreeGraphics.js @@ -178,18 +178,16 @@ class ThreeGraphics extends GraphicsInterface { } - // called by systemsManager on each game tick - update() { - this.updateCameraFollow(); - } + // update() is called by the SystemsManager, it's the main game loop + // Remark: Graphics generally should run in the render() loop, not the update() loop + // update() {} - updateCameraFollow() { + // Remark: updateCamera() is called from within the render() loop + updateCamera() { let game = this.game; - // if (this.isManualControlActive) return; // Skip automatic following if manual control is active - // Get the current player entity const currentPlayer = game.getEntity(game.currentPlayerId); if (!currentPlayer) { @@ -208,6 +206,7 @@ class ThreeGraphics extends GraphicsInterface { // Determine the camera mode and update accordingly switch (game.data.camera.mode) { case 'fpv': + case 'none': this.setFirstPersonView(playerGraphic); break; case 'follow': diff --git a/mantra-game/plugins/graphics-three/lib/inflateTexture.js b/mantra-game/plugins/graphics-three/lib/inflateTexture.js index 9bd76c96..b5ac626e 100644 --- a/mantra-game/plugins/graphics-three/lib/inflateTexture.js +++ b/mantra-game/plugins/graphics-three/lib/inflateTexture.js @@ -8,6 +8,10 @@ export default async function inflateTexture(entityData) { return; } + if (entityData.texture === 'none') { + return; + } + let texture = this.game.getTexture(entityData.texture); if (!texture) { console.warn('Warning: Texture not found', entityData.texture); diff --git a/mantra-game/plugins/graphics-three/lib/render.js b/mantra-game/plugins/graphics-three/lib/render.js index d3ba8dc3..7fc0fab7 100644 --- a/mantra-game/plugins/graphics-three/lib/render.js +++ b/mantra-game/plugins/graphics-three/lib/render.js @@ -23,14 +23,13 @@ export default function render(game, alpha) { }); } else { - for (let [eId, state] of this.game.entities.entries()) { let ent = this.game.entities.get(eId); this.inflateGraphic(ent, alpha); } - } + this.updateCamera(); this.renderer.render(this.scene, this.camera); } \ No newline at end of file diff --git a/mantra-game/plugins/rbush/RBush.js b/mantra-game/plugins/rbush/RBush.js index 25f8e5dd..cea513fc 100644 --- a/mantra-game/plugins/rbush/RBush.js +++ b/mantra-game/plugins/rbush/RBush.js @@ -40,6 +40,8 @@ class RBush { const spatialData = this.idToNodeMap.get(entity.id.toString()); if (spatialData) { this.tree.remove(spatialData); + // TODO: fix possible data leak + console.log('deleting id from node map', entity.id.toString()) this.idToNodeMap.delete(entity.id.toString()); } } diff --git a/mantra-game/plugins/sutra/Sutra.js b/mantra-game/plugins/sutra/Sutra.js index 200befdd..74fba54c 100644 --- a/mantra-game/plugins/sutra/Sutra.js +++ b/mantra-game/plugins/sutra/Sutra.js @@ -40,28 +40,27 @@ class Sutra { // for each key in game.controls, add a condition that checks if the key is pressed // these are currently explicitly bound to the player entity, we may want to make this more generic self.bindInputsToSutraConditions(); - if (self.defaultMovement) { - if (self.game.config.mode === 'topdown') { - self.game.useSutra(topdown(self.game), 'mode-topdown'); - } - if (self.game.config.mode === 'platform') { - // TODO: better platform control - self.game.useSutra(platform(self.game), 'mode-platform'); - } - } self.inputsBound = true; + if (self.defaultMovement) { // TODO: remove defaultMovement flag? + this.bindDefaultMovementSutra(self.game.config.mode) + } } } bindDefaultMovementSutra(mode = 'topdown') { if (mode === 'topdown') { - this.game.useSutra(topdown(this.game), 'mode-topdown'); + this.game.useSutra(topdown(this.game), 'mode-topdown', { + shareListeners: true, + shareTree: false, + shareMap: false + }); } if (mode=== 'platform') { this.game.useSutra(platform(this.game), 'mode-platform'); } } + bindInputsToSutraConditions() { this.bindKeyCodesToSutraConditions(); this.bindGamepadToSutraConditions(); diff --git a/mantra-worlds/Home/sutras.js b/mantra-worlds/Home/sutras.js index f011077c..9e145e1e 100644 --- a/mantra-worlds/Home/sutras.js +++ b/mantra-worlds/Home/sutras.js @@ -11,7 +11,7 @@ import routing from '../sutras/routing.js'; import block from "./sutras/block.js"; import demon from "../../mantra-sutras/demon.js"; // import hexapod from '../../mantra-sutras/hexapod.js'; -import movement from "../../mantra-sutras/player-movement/top-down.js"; +// import movement from "../../mantra-sutras/player-movement/top-down.js"; import bomb from '../../mantra-sutras/bomb.js'; diff --git a/mantra-worlds/Music/Music.js b/mantra-worlds/Music/Music.js index fb933d5d..e128b78e 100644 --- a/mantra-worlds/Music/Music.js +++ b/mantra-worlds/Music/Music.js @@ -1,6 +1,6 @@ import createPiano from "./instruments/createPiano.js"; import createDrumKit from "./instruments/createDrumKit.js"; -import movement from '../../mantra-sutras/player-movement/top-down.js'; +// import movement from '../../mantra-sutras/player-movement/top-down.js'; import sutras from "./sutras.js"; @@ -23,7 +23,7 @@ class Music { // Actions with left click game.config.mouseActionButton = 'RIGHT'; // enables the default top-down mouse movements - game.config.defaultMouseMovement = true; +// game.config.defaultMouseMovement = true; game.reset();