Explorar el Código

Do not attempt to use a buttonless joystick (Android / iOS accelerometer) for control in NinjaSnowWar.
Fixed crash in JoystickState when querying nonexistent buttons.

Lasse Öörni hace 13 años
padre
commit
f61e8d7428
Se han modificado 2 ficheros con 51 adiciones y 48 borrados
  1. 47 44
      Bin/Data/Scripts/NinjaSnowWar.as
  2. 4 4
      Engine/Input/Input.h

+ 47 - 44
Bin/Data/Scripts/NinjaSnowWar.as

@@ -830,53 +830,56 @@ void UpdateControls()
         if (input.numJoysticks > 0)
         {
             JoystickState@ joystick = input.joysticks[0];
-            if (joystick.buttonDown[0])
-                playerControls.Set(CTRL_JUMP, true);
-            if (joystick.buttonDown[1])
-                playerControls.Set(CTRL_FIRE, true);
-            if (joystick.numButtons >= 6)
+            if (joystick.numButtons > 0)
             {
-                if (joystick.buttonDown[4])
+                if (joystick.buttonDown[0])
                     playerControls.Set(CTRL_JUMP, true);
-                if (joystick.buttonDown[5])
+                if (joystick.buttonDown[1])
                     playerControls.Set(CTRL_FIRE, true);
-            }
-
-            if (joystick.numHats > 0)
-            {
-                if (joystick.hatPosition[0] & HAT_LEFT != 0)
-                    playerControls.Set(CTRL_LEFT, true);
-                if (joystick.hatPosition[0] & HAT_RIGHT != 0)
-                    playerControls.Set(CTRL_RIGHT, true);
-                if (joystick.hatPosition[0] & HAT_UP != 0)
-                    playerControls.Set(CTRL_UP, true);
-                if (joystick.hatPosition[0] & HAT_DOWN != 0)
-                    playerControls.Set(CTRL_DOWN, true);
-            }
-            if (joystick.numAxes >= 2)
-            {
-                if (joystick.axisPosition[0] < -joyMoveDeadZone)
-                    playerControls.Set(CTRL_LEFT, true);
-                if (joystick.axisPosition[0] > joyMoveDeadZone)
-                    playerControls.Set(CTRL_RIGHT, true);
-                if (joystick.axisPosition[1] < -joyMoveDeadZone)
-                    playerControls.Set(CTRL_UP, true);
-                if (joystick.axisPosition[1] > joyMoveDeadZone)
-                    playerControls.Set(CTRL_DOWN, true);
-            }
-            if (joystick.numAxes >= 4)
-            {
-                float lookX = joystick.axisPosition[joystick.numAxes - 2];
-                float lookY = joystick.axisPosition[joystick.numAxes - 1];
-
-                if (lookX < -joyLookDeadZone)
-                    playerControls.yaw -= joySensitivity * lookX * lookX;
-                if (lookX > joyLookDeadZone)
-                    playerControls.yaw += joySensitivity * lookX * lookX;
-                if (lookY < -joyLookDeadZone)
-                    playerControls.pitch -= joySensitivity * lookY * lookY;
-                if (lookY > joyLookDeadZone)
-                    playerControls.pitch += joySensitivity * lookY * lookY;
+                if (joystick.numButtons >= 6)
+                {
+                    if (joystick.buttonDown[4])
+                        playerControls.Set(CTRL_JUMP, true);
+                    if (joystick.buttonDown[5])
+                        playerControls.Set(CTRL_FIRE, true);
+                }
+    
+                if (joystick.numHats > 0)
+                {
+                    if (joystick.hatPosition[0] & HAT_LEFT != 0)
+                        playerControls.Set(CTRL_LEFT, true);
+                    if (joystick.hatPosition[0] & HAT_RIGHT != 0)
+                        playerControls.Set(CTRL_RIGHT, true);
+                    if (joystick.hatPosition[0] & HAT_UP != 0)
+                        playerControls.Set(CTRL_UP, true);
+                    if (joystick.hatPosition[0] & HAT_DOWN != 0)
+                        playerControls.Set(CTRL_DOWN, true);
+                }
+                if (joystick.numAxes >= 2)
+                {
+                    if (joystick.axisPosition[0] < -joyMoveDeadZone)
+                        playerControls.Set(CTRL_LEFT, true);
+                    if (joystick.axisPosition[0] > joyMoveDeadZone)
+                        playerControls.Set(CTRL_RIGHT, true);
+                    if (joystick.axisPosition[1] < -joyMoveDeadZone)
+                        playerControls.Set(CTRL_UP, true);
+                    if (joystick.axisPosition[1] > joyMoveDeadZone)
+                        playerControls.Set(CTRL_DOWN, true);
+                }
+                if (joystick.numAxes >= 4)
+                {
+                    float lookX = joystick.axisPosition[joystick.numAxes - 2];
+                    float lookY = joystick.axisPosition[joystick.numAxes - 1];
+    
+                    if (lookX < -joyLookDeadZone)
+                        playerControls.yaw -= joySensitivity * lookX * lookX;
+                    if (lookX > joyLookDeadZone)
+                        playerControls.yaw += joySensitivity * lookX * lookX;
+                    if (lookY < -joyLookDeadZone)
+                        playerControls.pitch -= joySensitivity * lookY * lookY;
+                    if (lookY > joyLookDeadZone)
+                        playerControls.pitch += joySensitivity * lookY * lookY;
+                }
             }
         }
 

+ 4 - 4
Engine/Input/Input.h

@@ -64,7 +64,7 @@ struct JoystickState
     /// Check if a button is held down.
     bool GetButtonDown(unsigned index) const
     {
-        if (index <= buttons_.Size())
+        if (index < buttons_.Size())
             return buttons_[index];
         else
             return false;
@@ -73,7 +73,7 @@ struct JoystickState
     /// Check if a button has been pressed on this frame.
     bool GetButtonPress(unsigned index) const
     {
-        if (index <= buttons_.Size())
+        if (index < buttons_.Size())
             return buttonPress_[index];
         else
             return false;
@@ -82,7 +82,7 @@ struct JoystickState
     /// Return axis position.
     float GetAxisPosition(unsigned index) const
     {
-        if (index <= axes_.Size())
+        if (index < axes_.Size())
             return axes_[index];
         else
             return 0.0f;
@@ -91,7 +91,7 @@ struct JoystickState
     /// Return hat position.
     int GetHatPosition(unsigned index) const
     {
-        if (index <= hats_.Size())
+        if (index < hats_.Size())
             return hats_[index];
         else
             return HAT_CENTER;