Skip to content

Commit

Permalink
ThreeGraphics - Smoother camera follows
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
Marak committed Mar 13, 2024
1 parent cb11019 commit 853af6f
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 25 deletions.
18 changes: 16 additions & 2 deletions mantra-game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion mantra-game/lib/Game/defaults/defaultPlatformMovement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 6 additions & 7 deletions mantra-game/plugins/graphics-three/ThreeGraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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':
Expand Down
4 changes: 4 additions & 0 deletions mantra-game/plugins/graphics-three/lib/inflateTexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions mantra-game/plugins/graphics-three/lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions mantra-game/plugins/rbush/RBush.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
19 changes: 9 additions & 10 deletions mantra-game/plugins/sutra/Sutra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion mantra-worlds/Home/sutras.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions mantra-worlds/Music/Music.js
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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();
Expand Down

0 comments on commit 853af6f

Please sign in to comment.