My main.js file calls a load manager and a scene manager.
//Load Manager
import { LoadManager } from './sceneSubjects/LoadManager.js';
//Scene Manager
import { SceneManager } from './SceneManager.js'
//Load manager
const loadmn = new LoadManager();
//Scene Manager
const canvas = document.getElementById("canvas");
const sceneManager = new SceneManager(canvas, loadmn);
bindEventListeners();
render();
function bindEventListeners() {
window.onresize = resizeCanvas;
resizeCanvas();
}
function resizeCanvas() {
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
sceneManager.onWindowResize();
}
function render() {
requestAnimationFrame(render);
sceneManager.update();
}
In the load manager
The load manager contains functions related to loading two models: a player model and an enemy model.
import * as THREE from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f08498829595b0c0dec1c1c8">[email protected]</a>/build/three.module.js';
//
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60140812050520504e515158">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js';
export class LoadManager {
constructor() {
console.log("hey hey LoadManager");
//Load manager
const loadingpage = document.getElementById('loading-screen');
this.loadManager = new THREE.LoadingManager();
this.loadManager.onProgress = function (item, loaded, total) {//display progress when loading
// console.log('Loading file: ' + item, '.\n Loaded ' + loaded + ' of ' + total + ' files.');
document.getElementById("loadingMessage").innerHTML = 'Loading file: ' + item;
console.log('Loading file: ' + '.\n Loaded ' + loaded + ' of ' + total + ' files.');
// document.getElementById("loadingMessage").innerHTML = loaded + ' of ' + total + ' files.';
};
this.loadManager.onError = function (item) {//display error when loading erroe appears
console.log('There was an error loading ' + item);
document.getElementById("loadingMessage").innerHTML = 'There was an error loading : ' + item;
}
this.loadManager.onLoad = function () {//loading complete
console.log('Loading complete!');
document.getElementById("loadingMessage").innerHTML = 'Loading complete!';
loadingpage.style.display = 'none';
};
this.playerModel = null;
this.enemyModel = null;
//load the player and enemy ships
const loader = new GLTFLoader(this.loadManager);
loader.load(~~~
this.playerModel = gltf.scene;
console.log(" player model loaded");
});
loader.load(~~~
this.enemyModel = gltf.scene;
console.log(" enemy model loaded");
});
}
get getPlayerModel() {
return this.playerModel;
}
get getEnemyModel() {
return this.enemyModel;
}
}
In the scene manager
import * as THREE from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b7abb1a6a683f3edf2f2fb">[email protected]</a>/build/three.module.js';
import * as YUKA from './libs/yuka.module.js';
//loader
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5b475d4a4a6f1f011e1e17">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js';
//effect
import { EffectComposer } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7b677d6a6a4f3f213e3e37">[email protected]</a>/examples/jsm/postprocessing/EffectComposer.js';
import { RenderPass } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcc8d4ced9d9fc8c928d8d84">[email protected]</a>/examples/jsm/postprocessing/RenderPass.js';
import { ShaderPass } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cf8e4fee9e9ccbca2bdbdb4">[email protected]</a>/examples/jsm/postprocessing/ShaderPass.js';
import { UnrealBloomPass } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="37435f45525277071906060f">[email protected]</a>/examples/jsm/postprocessing/UnrealBloomPass.js';
import { GlitchPass } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8b978d9a9abfcfd1cecec7">[email protected]</a>/examples/jsm/postprocessing/GlitchPass.js';
//lock the mouse pointer while in game
import { PointerLockControls } from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75011d07101035455b44444d">[email protected]</a>/examples/jsm/controls/PointerLockControls.js';
//scene subjects
import { Player } from './sceneSubjects/Player.js';
import { Enemy } from './sceneSubjects/enemy.js';
import { EnemyBehaviour } from './sceneSubjects/enemyBehaviour.js';
import { World } from './sceneSubjects/world.js'
export function SceneManager(canvas, loadmn) {
console.log("hey hey SceneManager")
const entityManager = new YUKA.EntityManager();
const clock = new THREE.Clock();
const enemies = [];
const gameState = ['running', 'paused', 'end'];
let currgameState = null;
const screenDimensions = {
width: canvas.width,
height: canvas.height
}
//Get models
let playerModel = loadmn.getPlayerModel;
console.log(playerModel);
let enemyModel = loadmn.getEnemyModel;
console.log(enemyModel);
I'm facing an issue where the load manager is being called before the models are loaded. I want to ensure that the models are loaded before the scene manager is called. Does anyone have any suggestions on how to fix this? I'm relatively new to JavaScript.