summaryrefslogtreecommitdiff
path: root/game/plugins/orientation.js
blob: 8ab922b39245ff5156e40bcdc91d25ca6520856e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';

var Orientation = function() {
	var self = this;

	self.onLeft = new Phaser.Signal();
	self.onRight = new Phaser.Signal();
	self.onUp = new Phaser.Signal();
	self.onDown = new Phaser.Signal();

	self.previousEvent = {
		gamma: 0,
		beta: 0
	};

	self.threshhold = 15;

	if (window.DeviceOrientationEvent) {
		window.addEventListener('deviceorientation', function(eventData) {
			if (eventData.gamma < -self.threshhold && self.previousEvent.gamma >= -self.threshhold) {
				self.onLeft.dispatch();
			}
			if (eventData.gamma > self.threshhold && self.previousEvent.gamma <= self.threshhold) {
				self.onRight.dispatch();
			}
			if (eventData.beta < -self.threshhold && self.previousEvent.beta >= -self.threshhold) {
				self.onUp.dispatch();
			}
			if (eventData.beta > self.threshhold && self.previousEvent.beta <= self.threshhold) {
				self.onDown.dispatch();
			}

			self.previousEvent.gamma = eventData.gamma;
			self.previousEvent.beta = eventData.beta;
		});
	}
};


module.exports = Orientation;