From ddbfe104a783eb2af8a4fccdbfef7761b8428b83 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Fri, 14 Feb 2014 17:33:53 +0200 Subject: [PATCH 01/10] ua -> device --- src/engine/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/loader.js b/src/engine/loader.js index a64e25b3..741f1a8f 100755 --- a/src/engine/loader.js +++ b/src/engine/loader.js @@ -80,7 +80,7 @@ game.Loader = game.Class.extend({ game.system.stage.addChild(this.text); var imageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJMAAACDCAMAAAC+7dm8AAAAolBMVEUAAAAjHyAjHyAjHyAjHyAjHyAjHyAjHyAjHyAmIiMjHyAjHyAjHyAjHyAjHyAjHyAjHyAjHyAjHyD////m5+grKCn8/Pz3+PgzMDH09PVKR0jw8fGenp7t7u6ko6SqqapTUVLLy8zY2Nnq6uvj4+Q5NjhkYmNcWltAPT+Qj5CBf4B6eXqXlpfe3t9zcnPEw8RsamyHhoe8vL22tbbS0tKvr7CGaULPAAAAEnRSTlMAg3FU0uK/ChX+QzIj8LGdkWTaWSIZAAALGUlEQVR42r1c2ZaiMBBFRXFfIiAqsijI5or6/782rSyBVAJRu+c+zDndQ8dL1U1tAYVfQHfQajYmY7E9QilGbXE8bbYGkvBfIQ37CZMRqkB73GgNhT+HNJg1xiJ6A71x8w95dVtTEX2EdmMg/AGGTSYfRdNcLYWCGBCb0i97rNVBAK4VBebNPjryD3br5XKxWG52snPc3s/xwwjCk+eXKE6Hv8ioSSpZsa6PrZxit16ocxZ2thl6ObNJ97cYkfY5PRw5A+ZTAXWr71Nejd/wYL9dttDlJufYLObcWMSnF612/2sjTcsmCo4ydpk6fw9Lw3/J6jtTDdslRrqDGS3nH0C9rZ6mGnzjt5LXAsxIBoy4WRkaQmj2MaUZKuCyLehIrfeTs7VteyvDK3eX5wb8BUqaiRntqpV9vAWXlZZnltXJkIkrTAWhjvSusrtSmZLFZ6SFHew1B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i1BkRubWerSML3EKEApC6GJGU/SZWf1km0/SvoJ0L4Gq1ZS/g9UmJX+C/4BxGsxy8xC3cVAAAAAElFTkSuQmCC'; - if(game.ua.ie) imageData += '?' + Date.now(); + if(game.device.ie) imageData += '?' + Date.now(); this.symbol = new game.Sprite(game.system.width/2 - 8, game.system.height/2 + 70, PIXI.Texture.fromImage(imageData, true), { anchor: {x: 0.5, y: 1.0}, rotation: -0.1 From 8a6c729ec5657010dd93ad59f89022837ef22760 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 11:29:54 +0200 Subject: [PATCH 02/10] Fix: Loader not working, if tween module was not loaded --- src/engine/loader.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/engine/loader.js b/src/engine/loader.js index 741f1a8f..20ac2637 100755 --- a/src/engine/loader.js +++ b/src/engine/loader.js @@ -87,12 +87,14 @@ game.Loader = game.Class.extend({ }); game.system.stage.addChild(this.symbol); - var tween = new game.Tween(this.symbol, {rotation: 0.1}, 0.5, { - easing: game.Tween.Easing.Cubic.InOut, - loop: game.Tween.Loop.Reverse - }); - this.tweens.push(tween); - tween.start(); + if(game.Tween) { + var tween = new game.Tween(this.symbol, {rotation: 0.1}, 0.5, { + easing: game.Tween.Easing.Cubic.InOut, + loop: game.Tween.Loop.Reverse + }); + this.tweens.push(tween); + tween.start(); + } }, getPath: function(path) { From a54c4bd12957d5d6352e58e3187fa520b2e30b6b Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 11:32:30 +0200 Subject: [PATCH 03/10] Fix: Core not working, if sound/pool/storage/analytics module is not loaded --- src/engine/core.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/engine/core.js b/src/engine/core.js index b2cd41b0..24837f54 100755 --- a/src/engine/core.js +++ b/src/engine/core.js @@ -258,8 +258,6 @@ var core = { }); **/ module: function(name, version) { - if(name === 'engine.debug' && window.pandaMinified) return this; - if(this._current) throw('Module ' + this._current.name + ' has no body'); if(this.modules[name] && this.modules[name].body) throw('Module ' + name + ' is already defined'); @@ -277,7 +275,6 @@ var core = { require: function() { var i, modules = Array.prototype.slice.call(arguments); for (i = 0; i < modules.length; i++) { - if(modules[i] === 'engine.debug' && window.pandaMinified) continue; if(modules[i]) this._current.requires.push(modules[i]); } return this; @@ -308,12 +305,12 @@ var core = { height = height || (game.System.orientation === game.System.PORTRAIT ? 927 : 672); this.system = new game.System(width, height, canvasId); - this.sound = new game.SoundManager(); - this.pool = new game.Pool(); + if(game.SoundManager) this.sound = new game.SoundManager(); + if(game.Pool) this.pool = new game.Pool(); if(game.Debug && game.Debug.enabled && !navigator.isCocoonJS) this.debug = new game.Debug(); if(game.DebugDraw && game.DebugDraw.enabled) this.debugDraw = new game.DebugDraw(); - if(game.Storage.id) this.storage = new game.Storage(game.Storage.id); - if(game.Analytics.id) this.analytics = new game.Analytics(game.Analytics.id); + if(game.Storage && game.Storage.id) this.storage = new game.Storage(game.Storage.id); + if(game.Analytics && game.Analytics.id) this.analytics = new game.Analytics(game.Analytics.id); this.ready = true; From 2dffee7cd9356506d0f13d13a497b8d5cafcf484 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 11:33:02 +0200 Subject: [PATCH 04/10] Fix: Optimized build script --- src/engine/build.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/engine/build.js b/src/engine/build.js index 136e153a..15b28f20 100644 --- a/src/engine/build.js +++ b/src/engine/build.js @@ -1,13 +1,11 @@ var UglifyJS = require('uglify-js'); var fs = require('fs'); -var i, file, size, result; +var i, file, size, result, totalSize = 0; var outputFile = process.argv[2] || 'game.min.js'; var outputDir = process.argv[3] || './'; var header = '// Made with Panda.js - http://www.pandajs.net'; -var totalSize = 0; var include = ['engine/core.js', 'game/main.js']; -var exclude = ['engine/debug.js']; var dir = process.cwd() + '/src/'; global['game'] = {}; @@ -36,10 +34,6 @@ for (i = 0; i < include.length; i++) { require(dir + include[i]); } -for (var i = game.modules.length - 1; i >= 0; i--) { - if(exclude.indexOf(game.modules[i]) !== -1) game.modules.splice(i, 1); -} - for (i = 0; i < game.modules.length; i++) { file = game.modules[i]; game.modules[i] = dir + game.modules[i]; @@ -52,7 +46,7 @@ console.log('Total ' + totalSize + ' bytes'); result = UglifyJS.minify(game.modules); -result.code = header + '\n' + 'window.pandaMinified = true;' + '\n' + result.code; +result.code = header + '\n' + result.code; fs.writeFile(outputDir + outputFile, result.code, function(err) { if(err) console.log(err); From 78b9a0361ac8ed0a0272f5a5fb9b6a2d29584c70 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 13:43:54 +0200 Subject: [PATCH 05/10] Fix: mouseup event not firing, when mouse is out of document body --- src/engine/renderer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/renderer.js b/src/engine/renderer.js index bf5dd7aa..86a6b973 100644 --- a/src/engine/renderer.js +++ b/src/engine/renderer.js @@ -3089,7 +3089,7 @@ PIXI.InteractionManager.prototype.setTargetDomElement = function(domElement) domElement.addEventListener('touchend', this.onTouchEnd, true); domElement.addEventListener('touchmove', this.onTouchMove, true); - document.body.addEventListener('mouseup', this.onMouseUp, true); + window.addEventListener('mouseup', this.onMouseUp, true); }; @@ -3111,7 +3111,7 @@ PIXI.InteractionManager.prototype.removeEvents = function() this.interactionDOMElement = null; - document.body.removeEventListener('mouseup', this.onMouseUp, true); + window.removeEventListener('mouseup', this.onMouseUp, true); }; /** From 614d6e776d5ab02f8f06ff88f1cba9e635ae8e93 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 14:23:12 +0200 Subject: [PATCH 06/10] New: Added circle shape to debugDraw --- src/engine/debug.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/engine/debug.js b/src/engine/debug.js index d1e0493d..2c1731b7 100644 --- a/src/engine/debug.js +++ b/src/engine/debug.js @@ -55,10 +55,13 @@ game.DebugDraw = game.Class.extend({ var sprite = new game.Graphics(); sprite.beginFill(game.DebugDraw.shapeColor); - // TODO add support for game.Circle and game.Line if(body.shape instanceof game.Rectangle) { sprite.drawRect(-body.shape.width/2, -body.shape.height/2, body.shape.width, body.shape.height); } + if(body.shape instanceof game.Circle) { + sprite.drawCircle(0, 0, body.shape.radius); + } + // TODO add support for game.Line sprite.target = body; sprite.alpha = game.DebugDraw.shapeAlpha; From 9770461da92fa2859b0b58a578687d1a51060af9 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 14:25:35 +0200 Subject: [PATCH 07/10] Misc: Cleaning physics module --- src/engine/physics.js | 155 +++++++----------------------------------- 1 file changed, 24 insertions(+), 131 deletions(-) diff --git a/src/engine/physics.js b/src/engine/physics.js index 1e1df77a..93ec482b 100644 --- a/src/engine/physics.js +++ b/src/engine/physics.js @@ -124,8 +124,6 @@ game.World = game.Class.extend({ @extends game.Class **/ game.CollisionSolver = game.Class.extend({ - hitTestData: [], - /** Solve collision a versus b. @method solve @@ -133,9 +131,7 @@ game.CollisionSolver = game.Class.extend({ @param {Body} b **/ solve: function(a, b) { - this.hitTestData.length = 0; - if(!this.hitTest(a, b)) return; - if(!this.hitResponse(a, b)) return; + if(this.hitTest(a, b)) this.hitResponse(a, b); }, /** @@ -154,7 +150,7 @@ game.CollisionSolver = game.Class.extend({ ); } if(a.shape instanceof game.Circle && b.shape instanceof game.Circle) { - return (a.shape.radius + b.shape.radius > this.distance(a.position.x, a.position.y, b.position.x, b.position.y)); + return (a.shape.radius + b.shape.radius > a.position.distance(b.position)); } if( a.shape instanceof game.Rectangle && b.shape instanceof game.Circle || @@ -216,36 +212,15 @@ game.CollisionSolver = game.Class.extend({ if(t < 0) { var d = Math.sqrt(px * px + py * py); - if(d < circle.shape.radius) { - this.hitTestData[0] = a1x; - this.hitTestData[1] = a1y; - this.hitTestData[2] = d; - this.hitTestData[3] = px / d; - this.hitTestData[4] = py / d; - return true; - } + if(d < circle.shape.radius) return true; } else if(t > 1) { var d = this.distance(cx, cy, a2x, a2y); - if(d < circle.shape.radius) { - this.hitTestData[0] = a2x; - this.hitTestData[1] = a2y; - this.hitTestData[2] = d; - this.hitTestData[3] = (cx - a2x) / d; - this.hitTestData[4] = (cy - a2y) / d; - return true; - } + if(d < circle.shape.radius) return true; } else { var d = this.distance(px, py, dx * t, dy * t); - if(d < circle.shape.radius) { - this.hitTestData[0] = a1x + dx * t; - this.hitTestData[1] = a1y + dy * t; - this.hitTestData[2] = d; - this.hitTestData[3] = (px - dx * t) / d; - this.hitTestData[4] = (py - dy * t) / d; - return true; - } + if(d < circle.shape.radius) return true; } return false; } @@ -260,92 +235,65 @@ game.CollisionSolver = game.Class.extend({ **/ hitResponse: function(a, b) { if(a.shape instanceof game.Rectangle && b.shape instanceof game.Rectangle) { - // 0 = up, 1 = down, 2 = left, 3 = right + // TODO can this be optimized? if(a.last.y + a.shape.height / 2 <= b.last.y - b.shape.height / 2) { - if(a.collide(b, 0)) { + if(a.collide(b)) { a.position.y = b.position.y - b.shape.height / 2 - a.shape.height / 2; } } else if(a.last.y - a.shape.height / 2 >= b.last.y + b.shape.height / 2) { - if(a.collide(b, 1)) { + if(a.collide(b)) { a.position.y = b.position.y + b.shape.height / 2 + a.shape.height / 2; } } else if(a.last.x + a.shape.width /2 <= b.last.x - b.shape.width / 2) { - if(a.collide(b, 2)) { + if(a.collide(b)) { a.position.x = b.position.x - b.shape.width / 2 - a.shape.width / 2; } } else if(a.last.x - a.shape.width /2 >= b.last.x + b.shape.width / 2) { - if(a.collide(b, 3)) { + if(a.collide(b)) { a.position.x = b.position.x + b.shape.width / 2 + a.shape.width / 2; } } } else if(a.shape instanceof game.Circle && b.shape instanceof game.Circle) { if(a.collide(b)) { - var angle = Math.atan2(a.position.x - b.position.x, a.position.y - b.position.y); + var angle = b.position.angle(a.position); var dist = a.shape.radius + b.shape.radius; - var aSpeed = Math.sqrt(Math.pow(a.velocity.x, 2) + Math.pow(a.velocity.y, 2)); - var bSpeed = Math.sqrt(Math.pow(b.velocity.x, 2) + Math.pow(b.velocity.y, 2)); - - a.position.x = b.position.x + Math.sin(angle) * dist; - a.position.y = b.position.y + Math.cos(angle) * dist; - - a.velocity.x = Math.sin(angle) * bSpeed; - a.velocity.y = Math.cos(angle) * bSpeed; - - b.velocity.x = Math.sin(angle + Math.PI) * aSpeed; - b.velocity.y = Math.cos(angle + Math.PI) * aSpeed; + a.position.x = b.position.x + Math.cos(angle) * dist; + a.position.y = b.position.y + Math.sin(angle) * dist; } } else if(a.shape instanceof game.Rectangle && b.shape instanceof game.Circle) { if(a.collide(b)) { + // TODO + return; } } else if(a.shape instanceof game.Circle && b.shape instanceof game.Rectangle) { if(a.collide(b)) { - if(a.last.x - a.shape.radius < b.last.x + b.shape.width) { - var angle = Math.atan2(a.position.x - a.last.x, a.position.y - a.last.y); - var dist = this.distance(a.last.x, a.last.y, a.position.x, a.position.y); - - // how much to push circle forward from last position ??? - var dist = 0; - - a.position.x = a.last.x + Math.sin(angle) * dist; - a.position.y = a.last.y + Math.cos(angle) * dist; - - a.velocity.x *= -1; - } + // TODO + return; } } else if(a.shape instanceof game.Line && b.shape instanceof game.Line) { if(a.collide(b)) { + // TODO + return; } } else if(a.shape instanceof game.Circle && b.shape instanceof game.Line) { if(a.collide(b)) { - var cx = this.hitTestData[0]; - var cy = this.hitTestData[1]; - var d = this.hitTestData[2]; - var nx = this.hitTestData[3]; - var ny = this.hitTestData[4]; - - var x = a.position.x - cx; - var y = a.position.y - cy; - - a.position.x = cx + a.shape.radius * x / d; - a.position.y = cy + a.shape.radius * y / d; - - var m = a.velocity.x * nx + a.velocity.y * ny; - - a.velocity.x -= nx * m * (1 + 1); - a.velocity.y -= ny * m * (1 + 1); + // TODO + return; } } else if(a.shape instanceof game.Line && b.shape instanceof game.Circle) { if(a.collide(b)) { + // TODO + return; } } } @@ -719,7 +667,7 @@ game.Vector = game.Class.extend({ @method round @return {game.Vector} **/ - round: function(target) { + round: function() { this.x = Math.round(this.x); this.y = Math.round(this.y); return this; @@ -750,59 +698,4 @@ Object.defineProperty(game.Vector.prototype, 'y', { } }); -game.BodySprite = game.Class.extend({ - collisionGroup: 0, - collideAgainst: null, - mass: 0, - velocity: {x:0, y:0}, - color: 0xff0000, - type: 'Circle', - a: 50, - b: 50, - - init: function(x, y, settings) { - game.merge(this, settings); - - var shape = new game[this.type](this.a, this.b); - - this.body = new game.Body({ - position: {x: x, y: y}, - velocity: {x: this.velocity.x, y: this.velocity.y}, - collisionGroup: this.collisionGroup, - collideAgainst: this.collideAgainst, - mass: this.mass - }); - this.body.addShape(shape); - this.body.collide = this.collide.bind(this); - - this.sprite = new PIXI.Graphics(); - this.sprite.beginFill(this.color); - if(this.type === 'Circle') { - this.sprite.drawCircle(0, 0, this.a); - } - if(this.type === 'Rectangle') { - this.sprite.drawRect(-this.a / 2, -this.b / 2, this.a, this.b); - } - if(this.type === 'Line') { - this.sprite.lineStyle(1, this.color); - this.sprite.moveTo(-Math.sin(this.b) * (this.a / 2), -Math.cos(this.b) * (this.a / 2)); - this.sprite.lineTo(Math.sin(this.b) * (this.a / 2), Math.cos(this.b) * (this.a / 2)); - } - this.sprite.position.x = x; - this.sprite.position.y = y; - - game.scene.world.addBody(this.body); - }, - - collide: function() { - return true; - }, - - update: function() { - this.sprite.position.x = this.body.position.x; - this.sprite.position.y = this.body.position.y; - this.sprite.rotation = this.body.rotation; - } -}); - }); \ No newline at end of file From 0f9c59262ef7f66846c7ee6588b6a6798ed87969 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 15:23:49 +0200 Subject: [PATCH 08/10] Fix: game.copy not working, if game.Container not defined --- src/engine/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/core.js b/src/engine/core.js index 24837f54..834096a0 100755 --- a/src/engine/core.js +++ b/src/engine/core.js @@ -119,7 +119,7 @@ var core = { !object || typeof(object) !== 'object' || object instanceof HTMLElement || object instanceof game.Class || - object instanceof game.Container + (game.Container && object instanceof game.Container) ) { return object; } From fbb4ced05ee15504da1c830038c90d61577c897b Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 16:52:05 +0200 Subject: [PATCH 09/10] New: Added keyboard module (fix #8) --- src/engine/keyboard.js | 128 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/engine/keyboard.js diff --git a/src/engine/keyboard.js b/src/engine/keyboard.js new file mode 100644 index 00000000..029b6daa --- /dev/null +++ b/src/engine/keyboard.js @@ -0,0 +1,128 @@ +game.module( + 'engine.keyboard' +) +.body(function() { + +game.Keyboard = game.Class.extend({ + keys: [], + + init: function() { + window.addEventListener('keydown', this.keydown.bind(this), false); + window.addEventListener('keyup', this.keyup.bind(this), false); + }, + + keydown: function(event) { + if(this.keys[event.keyCode]) return; + this.keys[event.keyCode] = true; + if(game.scene && typeof(game.scene.keydown) === 'function') game.scene.keydown(event.keyCode); + }, + + keyup: function(event) { + this.keys[event.keyCode] = false; + if(game.scene && typeof(game.scene.keyup) === 'function') game.scene.keyup(event.keyCode); + } +}); + +// https://code.google.com/p/closure-library/source/browse/closure/goog/events/keycodes.js +game.KEY = { + WIN_KEY_FF_LINUX: 0, + MAC_ENTER: 3, + BACKSPACE: 8, + TAB: 9, + NUM_CENTER: 12, // NUMLOCK on FF/Safari Mac + ENTER: 13, + SHIFT: 16, + CTRL: 17, + ALT: 18, + PAUSE: 19, + CAPS_LOCK: 20, + ESC: 27, + SPACE: 32, + PAGE_UP: 33, // also NUM_NORTH_EAST + PAGE_DOWN: 34, // also NUM_SOUTH_EAST + END: 35, // also NUM_SOUTH_WEST + HOME: 36, // also NUM_NORTH_WEST + LEFT: 37, // also NUM_WEST + UP: 38, // also NUM_NORTH + RIGHT: 39, // also NUM_EAST + DOWN: 40, // also NUM_SOUTH + PRINT_SCREEN: 44, + INSERT: 45, // also NUM_INSERT + DELETE: 46, // also NUM_DELETE + ZERO: 48, + ONE: 49, + TWO: 50, + THREE: 51, + FOUR: 52, + FIVE: 53, + SIX: 54, + SEVEN: 55, + EIGHT: 56, + NINE: 57, + FF_SEMICOLON: 59, // Firefox (Gecko) fires this for semicolon instead of 186 + FF_EQUALS: 61, // Firefox (Gecko) fires this for equals instead of 187 + FF_DASH: 173, // Firefox (Gecko) fires this for dash instead of 189 + QUESTION_MARK: 63, // needs localization + A: 65, + B: 66, + C: 67, + D: 68, + E: 69, + F: 70, + G: 71, + H: 72, + I: 73, + J: 74, + K: 75, + L: 76, + M: 77, + N: 78, + O: 79, + P: 80, + Q: 81, + R: 82, + S: 83, + T: 84, + U: 85, + V: 86, + W: 87, + X: 88, + Y: 89, + Z: 90, + META: 91, // WIN_KEY_LEFT + WIN_KEY_RIGHT: 92, + CONTEXT_MENU: 93, + NUM_ZERO: 96, + NUM_ONE: 97, + NUM_TWO: 98, + NUM_THREE: 99, + NUM_FOUR: 100, + NUM_FIVE: 101, + NUM_SIX: 102, + NUM_SEVEN: 103, + NUM_EIGHT: 104, + NUM_NINE: 105, + NUM_MULTIPLY: 106, + NUM_PLUS: 107, + NUM_MINUS: 109, + NUM_PERIOD: 110, + NUM_DIVISION: 111, + F1: 112, + F2: 113, + F3: 114, + F4: 115, + F5: 116, + F6: 117, + F7: 118, + F8: 119, + F9: 120, + F10: 121, + F11: 122, + F12: 123, + NUMLOCK: 144, + SCROLL_LOCK: 145 +}; + +game.keyboard = new game.Keyboard(); + +}); \ No newline at end of file From 55b3450eaa42f2d05692af34625c8d55f822b5c9 Mon Sep 17 00:00:00 2001 From: Eemeli Kelokorpi Date: Sat, 15 Feb 2014 19:40:38 +0200 Subject: [PATCH 10/10] Keyboard module documentation and redefine --- src/engine/core.js | 5 + src/engine/keyboard.js | 327 ++++++++++++++++++++++++++++------------- src/engine/scene.js | 10 ++ 3 files changed, 237 insertions(+), 105 deletions(-) diff --git a/src/engine/core.js b/src/engine/core.js index 834096a0..03cf1324 100755 --- a/src/engine/core.js +++ b/src/engine/core.js @@ -71,6 +71,11 @@ var core = { @property {game.Storage} storage **/ storage: null, + /** + Instance of {{#crossLink "game.Keyboard"}}{{/crossLink}}. + @property {game.Keyboard} keyboard + **/ + keyboard: null, /** Device / browser detection. diff --git a/src/engine/keyboard.js b/src/engine/keyboard.js index 029b6daa..940212e9 100644 --- a/src/engine/keyboard.js +++ b/src/engine/keyboard.js @@ -1,127 +1,244 @@ +/** + @module keyboard + @namespace game +**/ game.module( 'engine.keyboard' ) .body(function() { - + +/** + Instance automatically created at {{#crossLink "game.Core"}}{{/crossLink}} + @class Keyboard + @extends game.Class +**/ game.Keyboard = game.Class.extend({ + /** + List of keys. + + BACKSPACE + TAB + ENTER + SHIFT + CTRL + ALT + PAUSE + CAPS_LOCK + ESC + SPACE + PAGE_UP + PAGE_DOWN + END + HOME + LEFT + UP + RIGHT + DOWN + PRINT_SCREEN + INSERT + DELETE + ZERO + ONE + TWO + THREE + FOUR + FIVE + SIX + SEVEN + EIGHT + NINE + A + B + C + D + E + F + G + H + I + J + K + L + M + N + O + P + Q + R + S + T + U + V + W + X + Y + Z + NUM_ZERO + NUM_ONE + NUM_TWO + NUM_THREE + NUM_FOUR + NUM_FIVE + NUM_SIX + NUM_SEVEN + NUM_EIGHT + NUM_NINE + NUM_MULTIPLY + NUM_PLUS + NUM_MINUS + NUM_PERIOD + NUM_DIVISION + F1 + F2 + F3 + F4 + F5 + F6 + F7 + F8 + F9 + F10 + F11 + F12 + + @property {Array} keys + **/ keys: [], + /** + List of keys, that are pressed down. + @property {Array} keysDown + **/ + keysDown: [], init: function() { + this.keys[8] = 'BACKSPACE'; + this.keys[9] = 'TAB'; + this.keys[13] = 'ENTER'; + this.keys[16] = 'SHIFT'; + this.keys[17] = 'CTRL'; + this.keys[18] = 'ALT'; + this.keys[19] = 'PAUSE'; + this.keys[20] = 'CAPS_LOCK'; + this.keys[27] = 'ESC'; + this.keys[32] = 'SPACE'; + this.keys[33] = 'PAGE_UP'; + this.keys[34] = 'PAGE_DOWN'; + this.keys[35] = 'END'; + this.keys[36] = 'HOME'; + this.keys[37] = 'LEFT'; + this.keys[38] = 'UP'; + this.keys[39] = 'RIGHT'; + this.keys[40] = 'DOWN'; + this.keys[44] = 'PRINT_SCREEN'; + this.keys[45] = 'INSERT'; + this.keys[46] = 'DELETE'; + this.keys[48] = 'ZERO'; + this.keys[49] = 'ONE'; + this.keys[50] = 'TWO'; + this.keys[51] = 'THREE'; + this.keys[52] = 'FOUR'; + this.keys[53] = 'FIVE'; + this.keys[54] = 'SIX'; + this.keys[55] = 'SEVEN'; + this.keys[56] = 'EIGHT'; + this.keys[57] = 'NINE'; + this.keys[65] = 'A'; + this.keys[66] = 'B'; + this.keys[67] = 'C'; + this.keys[68] = 'D'; + this.keys[69] = 'E'; + this.keys[70] = 'F'; + this.keys[71] = 'G'; + this.keys[72] = 'H'; + this.keys[73] = 'I'; + this.keys[74] = 'J'; + this.keys[75] = 'K'; + this.keys[76] = 'L'; + this.keys[77] = 'M'; + this.keys[78] = 'N'; + this.keys[79] = 'O'; + this.keys[80] = 'P'; + this.keys[81] = 'Q'; + this.keys[82] = 'R'; + this.keys[83] = 'S'; + this.keys[84] = 'T'; + this.keys[85] = 'U'; + this.keys[86] = 'V'; + this.keys[87] = 'W'; + this.keys[88] = 'X'; + this.keys[89] = 'Y'; + this.keys[90] = 'Z'; + this.keys[96] = 'NUM_ZERO'; + this.keys[97] = 'NUM_ONE'; + this.keys[98] = 'NUM_TWO'; + this.keys[99] = 'NUM_THREE'; + this.keys[100] = 'NUM_FOUR'; + this.keys[101] = 'NUM_FIVE'; + this.keys[102] = 'NUM_SIX'; + this.keys[103] = 'NUM_SEVEN'; + this.keys[104] = 'NUM_EIGHT'; + this.keys[105] = 'NUM_NINE'; + this.keys[106] = 'NUM_MULTIPLY'; + this.keys[107] = 'NUM_PLUS'; + this.keys[109] = 'NUM_MINUS'; + this.keys[110] = 'NUM_PERIOD'; + this.keys[111] = 'NUM_DIVISION'; + this.keys[112] = 'F1'; + this.keys[113] = 'F2'; + this.keys[114] = 'F3'; + this.keys[115] = 'F4'; + this.keys[116] = 'F5'; + this.keys[117] = 'F6'; + this.keys[118] = 'F7'; + this.keys[119] = 'F8'; + this.keys[120] = 'F9'; + this.keys[121] = 'F10'; + this.keys[122] = 'F11'; + this.keys[123] = 'F12'; + window.addEventListener('keydown', this.keydown.bind(this), false); window.addEventListener('keyup', this.keyup.bind(this), false); }, + /** + @method keydown + **/ keydown: function(event) { - if(this.keys[event.keyCode]) return; - this.keys[event.keyCode] = true; - if(game.scene && typeof(game.scene.keydown) === 'function') game.scene.keydown(event.keyCode); + if(game.Keyboard.preventDefault) event.preventDefault(); + + if(!this.keys[event.keyCode]) return; // unkown key + if(this.keysDown[this.keys[event.keyCode]]) return; // key already down + + this.keysDown[this.keys[event.keyCode]] = true; + if(game.scene) game.scene.keydown(this.keys[event.keyCode]); }, + /** + @method keyup + **/ keyup: function(event) { - this.keys[event.keyCode] = false; - if(game.scene && typeof(game.scene.keyup) === 'function') game.scene.keyup(event.keyCode); + if(game.Keyboard.preventDefault) event.preventDefault(); + + this.keysDown[this.keys[event.keyCode]] = false; + if(game.scene) game.scene.keyup(this.keys[event.keyCode]); + }, + + /** + Check if key is pressed down. + @method down + @return {Boolean} + **/ + down: function(key) { + return (this.keysDown[key]); } }); -// https://code.google.com/p/closure-library/source/browse/closure/goog/events/keycodes.js -game.KEY = { - WIN_KEY_FF_LINUX: 0, - MAC_ENTER: 3, - BACKSPACE: 8, - TAB: 9, - NUM_CENTER: 12, // NUMLOCK on FF/Safari Mac - ENTER: 13, - SHIFT: 16, - CTRL: 17, - ALT: 18, - PAUSE: 19, - CAPS_LOCK: 20, - ESC: 27, - SPACE: 32, - PAGE_UP: 33, // also NUM_NORTH_EAST - PAGE_DOWN: 34, // also NUM_SOUTH_EAST - END: 35, // also NUM_SOUTH_WEST - HOME: 36, // also NUM_NORTH_WEST - LEFT: 37, // also NUM_WEST - UP: 38, // also NUM_NORTH - RIGHT: 39, // also NUM_EAST - DOWN: 40, // also NUM_SOUTH - PRINT_SCREEN: 44, - INSERT: 45, // also NUM_INSERT - DELETE: 46, // also NUM_DELETE - ZERO: 48, - ONE: 49, - TWO: 50, - THREE: 51, - FOUR: 52, - FIVE: 53, - SIX: 54, - SEVEN: 55, - EIGHT: 56, - NINE: 57, - FF_SEMICOLON: 59, // Firefox (Gecko) fires this for semicolon instead of 186 - FF_EQUALS: 61, // Firefox (Gecko) fires this for equals instead of 187 - FF_DASH: 173, // Firefox (Gecko) fires this for dash instead of 189 - QUESTION_MARK: 63, // needs localization - A: 65, - B: 66, - C: 67, - D: 68, - E: 69, - F: 70, - G: 71, - H: 72, - I: 73, - J: 74, - K: 75, - L: 76, - M: 77, - N: 78, - O: 79, - P: 80, - Q: 81, - R: 82, - S: 83, - T: 84, - U: 85, - V: 86, - W: 87, - X: 88, - Y: 89, - Z: 90, - META: 91, // WIN_KEY_LEFT - WIN_KEY_RIGHT: 92, - CONTEXT_MENU: 93, - NUM_ZERO: 96, - NUM_ONE: 97, - NUM_TWO: 98, - NUM_THREE: 99, - NUM_FOUR: 100, - NUM_FIVE: 101, - NUM_SIX: 102, - NUM_SEVEN: 103, - NUM_EIGHT: 104, - NUM_NINE: 105, - NUM_MULTIPLY: 106, - NUM_PLUS: 107, - NUM_MINUS: 109, - NUM_PERIOD: 110, - NUM_DIVISION: 111, - F1: 112, - F2: 113, - F3: 114, - F4: 115, - F5: 116, - F6: 117, - F7: 118, - F8: 119, - F9: 120, - F10: 121, - F11: 122, - F12: 123, - NUMLOCK: 144, - SCROLL_LOCK: 145 -}; +/** + Prevent default keyboard action. + @attribute {Boolean} preventDefault + @default false +**/ +game.Keyboard.preventDefault = false; game.keyboard = new game.Keyboard(); diff --git a/src/engine/scene.js b/src/engine/scene.js index 3aec0466..3683a578 100755 --- a/src/engine/scene.js +++ b/src/engine/scene.js @@ -218,6 +218,16 @@ game.Scene = game.Class.extend({ **/ mouseout: function() {}, + /** + @method keydown + **/ + keydown: function() {}, + + /** + @method keyup + **/ + keyup: function() {}, + run: function() { this.update(); if(game.debugDraw) game.debugDraw.update();