summaryrefslogtreecommitdiff
path: root/game/plugins/orientation.js
blob: af5a64983da7040fecf2ebd37db8e76211167151 (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
'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;

	if (window.DeviceOrientationEvent) {
		window.addEventListener('deviceorientation', 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.gamma = event.gamma;
			previousEvent.beta = event.beta;
		});
	}
};


module.exports = Orientation;