Permalink
Browse files

added tquery.playerinput - fix jeromeetienne/chatandfight#2

  • Loading branch information...
1 parent 7492ffd commit efd04bfa99fed8c574c5108dc010ec17cfefd514 @jeromeetienne committed Mar 4, 2013
@@ -0,0 +1,24 @@
+<!doctype html><title>Minimal tQuery Page</title>
+<script src="../../../build/tquery-bundle-require.js"></script>
+<body><script>
+require(['tquery.playerinput'], function(){
+ var world = tQuery.createWorld().boilerplate().start();
+ var object = tQuery.createTorusKnot().addTo(world);
+
+ // your code goes here
+ world.removeCameraControls();
+
+ var onMobile = 'ontouchstart' in window ? true : false;
+ var playerInput = tQuery.createPlayerInput();
+ onMobile === true && tQuery.PlayerInput.createVirtualJoystick(playerInput)
+ onMobile === false && tQuery.PlayerInput.createKeyboard(playerInput)
+
+ world.loop().hook(function(delta, now){
+ var displacement = 1 * delta;
+ if( playerInput.up ) object.translateZ(-displacement)
+ if( playerInput.down ) object.translateZ( displacement)
+ if( playerInput.left ) object.translateX(-displacement)
+ if( playerInput.right ) object.translateX( displacement)
+ });
+});
+</script></body>
@@ -0,0 +1,31 @@
+<!doctype html><title>Minimal tQuery Page</title>
+<script src="../../../build/tquery-bundle-require.js"></script>
+
+<script src='../tquery.playerinput.js'></script>
+
+<script src="../../../vendor/threex/THREEx.KeyboardState.js"></script>
+<script src="../../keyboard/tquery.keyboard.js"></script>
+<script src='../tquery.playerinput.keyboard.js'></script>
+
+<script src='../../virtualjoystick/vendor/virtualjoystick.js'></script>
+<script src='../tquery.playerinput.virtualjoystick.js'></script>
+
+<body><script>
+ var world = tQuery.createWorld().boilerplate().start();
+ var object = tQuery.createTorusKnot().addTo(world);
+
+ world.removeCameraControls();
+
+ var onMobile = 'ontouchstart' in window ? true : false;
+ var playerInput = tQuery.createPlayerInput();
+ onMobile === true && tQuery.PlayerInput.createVirtualJoystick(playerInput)
+ onMobile === false && tQuery.PlayerInput.createKeyboard(playerInput)
+
+ world.loop().hook(function(delta, now){
+ var displacement = 1 * delta;
+ if( playerInput.up ) object.translateZ(-displacement)
+ if( playerInput.down ) object.translateZ( displacement)
+ if( playerInput.left ) object.translateX(-displacement)
+ if( playerInput.right ) object.translateX( displacement)
+ });
+</script></body>
@@ -0,0 +1,20 @@
+//////////////////////////////////////////////////////////////////////////////////
+// comment //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.registerStatic('PlayerInput', function(){
+ // this is left intentionnaly empty
+ // - up to the drivers to handle it
+});
+
+tQuery.registerStatic('createPlayerInput', function(opts){
+ return new tQuery.PlayerInput(opts)
+});
+
+// make it eventable
+tQuery.MicroeventMixin(tQuery.PlayerInput.prototype)
+
+// make it pluginable as static
+tQuery.pluginsStaticOn(tQuery.PlayerInput);
+
+
@@ -0,0 +1,77 @@
+//////////////////////////////////////////////////////////////////////////////////
+// Constructor //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.PlayerInput.registerStatic('Keyboard', function(opts){
+ // handle arguments polymorphism
+ if( opts instanceof tQuery.PlayerInput ) opts.playerInput = opts;
+ // handle arguments default value
+ opts = tQuery.extend(opts, {
+ world : tQuery.world
+ });
+ // argments sanity check
+ console.assert(opts.playerInput instanceof tQuery.PlayerInput)
+ // your code goes here
+ var input = opts.playerInput;
+ this._world = opts.world;
+ // init input if needed
+ var actions = this._actions();
+ if( input.right=== undefined ) input.right = actions.right
+ if( input.up === undefined ) input.up = actions.up
+ if( input.left === undefined ) input.left = actions.left
+ if( input.down === undefined ) input.down = actions.down
+ // init callback to update inputPlayer
+ this._callback = this._world.loop().hook(function(delta, now){
+ var actions = this._actions();
+ if( actions.right !== input.right ){
+ input.right = actions.right;
+ input.dispatchEvent('change', 'right', input.right)
+ }
+ if( actions.up !== input.up ){
+ input.up = actions.up;
+ input.dispatchEvent('change', 'up', input.up)
+ }
+ if( actions.left !== input.left ){
+ input.left = actions.left;
+ input.dispatchEvent('change', 'left', input.left)
+ }
+ if( actions.down !== input.down ){
+ input.down = actions.down;
+ input.dispatchEvent('change', 'down', input.down)
+ }
+ }.bind(this));
+});
+
+/**
+ * explicit destructor
+ */
+tQuery.PlayerInput.Keyboard.prototype.destroy = function(){
+ this._world.loop().unhook(this._callback)
+
+ this._joystick.destroy()
+};
+
+//////////////////////////////////////////////////////////////////////////////////
+// comment //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.PlayerInput.registerStatic('createKeyboard', function(opts){
+ return new tQuery.PlayerInput.Keyboard(opts)
+});
+
+/**
+ * return current actions based on keyboard.pressed
+ * compatible with arrow, asdw
+ * @return {[type]} [description]
+ */
+tQuery.PlayerInput.Keyboard.prototype._actions = function() {
+ var keyboard = tQuery.keyboard();
+ var actions = {
+ left : keyboard.pressed("left") || keyboard.pressed("a") || keyboard.pressed("q"),
+ right : keyboard.pressed("right") || keyboard.pressed("d"),
+ up : keyboard.pressed("up") || keyboard.pressed("w") || keyboard.pressed("z"),
+ down : keyboard.pressed("down") || keyboard.pressed("s"),
+ };
+ return actions
+};
+
@@ -0,0 +1,63 @@
+//////////////////////////////////////////////////////////////////////////////////
+// Constructor //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.PlayerInput.registerStatic('VirtualJoystick', function(opts){
+ // handle arguments polymorphism
+ if( opts instanceof tQuery.PlayerInput ) opts.playerInput = opts;
+ // handle arguments default value
+ opts = tQuery.extend(opts, {
+ world : tQuery.world
+ });
+ // argments sanity check
+ console.assert(opts.playerInput instanceof tQuery.PlayerInput)
+ // your code goes here
+ var input = opts.playerInput;
+ this._world = opts.world;
+ // init joystick
+ this._joystick = new VirtualJoystick({
+ container : document.body,
+ mouseSupport : true
+ });
+ // init input if needed
+ if( input.right=== undefined ) input.right = this._joystick.right()
+ if( input.up === undefined ) input.up = this._joystick.up()
+ if( input.left === undefined ) input.left = this._joystick.left()
+ if( input.down === undefined ) input.down = this._joystick.down()
+ // init callback to update input
+ this._callback = this._world.loop().hook(function(delta, now){
+ if( this._joystick.right() !== input.right ){
+ input.right = this._joystick.right();
+ input.dispatchEvent('change', 'right', input.right)
+ }
+ if( this._joystick.up() !== input.up ){
+ input.up = this._joystick.up();
+ input.dispatchEvent('change', 'up', input.up)
+ }
+ if( this._joystick.left() !== input.left ){
+ input.left = this._joystick.left();
+ input.dispatchEvent('change', 'left', input.left)
+ }
+ if( this._joystick.down() !== input.down ){
+ input.down = this._joystick.down();
+ input.dispatchEvent('change', 'down', input.down)
+ }
+ }.bind(this));
+});
+
+/**
+ * explicit destructor
+ */
+tQuery.PlayerInput.VirtualJoystick.prototype.destroy = function(){
+ this._world.loop().unhook(this._callback)
+
+ this._joystick.destroy()
+};
+
+//////////////////////////////////////////////////////////////////////////////////
+// comment //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.PlayerInput.registerStatic('createVirtualJoystick', function(opts){
+ return new tQuery.PlayerInput.VirtualJoystick(opts)
+});
@@ -1,5 +1,5 @@
compile:
- node bin/tomainconfigfile.js > build/all.confrequire.js
+ /usr/local/bin/node bin/tomainconfigfile.js > build/all.confrequire.js
build: compile
cd examples && r.js -o name=main-dev.js out=../build/main-prod.js \
@@ -34,6 +34,7 @@ requirejs.config({
"tquery.objectcoord": "plugins/objectcoord/tquery.object3d.coordinate",
"tquery.physics": "plugins/physics/tquery.physijs",
"tquery.planets": "plugins/requirejs/confrequire/planets.initrequire",
+ "tquery.playerinput": "plugins/playerinput/tquery.playerinput.keyboard",
"tquery.poolball": "plugins/poolball/tquery.poolball",
"tquery.pproc": "plugins/pproc/tquery.effectcomposer",
"tquery.shape": "plugins/shape/tquery.shape",
@@ -165,6 +166,15 @@ requirejs.config({
"plugins/requirejs/confrequire/planets.initrequire": [
"plugins/planets/tquery.createplanet"
],
+ "plugins/playerinput/tquery.playerinput.keyboard": [
+ "tquery.keyboard",
+ "plugins/playerinput/tquery.playerinput",
+ "plugins/playerinput/tquery.playerinput.virtualjoystick"
+ ],
+ "plugins/playerinput/tquery.playerinput.virtualjoystick": [
+ "tquery.virtualjoystick",
+ "plugins/playerinput/tquery.playerinput"
+ ],
"plugins/pproc/tquery.effectcomposer": [
"three.js/shaders/BleachBypassShader",
"three.js/shaders/BlendShader",
@@ -0,0 +1,19 @@
+requirejs.config({
+ map : {
+ "*" : {
+ 'tquery.playerinput' : 'plugins/playerinput/tquery.playerinput.keyboard',
+ }
+ },
+ shim : {
+ 'plugins/playerinput/tquery.playerinput.keyboard' : [
+ 'tquery.keyboard',
+ 'plugins/playerinput/tquery.playerinput',
+ // not really needed, just to chain the load
+ 'plugins/playerinput/tquery.playerinput.virtualjoystick',
+ ],
+ 'plugins/playerinput/tquery.playerinput.virtualjoystick': [
+ 'tquery.virtualjoystick',
+ 'plugins/playerinput/tquery.playerinput'
+ ]
+ }
+});

0 comments on commit efd04bf

Please sign in to comment.