Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.github.skittishSloth.openSkies.battles; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.utils.DelayedRemovalArray; import com.github.skittishSloth.openSkies.battles.ships.BankState; import com.github.skittishSloth.openSkies.battles.ships.PlayerShip; import com.github.skittishSloth.openSkies.battles.weapons.Laser; /** * * @author mcory01 */ public class BattleScreen implements Screen { public BattleScreen() { batch = new SpriteBatch(); player = new PlayerShip("gfx/ships/spaceship_banking64.pack"); camera = new OrthographicCamera(); camera.setToOrtho(true); } @Override public void render(final float delta) { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); if (Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)) { if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { player.setBankState(BankState.LEFT); player.bankLeft(delta); } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { player.setBankState(BankState.RIGHT); player.bankRight(delta); } else { player.setBankState(BankState.NONE); } } else { player.setBankState(BankState.NONE); if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { player.rotateLeft(delta); } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { player.rotateRight(delta); } } if (Gdx.input.isKeyPressed(Input.Keys.UP)) { player.moveForward(delta); } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) { player.moveBackward(delta); } if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) { if (player.canFire()) { final Laser laser = player.fireLaser(); lasers.add(laser); } } camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); updateLasers(delta); player.render(batch, delta); batch.end(); } @Override public void resize(int width, int height) { } @Override public void show() { } @Override public void hide() { } @Override public void pause() { } @Override public void resume() { } @Override public void dispose() { batch.dispose(); player.dispose(); for (final Laser laser : lasers) { laser.dispose(); } lasers.clear(); } private void updateLasers(final float delta) { lasers.begin(); for (final Laser laser : lasers) { if (laser.isAlive()) { laser.render(batch, delta); handleLaserState(laser); } else { laser.dispose(); lasers.removeValue(laser, true); } } lasers.end(); } private void handleLaserState(final Laser laser) { final float x = laser.getX(); final float y = laser.getY(); if ((x < 0) || (x > Gdx.graphics.getWidth())) { laser.setAlive(false); } if ((y < 0) || (y > Gdx.graphics.getHeight())) { laser.setAlive(false); } } private final DelayedRemovalArray<Laser> lasers = new DelayedRemovalArray<Laser>(); private final SpriteBatch batch; private final PlayerShip player; private final OrthographicCamera camera; }