Sfoglia il codice sorgente

update for joystick and rumble

JimMarlowe 9 anni fa
parent
commit
f09b59266e

+ 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", {

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

@@ -14,8 +14,43 @@ 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 )
+                    self.fire();
+
+            if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_LEFT )
+                self.moveLeft();
+            if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_RIGHT )
+                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 +78,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 +91,7 @@ exports.component = function(self) {
 
     SpaceGame.spawnBullet(pos, true);
 
+    self.js_fire = false; // reset js request
   }
 
   function moveShip(timeStep) {
@@ -64,10 +103,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 +142,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;
+  }
+
 };

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

@@ -196,6 +196,13 @@ exports.component = function(self) {
     self.camera = camera;
     self.viewport = viewport;
 
+    if ( Atomic.input.numJoysticks > 0 ) {
+         game.jsid = 0
+
+        scene.subscribeToEvent("JoystickConnected", function(ev) {
+            game.jsid = ev.JoystickID; // get the joystick id for future calls.
+        });
+    }
   }
 
 
@@ -247,6 +254,7 @@ exports.component = function(self) {
       Atomic.input.bindButton(self.fireButton, Atomic.KEY_SPACE);
     }
 
+
     spawnPlayer();
     spawnEnemies();
 

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

@@ -8,6 +8,7 @@ function Game() {
 	this.renderer = Atomic.getRenderer();
 	this.graphics = Atomic.getGraphics();
 	this.input = Atomic.getInput();
+    this.jsid = -1;  // joystick, rumble start off
 
   this.input.setMouseVisible(true);