var baseURL = "http://www.timfalken.com/GameBuilder/v2"; var requiredFiles = [ baseURL + "/vector.js", baseURL + "/object.js", baseURL + "/window.js", baseURL + "/input.js", baseURL + "/ion.sound.min.js", baseURL + "/audio.js" ]; var Engine = { x: 0, y: 0, w: 0, h: 0,//stored for window-building loadRequiredFiles: function(callback) { Engine.loadScripts(requiredFiles, callback); }, /* //function that can include an array of scripts one-by-one Usage: Engine.loadScripts(scripts, callback) scripts: Array of strings, each an URL to a .js file. callback: A function to call when loading has finished. Note: Function has an "index" parameter. This is for internal use and should be ignored. */ // category Engine // Engine.loadScripts(scripts, callback)

Include an array 'scripts' from an external source. When finished, start the function 'callback'. loadScripts: function(scripts, callback, index) { if(index == null) { index = 0; console.log("GAME: Loading " + scripts.length + " scripts."); } console.log("GAME: Loading [" + scripts[index] + "]"); $.getScript(scripts[index], function() //jQuery required for convenience! It's either that, or manually loading each required file. { if(index <= scripts.length - 2) Engine.loadScripts(scripts, callback, index + 1); else { console.log("GAME: Scripts loaded."); callback(); } }); } } // category Game var Game = { stepPrevious: 0, // Game.activeObjects

array containing all existing objects. activeObjects: [], userUpdateCallback: null, // Game.stepsSinceStart

Total amount of update steps since the start of the game. stepsSinceStart: 0, // Game.initialize(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)

Starts the game, and creates a window at ('windowX', 'windowY'), using 'windowWidth' and 'windowHeight' as sizes for the game window in pixels. 'fps' controls the game's frames-per-second'. As soon as the game is loaded up, it'll call the function 'loadedCallback', so you can properly start the game. initialize: function(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback) { console.log("GAME: Initializing. Loading dependencies..."); Engine.x = windowX; Engine.y = windowY; Engine.w = windowWidth; Engine.h = windowHeight; Engine.loadRequiredFiles(function() { GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h); GameInput.g_init(); window.setInterval(Game.gameStep, 1000 / fps); loadedCallback(); }); }, // Game.setUpdateFunction(callback)

Assigns a function 'callback(deltaTime)' to be called every frame of the game. You can use this to run global code for your game. setUpdateFunction: function(callback) { Game.userUpdateCallback = callback; }, // Game.clearScene()

destroys every object in the scene. clearScene: function() { for(var i = 0; i < Game.activeObjects.length; i++) Game.activeObjects[i].destroy(); }, gameStep: function() { var stepTime = Date.now(); var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001; if(isNaN(stepTimeCurr)) stepTimeCurr = 0; Game.stepsSinceStart++; GameInput.g_updateKeys(); var toUpdate = Game.activeObjects.slice(); for(var i = 0; i < toUpdate.length; i++) { if(toUpdate[i].active) toUpdate[i].g_updateStep(stepTimeCurr); } if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); } GameWindow.redraw(); this.stepPrevious = stepTime; } }; // Image // Constructor: new image(urlToImage, subImages = 1, subImagesPerRow = 1) function image(url, subImgTotal = 1, perRow = 1) { // image.subImages

The total number of subimages in the image. Defaults to 1. this.subImages = subImgTotal; // image.subImagesPerRow

The total number of subimages per row. Defaults to 1. this.subImagesPerRow = perRow; $("body").append(''); this.g_image = $("#image")[0]; $("#image").attr("id",""); // image.draw(sourceX, sourceY, sourceWidth, destX, destY, destWidth, destHeight, rotation, alpha)

Draw the image on screen. this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha) { var shouldDraw = true; if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) shouldDraw = false; if(shouldDraw) { var context = GameWindow.canvasContext; context.save(); var oldAlpha = context.globalAlpha; context.globalAlpha = alpha; context.translate(dx, dy); context.rotate(rotation * (Math.PI/180)); context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh); context.restore(); context.globalAlpha = oldAlpha; } } }