Пример #1
0
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
      Gdx.app.log("TOUCH", "pointer: " + pointer);
      Vector3 touchPos = new Vector3();
      touchPos.set(screenX, screenY, 0);
      camera.unproject(touchPos);
      if (pointer == 0) { // the first finger is for movement
        if (controls == ControlScheme.TOUCH_JOYSTICK) {

          originX = touchPos.x - camera.position.x;
          originY = touchPos.y - camera.position.y;

          // update control-visuals
          psX = (int) (touchPos.x - camera.position.x);
          peX = psX;
          psY = (int) (touchPos.y - camera.position.y);
          peY = psY;
        } else if (controls == ControlScheme.TOUCH_AIM) {
          //		player.aimX=touchPos.x;
          //	player.aimY=touchPos.y;
        }

      } else { // the second finger makes our ship shoot
        // player.shoot();
        player.isShooting = true;
        // return false;
      }
      if (controls == ControlScheme.MOUSE_AIM && button == 0) {
        player.isShooting = true;
      }

      // we want to be able to shoot while moving
      if (touchPos.y - camera.position.y < -camera.getHeight() / 2 + 64
          && touchPos.x - camera.position.x > camera.getWidth() / 2 - 64) {
        if (avaiableToLandOnThisFrame != null) {
          // game.setScreen(new GameOverScreen(game));

          SpaceGame.screen(Res.shop);
        } else {
          player.switchWeapon();
        }
      }

      if (controls == ControlScheme.ACCELERATION) {
        player.isShooting = true;
      }
      return false;
    }
