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 scenes; import ammunition.Projectile; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import java.awt.Point; import match.Match; import match.Player; import object.CombustibleBarrel; import object.MapObject; import object.Obstacle; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.utils.Align; import com.circle.game.Config; import com.esotericsoftware.kryonet.Client; import java.util.Iterator; import java.util.List; import java.util.Timer; import java.util.TimerTask; import match.GameText; import misc.CircleColors; import networking.GameClient; import object.DominationCircle; import powerup.PowerUpPlatform; /** * * @author <a href="mailto:d.vangils@student.fontys.nl">Dennis van Gils</a> */ public class MatchScreen extends ApplicationAdapter { private Client client; private SpriteBatch batch; private Texture img; private Match match; private ShapeRenderer shapeRenderer; private int xOffset; private int yOffset; private GlyphLayout layout; private BitmapFont font30; private BitmapFont font20; private int timeLeft = 300; //time in seconds, default 300 private Texture barrelTexture = new Texture(Gdx.files.internal("sprites/Barrel.png")); private boolean DEBUG = false; private final float SCALE = 2.0f; private OrthographicCamera camera; private Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer(); private Matrix4 matrix; private Texture crownTexture = new Texture(Gdx.files.internal("sprites/Crown.png")); private int i = 0; public MatchScreen() { this.match = GameClient.match; create(); } @Override public void create() { xOffset = Gdx.graphics.getWidth() / 2 - (match.getMap().getWidth() / 2); yOffset = Gdx.graphics.getHeight() / 2 - (match.getMap().getHeight() / 2) + 100; layout = new GlyphLayout(); shapeRenderer = new ShapeRenderer(); shapeRenderer.setAutoShapeType(true); batch = new SpriteBatch(); //img = new Texture("Badlogic.jpg"); FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/MunroSmall.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); parameter.size = 35; font30 = generator.generateFont(parameter); // font size 12 pixels font30.setColor(CircleColors.black); parameter.size = 20; font20 = generator.generateFont(parameter); // font size 12 pixels font20.setColor(CircleColors.black); generator.dispose(); //Box2D float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); camera = new OrthographicCamera(); camera.setToOrtho(false, w / SCALE, h / SCALE); matrix = new Matrix4(); matrix.scale(w, h, 1f); if (match.GetWorld() == null) { match.SetWorld(new World(new Vector2(0, 0), false)); } //b2dr = new Box2DDebugRenderer(); batch = new SpriteBatch(); new Timer().schedule(new TimerTask() { @Override public void run() { timeLeft--; } }, 1000, 1000); Gdx.input.setInputProcessor(new InputAdapter() { @Override public boolean keyDown(int keyCode) { if (keyCode == Input.Keys.W) { // goforward SendMessage("pressed goforward"); } if (keyCode == Input.Keys.A) { // strafeleft SendMessage("pressed strafeleft"); } if (keyCode == Input.Keys.S) { // gobackward SendMessage("pressed gobackward"); } if (keyCode == Input.Keys.D) { // strafeleft SendMessage("pressed straferight"); } if (keyCode == Input.Keys.UP || keyCode == Input.Keys.SPACE || keyCode == Input.Keys.NUMPAD_8) { // attack SendMessage("pressed attack"); } if (keyCode == Input.Keys.LEFT || keyCode == Input.Keys.NUMPAD_4) { // turnleft SendMessage("pressed turnleft"); } if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.NUMPAD_6) { // turnright SendMessage("pressed turnright"); } if (keyCode == Input.Keys.ESCAPE) { //overlay -> pausescreen Config.overlay = new PauseScreen(); } return true; } @Override public boolean keyUp(int keyCode) { if (keyCode == Input.Keys.W) { // goforward SendMessage("released goforward"); } if (keyCode == Input.Keys.A) { // strafeleft SendMessage("released strafeleft"); } if (keyCode == Input.Keys.S) { // gobackward SendMessage("released gobackward"); } if (keyCode == Input.Keys.D) { // strafeleft SendMessage("released straferight"); } if (keyCode == Input.Keys.UP || keyCode == Input.Keys.SPACE || keyCode == Input.Keys.NUMPAD_8) { // attack SendMessage("released attack"); } if (keyCode == Input.Keys.LEFT || keyCode == Input.Keys.NUMPAD_4) { // turnleft SendMessage("released turnleft"); } if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.NUMPAD_6) { // turnright SendMessage("released turnright"); } if (keyCode == Input.Keys.Q) { // previousWeapon SendMessage("released previousWeapon"); } if (keyCode == Input.Keys.E) { // nextWeapon SendMessage("released nextWeapon"); } return true; } }); if (match.tUpdate != null) { match.tUpdate.interrupt(); } if (match.tRefresh != null) { match.tRefresh.interrupt(); } match.tUpdate = new Thread() { public void run() { try { while (true) { match.Update(); Thread.sleep(17); } } catch (InterruptedException ex) { } } }; match.tRefresh = new Thread() { public void run() { try { while (true) { GameClient.sendMatch(match); Thread.sleep(250); } } catch (InterruptedException ex) { } } }; if (match.getPlayers().get(0).getName().equals(Config.username)) { match.tRefresh.start(); } match.tUpdate.start(); } private void SendMessage(String text) { GameClient.sendInput(text); } @Override public void render() { HandleEnd(); shapeRenderer.begin(); Gdx.gl.glLineWidth(2); shapeRenderer.set(ShapeType.Filled); shapeRenderer.setColor(CircleColors.white); shapeRenderer.rect(xOffset, yOffset, match.getMap().getWidth(), match.getMap().getHeight()); shapeRenderer.set(ShapeType.Line); shapeRenderer.setColor(CircleColors.black); shapeRenderer.rect(xOffset, yOffset, match.getMap().getWidth(), match.getMap().getHeight()); shapeRenderer.end(); shapeRenderer.begin(); match.setHighestPlayers(); SpriteBatch spriteBatch = new SpriteBatch(); spriteBatch.begin(); for (int ii = 0; ii < match.getPlayers().size(); ii++) { Player player = match.getPlayers().get(ii); Gdx.gl.glLineWidth(3); if (player.getCharacter().getCanBeHit()) { shapeRenderer.setColor(player.getCharacter().getColor()); } else { shapeRenderer.setColor(CircleColors.light_gray); } shapeRenderer.set(ShapeType.Line); Point aimPoint = new Point(); aimPoint.x = xOffset + player.getCharacter().getPosition().x + (int) (Math.sin(Math.toRadians(player.getCharacter().getAngle())) * player.getCharacter().getRadius() * 1.5); aimPoint.y = yOffset + player.getCharacter().getPosition().y + (int) (Math.cos(Math.toRadians(player.getCharacter().getAngle())) * player.getCharacter().getRadius() * 1.5); if (player.getCharacter().getLives() > 0) { shapeRenderer.circle(xOffset + player.getCharacter().getPosition().x, yOffset + player.getCharacter().getPosition().y, player.getCharacter().getRadius()); if (player.getCharacter().getWeapon().getOnCoolDown()) { shapeRenderer.setColor(Color.LIGHT_GRAY); } shapeRenderer.line(xOffset + player.getCharacter().getPosition().x, yOffset + player.getCharacter().getPosition().y, aimPoint.x, aimPoint.y); if (player.isHighest()) { Sprite crownSprite = new Sprite(crownTexture); spriteBatch.draw(crownSprite, player.getCharacter().getPosition().x + xOffset - player.getCharacter().getRadius(), player.getCharacter().getPosition().y + yOffset + player.getCharacter().getRadius() + 2, player.getCharacter().getRadius() * 2, (float) (player.getCharacter().getRadius() * 1.5)); } } } // match.setHighestPlayers(); // // SpriteBatch spriteBatch = new SpriteBatch(); // spriteBatch.begin(); // // for (Player player : match.getPlayers()) // { // if (player.isHighest()) // { // Sprite crownSprite = new Sprite(crownTexture); // spriteBatch.draw(crownSprite, player.getCharacter().getPosition().x + xOffset - player.getCharacter().getRadius(), player.getCharacter().getPosition().y + yOffset + player.getCharacter().getRadius() + 2, player.getCharacter().getRadius() * 2, (float) (player.getCharacter().getRadius() * 1.5)); // } // //// if (player.getCharacter().isHeighest()) //// { //// spriteBatch.draw(player.getCharacter().getCrown(), player.getCharacter().getPosition().x + xOffset - player.getCharacter().getRadius(), player.getCharacter().getPosition().y + yOffset + player.getCharacter().getRadius() + 2, player.getCharacter().getRadius() * 2, (float) (player.getCharacter().getRadius() * 1.5)); //// } // } spriteBatch.end(); for (int ii = 0; ii < match.getProjectiles().size(); ii++) { Projectile projectile = match.getProjectiles().get(ii); shapeRenderer.setColor(projectile.getPlayer().getCharacter().getColor()); Point aimPoint = new Point(); aimPoint.x = xOffset + projectile.getPosition().x - (int) (Math.sin(Math.toRadians(projectile.getAngle())) * 28); aimPoint.y = yOffset + projectile.getPosition().y - (int) (Math.cos(Math.toRadians(projectile.getAngle())) * 28); shapeRenderer.line(xOffset + projectile.getPosition().x, yOffset + projectile.getPosition().y, aimPoint.x, aimPoint.y); } if (match.getMap() != null) { for (int ii = 0; ii < match.getMap().getObjects().size(); ii++) { MapObject mapObject = match.getMap().getObjects().get(ii); if (mapObject.getClass() == CombustibleBarrel.class) { CombustibleBarrel combustibleBarrel = (CombustibleBarrel) mapObject; shapeRenderer.setColor(CircleColors.red); shapeRenderer.set(ShapeType.Filled); batch.begin(); //shapeRenderer.circle(xOffset + combustibleBarrel.getPosition().x, yOffset + combustibleBarrel.getPosition().y, 18); batch.draw(barrelTexture, xOffset + combustibleBarrel.getPosition().x - 18, yOffset + combustibleBarrel.getPosition().y - 18, 32, 32); batch.end(); } if (mapObject.getClass() == Obstacle.class) { Obstacle obstacle = (Obstacle) mapObject; shapeRenderer.setColor(CircleColors.dark_gray); shapeRenderer.set(ShapeType.Filled); shapeRenderer.rect(xOffset + obstacle.getPosition().x, yOffset + obstacle.getPosition().y, obstacle.getWidth(), obstacle.getHeight()); } if (mapObject.getClass() == DominationCircle.class) { DominationCircle dominationCricle = (DominationCircle) mapObject; shapeRenderer.setColor(dominationCricle.getColor()); shapeRenderer.set(ShapeType.Line); shapeRenderer.circle(xOffset + dominationCricle.getPosition().x, yOffset + dominationCricle.getPosition().y, 25); } } } //batch.draw(img, 0, 0); shapeRenderer.end(); shapeRenderer.begin(); Gdx.gl.glLineWidth(5); for (int ii = 0; ii < match.getPowerUpPlatforms().size(); ii++) { PowerUpPlatform platform = match.getPowerUpPlatforms().get(ii); if (platform.getPosition().x != -1 || platform.getPosition().y != -1) { shapeRenderer.setColor(platform.getPowerUp().getColor()); shapeRenderer.set(ShapeType.Line); shapeRenderer.circle(xOffset + platform.getPosition().x, yOffset + platform.getPosition().y, platform.getRadius()); } } shapeRenderer.end(); batch.begin(); for (int ii = 0; ii < match.getTexts().size(); ii++) { GameText text = match.getTexts().get(ii); text.getFont().draw(batch, text.getText(), text.getPosition().x, text.getPosition().y); text.reduceLifeTime(); if (text.getLifeTime() == 0) { match.getTexts().remove(ii); ii--; } } HandleText(); batch.end(); if (Config.debug) { debugRenderer.render(match.GetWorld(), matrix); } } private void HandleEnd() { if (match != null) { if (match.isMatchEnded()) { if (match.tUpdate != null) { match.tUpdate.interrupt(); } if (match.tRefresh != null) { match.tRefresh.interrupt(); } Config.page = new GameSummary(match); } } } private void HandleText() { int i = 0; for (Player player : match.getPlayers()) { Color fontColor = player.getCharacter().getColor(); Label.LabelStyle style = new Label.LabelStyle(font30, fontColor); Label.LabelStyle style2 = new Label.LabelStyle(font20, fontColor); Label testLabel = new Label("XXXXXXXXXXX", style); float quarter = (match.getMap().getWidth() + (match.getMap().getWidth() / 4 - new Label(match.getPlayers().get(match.getPlayers().size() - 1).getName(), style).getWidth())) / 4; Label label = new Label(player.getName(), style); Label label2 = new Label(player.getCharacter().getWeapon().getName() + "\r\n" + player.getCharacter().getLives() + " lives\r\n" + (player.getKillsOnOthers() + (player.getDominatonScore() / 60)) + " points", style2); float x = (xOffset + (quarter * i)) + (match.getMap().getWidth() / 8) - testLabel.getWidth() / 2; float y = yOffset - label.getHeight() - 25; label.setPosition(x, y); label.setAlignment(Align.left); float x2 = label.getX(); float y2 = label.getY() - label2.getHeight(); label2.setPosition(x2, y2); label2.setAlignment(Align.left); label.draw(batch, 1); label2.draw(batch, 1); i++; } Label.LabelStyle style; if (timeLeft < 30) { style = new Label.LabelStyle(font30, CircleColors.red); } else { style = new Label.LabelStyle(font30, CircleColors.black); } Label label = new Label( String.format("%02d", (int) Math.floor(timeLeft / 60)) + ":" + String.format("%02d", timeLeft % 60), style); Label testLabel = new Label("88:88", style); label.setPosition(xOffset + match.getMap().getWidth() - testLabel.getWidth(), yOffset + match.getMap().getHeight()); label.draw(batch, 1); if (timeLeft <= 0) { Config.page = new GameSummary(match); } } }