if (!("WebGLFloatArray" in window)) WebGLFloatArray = window.CanvasFloatArray; if (!("WebGLByteArray" in window)) WebGLByteArray = window.CanvasByteArray; if (!("WebGLIntArray" in window)) WebGLIntArray = window.CanvasIntArray; if (!("WebGLShortArray" in window)) WebGLShortArray = window.CanvasShortArray; if (!("WebGLUnsignedByteArray" in window)) WebGLUnsignedByteArray = window.CanvasUnsignedByteArray; if (!("WebGLUnsignedIntArray" in window)) WebGLUnsignedIntArray = window.CanvasUnsignedIntArray; if (!("WebGLUnsignedShortArray" in window)) WebGLUnsignedShortArray = window.CanvasUnsignedShortArray; const UPDATE_INTERVAL = 1.0 / 80.0 function setProjection(gl) { gl.projectionMatrix.loadIdentity(); gl.projectionMatrix.applyPerspective(60, document.getElementById("webGLCanvas").width / document.getElementById("webGLCanvas").height, 1.5, 400.0); } function initWebGL() { var canvas = document.getElementById("webGLCanvas"); var gl; var vshader, fshader; try { gl = canvas.getContext("webkit-3d"); } catch (error) {} if (gl == null) { try { gl = canvas.getContext("moz-webgl"); } catch (error) {} } if (gl == null) { alert("No WebGL context found"); return null; } vshader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vshader, document.getElementById("vshader").text); gl.compileShader(vshader); fshader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fshader, document.getElementById("fshader").text); gl.compileShader(fshader); gl.program = gl.createProgram(); gl.attachShader(gl.program, vshader); gl.attachShader(gl.program, fshader); gl.linkProgram(gl.program); gl.useProgram(gl.program); gl.uniform4f(gl.getUniformLocation(gl.program, "clearColor"), 0.0, 0.25, 0.5, 1.0); gl.clearColor(0.0, 0.25, 0.5, 1.0); gl.clearDepth(1.0); gl.enable(gl.DEPTH_TEST); gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); gl.projectionMatrix = new Matrix(); gl.modelviewMatrix = new Matrix(); setProjection(gl); return gl; } function initGameLevel(game) { game.level = GameLevels.createLevel(game.levelIndex); game.state = new GameState(game.level, game.player); game.stateController = new GameStateController(game.state); game.playerController = new PlayerController(game.player, game.soundEffects); game.player.position.x = game.level.playerStartPosition.x; game.player.position.y = game.level.playerStartPosition.y; game.player.position.z = game.level.playerStartPosition.z; game.player.velocity = new Vector3(); game.camera.position = Vector3.withValues(game.player.position.x, game.player.position.y, game.player.position.z + 100.0); game.cameraController = new PlayerCameraController(game.camera, game.player); game.renderer = new GameRenderer(game.state, game.camera); } function initGame() { var game = new Object(); game.runLoop = new FixedIntervalRunLoop(UPDATE_INTERVAL, function() { return new Date().getTime() / 1000; }, function() { run(game); }); game.soundEffects = new SoundEffects(soundsLoaded, game); game.levelIndex = 0; game.camera = new Camera(); game.player = new Player(game.soundEffects); initGameLevel(game); return game; } function run(game) { game.playerController.update(); game.stateController.update(); game.cameraController.update(); if (game.state.goalReached) { game.levelIndex++; if (game.levelIndex == GameLevels.numberOfLevels()) { game.soundEffects.play("allLevelsComplete"); } else { game.soundEffects.play("levelComplete"); } game.levelIndex %= GameLevels.numberOfLevels(); initGameLevel(game); } else if (game.player.position.y < game.level.bottomHeight) { game.soundEffects.play("die"); initGameLevel(game); } } function draw(gl, game) { var error; gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); game.renderer.render(gl); error = gl.getError(); if (error != 0) { console.log("GL error: " + error); } } function update(gl, game) { game.runLoop.run(); draw(gl, game); } function start() { var game = initGame(); game.runLoop.pause(); } function soundsLoaded(game) { var gl = initWebGL(); $("loadingText").style.display = "none"; $("webGLCanvas").style.display = "block"; document.body.onkeydown = function(event) { keyDown(event, game); } document.body.onkeyup = function(event) { keyUp(event, game); } setInterval(function() {update(gl, game);}, UPDATE_INTERVAL); game.runLoop.resume(); } function keyDown(event, game) { if (event.keyCode == "R".charCodeAt(0)) { initGameLevel(game); } else { game.playerController.keyDown(event.keyCode); } } function keyUp(event, game) { game.playerController.keyUp(event.keyCode); }