Attempt at adding orientation based input
[interactive-pacbot.git] / game / plugins / orientation.js
1 'use strict';
2
3 var Orientation = function() {
4         this.onLeft = new Phaser.Signal();
5         this.onRight = new Phaser.Signal();
6         this.onUp = new Phaser.Signal();
7         this.onDown = new Phaser.Signal();
8
9         var previousEvent = {
10                 gamma: 0,
11                 beta: 0
12         };
13
14         var threshhold = 15;
15
16         var processOrientationEvent = function(event) {
17                 if (event.gamma < -threshhold && previousEvent.gamma >= -threshhold) {
18                         this.onLeft.dispatch();
19                 }
20                 if (event.gamma > threshhold && previousEvent.gamma <= threshhold) {
21                         this.onRight.dispatch();
22                 }
23                 if (event.beta < -threshhold && previousEvent.beta >= -threshhold) {
24                         this.onUp.dispatch();
25                 }
26                 if (event.beta > threshhold && previousEvent.beta <= threshhold) {
27                         this.onDown.dispatch();
28                 }
29
30
31                 previousEvent = event;
32         }
33
34         window.addEventListener('deviceorientation', processOrientationEvent);
35 }
36
37
38 module.exports = Orientation;