/** * Creates a new instance of {@link NinjaRabbit}, defining its graphical, audio and physical * properties. * * @param world * The Box2D {@link World} onto which to create the {@link Body} and {@link Fixture} * of the {@link Entity}. * @param loader * A {@link BodyEditorLoader} to handle creation of the Entity body and fixtures. * @param assets * The {@link AssetManager} from where to extract the graphical and audio resources. * Those resources should be loaded in the manager before calling this method and * won't be disposed. * @param status * A reference to the global status of the player to be updated from the changes in * the returned entity inner state. * @param observers * An array of event receivers. Events will fire when the active player status * changes (such as losing lives, collecting items, etc.). * @return A ready to use instance of a new {@link NinjaRabbit}. */ public static Entity createNinjaRabbit(final World world, final BodyEditorLoader loader, final AssetManager assets, final CurrentPlayerStatus status, final PlayerStatusObserver... observers) { PhysicsProcessor physics = new NinjaRabbitPhysicsProcessor(); CONTACT_LISTENER.add(physics); world.setContactListener(CONTACT_LISTENER); GraphicsProcessor graphics = new NinjaRabbitGraphicsProcessor(assets); BodyProcessor bodyProcessor = new NinjaRabbitBodyProcessor(world, loader); AudioProcessor audio = new NinjaRabbitAudioProcessor(assets); PlayerStatusProcessor player = new NinjaRabbitPlayerStatusProcessor(status); if (observers != null) { for (PlayerStatusObserver o : observers) { player.addObserver(o); } } NinjaRabbit ninjaRabbit = new NinjaRabbit(player, bodyProcessor, graphics, physics, audio); if (Ouya.isRunningOnOuya()) { Controllers.clearListeners(); Controllers.addListener(new NinjaRabbitControllerProcessor(ninjaRabbit)); } else { Gdx.input.setInputProcessor(new NinjaRabbitInputProcessor(ninjaRabbit)); } return ninjaRabbit; }
@Override public boolean axisMoved(final Controller controller, final int axisIndex, final float value) { if (axisIndex == MOVE_AXIS) { float axisValue = 0.5f * value; if (Math.abs(axisValue) > Ouya.STICK_DEADZONE) { if (axisValue > 0) { character.changeState(NinjaRabbitState.RIGHT); } else { character.changeState(NinjaRabbitState.LEFT); } } else { character.changeState(NinjaRabbitState.IDLE); } } return super.axisMoved(controller, axisIndex, value); }
@Override public boolean map(Controller controller, ControllerHelper controllerHelper) { if (!names.contains(controller.getName(), false)) { return false; } System.out.println("Automapping OUYA controller..."); System.out.println("OUYA controller actually: "+controller.getName()); controllerHelper.map(Input.up, new ControllerButton(controller, getB(Ouya.BUTTON_DPAD_UP))); controllerHelper.map(Input.down, new ControllerButton(controller, getB(Ouya.BUTTON_DPAD_DOWN))); controllerHelper.map(Input.left, new ControllerButton(controller, getB(Ouya.BUTTON_DPAD_LEFT))); controllerHelper.map(Input.right, new ControllerButton(controller, getB(Ouya.BUTTON_DPAD_RIGHT))); controllerHelper.map(Input.up, new ControllerAxis(controller, getA(Ouya.AXIS_LEFT_Y), true)); controllerHelper.map(Input.down, new ControllerAxis(controller, getA(Ouya.AXIS_LEFT_Y), false)); controllerHelper.map(Input.left, new ControllerAxis(controller, getA(Ouya.AXIS_LEFT_X), true)); controllerHelper.map(Input.right, new ControllerAxis(controller, getA(Ouya.AXIS_LEFT_X), false)); controllerHelper.map(Input.jump, new ControllerButton(controller, getB(Ouya.BUTTON_O))); controllerHelper.map(Input.pause, new ControllerButton(controller, getB(Ouya.BUTTON_MENU))); controllerHelper.map(Input.enter, new ControllerButton(controller, getB(Ouya.BUTTON_O))); controllerHelper.map(Input.androidBack, new ControllerButton(controller, getB(Ouya.BUTTON_A))); return true; }
@Override public boolean map(Controller controller, ControllerHelper controllerHelper) { if (Ouya.ID.equals(controller.getName())) { System.out.println("Automapping OUYA controller..."); controllerHelper.map(Input.up, new ControllerButton(controller, Ouya.BUTTON_DPAD_UP)); controllerHelper.map(Input.down, new ControllerButton(controller, Ouya.BUTTON_DPAD_DOWN)); controllerHelper.map(Input.left, new ControllerButton(controller, Ouya.BUTTON_DPAD_LEFT)); controllerHelper.map(Input.right, new ControllerButton(controller, Ouya.BUTTON_DPAD_RIGHT)); controllerHelper.map(Input.up, new ControllerAxis(controller, Ouya.AXIS_LEFT_Y, true)); controllerHelper.map(Input.down, new ControllerAxis(controller, Ouya.AXIS_LEFT_Y, false)); controllerHelper.map(Input.left, new ControllerAxis(controller, Ouya.AXIS_LEFT_X, true)); controllerHelper.map(Input.right, new ControllerAxis(controller, Ouya.AXIS_LEFT_X, false)); controllerHelper.map(Input.jump, new ControllerButton(controller, Ouya.BUTTON_O)); controllerHelper.map(Input.pause, new ControllerButton(controller, Ouya.BUTTON_MENU)); controllerHelper.map(Input.enter, new ControllerButton(controller, Ouya.BUTTON_O)); controllerHelper.map(Input.androidBack, new ControllerButton(controller, Ouya.BUTTON_A)); return true; } return false; }
@Override public void update(float delta) { simulation.update(delta); float accelerometerY = Gdx.input.getAccelerometerY(); if (accelerometerY < 0) simulation.moveShipLeft(delta, Math.abs(accelerometerY) / 10); else simulation.moveShipRight(delta, Math.abs(accelerometerY) / 10); if (invaders.getController() != null) { if (buttonsPressed > 0) { simulation.shot(); } // if the left stick moved, move the ship float axisValue = invaders.getController() .getAxis(Ouya.AXIS_LEFT_X) * 0.5f; if (Math.abs(axisValue) > 0.25f) { if (axisValue > 0) { simulation.moveShipRight(delta, axisValue); } else { simulation.moveShipLeft(delta, -axisValue); } } } if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT) || Gdx.input.isKeyPressed(Keys.A)) simulation.moveShipLeft(delta, 0.5f); if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT) || Gdx.input.isKeyPressed(Keys.D)) simulation.moveShipRight(delta, 0.5f); if (Gdx.input.isTouched() || Gdx.input.isKeyPressed(Keys.SPACE)) { simulation.shot(); i++; Log.d("aaa", "shot++++++++++++++++++++++++++++++++++ i = " + i); } }
@Override public void update (float delta) { simulation.update(delta); float accelerometerY = Gdx.input.getAccelerometerY(); if (accelerometerY < 0) simulation.moveShipLeft(delta, Math.abs(accelerometerY) / 10); else simulation.moveShipRight(delta, Math.abs(accelerometerY) / 10); if (invaders.getController() != null) { if (buttonsPressed > 0) { simulation.shot(); } // if the left stick moved, move the ship float axisValue = invaders.getController().getAxis(Ouya.AXIS_LEFT_X) * 0.5f; if (Math.abs(axisValue) > 0.25f) { if (axisValue > 0) { simulation.moveShipRight(delta, axisValue); } else { simulation.moveShipLeft(delta, -axisValue); } } } if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT) || Gdx.input.isKeyPressed(Keys.A)) simulation.moveShipLeft(delta, 0.5f); if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT) || Gdx.input.isKeyPressed(Keys.D)) simulation.moveShipRight(delta, 0.5f); if (Gdx.input.isTouched() || Gdx.input.isKeyPressed(Keys.SPACE)) simulation.shot(); }
private void handlePlayerInputs() { if(controller != null && controller.getName().equals(Ouya.ID)) { handleOuyaInputs(); } else { handleKeyboardInputs(); } }
@Override public float getX() { if (controller == null) { return 0f; } return controller.getAxis(Ouya.AXIS_RIGHT_X) * SPEED; }
@Override public float getY() { if (controller == null) { return 0f; } return controller.getAxis(Ouya.AXIS_RIGHT_Y) * SPEED; }
@Override public boolean getActionPress() { if (controller == null) { return false; } return controller.getButton(Ouya.BUTTON_O); }
@Override public float getForward() { if (controller == null) { return 0f; } if (controller.getAxis(Ouya.AXIS_LEFT_Y) == 0f) { return 0f; } return controller.getAxis(Ouya.AXIS_LEFT_Y); }
@Override public float getLeft() { if (controller == null) { return 0f; } if (controller.getAxis(Ouya.AXIS_LEFT_X) == 0f) { return 0f; } return controller.getAxis(Ouya.AXIS_LEFT_X); }
@Override public boolean getShift() { if (controller == null) { return false; } return controller.getButton(Ouya.BUTTON_A); }
@Override public void init(int id) { if (controller == null) { for (int i = 0; i < Controllers.getControllers().size; i++) { Controller current = Controllers.getControllers().get(i); // Dispose any irregular controllers if (current.getName().equals(Ouya.ID)) { controller = Controllers.getControllers().get(i); break; } } } }
private void initializePlayers() { Player player = new Player(); player.setColor(PlayerColor.BLUE); Array<Controller> controllers = Controllers.getControllers(); if (Ouya.runningOnOuya) { ControllerInputHandler inputHandler = new ControllerInputHandler(); inputHandler.setController(controllers.get(0)); inputHandler.setMapping(InputMapping.OUYAMapping()); player.setInputHandler(inputHandler); } else { if (Controllers.getControllers().size > 0) { ControllerInputHandler controllerInputHandler = new ControllerInputHandler(); controllerInputHandler.setController(Controllers .getControllers().first()); controllerInputHandler .setMapping(InputMapping.XBOX360Mapping()); player.setInputHandler(controllerInputHandler); } else { KeyboardInputHandler keyboardInputHandler = new KeyboardInputHandler(); keyboardInputHandler.setMapping(InputMapping .KeyboardArrowsMapping()); player.setInputHandler(keyboardInputHandler); } } players.add(player); }
public static InputMapping OUYAMapping() { InputMapping ouyaMapping = new InputMapping(); ouyaMapping.mapControl(Control.DOWN, Ouya.BUTTON_DPAD_DOWN); ouyaMapping.mapControl(Control.UP, Ouya.BUTTON_DPAD_UP); ouyaMapping.mapControl(Control.LEFT, Ouya.BUTTON_DPAD_LEFT); ouyaMapping.mapControl(Control.RIGHT, Ouya.BUTTON_DPAD_RIGHT); ouyaMapping.mapControl(Control.FIRE, Ouya.BUTTON_O); ouyaMapping.mapControl(Control.START, Ouya.BUTTON_MENU); return ouyaMapping; }
@Override public void update (float delta) { simulation.update(delta); // float q1 = BLEDeviceScanActivity.latest_Q0; // float q2 = BLEDeviceScanActivity.latest_Q1; // float q3 = BLEDeviceScanActivity.latest_Q2; // float q4 = BLEDeviceScanActivity.latest_Q3; // // //Equation from Omid's paper with conversions to make the math work // double pi_d = Math.PI; // float pi_f = (float)pi_d; // // double q1_double = q1; // double theta_double = 2*Math.acos(q1_double); // float theta = (float)theta_double*180/pi_f; // // double q2_double = q2; // double rx_double = -1 * q2_double / Math.sin(theta_double/2); // float rx = (float)rx_double; // // double q3_double = q3; // double ry_double = -1 * q3_double / Math.sin(theta_double/2); // float ry = (float)ry_double; // // double q4_double = q4; // double rz_double = -1 * q4_double / Math.sin(theta_double/2); // float rz = (float)rz_double; float q0 = (float) Invaders.mInvaderInterface.getQ0(); float q1 = (float) Invaders.mInvaderInterface.getQ1(); float q2 = (float) Invaders.mInvaderInterface.getQ2(); float q3 = (float) Invaders.mInvaderInterface.getQ3(); float accelerometerY = Gdx.input.getAccelerometerY(); // if (accelerometerY < 0) if (0.2f * (q0 * q1 + q2 * q3)>0) { // simulation.moveShipLeft(delta, Math.abs(accelerometerY) / 10); simulation.moveShipLeft(delta, Math.abs(0.2f * (q0 * q1 + q2 * q3))); } else // simulation.moveShipRight(delta, Math.abs(accelerometerY) / 10); simulation.moveShipRight(delta,Math.abs(0.2f * (q0 * q1 + q2 * q3))); if (invaders.getController() != null) { if (buttonsPressed > 0) { simulation.shot(); } // if the left stick moved, move the ship float axisValue = invaders.getController().getAxis(Ouya.AXIS_LEFT_X) * 0.5f; if (Math.abs(axisValue) > 0.25f) { if (axisValue > 0) { simulation.moveShipRight(delta, axisValue); } else { simulation.moveShipLeft(delta, -axisValue); } } } if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT) || Gdx.input.isKeyPressed(Keys.A)) simulation.moveShipLeft(delta, 0.5f); if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT) || Gdx.input.isKeyPressed(Keys.D)) simulation.moveShipRight(delta, 0.5f); if (Gdx.input.isTouched() || Gdx.input.isKeyPressed(Keys.SPACE)) simulation.shot(); }
private void handleOuyaInputs() { float leftXAxis = controller.getAxis(Ouya.AXIS_LEFT_X); boolean oButton = controller.getButton(Ouya.BUTTON_O); boolean dashLeft = controller.getButton(Ouya.BUTTON_L1) || controller.getButton(Ouya.BUTTON_L2); boolean dashRight = controller.getButton(Ouya.BUTTON_R1) || controller.getButton(Ouya.BUTTON_R2); // Execute shoot if (oButton) { if (!bulletJustShot) { bullets.add(new Bullet(hero.x, hero.y)); bulletJustShot = true; } } else { bulletJustShot = false; } // Execute dash if (dashLeft) { if (!justDashed) { hero.setVelocityX(-GamePlayParams.HERO_DASH_SPEED_X); justDashed = true; } } else if (dashRight) { if (!justDashed) { hero.setVelocityX(GamePlayParams.HERO_DASH_SPEED_X); justDashed = true; } } else { justDashed = false; } // Decelerate dash if (Math.abs(hero.getVelocityX()) > GamePlayParams.HERO_MOVE_SPEED_X) { hero.setVelocityX(hero.getVelocityX() * GamePlayParams.HERO_MOVE_DECELERATION_X); } // Fix for 'neutral' threshold if (Math.abs(leftXAxis) < 0.1) { leftXAxis = 0; } // Decelerate if (leftXAxis == 0 && Math.abs(hero.getVelocityX()) <= GamePlayParams.HERO_MOVE_SPEED_X) { hero.setVelocityX(hero.getVelocityX() * GamePlayParams.HERO_MOVE_DECELERATION_X); } // Move sideways else if (Math.abs(hero.getVelocityX()) <= GamePlayParams.HERO_MOVE_SPEED_X) { hero.setVelocityX(leftXAxis*GamePlayParams.HERO_MOVE_SPEED_X); } }
public static ControllerType getControllerType(Controller controller) { String controllerName = controller.getName(); String lowercaseControllerName = controllerName.toLowerCase(); if (controller.getName().equals(Ouya.ID)) { return ControllerType.OUYA; } if (lowercaseControllerName.contains(XboxOneController.ID)) { return ControllerType.XBOX_ONE; } if (lowercaseControllerName.contains(Xbox360Controller.ID)) { return ControllerType.XBOX_360; } if (lowercaseControllerName.contains(PS3Controller.ID_FULL)) { return ControllerType.PS3; } else if (lowercaseControllerName.contains(PS3Controller.ID_PREFIX) && lowercaseControllerName.contains(PS3Controller.ID_SUFFIX)) { return ControllerType.PS3; } switch(Mdx.os) { case WINDOWS: if(lowercaseControllerName.equals(PS4Controller.WINDOWS_ID)) { return ControllerType.PS4; } else if(lowercaseControllerName.equals(PS4Controller.MAC_ID)) { return ControllerType.PS4; } break; case MAC: if(lowercaseControllerName.equals(PS4Controller.MAC_ID)) { return ControllerType.PS4; } break; case UNIX: break; case UNKNOWN: case ANDROID: case IOS: default: break; } Gdx.app.log(LOGGING_TAG, "Could not find controller mappings for " + controller.getName()); return ControllerType.UNKNOWN; }