Пример #2
0
  @Override
  public void render(float delta) {
    super.render(delta);

    if (incZoom != 0) {
      changeZoom(incZoom * delta);
    }

    camera.zoom = gameZoom;

    // TODO only calculate one? do not reset position of the cam when drawing hud? use different
    // cam?
    camera.position.x = player.getX() + player.getOriginX();
    camera.position.y = player.getY() + player.getOriginY();

    avaiableToLandOnThisFrame = null;
    if (player.life <= 0) {
      SpaceGame.screen(Res.gameover);
      // Gdx.app.exit(); // GAME OVER
    }

    // control the player via acceleration
    if (controls == ControlScheme.ACCELERATION) {
      if (Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer)) {
        // TODO add option to switch axis and invert them
        float accelX = Gdx.input.getAccelerometerX();
        float accelY = Gdx.input.getAccelerometerY();
        Gdx.app.log("ACCELERATION", accelX + "," + accelY);
        player.speed = 200; // (float) Math.sqrt(accelX*accelX+accelY*accelY);
        player.angle = MathUtils.atan2(-accelX, accelY);
        // rotate the player acording to his angle
        player.setRotation(MathUtils.radDeg * player.angle - 90);
      }
    }

    player.act(getChunk(player.getX(), player.getY()), delta);

    // check if the player gets outside all loaded chunks
    checkChunks();

    // keep player inside the game field
    // if(player.getX()<0){player.setX(0);}else
    // if(player.getX()>chunk.width){player.setX(chunk.width);}
    // if(player.getY()<0){player.setY(0);}else
    // if(player.getY()>chunk.height){player.setY(chunk.height);}

    for (int i = 0; i < chunks.size(); i++) {
      chunks
          .get(i)
          .checkRemove(); // check wheter ships, shots or whatsoever have to be moved to another
      // chunk
    }
    for (int i = 0; i < chunks.size(); i++) {
      chunks.get(i).act(delta);
    }

    // System.out.println("chunks: "+chunks.size());

    // scrolling
    camera.position.x = player.getX() + player.getOriginX();
    camera.position.y = player.getY() + player.getOriginY();

    camera.update();
    camera.activate(batch);
    cameraRect.set(
        camera.position.x - camera.getWidth() / 2,
        camera.position.y - camera.getHeight() / 2,
        camera.getWidth(),
        camera.getHeight());

    batch.begin();
    batch.draw(
        background,
        camera.position.x - camera.getWidth() / 2,
        camera.position.y - camera.getHeight() / 2,
        camera.getWidth(),
        camera.getHeight());

    // draw only visible chunks
    for (int i = 0; i < chunks.size(); i++) {
      if (chunks.get(i).rect.overlaps(cameraRect)) {
        // this chunk is in the visible drawarea
        chunks.get(i).draw(cameraRect, batch);

        //// DEBUG
        // Resources.font.draw(batch, "chunk "+i, chunks.get(i).x,chunks.get(i).y+40);
      }
    }

    player.draw(batch, 1);

    // draw control-visuals
    if (psX != -1) {
      batch.draw(
          point_start,
          psX * camera.zoom + camera.position.x - 32,
          psY * camera.zoom + camera.position.y - 32);
      batch.draw(
          point_end,
          peX * camera.zoom + camera.position.x - 16,
          peY * camera.zoom + camera.position.y - 16);
    }

    for (int i = 0; i < marker.size(); i++) {
      marker.get(i).draw(cameraRect, batch);
    }
    batch.end();

    ///// DEBUG RENDERER /////
    /*	ShapeRenderer renderer=new ShapeRenderer();
    renderer.begin(ShapeType.Rectangle);
    renderer.setProjectionMatrix(camera.combined);
    renderer.setColor(255,0,0,10);
    for(int i=0;i<chunks.size();i++){
    	if(chunks.get(i).rect.overlaps(cameraRect)){
    		renderer.rect(chunks.get(i).x, chunks.get(i).y, Chunk.width, Chunk.height);
    	}
    }

    renderer.setColor(0,255,0,10);
    renderer.rect(camera.position.x-camera.getWidth()/2  +1 , camera.position.y-camera.getHeight()/2 +1, camera.getWidth()-2, camera.getHeight()-2);
    //for(float y=player.getY()-camera.getHeight()/2;y<player.getY()+camera.getHeight()/2;y+=Chunk.height){
    //	for(float x=player.getX()-camera.getWidth()/2;x<player.getX()+camera.getWidth()/2;x+=Chunk.width){


    renderer.setColor(0,0,255,10);
    for(float y=camera.position.y-camera.getHeight()/2 - Chunk.height ;y<camera.position.y+camera.getHeight()/2 +Chunk.height;y+=Chunk.height){
    	for(float x=camera.position.x-camera.getWidth()/2 - Chunk.width ;x<camera.position.x+camera.getWidth()/2 +Chunk.width;x+=Chunk.width){
    		if(getChunk(x, y)==null){
    			renderer.setColor(0,0,255,10);
    		}else{
    			renderer.setColor(0,0,0,10);
    		}
    		renderer.rect(x,y,Chunk.width,Chunk.height);
    	}
    }
    renderer.end();*/

    ///// HUD //////

    camera.position.x = 0;
    camera.position.y = 0;
    camera.zoom = guiZoom;
    camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    int padding = 10;
    if (player.weapon.maxAmmo == -1) {
      Res.font.draw(batch, "ammo inf", -camera.getWidth() / 2 + padding, camera.getHeight() / 2);
    } else {
      Res.font.draw(
          batch,
          "ammo " + player.weapon.ammo,
          -camera.getWidth() / 2 + padding,
          camera.getHeight() / 2);
    }

    Res.font.drawWrapped(
        batch,
        "money: " + (int) player.money,
        -camera.getWidth() / 2,
        camera.getHeight() / 2,
        camera.getWidth() - padding,
        HAlignment.CENTER);

    //		Resources.font.drawWrapped(batch, "life "+(int)player.life,
    // -camera.getWidth()/2,camera.getHeight()/2,camera.getWidth()-padding,HAlignment.RIGHT);
    // a really cool life bar
    lifefill.setRegionWidth((int) (player.life * 2));
    batch.draw(
        lifefill,
        camera.getWidth() / 2 - 200 - padding,
        camera.getHeight() / 2 - 40,
        player.life * 2,
        32); // , originX, originY, width, height, scaleX, scaleY, rotation)

    batch.draw(
        lifeborder,
        camera.getWidth() / 2 - 200 - padding,
        camera.getHeight() / 2
            - 40); // , originX, originY, width, height, scaleX, scaleY, rotation)

    if (avaiableToLandOnThisFrame != null) {
      Res.font.drawWrapped(
          batch,
          "land on this planet",
          -camera.getWidth() / 2,
          -camera.getHeight() / 2 + 40,
          camera.getWidth() - padding,
          HAlignment.RIGHT);
    } else {
      batch.draw(switchWeapons, camera.getWidth() / 2 - 62, -camera.getHeight() / 2);
    }

    Res.font.drawWrapped(
        batch,
        "FPS " + Gdx.graphics.getFramesPerSecond(),
        padding - camera.getWidth() / 2,
        -camera.getHeight() / 2 + 40,
        camera.getWidth(),
        HAlignment.LEFT);

    batch.end();
  }