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

	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;