Browse Source

Merge pull request #80 from JimMarlowe/JM-SPACE-JOY

Update for joystick and rumble
JoshEngebretson 9 years ago
parent
commit
54f4b62148

+ 3 - 0
SpaceGame/Resources/Components/CapitalShip.js

@@ -31,6 +31,9 @@ exports.component = function(self) {
 
   function die() {
 
+   if ( game.jsid >= 0 ) // has js and not muted
+       Atomic.input.joystickRumble (game.jsid, 0.9, 300 );
+
     SpaceGame.capitalShipDestroyed();
 
     for (var i = 0; i < 16; i++) {

+ 3 - 0
SpaceGame/Resources/Components/Enemy.js

@@ -15,6 +15,9 @@ exports.component = function(self) {
 
   self.onHit = function() {
 
+    if ( game.jsid >= 0 ) // has js and not muted
+        Atomic.input.joystickRumble (game.jsid, 0.45, 110 );
+
     var expNode = SpaceGame.myscene.createChild("Explosion");
 
     var exp = expNode.createJSComponent("Components/Explosion.js", {

+ 62 - 3
SpaceGame/Resources/Components/Player.js

@@ -14,8 +14,46 @@ exports.component = function(self) {
 
   self.health = 10;
 
+  // joystick support
+  self.js_fire = false;
+  self.js_left = false;
+  self.js_right = false;
+
+  if ( Atomic.input.numJoysticks > 0 ) {
+
+        var scene = SpaceGame.myscene;
+
+        scene.subscribeToEvent("JoystickButtonDown", function(ev) {
+           if ( ev.Button == Atomic.CONTROLLER_BUTTON_X
+                || ev.Button == Atomic.CONTROLLER_BUTTON_Y
+                || ev.Button == Atomic.CONTROLLER_BUTTON_A
+	            || ev.Button == Atomic.CONTROLLER_BUTTON_B 
+                || ev.Button == Atomic.CONTROLLER_BUTTON_LEFTSHOULDER
+                || ev.Button == Atomic.CONTROLLER_BUTTON_RIGHTSHOULDER )
+                if (!SpaceGame.gameOver)
+                    self.fire();
+
+            if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_LEFT )
+                if (!SpaceGame.gameOver)
+                    self.moveLeft();
+            if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_RIGHT )
+                if (!SpaceGame.gameOver)
+                    self.moveRight();
+        });
+
+        scene.subscribeToEvent("JoystickButtonUp", function(ev) {
+            if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_LEFT 
+            || ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_RIGHT )
+                self.moveStop();
+        });
+
+  }
+
   self.onHit = function() {
 
+    if ( game.jsid >= 0 ) // has js and not muted
+        Atomic.input.joystickRumble (game.jsid, 0.75, 250 );
+
     var expNode = SpaceGame.myscene.createChild("Explosion");
     var exp = expNode.createJSComponent("Components/Explosion.js", {
       spawnPosition: node.worldPosition2D
@@ -43,7 +81,10 @@ exports.component = function(self) {
       return;
     }
 
-    if (!input.getKeyDown(Atomic.KEY_W) && !input.getKeyDown(Atomic.KEY_UP) && !input.getKeyDown(Atomic.KEY_SPACE))
+    if (!input.getKeyDown(Atomic.KEY_W) 
+        && !input.getKeyDown(Atomic.KEY_UP) 
+        && !input.getKeyDown(Atomic.KEY_SPACE)
+        && !self.js_fire )
       return;
 
     self.shootDelta = 0.15;
@@ -53,6 +94,7 @@ exports.component = function(self) {
 
     SpaceGame.spawnBullet(pos, true);
 
+    self.js_fire = false; // reset js request
   }
 
   function moveShip(timeStep) {
@@ -64,10 +106,10 @@ exports.component = function(self) {
     var right = false;
 
 
-    if (input.getKeyDown(Atomic.KEY_A) || input.getKeyDown(Atomic.KEY_LEFT))
+    if (input.getKeyDown(Atomic.KEY_A) || input.getKeyDown(Atomic.KEY_LEFT) || self.js_left )
       pos[0] -= speed;
 
-    if (input.getKeyDown(Atomic.KEY_D) || input.getKeyDown(Atomic.KEY_RIGHT))
+    if (input.getKeyDown(Atomic.KEY_D) || input.getKeyDown(Atomic.KEY_RIGHT) || self.js_right )
       pos[0] += speed;
 
     if (pos[0] < -SpaceGame.halfWidth + 2)
@@ -103,4 +145,21 @@ exports.component = function(self) {
 
   };
 
+  self.moveStop = function() {
+    self.js_right = false;  // reset js move requests
+    self.js_left = false;
+  }
+
+  self.moveLeft = function() {
+      self.js_left = true;
+  }
+
+  self.moveRight = function() {
+      self.js_right = true;
+  }
+
+  self.fire = function() {
+      self.js_fire = true;
+  }
+
 };

+ 1 - 0
SpaceGame/Resources/Components/SpaceGame.js

@@ -247,6 +247,7 @@ exports.component = function(self) {
       Atomic.input.bindButton(self.fireButton, Atomic.KEY_SPACE);
     }
 
+
     spawnPlayer();
     spawnEnemies();
 

+ 58 - 0
SpaceGame/Resources/Modules/Game.js

@@ -8,6 +8,9 @@ function Game() {
 	this.renderer = Atomic.getRenderer();
 	this.graphics = Atomic.getGraphics();
 	this.input = Atomic.getInput();
+    this.jsid = -1;  // joystick, rumble start off
+    this.xsim = 0.0;
+    this.ysim = 0.0;
 
   this.input.setMouseVisible(true);
 
@@ -89,6 +92,61 @@ Game.prototype.createScene2D = function() {
     this.camera = camera;
     this.viewport = viewport;
 
+    if ( Atomic.input.numJoysticks > 0 ) {
+
+         this.jsid = 0
+
+        scene.subscribeToEvent("JoystickConnected", function(ev) {
+            this.jsid = ev.JoystickID; // get the joystick id for future calls.
+        });
+
+        // have the joystick drive the UI
+        scene.subscribeToEvent("JoystickButtonDown", function(ev) {
+ 
+           if ( ev.Button == Atomic.CONTROLLER_BUTTON_X 
+                    || ev.Button == Atomic.CONTROLLER_BUTTON_LEFTSTICK )
+                {
+                    Atomic.input.joystickSimulateMouseButton(1); // mouse button 1 press
+                }
+
+        });
+
+       scene.subscribeToEvent("JoystickAxisMove", function(ev) {
+
+            var joyLookDeadZone = 0.05;
+            var joyMoveDistance = 1.7;
+
+             if ( ev.Button < 2 )
+             {
+                    var look1 = 0.0
+                    var look2 = 0.0;
+                    if ( ev.Button == 0) look1 = ev.Position;
+                    if ( ev.Button == 1) look2 = ev.position;
+
+                    if (look1 < -joyLookDeadZone || look1 > joyLookDeadZone) // has a value other than 0
+                        this.xsim += joyMoveDistance * look1;
+                    else this.xsim = 0;
+                    if (look2 < -joyLookDeadZone || look2 > joyLookDeadZone)
+                        this.ysim += joyMoveDistance * look2;
+                    else this.ysim = 0;                       
+            }
+        });
+    }
+
+
+   scene.subscribeToEvent("SceneUpdate", function(ev) {
+
+       if ( Atomic.input.numJoysticks > 0 && Atomic.input.isMouseVisible()) {
+
+            if ( this.xsim == undefined || this.ysim == undefined ) return;  
+            if ( this.xsim != 0.0 || this.ysim != 0.0 )
+            {
+                var nowx = Atomic.input.mousePosition; // ui.cursor.screenPosition; // readonysim        
+                Atomic.input.joystickSimulateMouseMove( nowx[0] + this.xsim,  nowx[1] + this.ysim);
+            }
+       }
+  });
+
     return scene;
 
 };

+ 2 - 0
SpaceGame/Resources/UI/mainMenu.js

@@ -40,6 +40,8 @@ exports.init = function() {
   	var node = game.scene.createChild("SpaceGame");
   	node.createJSComponent("Components/SpaceGame.js");
 
+    if ( Atomic.input.isMouseVisible() )
+         Atomic.input.setMouseVisible(false);
   };
 
   window.getWidget("about").onClick = function () {

+ 12 - 0
SpaceGame/Resources/UI/ui.js

@@ -7,21 +7,33 @@ var options = require("./options");
 
 exports.showMainMenu = function() {
 
+   if ( !Atomic.input.isMouseVisible() )
+         Atomic.input.setMouseVisible(true);
+
     mainMenu.init();
 };
 
 exports.showGameOver = function() {
 
+   if ( !Atomic.input.isMouseVisible() )
+         Atomic.input.setMouseVisible(true);
+
     gameOver.init();
 };
 
 exports.showAbout = function(onClose) {
 
+   if ( !Atomic.input.isMouseVisible() )
+         Atomic.input.setMouseVisible(true);
+
     about.init(onClose);
 };
 
 exports.showOptions = function(onClose) {
 
+   if ( !Atomic.input.isMouseVisible() )
+         Atomic.input.setMouseVisible(true);
+
     options.init(onClose);
 };