Browse Source

add joystick UI control

JimMarlowe 9 years ago
parent
commit
083572afc6

+ 5 - 2
SpaceGame/Resources/Components/Player.js

@@ -30,12 +30,15 @@ exports.component = function(self) {
 	            || ev.Button == Atomic.CONTROLLER_BUTTON_B 
 	            || ev.Button == Atomic.CONTROLLER_BUTTON_B 
                 || ev.Button == Atomic.CONTROLLER_BUTTON_LEFTSHOULDER
                 || ev.Button == Atomic.CONTROLLER_BUTTON_LEFTSHOULDER
                 || ev.Button == Atomic.CONTROLLER_BUTTON_RIGHTSHOULDER )
                 || ev.Button == Atomic.CONTROLLER_BUTTON_RIGHTSHOULDER )
+                if (!SpaceGame.gameOver)
                     self.fire();
                     self.fire();
 
 
             if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_LEFT )
             if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_LEFT )
-                self.moveLeft();
+                if (!SpaceGame.gameOver)
+                    self.moveLeft();
             if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_RIGHT )
             if ( ev.Button == Atomic.CONTROLLER_BUTTON_DPAD_RIGHT )
-                self.moveRight();
+                if (!SpaceGame.gameOver)
+                    self.moveRight();
         });
         });
 
 
         scene.subscribeToEvent("JoystickButtonUp", function(ev) {
         scene.subscribeToEvent("JoystickButtonUp", function(ev) {

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

@@ -196,13 +196,6 @@ exports.component = function(self) {
     self.camera = camera;
     self.camera = camera;
     self.viewport = viewport;
     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.
-        });
-    }
   }
   }
 
 
 
 

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

@@ -9,6 +9,8 @@ function Game() {
 	this.graphics = Atomic.getGraphics();
 	this.graphics = Atomic.getGraphics();
 	this.input = Atomic.getInput();
 	this.input = Atomic.getInput();
     this.jsid = -1;  // joystick, rumble start off
     this.jsid = -1;  // joystick, rumble start off
+    this.xsim = 0.0;
+    this.ysim = 0.0;
 
 
   this.input.setMouseVisible(true);
   this.input.setMouseVisible(true);
 
 
@@ -90,6 +92,61 @@ Game.prototype.createScene2D = function() {
     this.camera = camera;
     this.camera = camera;
     this.viewport = viewport;
     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;
     return scene;
 
 
 };
 };

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

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

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

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