Last active
June 21, 2017 21:12
-
-
Save josuigoa/bd94fdb32c2b946bdf2fed027798a893 to your computer and use it in GitHub Desktop.
Getting accelerometer values with Luxe engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import luxe.Input; | |
typedef AccelEvent = { | |
timestamp:Float, | |
value:Float, | |
axis:Int | |
} | |
class Accelerometer { | |
static public var x: Float = 0; | |
static public var y: Float = 0; | |
static public var z: Float = 0; | |
static public function init() { | |
#if mobile | |
Luxe.on(luxe.Ev.gamepadaxis, onaccel); | |
#else | |
Luxe.on(luxe.Ev.mousemove, onaccel); | |
#end | |
} //init | |
static function onaccel( event:#if mobile luxe.Input.GamepadEvent #else luxe.Input.MouseEvent #end) { | |
#if mobile | |
if(event.type == GamepadEventType.axis) { | |
var val = luxe.utils.Maths.fixed(event.value, 2); | |
switch(event.axis) { | |
// facing left -> x = -1 | |
// facing front -> x = 0 | |
// facing right -> x = 1 | |
case 0: x = val; | |
// face up -> y = 1 | |
// flat -> y = 0 | |
// upside down -> y = -1 | |
case 1: y = val; | |
case 2: z = val; | |
} | |
} //if input | |
#else | |
if (Luxe.screen != null) { | |
// x | |
// left: -1, center: 0, right: 1 | |
// y | |
// top: -1, center: 0, bottom: 1 | |
x = (Luxe.screen.cursor.pos.x - Luxe.screen.mid.x) / Luxe.screen.mid.x; | |
y = -(Luxe.screen.cursor.pos.y - Luxe.screen.mid.y) / Luxe.screen.mid.y; | |
} | |
#end | |
//trace('accel: $x, $y, $z'); | |
} //onaccel | |
static public function dispose() { | |
#if mobile | |
Luxe.off(luxe.Ev.gamepadaxis, onaccel); | |
#else | |
Luxe.off(luxe.Ev.mousemove, onaccel); | |
#end | |
} //dispose | |
} //Accelerometer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment