diff --git a/README.md b/README.md index f9408d0..0f1894d 100644 --- a/README.md +++ b/README.md @@ -7,64 +7,100 @@ Installation instructions: Run this command: ~~~ -basil install accelerometer +devkit install https://github.com/gameclosure/accelerometer#devkit2 ~~~ Example usage from inside a JS game: ~~~ -import plugins.accelerometer.accelerometer as accelerometer; +import accelerometer; ~~~ ~~~ - var pi = Math.PI; - var abs = Math.abs; - var atan2 = Math.atan2; - - accelerometer.start(bind(this, function(evt) { - var x = -evt.x; - var y = -evt.y; - var z = -evt.z; - var forwardTilt = atan2(z, y); - var tilt = atan2(x, y); - var twist = atan2(x, z); - var t = abs(forwardTilt / (pi / 2)); - if (t > 1) { - t = 2 - t; - } - if (twist > pi / 2) { - twist = (pi - twist); - } - if (twist < -pi / 2) { - twist = -(pi + twist); - } - - var interpolatedTilt = tilt * (1-t) + twist * t; - - this.controller.tilt = -interpolatedTilt; - })); +var pi = Math.PI; +var abs = Math.abs; +var atan2 = Math.atan2; + +accelerometer.start(bind(this, function(evt) { + var x = -evt.x; + var y = -evt.y; + var z = -evt.z; + var forwardTilt = atan2(z, y); + var tilt = atan2(x, y); + var twist = atan2(x, z); + var t = abs(forwardTilt / (pi / 2)); + if (t > 1) { + t = 2 - t; + } + if (twist > pi / 2) { + twist = (pi - twist); + } + if (twist < -pi / 2) { + twist = -(pi + twist); + } + + var interpolatedTilt = tilt * (1-t) + twist * t; + + this.controller.tilt = -interpolatedTilt; +})); ... - accelerometer.stop(); +accelerometer.stop(); ~~~ -The Accelerometer plugin also includes a ShakeDetect JS module. +The Accelerometer plugin also includes a ShakeDetect JS module inside the util +folder. Example usage: ~~~ -import plugins.accelerometer.ShakeDetect as ShakeDetect; -~~~ - -~~~ -ShakeDetect.start(function() { - logger.log("User shook the phone"). +import ui.TextView as TextView; +import accelerometer.util.shakedetect as shakedetect; + + +SHAKE_COOLDOWN = 2000; + +exports = Class(GC.Application, function () { + + this.initUI = function () { + this.shakeText = new TextView({ + superview: this.view, + text: "...", + color: "white", + x: 0, + y: 150, + width: this.view.style.width, + height: 100 + }); + + // turn off shakedetect on hide, on on show + GC.on('hide', function () { shakedetect.stop(); }); + GC.on('show', bind(this, function () { this.startshakedetect(); })); + + // start listening for shakes + this.shakeCooldown = 0; + this.startshakedetect(); + }; + + this.startshakedetect = function () { + shakedetect.start(bind(this, function() { + logger.log("User shook the phone!"); + this.shakeText.setText("Don't shake me bro!"); + this.shakeCooldown = SHAKE_COOLDOWN; + })); + }; + + this.tick = function (dt) { + if (this.shakeCooldown > 0) { + this.shakeCooldown -= dt; + + if (this.shakeCooldown <= 0) { + this.shakeText.setText('...'); + } + } + }; }); - -... - -ShakeDetect.stop(); ~~~ Be sure to call .stop() when done handling events. Also, only one event handler diff --git a/index.js b/index.js deleted file mode 100644 index 88bf29f..0000000 --- a/index.js +++ /dev/null @@ -1,26 +0,0 @@ -var path = require("path"); -var fs = require("fs"); -var ff = require("ff"); -var clc = require("cli-color"); - -//called when addon is activated -exports.init = function (common) { - var logger = new common.Formatter('geoloc'); - - //logger.log("Initializing"); - - exports.load(common); -}; - -exports.load = function (common) { - var logger = new common.Formatter('geoloc'); - - //logger.log("Loading"); -} - -exports.testapp = function (common, opts, next) { - var logger = new common.Formatter('geoloc'); - - //logger.log("TestApp Initializing"); -} - diff --git a/js/accelerometer.js b/js/accelerometer/index.js similarity index 100% rename from js/accelerometer.js rename to js/accelerometer/index.js diff --git a/js/ShakeDetect.js b/js/accelerometer/util/shakedetect/index.js similarity index 99% rename from js/ShakeDetect.js rename to js/accelerometer/util/shakedetect/index.js index 61fb627..9fb502b 100644 --- a/js/ShakeDetect.js +++ b/js/accelerometer/util/shakedetect/index.js @@ -1,4 +1,4 @@ -import .accelerometer; +import accelerometer; import device; var samples = []; diff --git a/package.json b/package.json new file mode 100644 index 0000000..601ba16 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "devkit-accelerometer", + "version": "0.0.1", + "devkit": { + "clientPaths": { + "accelerometer": "js/accelerometer" + }, + "extensions": { + "android": "android", + "ios": "ios" + } + } +}