summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@entelect.co.za>2014-07-31 16:45:33 +0200
committerJustin Worthe <justin.worthe@entelect.co.za>2014-07-31 16:45:33 +0200
commit0a4b32158d6ddd17eb9df21ff5940c736eb8208e (patch)
treedfba00090556cbb2baf2e4a99d7c3929c0341b49
parenta5718bd0e29a6245d2e82a272a616782187ee283 (diff)
Attempt at adding orientation based input
-rw-r--r--game/main.js3
-rw-r--r--game/plugins/orientation.js38
-rw-r--r--game/states/play.js13
-rw-r--r--package.json4
-rw-r--r--templates/_main.js.tpl3
5 files changed, 59 insertions, 2 deletions
diff --git a/game/main.js b/game/main.js
index e2812da..a44c949 100644
--- a/game/main.js
+++ b/game/main.js
@@ -4,6 +4,9 @@
window.onload = function () {
var game = new Phaser.Game(1100, 950, Phaser.AUTO, 'interactive-pacbot');
+ var Orientation = require('./plugins/orientation');
+ game.orientation = new Orientation();
+
// Game States
game.state.add('boot', require('./states/boot'));
game.state.add('gameover', require('./states/gameover'));
diff --git a/game/plugins/orientation.js b/game/plugins/orientation.js
new file mode 100644
index 0000000..77ad9fc
--- /dev/null
+++ b/game/plugins/orientation.js
@@ -0,0 +1,38 @@
+'use strict';
+
+var Orientation = function() {
+ this.onLeft = new Phaser.Signal();
+ this.onRight = new Phaser.Signal();
+ this.onUp = new Phaser.Signal();
+ this.onDown = new Phaser.Signal();
+
+ var previousEvent = {
+ gamma: 0,
+ beta: 0
+ };
+
+ var threshhold = 15;
+
+ var processOrientationEvent = function(event) {
+ if (event.gamma < -threshhold && previousEvent.gamma >= -threshhold) {
+ this.onLeft.dispatch();
+ }
+ if (event.gamma > threshhold && previousEvent.gamma <= threshhold) {
+ this.onRight.dispatch();
+ }
+ if (event.beta < -threshhold && previousEvent.beta >= -threshhold) {
+ this.onUp.dispatch();
+ }
+ if (event.beta > threshhold && previousEvent.beta <= threshhold) {
+ this.onDown.dispatch();
+ }
+
+
+ previousEvent = event;
+ }
+
+ window.addEventListener('deviceorientation', processOrientationEvent);
+}
+
+
+module.exports = Orientation;
diff --git a/game/states/play.js b/game/states/play.js
index 1c4a926..6ea7611 100644
--- a/game/states/play.js
+++ b/game/states/play.js
@@ -183,6 +183,19 @@ Play.prototype = {
addKeyCaptures(this.playerBControls, this.game.input.keyboard);
this.game.input.gamepad.start();
+
+ this.game.orientation.onLeft.add(function() {
+ this.movePlayer(this.players.children[this.playerTurn], -1, 0);
+ }, this);
+ this.game.orientation.onRight.add(function() {
+ this.movePlayer(this.players.children[this.playerTurn], 1, 0);
+ }, this);
+ this.game.orientation.onUp.add(function() {
+ this.movePlayer(this.players.children[this.playerTurn], 0, -1);
+ }, this);
+ this.game.orientation.onDown.add(function() {
+ this.movePlayer(this.players.children[this.playerTurn], 0, 1);
+ }, this);
},
movePlayer: function(player, deltaX, deltaY) {
var newX = player.x + deltaX;
diff --git a/package.json b/package.json
index cd6c109..0128a7c 100644
--- a/package.json
+++ b/package.json
@@ -24,8 +24,8 @@
"title": "Interactive Pacbot",
"toolbar": false,
"frame": true,
- "width": 820,
- "height": 620,
+ "width": 1100,
+ "height": 950,
"position": "center"
}
} \ No newline at end of file
diff --git a/templates/_main.js.tpl b/templates/_main.js.tpl
index 0b1ddb9..6a559db 100644
--- a/templates/_main.js.tpl
+++ b/templates/_main.js.tpl
@@ -4,6 +4,9 @@
window.onload = function () {
var game = new Phaser.Game(<%= gameWidth %>, <%= gameHeight %>, Phaser.AUTO, '<%= _.slugify(projectName) %>');
+ var Orientation = require('./plugins/orientation');
+ game.orientation = new Orientation();
+
// Game States
<% _.forEach(gameStates, function(gameState) { %>game.state.add('<%= gameState.shortName %>', require('./states/<%= gameState.shortName %>'));
<% }); %>