| 1 | var baseURL = "http://www.timfalken.com/GameBuilder/v2"; |
| 2 | |
| 3 | var requiredFiles = |
| 4 | [ |
| 5 | baseURL + "/vector.js", |
| 6 | baseURL + "/object.js", |
| 7 | baseURL + "/window.js", |
| 8 | baseURL + "/input.js", |
| 9 | baseURL + "/ion.sound.min.js", |
| 10 | baseURL + "/audio.js" |
| 11 | ]; |
| 12 | |
| 13 | var Engine = |
| 14 | { |
| 15 | x: 0, |
| 16 | y: 0, |
| 17 | w: 0, |
| 18 | h: 0,//stored for window-building |
| 19 | |
| 20 | loadRequiredFiles: function(callback) |
| 21 | { |
| 22 | Engine.loadScripts(requiredFiles, callback); |
| 23 | }, |
| 24 | |
| 25 | /* |
| 26 | //function that can include an array of scripts one-by-one |
| 27 | |
| 28 | Usage: Engine.loadScripts(scripts, callback) |
| 29 | |
| 30 | scripts: Array of strings, each an URL to a .js file. |
| 31 | callback: A function to call when loading has finished. |
| 32 | |
| 33 | Note: Function has an "index" parameter. This is for internal use and should be ignored. |
| 34 | */ |
| 35 | |
| 36 | //<DOC> category Engine |
| 37 | //<DOC> <function> Engine.loadScripts(scripts, callback)<br><br>Include an array 'scripts' from an external source. When finished, start the function 'callback'. |
| 38 | loadScripts: function(scripts, callback, index) |
| 39 | { |
| 40 | if(index == null) |
| 41 | { |
| 42 | index = 0; |
| 43 | console.log("GAME: Loading " + scripts.length + " scripts."); |
| 44 | } |
| 45 | |
| 46 | console.log("GAME: Loading [" + scripts[index] + "]"); |
| 47 | |
| 48 | $.getScript(scripts[index], function() //jQuery required for convenience! It's either that, or manually loading each required file. |
| 49 | { |
| 50 | if(index <= scripts.length - 2) |
| 51 | Engine.loadScripts(scripts, callback, index + 1); |
| 52 | else |
| 53 | { |
| 54 | console.log("GAME: Scripts loaded."); |
| 55 | callback(); |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | } |
| 60 | //<DOC> category Game |
| 61 | var Game = |
| 62 | { |
| 63 | stepPrevious: 0, |
| 64 | |
| 65 | //<DOC> <parameter> Game.activeObjects<br><br>array containing all existing objects. |
| 66 | activeObjects: [], |
| 67 | userUpdateCallback: null, |
| 68 | |
| 69 | //<DOC> <parameter> Game.stepsSinceStart<br><br> Total amount of update steps since the start of the game. |
| 70 | stepsSinceStart: 0, |
| 71 | |
| 72 | //<DOC> <function> Game.initialize(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)<br><br>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. |
| 73 | initialize: function(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback) |
| 74 | { |
| 75 | console.log("GAME: Initializing. Loading dependencies..."); |
| 76 | Engine.x = windowX; |
| 77 | Engine.y = windowY; |
| 78 | Engine.w = windowWidth; |
| 79 | Engine.h = windowHeight; |
| 80 | |
| 81 | Engine.loadRequiredFiles(function() |
| 82 | { |
| 83 | GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h); |
| 84 | GameInput.g_init(); |
| 85 | window.setInterval(Game.gameStep, 1000 / fps); |
| 86 | loadedCallback(); |
| 87 | }); |
| 88 | }, |
| 89 | |
| 90 | //<DOC> <function> Game.setUpdateFunction(callback)<br><br>Assigns a function 'callback(deltaTime)' to be called every frame of the game. You can use this to run global code for your game. |
| 91 | setUpdateFunction: function(callback) |
| 92 | { |
| 93 | Game.userUpdateCallback = callback; |
| 94 | }, |
| 95 | |
| 96 | //<DOC> <function> Game.clearScene()<br><br>destroys every object in the scene. |
| 97 | clearScene: function() |
| 98 | { |
| 99 | for(var i = 0; i < Game.activeObjects.length; i++) |
| 100 | Game.activeObjects[i].destroy(); |
| 101 | }, |
| 102 | |
| 103 | gameStep: function() |
| 104 | { |
| 105 | var stepTime = Date.now(); |
| 106 | |
| 107 | var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001; |
| 108 | |
| 109 | if(isNaN(stepTimeCurr)) |
| 110 | stepTimeCurr = 0; |
| 111 | |
| 112 | Game.stepsSinceStart++; |
| 113 | |
| 114 | GameInput.g_updateKeys(); |
| 115 | |
| 116 | var toUpdate = Game.activeObjects.slice(); |
| 117 | |
| 118 | for(var i = 0; i < toUpdate.length; i++) |
| 119 | { |
| 120 | if(toUpdate[i].active) |
| 121 | toUpdate[i].g_updateStep(stepTimeCurr); |
| 122 | } |
| 123 | |
| 124 | if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); } |
| 125 | |
| 126 | GameWindow.redraw(); |
| 127 | |
| 128 | this.stepPrevious = stepTime; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | //<DOC> <category> Image |
| 133 | //<DOC> <function> Constructor: new image(urlToImage, subImages = 1, subImagesPerRow = 1) |
| 134 | function image(url, subImgTotal = 1, perRow = 1) |
| 135 | { |
| 136 | //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1. |
| 137 | this.subImages = subImgTotal; |
| 138 | //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1. |
| 139 | this.subImagesPerRow = perRow; |
| 140 | |
| 141 | $("body").append('<img id="image" src="' + url + '" style="display:none;"></img>'); |
| 142 | this.g_image = $("#image")[0]; |
| 143 | $("#image").attr("id",""); |
| 144 | |
| 145 | //<DOC> <function> image.draw(sourceX, sourceY, sourceWidth, destX, destY, destWidth, destHeight, rotation, alpha)<br><br>Draw the image on screen. |
| 146 | this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha) |
| 147 | { |
| 148 | var shouldDraw = true; |
| 149 | |
| 150 | if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 151 | shouldDraw = false; |
| 152 | |
| 153 | if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 154 | shouldDraw = false; |
| 155 | |
| 156 | if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 157 | shouldDraw = false; |
| 158 | |
| 159 | if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2))) |
| 160 | shouldDraw = false; |
| 161 | |
| 162 | if(shouldDraw) |
| 163 | { |
| 164 | var context = GameWindow.canvasContext; |
| 165 | context.save(); |
| 166 | var oldAlpha = context.globalAlpha; |
| 167 | context.globalAlpha = alpha; |
| 168 | |
| 169 | context.translate(dx, dy); |
| 170 | context.rotate(rotation * (Math.PI/180)); |
| 171 | |
| 172 | context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh); |
| 173 | |
| 174 | context.restore(); |
| 175 | context.globalAlpha = oldAlpha; |
| 176 | } |
| 177 | } |
| 178 | } |