Skip to content

Commit

Permalink
Merge pull request #1 from gameclosure/devkit2
Browse files Browse the repository at this point in the history
Devkit2
  • Loading branch information
collingreen committed Nov 25, 2014
2 parents 57b96f5 + c35b4af commit 007c7be
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 67 deletions.
116 changes: 76 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions index.js

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import .accelerometer;
import accelerometer;
import device;

var samples = [];
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "devkit-accelerometer",
"version": "0.0.1",
"devkit": {
"clientPaths": {
"accelerometer": "js/accelerometer"
},
"extensions": {
"android": "android",
"ios": "ios"
}
}
}

0 comments on commit 007c7be

Please sign in to comment.