public IngameScreen() { Res.ingame = this; chunks = new ArrayList<Chunk>(); marker = new ArrayList<Marker>(); Chunk chunk = new Chunk(0, 0); chunks.add(chunk); player = Res.player; // set player to the center of the world player.setX(-player.getWidth() / 2); player.setY(-player.getHeight() / 2); player.setRotation(-90); // get preferences gameZoom = Preferences.getFloat("gameZoom", 1f); cameraRect = new Rectangle(); // get textures point_start = Res.atlas.findRegion("point_start"); point_end = Res.atlas.findRegion("point_end"); background = Res.atlas.findRegion("background"); lifeborder = Res.atlas.findRegion("life_border"); lifefill = Res.atlas.findRegion("life_fill"); switchWeapons = Res.atlas.findRegion("switchWeapons"); marker.add(new Marker(0, 0)); Controllers.addListener(new ControllerControlls()); // TODO only when screen visib? }
public void show() { GameInputProcessor inputProcessor = new GameInputProcessor(); Gdx.input.setInputProcessor(inputProcessor); System.out.println(Controllers.getControllers().size + ":O"); // weapon of player might has changed while in shop player.weapon = Res.weapons.get(player.currentWeapon).getCurrent(); }
@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(); }