예제 #1
0
  @Override
  public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    scene = new Scene();
    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

    final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
    final Sprite face =
        new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
    final PhysicsHandler physicsHandler = new PhysicsHandler(face);
    face.registerUpdateHandler(physicsHandler);

    scene.attachChild(face);

    final AnalogOnScreenControl analogOnScreenControl =
        new AnalogOnScreenControl(
            0,
            CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(),
            this.mCamera,
            this.mOnScreenControlBaseTextureRegion,
            this.mOnScreenControlKnobTextureRegion,
            0.1f,
            200,
            this.getVertexBufferObjectManager(),
            new IAnalogOnScreenControlListener() {
              @Override
              public void onControlChange(
                  final BaseOnScreenControl pBaseOnScreenControl,
                  final float pValueX,
                  final float pValueY) {
                physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
              }

              @Override
              public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
                face.registerEntityModifier(
                    new SequenceEntityModifier(
                        new ScaleModifier(0.25f, 1, 1.5f), new ScaleModifier(0.25f, 1.5f, 1)));
              }
            });
    analogOnScreenControl
        .getControlBase()
        .setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    analogOnScreenControl.getControlBase().setAlpha(0.5f);
    analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
    analogOnScreenControl.getControlBase().setScale(1.25f);
    analogOnScreenControl.getControlKnob().setScale(1.25f);
    analogOnScreenControl.refreshControlKnobPosition();

    scene.setChildScene(analogOnScreenControl);

    pOnCreateSceneCallback.onCreateSceneFinished(scene);
  }
  @Override
  public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    scene.getBackground().setColor(0.09804f, 0.6274f, 0.8784f);

    final int centerX = CAMERA_WIDTH / 2;
    final int centerY = CAMERA_HEIGHT / 2;

    final Sprite sprite =
        new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
    final PhysicsHandler physicsHandler = new PhysicsHandler(sprite);
    sprite.registerUpdateHandler(physicsHandler);

    scene.attachChild(sprite);

    /* Velocity control (left). */
    final AnalogOnScreenControl velocityOnScreenControl =
        new AnalogOnScreenControl(
            0,
            0,
            this.mCamera,
            this.mOnScreenControlBaseTextureRegion,
            this.mOnScreenControlKnobTextureRegion,
            0.1f,
            this.getVertexBufferObjectManager(),
            new IAnalogOnScreenControlListener() {
              @Override
              public void onControlChange(
                  final BaseOnScreenControl pBaseOnScreenControl,
                  final float pValueX,
                  final float pValueY) {
                physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
              }

              @Override
              public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
                /* Nothing. */
              }
            });

    {
      final Sprite controlBase = velocityOnScreenControl.getControlBase();
      controlBase.setAlpha(0.5f);
      controlBase.setOffsetCenter(0, 0);

      scene.setChildScene(velocityOnScreenControl);
    }

    /* Rotation control (right). */
    final float y = (this.mPlaceOnScreenControlsAtDifferentVerticalLocations) ? CAMERA_HEIGHT : 0;
    final AnalogOnScreenControl rotationOnScreenControl =
        new AnalogOnScreenControl(
            CAMERA_WIDTH,
            y,
            this.mCamera,
            this.mOnScreenControlBaseTextureRegion,
            this.mOnScreenControlKnobTextureRegion,
            0.1f,
            this.getVertexBufferObjectManager(),
            new IAnalogOnScreenControlListener() {
              @Override
              public void onControlChange(
                  final BaseOnScreenControl pBaseOnScreenControl,
                  final float pValueX,
                  final float pValueY) {
                if (pValueX == 0 && pValueY == 0) {
                  sprite.setRotation(0);
                } else {
                  sprite.setRotation(MathUtils.radToDeg((float) Math.atan2(pValueX, pValueY)));
                }
              }

              @Override
              public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
                /* Nothing. */
              }
            });

    {
      final Sprite controlBase = rotationOnScreenControl.getControlBase();
      if (this.mPlaceOnScreenControlsAtDifferentVerticalLocations) {
        controlBase.setOffsetCenter(1, 1);
      } else {
        controlBase.setOffsetCenter(1, 0);
      }
      controlBase.setAlpha(0.5f);

      velocityOnScreenControl.setChildScene(rotationOnScreenControl);
    }

    return scene;
  }
예제 #3
0
  private void createAnalogJoystick() {
    engine.registerUpdateHandler(new FPSLogger());
    final PhysicsHandler physicsHandler = new PhysicsHandler(aim);
    aim.registerUpdateHandler(physicsHandler);
    joystick =
        new AnalogOnScreenControl(
            0,
            camera.getHeight() - resourcesManager.mOnScreenControlBaseTextureRegion.getHeight(),
            camera,
            resourcesManager.mOnScreenControlBaseTextureRegion,
            resourcesManager.mOnScreenControlKnobTextureRegion,
            0.1f,
            500,
            vbom,
            new IAnalogOnScreenControlListener() {
              @Override
              public void onControlChange(
                  final BaseOnScreenControl pBaseOnScreenControl,
                  final float pValueX,
                  final float pValueY) {
                // границы передвижения прицела по оси Y
                physicsHandler.setVelocity(0, 0);
                float moveX = 0;
                float moveY = 0;

                if (pValueX != 0) {

                  float signX = 1;
                  if (pValueX < 0) signX = -1;

                  float signY = 1;
                  if (pValueY < 0) signY = -1;

                  float alpha = (float) Math.atan(pValueY / pValueX);
                  float speed = 1;

                  moveX = Math.abs(speed * (float) Math.cos(alpha)) * signX;
                  moveY = Math.abs(speed * (float) Math.sin(alpha)) * signY;
                }

                if (aim.getY() < camera.getYMin() && pValueY < 0) {
                  physicsHandler.setVelocity(moveX * 200, 0); // speed
                } else if (aim.getY() > camera.getHeight() - aim.getHeight() && pValueY > 0) {
                  physicsHandler.setVelocity(moveX * 200, 0); // speed
                } else
                // границы передвижения прицела по оси X
                if (aim.getX() < background.getX() && pValueX < 0) {
                  physicsHandler.setVelocity(0, moveY * 200); // speed
                } else if (aim.getX() > background.getWidth() - aim.getWidth() && pValueX > 0) {
                  physicsHandler.setVelocity(0, moveY * 200); // speed
                } else {
                  physicsHandler.setVelocity(moveX * 200, moveY * 200); // speed
                }
                // следование камеры за прицелом
                float difference = camera.getCenterX() - aim.getX();
                if (Math.abs(difference) > 200) {
                  if (difference > 0)
                    SmoothCameraMove(
                        camera.getCenterX(),
                        camera.getCenterY(),
                        camera.getCenterX() - difference,
                        camera.getCenterY());
                  if (difference < 0)
                    SmoothCameraMove(
                        camera.getCenterX(),
                        camera.getCenterY(),
                        camera.getCenterX() + Math.abs(difference),
                        camera.getCenterY());
                }
              }

              @Override
              public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {}
            });
    joystick.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    joystick.getControlBase().setAlpha(0.5f);
    joystick.getControlBase().setScaleCenter(0, 128);
    joystick.getControlBase().setScale(1.25f);
    joystick.getControlKnob().setScale(1.25f);
    joystick.refreshControlKnobPosition();
  }