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 character.CircleCharacter; 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.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.Timer; import java.util.TimerTask; import match.GameText; import misc.CircleColors; import networking.GameClient; import weapon.HandGun; /** * * @author <a href="mailto:d.vangils@student.fontys.nl">Dennis van Gils</a> */ public class TutorialMatchScreen 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 int i = 0; private Object testPlayer; private GameText text; public TutorialMatchScreen() { 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 moveForward(); SendMessage("pressed goforward"); } if (keyCode == Input.Keys.A) { // strafeleft strafeLeft(); SendMessage("pressed strafeleft"); } if (keyCode == Input.Keys.S) { // gobackward moveBackwards(); SendMessage("pressed gobackward"); } if (keyCode == Input.Keys.D) { // strafeleft strafeRight(); SendMessage("pressed straferight"); } if (keyCode == Input.Keys.UP || keyCode == Input.Keys.SPACE || keyCode == Input.Keys.NUMPAD_8) { // attack shoot(); SendMessage("pressed attack"); } if (keyCode == Input.Keys.LEFT || keyCode == Input.Keys.NUMPAD_4) { // turnleft turnLeft(); SendMessage("pressed turnleft"); } if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.NUMPAD_6) { // turnright 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(); moveForward(); } 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()); for (Player player : match.getPlayers()) { 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); } } //Test(); for (Projectile projectile : match.getProjectiles()) { 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 (MapObject mapObject : match.getMap().getObjects()) { 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()); } } } //batch.draw(img, 0, 0); shapeRenderer.end(); batch.begin(); for (int i = 0; i < match.getTexts().size(); i++) { GameText text = match.getTexts().get(i); text.getFont().draw(batch, text.getText(), text.getPosition().x, text.getPosition().y); text.reduceLifeTime(); if (text.getLifeTime() == 0) { match.getTexts().remove(i); i--; } } 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() + " 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); } } /** * starts the move forward stage of the tutorial after it is done it starts * the move backwards stage of the tutorial */ public void moveForward() { if (i == 1) { text.setLifeTime(0); i++; moveBackwards(); } if (i == 0) { text = new GameText("Press W to walk forward", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } /** * starts the move backwards stage of the tutorial after its done starts the * strafe left stage of the tutorial */ public void moveBackwards() { if (i == 3) { text.setLifeTime(0); i++; strafeLeft(); } if (i == 2) { text = new GameText("Great! Now press S to walk backwards", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } /** * starts the strafeleft stage of the tutorial after its done starts the * strafe right stage of the tutorial */ public void strafeLeft() { if (i == 5) { text.setLifeTime(0); i++; strafeRight(); } if (i == 4) { text = new GameText("A to strafe left", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } /** * starts the strafe right stage of the tutorial after its done starts the * turn left stage of the tutorial */ public void strafeRight() { if (i == 7) { text.setLifeTime(0); i++; turnLeft(); } if (i == 6) { text = new GameText("And D to strafe right", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } /** * starts the turn left stage of the tutorial after its done starts the turn * right stage of the tutorial */ public void turnLeft() { if (i == 9) { text.setLifeTime(0); i++; turnRight(); } if (i == 8) { text = new GameText("You can turn left by pressing the left arrow key", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } /** * starts the turn right stage of the tutorial after its done starts the * shoot stage of the tutorial */ public void turnRight() { if (i == 11) { text.setLifeTime(0); i++; shoot(); } if (i == 10) { text = new GameText("And turn right by pressing the right arrow key", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } public void shoot() { if (i == 13) { text.setLifeTime(0); i++; finishTutorial(); } if (i == 12) { text = new GameText("Finally you can use the up arrow or spacebar to shoot.", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); i++; } } /** * lets the player finish the tutorial */ public void finishTutorial() { final GameText text = new GameText( "End of the tutorial. You can now hit the escape key to return to the main menu", new Point(0, 0)); layout.setText(text.getFont(), text.getText()); int xPosition = Gdx.graphics.getWidth() / 2 - (int) layout.width / 2; int yPosition = Gdx.graphics.getHeight() / 2 - match.getMap().getHeight() / 2 - 30; text.setPosition(new Point(xPosition, yPosition)); match.addText(text); } }