diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml deleted file mode 100644 index 0e12312..0000000 --- a/.github/workflows/gradle.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Java CI - -on: [push] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Build with Gradle - run: ./gradlew build diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..a0f370a --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,26 @@ +name: Java CI with Maven + +on: [push, pull_request] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 8 + uses: actions/setup-java@v3 + with: + java-version: '8' + distribution: 'temurin' + cache: maven + - name: Build with Maven - Install + run: mvn install:install-file -Dfile=res/jars/JSplashScreen.jar -DgroupId=com.thehowtotutorial.splashscreen -DartifactId=JSplashScreen -Dversion=1.0 -Dpackaging=jar + - name: Build with Maven - From Pom + run: mvn -B package --file pom.xml + + + # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive + - name: Update dependency graph + uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 diff --git a/.gitignore b/.gitignore index 9ced6d8..25ad1df 100644 --- a/.gitignore +++ b/.gitignore @@ -190,3 +190,5 @@ out/ .log.txt /.gradle/ Package game.png +target/* +.PrintType-TEST.txt \ No newline at end of file diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 34cd597..0000000 --- a/build.gradle +++ /dev/null @@ -1,53 +0,0 @@ -plugins { - id 'com.adarshr.test-logger' version '2.0.0' - id "nebula.lint" version "16.0.2" -} - -repositories{ - mavenCentral() -} - -apply plugin: 'java' -apply plugin: 'maven' - -sourceSets{ - main{ - java.srcDirs = ['src'] - resources.srcDirs = ['res'] - } - test{ - java.srcDirs = ['test'] - } -} - -dependencies{ - implementation 'org.apache.commons:commons-text:+' - implementation 'org.apache.commons:commons-lang3:3.+' - testImplementation 'junit:junit:4.13' - implementation files('res/jars/JSplashScreen.jar') -} - -jar { - from configurations.compileClasspath.collect { zipTree it } - manifest.attributes 'Main-Class' : 'com.redomar.game.Launcher' -} - -task createPom { - pom { - project { - groupId 'com.redomar.game' - artifactId 'javagame' - version 'Alpha 1.8.5' - - inceptionYear '2013' - licenses { - license { - name 'GNU AFFERO GENERAL PUBLIC LICENSE 3.0' - url 'https://www.gnu.org/licenses/agpl-3.0.txt' - distribution 'repo' - } - } - - } - }.writeTo("pom.xml") -} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index cc4fdc2..0000000 Binary files a/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 5028f28..0000000 --- a/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,5 +0,0 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists diff --git a/pom.xml b/pom.xml index 199fa6a..9b46a42 100644 --- a/pom.xml +++ b/pom.xml @@ -1,10 +1,11 @@ - + 4.0.0 com.redomar.game javagame - Alpha 1.8.5 + Alpha 1.8.6 2013 @@ -13,24 +14,136 @@ repo + + + local-maven-repo + file://${project.basedir}/res/jars + + org.apache.commons commons-text - + - runtime + 1.10.0 org.apache.commons commons-lang3 - 3.+ - runtime + 3.12.0 + + + org.jetbrains + annotations + 23.1.0 + + + com.thehowtotutorial.splashscreen + JSplashScreen + 1.0 junit junit - 4.13.1 + 4.13.2 test + + 8 + 8 + UTF-8 + UTF-8 + + + src + + + res + + + + test + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + false + + ${*} + @ + + UTF-8 + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.3.0 + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/lib + false + false + true + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + + true + lib/ + com.redomar.game.Launcher + + + + + + com.thehowtotutorial.splashscreen + JSplashScreen + 1.0 + + + + + org.apache.maven.plugins + maven-install-plugin + 2.4 + + + install-external-non-maven-jar-MWS-Client-into-local-maven-repo + clean + + default + com.thehowtotutorial.splashscreen + JSplashScreen + 1.0 + ${project.basedir}/res/jars/JSplashScreen.jar + jar + true + + + install-file + + + + + + diff --git a/src/com/redomar/game/Game.java b/src/com/redomar/game/Game.java index f47f56c..4cfd945 100644 --- a/src/com/redomar/game/Game.java +++ b/src/com/redomar/game/Game.java @@ -5,13 +5,16 @@ import com.redomar.game.entities.Player; import com.redomar.game.entities.Vendor; import com.redomar.game.entities.trees.Spruce; +import com.redomar.game.event.InputHandler; +import com.redomar.game.event.MouseHandler; import com.redomar.game.gfx.Screen; import com.redomar.game.gfx.SpriteSheet; import com.redomar.game.level.LevelHandler; +import com.redomar.game.lib.Either; import com.redomar.game.lib.Font; import com.redomar.game.lib.Time; -import com.redomar.game.script.PrintTypes; -import com.redomar.game.script.Printing; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; import org.apache.commons.text.WordUtils; import javax.swing.*; @@ -23,7 +26,7 @@ /* * This module forms the core architecture of the JavaGame. It coordinates the various - * audio and input handler components, generates the frame, renders the screen graphics, spawns + * audio and input handler components, generates the frame, renders the screen graphics, spawns * NPCs and customizes the player. Game is also responsible for changing the maps and levels, as well * as displaying various messages on the screen (e.g. fps) */ @@ -31,65 +34,54 @@ public class Game extends Canvas implements Runnable { // Setting the size and name of the frame/canvas private static final long serialVersionUID = 1L; - private static final String game_Version = "v1.8.5 Alpha"; + private static final String game_Version = "v1.8.6 Alpha"; private static final int WIDTH = 160; private static final int HEIGHT = (WIDTH / 3 * 2); private static final int SCALE = 3; - private static final String NAME = "Game"; // The name of the JFrame panel + private static final String NAME = "Game"; // The name of the JFrame panel + private static final Time time = new Time(); // Represents the calendar's time value, in hh:mm:ss + private static final boolean[] alternateCols = new boolean[2]; // Boolean array describing shirt and face colour + private static Game game; - private static Time time = new Time(); // Represents the calender's time value, in hh:mm:ss // The properties of the player, npc, and fps/tps - private static int Jdata_Host; // The host of a multiplayer game (only available in earlier versions) - private static String Jdata_UserName = ""; - private static String Jdata_IP = "127.0.0.1"; - private static boolean changeLevel = false; // Determines whether the player teleports to another level - private static boolean npc = false; // Non-player character (NPC) initialized to non-existing - private static int map = 0; // Map of the level, initialized to map default map - private static int shirtCol; // The colour of the character's shirt - private static int faceCol; // The colour (ethnicity) of the character (their face) - private static boolean[] alternateCols = new boolean[2]; // Boolean array describing shirt and face colour - private static int fps; // The frame rate (frames per second), frequency at which images are displayed on the canvas - private static int tps; // The ticks (ticks per second), unit measure of time for one iteration of the game logic loop. + private static boolean changeLevel = false; // Determines whether the player teleports to another level + private static boolean npc = false; // Non-player character (NPC) initialized to non-existing + private static int map = 0; // Map of the level, initialized to map default map + private static int shirtCol; // The colour of the character's shirt + private static int faceCol; // The colour (ethnicity) of the character (their face) + private static int fps; // The frame rate (frames per second), frequency at which images are displayed on the canvas + private static int tps; // The ticks (ticks per second), unit measure of time for one iteration of the game logic loop. private static int steps; - private static boolean devMode; // Determines whether the game is in developer mode - private static boolean closingMode; // Determines whether the game will exit + private static boolean devMode; // Determines whether the game is in developer mode + private static boolean closingMode; // Determines whether the game will exit // Audio, input, and mouse handler objects private static JFrame frame; private static AudioHandler backgroundMusic; - private static boolean running = false; // Determines whether the game is currently in process - private static InputHandler input; // Accepts keyboard input and follows the appropriate actions - private static MouseHandler mouse; // Tracks mouse movement and clicks, and follows the appropriate actions - private static InputContext context; // Provides methods to control text input facilities - private int tickCount = 0; + private static boolean running = false; // Determines whether the game is currently in process + private static InputHandler input; // Accepts keyboard input and follows the appropriate actions + private static MouseHandler mouse; // Tracks mouse movement and clicks, and follows the appropriate actions + private static InputContext context; // Provides methods to control text input facilities // Graphics - private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, - BufferedImage.TYPE_INT_RGB); - private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()) // Array of red, green and blue values for each pixel - .getData(); - private int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours (6 shades of red, 6 of green, and 6 of blue) - private BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, - BufferedImage.TYPE_INT_RGB); + private final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); + private final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Array of red, green and blue values for each pixel + private final int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours (6 shades of red, 6 of green, and 6 of blue) + private final BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, BufferedImage.TYPE_INT_RGB); + private final Font font = new Font(); // Font object capable of displaying 2 fonts: Arial and Segoe UI + private final Printer printer = new Printer(); + boolean musicPlaying = false; + private int tickCount = 0; private Screen screen; - private WindowHandler window; - private LevelHandler level; // Loads and renders levels along with tiles, entities, projectiles and more. - + private LevelHandler level; // Loads and renders levels along with tiles, entities, projectiles and more. //The entities of the game private Player player; - private Dummy dummy; // Dummy NPC follows the player around - private Vendor vendor; // Vendor NPC exhibits random movement and is only found on cutom_level - private Spruce spruce; // Tree -- Spruce - private Font font = new Font(); // Font object capable of displaying 2 fonts: Arial and Segoe UI - private String nowPlaying; - private boolean notActive = true; - private int trigger = 0; - private Printing print = new Printing(); + private Dummy dummy; // Dummy NPC follows the player around + private Vendor vendor; // Vendor NPC exhibits random movement and is only found on custom_level /** * @author Redomar - * @version Alpha 1.8.4 */ public Game() { context = InputContext.getInstance(); @@ -99,13 +91,13 @@ public Game() { setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); - setFrame(new JFrame(NAME)); // Creates the frame with a defined name - getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when user closes the frame + setFrame(new JFrame(NAME)); // Creates the frame with a defined name + getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when user closes the frame getFrame().setLayout(new BorderLayout()); - getFrame().add(this, BorderLayout.CENTER); // Centers the canvas inside the JFrame - getFrame().pack(); // Sizes the frame so that all its contents are at or above their preferred sizes + getFrame().add(this, BorderLayout.CENTER); // Centers the canvas inside the JFrame + getFrame().pack(); // Sizes the frame so that all its contents are at or above their preferred sizes getFrame().setResizable(false); - getFrame().setLocationRelativeTo(null); // Centres the window on the screen + getFrame().setLocationRelativeTo(null); // Centres the window on the screen getFrame().setVisible(true); requestFocus(); @@ -114,22 +106,23 @@ public Game() { } /** - * This method will spawn a dummy NPC into the level only if they are allowed to be spawned in. + * This method will spawn a dummy NPC into the level only if they are allowed to be spawned in. * They will be spawned at position (100, 150) with a red shirt and caucasian face. */ public static void npcSpawn() { - if (isNpc() == true) { // If NPCs are allowed in the level - game.setDummy(new Dummy(game.level, "Dummy", 100, 150, 500, // Create a new dummy NPC on the current game level, with name 'Dummy' - 543)); // at position (100, 150), with a red shirt and caucasian ethnicity + // If NPCs are allowed in the level + if (isNpc()) { + // Create a new dummy NPC to the current game level + game.setDummy(new Dummy(game.level, "Dummy", 100, 150, 500, 543)); game.level.addEntity(Game.getDummy()); } } /** - * This method will remove a dummy NPC from the level only if they are not allowed to be in it. + * This method will remove a dummy NPC from the level only if they are not allowed to be in it. */ public static void npcKill() { - if (isNpc() == false) { // If NPCs are not allowed in the level + if (!isNpc()) { // If NPCs are not allowed in the level game.level.removeEntity(Game.getDummy()); } } @@ -158,13 +151,6 @@ public void setLevel(LevelHandler level) { this.level = level; } - public static Time getTime() { - return Game.time; - } - - public void setTime(Time time) { - Game.time = time; - } public static Game getGame() { return game; @@ -196,28 +182,27 @@ public static int getMap() { /** * Sets the level to the map [.png] provided. Starts at x100 y100. - * @param Map_str * - * Also sets predefined character colours. + * @param Map_str Also sets predefined character colours. */ public void setMap(String Map_str) { setLevel(new LevelHandler(Map_str)); - if (alternateCols[0]) { // If the first element (shirt colour) is set to True - Game.setShirtCol(240); // The player's shirt colour will be green + if (alternateCols[0]) { // If the first element (shirt colour) is set to True + Game.setShirtCol(240); // The player's shirt colour will be green } - if (!alternateCols[0]) { // If the first element (shirt colour) is set to False - Game.setShirtCol(111); // The player's shirt colour will be black + if (!alternateCols[0]) { // If the first element (shirt colour) is set to False + Game.setShirtCol(111); // The player's shirt colour will be black } - if (alternateCols[1]) { // If the last element (face colour) is set to True - Game.setFaceCol(310); // The player will be African + if (alternateCols[1]) { // If the last element (face colour) is set to True + Game.setFaceCol(310); // The player will be African } - if (!alternateCols[1]) { // If the last element (face colour) is set to False - Game.setFaceCol(543); // The player will be caucasian + if (!alternateCols[1]) { // If the last element (face colour) is set to False + Game.setFaceCol(543); // The player will be caucasian } - setPlayer(new Player(level, 100, 100, input, - getJdata_UserName(), shirtCol, faceCol)); + setPlayer(new Player(level, 100, 100, input, "", shirtCol, faceCol)); level.addEntity(player); - spruce = new Spruce(level, 70,170, 2 ); + // Tree -- Spruce + Spruce spruce = new Spruce(level, 70, 170, 2); level.addEntity(spruce); } @@ -241,30 +226,6 @@ public void setDummy(Dummy dummy) { this.dummy = dummy; } - public static String getJdata_IP() { - return Jdata_IP; - } - - public static void setJdata_IP(String jdata_IP) { - Jdata_IP = jdata_IP; - } - - public static int getJdata_Host() { - return Jdata_Host; - } - - public static void setJdata_Host(int jdata_Host) { - Jdata_Host = jdata_Host; - } - - public static String getJdata_UserName() { - return Jdata_UserName; - } - - public static void setJdata_UserName(String jdata_UserName) { - Jdata_UserName = jdata_UserName; - } - public static String getGameVersion() { return game_Version; } @@ -285,14 +246,6 @@ public static void setFaceCol(int faceCol) { Game.faceCol = faceCol; } - public static boolean[] getAlternateCols() { - return alternateCols; - } - - public static void setAlternateCols(boolean[] alternateCols) { // Boolean array should have a size of only two elements - Game.alternateCols = alternateCols; - } - // Sets ethnicity/face colour for the player public static void setAlternateColsR(boolean alternateCols) { Game.alternateCols[1] = alternateCols; @@ -311,14 +264,6 @@ public static void setBackgroundMusic(AudioHandler backgroundMusic) { Game.backgroundMusic = backgroundMusic; } - public static InputHandler getInput() { - return input; - } - - public void setInput(InputHandler input) { - Game.input = input; - } - public static MouseHandler getMouse() { return mouse; } @@ -344,9 +289,9 @@ public static void setClosing(boolean closing) { } /* - * This method initializes the game once it starts. It populates the colour array with actual colours (6 shades each of RGB). - * This method also builds the initial game level (custom_level), spawns a new vendor NPC, and begins accepting keyboard and mouse input/tracking. - */ + * This method initializes the game once it starts. It populates the colour array with actual colours (6 shades each of RGB). + * This method also builds the initial game level (custom_level), spawns a new vendor NPC, and begins accepting keyboard and mouse input/tracking. + */ public void init() { setGame(this); int index = 0; @@ -356,74 +301,69 @@ public void init() { int rr = (r * 255 / 5); // Split all 256 colours into 6 shades (0, 51, 102 ... 255) int gg = (g * 255 / 5); int bb = (b * 255 / 5); - colours[index++] = rr << 16 | gg << 8 | bb; // All colour values (RGB) are placed into one 32-bit integer, populating the colour array + colours[index++] = rr << 16 | gg << 8 | bb; // All colour values (RGB) are placed into one 32-bit integer, populating the colour array } } } screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png")); - input = new InputHandler(this); // Input begins to record key presses - setMouse(new MouseHandler(this)); // Mouse tracking and clicking is now recorded - setWindow(new WindowHandler(this)); - try{ + input = new InputHandler(this); // Input begins to record key presses + setMouse(new MouseHandler(this)); // Mouse tracking and clicking is now recorded +// setWindow(new WindowHandler(this)); + try { setMap("/levels/custom_level.png"); - } catch (Exception e){ - System.err.println(e); + } catch (Exception e) { + printer.print(e.toString(), PrintTypes.ERROR); } - setMap(1); // 1 corresponds to custom_level + setMap(1); // 1 corresponds to custom_level game.setVendor(new Vendor(level, "Vendor", 215, 215, 304, 543)); level.addEntity(getVendor()); } /** - * This method will start the game and allow the user to start playing - */ + * This method will start the game and allow the user to start playing + */ public synchronized void start() { - Game.setRunning(true); // Game will run - new Thread(this, "GAME").start(); // Thread is an instance of Runnable. Whenever it is started, it will run the run() method + Game.setRunning(true); // Game will run + new Thread(this, "GAME").start(); // Thread is an instance of Runnable. Whenever it is started, it will run the run() method } /** - * This method will stop the game - */ + * This method will stop the game + */ public synchronized void stop() { - Game.setRunning(false); // Game will not run + Game.getBackgroundMusic().close(); + Game.setRunning(false); // Game will not run } /** - * This method forms the game loop, determining how the game runs. It runs throughout the entire game, - * continuously updating the game state and rendering the game. - */ + * This method forms the game loop, determining how the game runs. It runs throughout the entire game, + * continuously updating the game state and rendering the game. + */ public void run() { long lastTime = System.nanoTime(); - double nsPerTick = 1000000000D / 60D; // The number of nanoseconds in one tick (number of ticks limited to 60 per update) + double nsPerTick = 1000000000D / 60D; // The number of nanoseconds in one tick (number of ticks limited to 60 per update) // 1 billion nanoseconds in one second int ticks = 0; int frames = 0; - long lastTimer = System.currentTimeMillis(); // Used for updating ticks and frames once every second + long lastTimer = System.currentTimeMillis(); // Used for updating ticks and frames once every second double delta = 0; - init(); // Initialize the game environment + init(); // Initialize the game environment while (Game.isRunning()) { - long now = System.nanoTime(); // Current time (now) compared to lastTime to calculate elapsed time - delta += (now - lastTime) / nsPerTick; // Elapsed time in seconds multiplied by 60 + long now = System.nanoTime(); // Current time (now) compared to lastTime to calculate elapsed time + delta += (now - lastTime) / nsPerTick; // Elapsed time in seconds multiplied by 60 lastTime = now; boolean shouldRender = false; - while (delta >= 1) { // Once 1/60 seconds or more have passed + while (delta >= 1) { // Once 1/60 seconds or more have passed ticks++; - tick(); // Tick updates - delta -= 1; // Delta becomes less than one again and the loop will close - shouldRender = true; // Limits the frames to 60 per second - } - - try { - Thread.sleep(2); // Delays the thread by 2 milliseconds - prevents the loop from using too much CPU - } catch (InterruptedException e) { // If the current thread is interrupted, the interrupted status is cleared - e.printStackTrace(); + tick(); // Tick updates + delta -= 1; // Delta becomes less than one again and the loop will close + shouldRender = true; // Limits the frames to 60 per second } if (shouldRender) { @@ -431,143 +371,135 @@ public void run() { render(); } - if (System.currentTimeMillis() - lastTimer >= 1000) { // If elapsed time is greater than or equal to 1 second, update - lastTimer += 1000; // Updates in another second - getFrame().setTitle( - "JavaGame - Version " - + WordUtils.capitalize(game_Version).substring( - 1, game_Version.length())); + if (System.currentTimeMillis() - lastTimer >= 1000) { // If elapsed time is greater than or equal to 1 second, update + lastTimer += 1000; // Updates in another second + getFrame().setTitle("JavaGame - Version " + WordUtils.capitalize(game_Version).substring(1, game_Version.length())); fps = frames; tps = ticks; - frames = 0; // Reset the frames once per second - ticks = 0; // Reset the ticks once per second + frames = 0; // Reset the frames once per second + ticks = 0; // Reset the ticks once per second } } } /** - * This method updates the logic of the game. - */ + * This method updates the logic of the game. + */ public void tick() { setTickCount(getTickCount() + 1); + Either musicKeyAction = input.toggleActionWithCheckedRunnable(input.getM_KEY(), musicPlaying, () -> Game.getBackgroundMusic().play(), () -> Game.getBackgroundMusic().stop()); + musicKeyAction.either(exception -> { + printer.cast().print("Failed to play music", PrintTypes.MUSIC); + printer.exception(exception.toString()); + musicPlaying = false; + }, isPlaying -> musicPlaying = isPlaying); + level.tick(); } + /** - * This method displays the current state of the game. - */ + * This method displays the current state of the game. + */ public void render() { BufferStrategy bs = getBufferStrategy(); if (bs == null) { - createBufferStrategy(3); // Creates a new bs with triple buffering, which reduces tearing and cross-image pixelation + createBufferStrategy(3); return; } - // Centres the player in the middle of the screen + int xOffset = (int) getPlayer().getX() - (screen.getWidth() / 2); int yOffset = (int) getPlayer().getY() - (screen.getHeight() / 2); level.renderTiles(screen, xOffset, yOffset); - - /* - * for (int x = 0; x < level.width; x++) { int colour = Colours.get(-1, - * -1, -1, 000); if (x % 10 == 0 && x != 0) { colour = Colours.get(-1, - * -1, -1, 500); } Font.render((x % 10) + "", screen, 0 + (x * 8), 0, - * colour, 1); } - */ - level.renderEntities(screen); level.renderProjectileEntities(screen); - // Copies pixel data from the screen into the game for (int y = 0; y < screen.getHeight(); y++) { for (int x = 0; x < screen.getWidth(); x++) { int colourCode = screen.getPixels()[x + y * screen.getWidth()]; - if (colourCode < 255) { // If it is a valid colour code - pixels[x + y * WIDTH] = colours[colourCode]; // Sets the corresponding pixel from the screen to the game + if (colourCode < 255) { + pixels[x + y * WIDTH] = colours[colourCode]; } } } - if (isChangeLevel() == true && getTickCount() % 60 == 0) { + if (isChangeLevel() && getTickCount() % 60 == 0) { Game.setChangeLevel(true); setChangeLevel(false); } - if (changeLevel == true) { // If the player is teleporting to a different level - print.print("Teleported into new world", PrintTypes.GAME); - if (getMap() == 1) { // If the player is currently on custom_level + if (changeLevel) { + printer.print("Teleported into new world", PrintTypes.GAME); + if (getMap() == 1) { setMap("/levels/water_level.png"); - if (getDummy() != null) { // Gave nullPointerException(); upon - // entering new world. + if (getDummy() != null) { + level.removeEntity(getDummy()); setNpc(false); } - level.removeEntity(getVendor()); // When teleporting away from custom_level, remove vendor NPC (always found on custom_level) + level.removeEntity(getVendor()); setMap(2); - } else if (getMap() == 2) { // If the player is currently on water_level + } else if (getMap() == 2) { setMap("/levels/custom_level.png"); level.removeEntity(getDummy()); setNpc(false); - level.addEntity(getVendor()); // Add a vendor NPC - they are always found on custom_level + level.addEntity(getVendor()); setMap(1); } changeLevel = false; } Graphics g = bs.getDrawGraphics(); - g.drawRect(0, 0, getWidth(), getHeight()); // Creates a rectangle the same size as the screen + g.drawRect(0, 0, getWidth(), getHeight()); g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null); status(g, isDevMode(), isClosing()); - // Font.render("Hi", screen, 0, 0, Colours.get(-1, -1, -1, 555), 1); g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null); g.setColor(Color.WHITE); g.setFont(font.getSegoe()); - g.drawString( - "Welcome " - + WordUtils.capitalizeFully(player - .getSanitisedUsername()), 3, getHeight() - 17); + g.drawString("Welcome " + WordUtils.capitalize(player.getName()), 3, getHeight() - 17); g.setColor(Color.ORANGE); - if (context.getLocale().getCountry().equals("BE") // If the player resides in Belgium or France (i.e. uses AZERTY keyboard) - || context.getLocale().getCountry().equals("FR")) { // Displays "Press A to quit" in orange at the bottom-middle portion of the screen - g.drawString("Press A to quit", (getWidth() / 2) - - ("Press A to quit".length() * 3), getHeight() - 17); - } else { // If the player resides anywhere else (i.e. uses QWERTY keyboard) - g.drawString("Press Q to quit", (getWidth() / 2) // Displays "Press Q to quit" in orange at the bottom-middle portion of the screen - - ("Press Q to quit".length() * 3), getHeight() - 17); + if (context.getLocale().getCountry().equals("BE") || context.getLocale().getCountry().equals("FR")) { + g.drawString("Press A to quit", (getWidth() / 2) - ("Press A to quit".length() * 3), getHeight() - 17); + } else { + g.drawString("Press Q to quit", (getWidth() / 2) - ("Press Q to quit".length() * 3), getHeight() - 17); } + g.setColor(Color.YELLOW); - g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3)); // Displays the current time in yellow in the bottom right corner of the screen (hh:mm:ss) + g.drawString(time.getTime(), (getWidth() - 63), (getHeight() - 3)); + if (devMode) { + g.setColor(Color.CYAN); + g.drawString("Debug Mode Enabled", (getWidth() - 153), (getHeight() - 17)); + } g.setColor(Color.GREEN); - if (backgroundMusic.getActive()) { // If music is turned on - g.drawString("MUSIC is ON ", 3, getHeight() - 3); // Displays "MUSIC IS ON" in green in the bottom left corner of the screen. + + if (backgroundMusic.getActive()) { + g.drawString("MUSIC is ON ", 3, getHeight() - 3); } - g.dispose(); // Frees up memory and resources for graphics + + g.dispose(); bs.show(); } /* - * This method displays information regarding various aspects/stats of the game, dependent upon - * whether it is running in developer mode, or if the application is closing. - */ + * This method displays information regarding various aspects/stats of the game, dependent upon + * whether it is running in developer mode, or if the application is closing. + */ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) { - if (TerminalMode) { // If running in developer mode + if (TerminalMode) { g.setColor(Color.CYAN); - g.drawString("JavaGame Stats", 0, 10); // Display "JavaGame Stats" in cyan at the bottom left of the screen - g.drawString("FPS/TPS: " + fps + "/" + tps, 0, 25); // Display the FPS and TPS in cyan directly above "JavaGame Stats" + g.drawString("JavaGame Stats", 0, 10); + g.drawString("FPS/TPS: " + fps + "/" + tps, 0, 25); if ((player.getNumSteps() & 15) == 15) { steps += 1; } - g.drawString("Foot Steps: " + steps, 0, 40); // Display the number of "Foot Steps" (in cyan, above the previous) - g.drawString( - "NPC: " + WordUtils.capitalize(String.valueOf(isNpc())), 0, // Displays whether the NPC is on the level (in cyan, above the previous) - 55); - g.drawString("Mouse: " + getMouse().getX() + "x |" // Displays the position of the cursor (in cyan, above the previous) - + getMouse().getY() + "y", 0, 70); - if (getMouse().getButton() != -1) // If a mouse button is pressed - g.drawString("Button: " + getMouse().getButton(), 0, 85); // Displays the mouse button that is pressed (in cyan, above the previous) + g.drawString("Foot Steps: " + steps, 0, 40); + g.drawString("NPC: " + WordUtils.capitalize(String.valueOf(isNpc())), 0, 55); + g.drawString("Mouse: " + getMouse().getX() + "x |" + getMouse().getY() + "y", 0, 70); + if (getMouse().getButton() != -1) g.drawString("Button: " + getMouse().getButton(), 0, 85); g.setColor(Color.CYAN); g.fillRect(getMouse().getX() - 12, getMouse().getY() - 12, 24, 24); } @@ -576,27 +508,10 @@ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) { return; } g.setColor(Color.BLACK); - g.fillRect(0, 0, getWidth(), getHeight()); // Make the screen fully black + g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.RED); - g.drawString("Shutting down the Game", (getWidth() / 2) - 70, // Display "Shutting down the Game" in red in the middle of the screen - (getHeight() / 2) - 8); - g.dispose(); // Free up memory for graphics - } - - public WindowHandler getWindow() { - return window; - } - - public void setWindow(WindowHandler window) { - this.window = window; - } - - public String getNowPlaying() { - return nowPlaying; - } - - public void setNowPlaying(String nowPlaying) { - this.nowPlaying = nowPlaying; + g.drawString("Shutting down the Game", (getWidth() / 2) - 70, (getHeight() / 2) - 8); + g.dispose(); } public int getTickCount() { diff --git a/src/com/redomar/game/InputHandler.java b/src/com/redomar/game/InputHandler.java deleted file mode 100644 index b39185b..0000000 --- a/src/com/redomar/game/InputHandler.java +++ /dev/null @@ -1,230 +0,0 @@ -package com.redomar.game; - -import com.redomar.game.lib.SleepThread; -import com.redomar.game.script.PopUp; -import com.redomar.game.script.PrintTypes; -import com.redomar.game.script.Printing; - -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.awt.im.InputContext; - -public class InputHandler implements KeyListener { - - private boolean isAzertyCountry; - private Key up = new Key(); - private Key down = new Key(); - private Key left = new Key(); - private Key right = new Key(); - private Printing print = new Printing(); - private int map; - private boolean ignoreInput = false; - private boolean toggleMusic = false; - private PopUp popup = new PopUp(); - - public InputHandler(Game game) { - InputContext context = InputContext.getInstance(); - // Important to know whether the keyboard is in Azerty or Qwerty. - // Azerty countries used QZSD instead of WASD keys. - isAzertyCountry = context.getLocale().getCountry().equals("BE") - || context.getLocale().getCountry().equals("FR"); - game.addKeyListener(this); - } - - public void keyPressed(KeyEvent e) { - toggleKey(e.getKeyCode(), true); - } - - public void keyReleased(KeyEvent e) { - toggleKey(e.getKeyCode(), false); - } - - public void keyTyped(KeyEvent e) { - - } - - private void toggleKey(int keyCode, boolean isPressed) { - if (!isIgnoreInput()) { - if (keyCode == KeyEvent.VK_Z && isAzertyCountry || keyCode == KeyEvent.VK_W && !isAzertyCountry - || keyCode == KeyEvent.VK_UP) { - up.toggle(isPressed); - } - - if (keyCode == KeyEvent.VK_Q && isAzertyCountry || keyCode == KeyEvent.VK_A && !isAzertyCountry - || keyCode == KeyEvent.VK_LEFT) { - left.toggle(isPressed); - } - - if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) { - down.toggle(isPressed); - } - - if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) { - right.toggle(isPressed); - } - } - if (isIgnoreInput()) { - up.toggle(false); - down.toggle(false); - left.toggle(false); - right.toggle(false); - } - - if (keyCode == KeyEvent.VK_M) { - if(!toggleMusic){ - Game.getBackgroundMusic().play(); - print.print("Playing Music", PrintTypes.MUSIC); - toggleMusic = true; - } - } - - if (keyCode == KeyEvent.VK_COMMA) { - Game.getBackgroundMusic().stop(); - if(toggleMusic) - print.print("Stopping Music", PrintTypes.MUSIC); - toggleMusic = false; - } - - - if (keyCode == KeyEvent.VK_W && isAzertyCountry || keyCode == KeyEvent.VK_Z && !isAzertyCountry) { - // if (map == 0){ - // Game.getGame().setMap("/levels/water_level.png"); - // map++; - // } else{ - // Game.getGame().setMap("/levels/custom_level.png"); - // map--; - // } - if (Game.getMap() == 2) { - if (Game.isNpc()) { - Game.setNpc(false); - } - Game.setChangeLevel(true); - } - } - if (keyCode == KeyEvent.VK_N) { - if (Game.getPlayer().isMoving()) { - setIgnoreInput(true); - int n = popup.Warn("Stop moving before spawning dummy AI"); - if (n == 0) { - setIgnoreInput(false); - } - return; - } - if (!Game.isNpc()) { - Game.setNpc(true); - Game.npcSpawn(); - print.print("Dummy has been spawned", PrintTypes.GAME); - } - } - if (keyCode == KeyEvent.VK_K) { - if (Game.isNpc()) { - Game.setNpc(false); - Game.npcKill(); - print.print("Dummy has been removed", PrintTypes.GAME); - } - } - - if (keyCode == KeyEvent.VK_A && isAzertyCountry || keyCode == KeyEvent.VK_Q && !isAzertyCountry) - this.quitGame(); - - if (keyCode == KeyEvent.VK_BACK_QUOTE) { - if (!Game.isClosing() && !Game.isDevMode()) { - Game.setDevMode(true); - new Thread(new SleepThread()); - } - } - } - - private void quitGame() { - Game.setClosing(true); - print.removeLog(); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - Game.getLevel().removeEntity( - Game.getPlayer().getSanitisedUsername()); - Game.setRunning(false); - Game.getFrame().dispose(); - System.exit(0); - } - - public void untoggle(boolean toggle) { - this.ignoreInput = toggle; - } - - public int getMap() { - return map; - } - - public void setMap(int map) { - this.map = map; - } - - public Key getUp() { - return up; - } - - public void setUp(Key up) { - this.up = up; - } - - public Key getDown() { - return down; - } - - public void setDown(Key down) { - this.down = down; - } - - public Key getLeft() { - return left; - } - - public void setLeft(Key left) { - this.left = left; - } - - public Key getRight() { - return right; - } - - public void setRight(Key right) { - this.right = right; - } - - public boolean isIgnoreInput() { - return ignoreInput; - } - - private void setIgnoreInput(boolean ignoreInput) { - this.ignoreInput = ignoreInput; - } - - public class Key { - private int numTimesPressed = 0; - private boolean pressed = false; - - public int getNumTimesPressed() { - return numTimesPressed; - } - - public boolean isPressed() { - return pressed; - } - - void toggle(boolean isPressed) { - pressed = isPressed; - if (isPressed) { - numTimesPressed++; - } - } - - public void off() { - pressed = false; - numTimesPressed = 0; - } - } - -} diff --git a/src/com/redomar/game/Launcher.java b/src/com/redomar/game/Launcher.java index 224ac35..784b9f9 100644 --- a/src/com/redomar/game/Launcher.java +++ b/src/com/redomar/game/Launcher.java @@ -2,10 +2,8 @@ import com.redomar.game.menu.Menu; -public class Launcher -{ - public static void main(String[] args) - { +public class Launcher { + public static void main(String[] args) { new Menu().start(); } } diff --git a/src/com/redomar/game/UML.ucls b/src/com/redomar/game/UML.ucls deleted file mode 100644 index 6c9da34..0000000 --- a/src/com/redomar/game/UML.ucls +++ /dev/null @@ -1,1119 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/com/redomar/game/audio/AudioHandler.java b/src/com/redomar/game/audio/AudioHandler.java index a13f7f2..b3d8e2c 100644 --- a/src/com/redomar/game/audio/AudioHandler.java +++ b/src/com/redomar/game/audio/AudioHandler.java @@ -1,93 +1,105 @@ package com.redomar.game.audio; -import com.redomar.game.script.PrintTypes; -import com.redomar.game.script.Printing; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; +import org.jetbrains.annotations.NotNull; import javax.sound.sampled.*; +import java.io.BufferedInputStream; import java.io.File; -import java.util.Arrays; - +import java.io.IOException; +import java.io.InputStream; +import java.util.Objects; public class AudioHandler { + public static final Printer musicPrinter = new Printer(PrintTypes.MUSIC); private Clip clip; private boolean active = false; - private final Printing p = new Printing(); + private boolean music = false; - public AudioHandler(String path){ + public AudioHandler(String path) { check(path); } - public AudioHandler(File file){ + public AudioHandler(@NotNull File file) { check(file.toString()); } - private void check(String path){ + public AudioHandler(String path, boolean music) { + this.music = music; + check(path); + } + + private void check(String path) { try { - if(!path.equals("")){ + if (!path.equals("")) { initiate(path); } else { throw new NullPointerException(); } - } catch (NullPointerException e){ - p.print("Destination Cannot be empty", PrintTypes.ERROR); + } catch (NullPointerException e) { + musicPrinter.print("Destination Cannot be empty", PrintTypes.ERROR); throw e; } } - private void initiate(String path){ - try{ - AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(AudioHandler.class.getResourceAsStream(path)); + /** + * Initialises an audio clip from the specified file path. + * + * @param path the file path of the audio clip + */ + private void initiate(String path) { + try { + InputStream inputStream = new BufferedInputStream(Objects.requireNonNull(AudioHandler.class.getResourceAsStream(path))); + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputStream); + audioInputStream.mark(Integer.MAX_VALUE); AudioFormat baseFormat = audioInputStream.getFormat(); - AudioFormat decodeFormat = new AudioFormat( - AudioFormat.Encoding.PCM_SIGNED, - baseFormat.getSampleRate(), 16, - baseFormat.getChannels(), - baseFormat.getChannels() * 2, - baseFormat.getSampleRate(), - false - ); - AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream( - decodeFormat, audioInputStream); + AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false); + AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodeFormat, audioInputStream); clip = AudioSystem.getClip(); clip.open(decodedAudioInputStream); - } catch (Exception e){ - System.err.println(Arrays.toString(e.getStackTrace())); - p.print("Audio Failed to initiate", PrintTypes.ERROR); + } catch (IOException e) { + musicPrinter.cast().exception("Audio file not found " + path); + musicPrinter.cast().exception(e.getMessage()); + + } catch (Exception e) { + musicPrinter.print("Audio Failed to initiate", PrintTypes.ERROR); + musicPrinter.cast().exception(e.getMessage()); } } - public void play(){ - try{ - if(clip == null) return; - stop(); + + public void play() { + try { + if (clip == null) return; clip.setFramePosition(0); clip.start(); + if (music) musicPrinter.print("Playing Music"); active = true; } catch (Exception e) { - p.print("Audio Failed to play", PrintTypes.ERROR); + musicPrinter.print("Audio Failed to play", PrintTypes.ERROR); throw e; } } - public void setVolume(float velocity){ + public void setVolume(float velocity) throws NullPointerException { FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); volume.setValue(velocity); - } public void stop() { if (clip.isRunning()) clip.stop(); + if (music) musicPrinter.print("Stopping Music"); active = false; } - public void close(){ + public void close() { stop(); clip.close(); } - public boolean getActive(){ + public boolean getActive() { return this.active; } - } diff --git a/src/com/redomar/game/entities/Dummy.java b/src/com/redomar/game/entities/Dummy.java index 89cd7bf..9916101 100644 --- a/src/com/redomar/game/entities/Dummy.java +++ b/src/com/redomar/game/entities/Dummy.java @@ -2,40 +2,20 @@ import com.redomar.game.Game; import com.redomar.game.entities.efx.Swim; -import com.redomar.game.gfx.Colours; -import com.redomar.game.gfx.Screen; import com.redomar.game.level.LevelHandler; -import com.redomar.game.level.Node; - -import java.util.List; public class Dummy extends Mob { - private static double speed = 0.75; - private static int[] collisionBoders = {0, 7, 0, 7}; - private int colour, shirtCol, faceCol; // = Colours.get(-1, 111, 240, 310); - private int tickCount = 0; - private double xa = 0; - private double ya = 0; - private boolean[] swimType; - private int[] swimColour; - private List path = null; - private int time = 0; - private Swim swim; + private static final double SPEED = 0.75; + private static final int[] MOB_TILE = {8, 28}; + private static final int[] COLLISION_BORDERS = {0, 7, 0, 7}; - public Dummy(LevelHandler level, String name, int x, int y, int shirtCol, - int faceCol) { - super(level, name, x, y, speed, collisionBoders); - this.faceCol = faceCol; - this.shirtCol = shirtCol; - this.colour = Colours.get(-1, 111, shirtCol, faceCol); + public Dummy(LevelHandler level, String name, int x, int y, int shirtCol, int faceCol) { + super(level, name, x, y, MOB_TILE, SPEED, COLLISION_BORDERS, shirtCol, faceCol); } public void tick() { - - //List players = level.getPlayers(this, 8); - aStarMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game - .getPlayer().getY(), xa, ya, speed, this, path, time); + aStarMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game.getPlayer().getY(), SPEED, this); setSwim(new Swim(level, (int) getX(), (int) getY())); @@ -48,78 +28,6 @@ public void tick() { } - public void render(Screen screen) { - time++; - int xTile = 8; - int yTile = 28; - int walkingSpeed = 4; - int flipTop = (numSteps >> walkingSpeed) & 1; - int flipBottom = (numSteps >> walkingSpeed) & 1; - - if (movingDir == 1) { - xTile += 2; - if (!isMoving || swim.isActive(swimType)){ - yTile -= 2; - } - } else if (movingDir == 0 && !isMoving || movingDir == 0 && swim.isActive(swimType)) { - yTile -= 2; - } else if (movingDir > 1) { - xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2; - flipTop = (movingDir - 1) % 2; - if(!isMoving){ - xTile = 4; - } - } - - int modifier = 8 * scale; - int xOffset = (int) getX() - modifier / 2; - int yOffset = (int) getY() - modifier / 2 - 4; - - if (isSwimming || isMagma || isMuddy) { - swimColour = swim.waveCols(isSwimming, isMagma, isMuddy); - - int waterColour = 0; - yOffset += 4; - - colour = Colours.get(-1, 111, -1, faceCol); - - if (tickCount % 60 < 15) { - waterColour = Colours.get(-1, -1, swimColour[0], -1); - } else if (15 <= tickCount % 60 && tickCount % 60 < 30) { - yOffset--; - waterColour = Colours.get(-1, swimColour[1], swimColour[2], -1); - } else if (30 <= tickCount % 60 && tickCount % 60 < 45) { - waterColour = Colours.get(-1, swimColour[2], -1, swimColour[1]); - } else { - yOffset--; - waterColour = Colours.get(-1, -1, swimColour[1], swimColour[2]); - } - - screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour, - 0x00, 1); - screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour, - 0x01, 1); - } - - screen.render((xOffset + (modifier * flipTop)), yOffset, - (xTile + yTile * 32), colour, flipTop, scale); - screen.render((xOffset + modifier - (modifier * flipTop)), yOffset, - ((xTile + 1) + yTile * 32), colour, flipTop, scale); - if (!isSwimming && !isMagma && !isMuddy) { - screen.render((xOffset + (modifier * flipBottom)), - (yOffset + modifier), (xTile + (yTile + 1) * 32), colour, - flipBottom, scale); - screen.render((xOffset + modifier - (modifier * flipBottom)), - (yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32), - colour, flipBottom, scale); - colour = Colours.get(-1, 111, shirtCol, faceCol); - } - } - - public Swim getSwim() { - return swim; - } - public void setSwim(Swim swim) { this.swim = swim; } diff --git a/src/com/redomar/game/entities/Entity.java b/src/com/redomar/game/entities/Entity.java index c423d44..7bb9a82 100644 --- a/src/com/redomar/game/entities/Entity.java +++ b/src/com/redomar/game/entities/Entity.java @@ -2,12 +2,16 @@ import com.redomar.game.gfx.Screen; import com.redomar.game.level.LevelHandler; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; public abstract class Entity { protected double x, y; + protected int xTile, yTile; protected String name; protected LevelHandler level; + protected Printer entityPrinter = new Printer(PrintTypes.ENTITY); public Entity(LevelHandler level) { init(level); @@ -19,6 +23,8 @@ public final void init(LevelHandler level) { public abstract void tick(); + public abstract void render(Screen screen, int xTile, int yTile); + public abstract void render(Screen screen); public double getX() { @@ -44,4 +50,20 @@ public String getName() { public void setName(String name) { this.name = name; } + + public int getXTile() { + return xTile; + } + + public void setXTile(int xTile) { + this.xTile = xTile; + } + + public int getYTile() { + return yTile; + } + + public void setYTile(int yTile) { + this.yTile = yTile; + } } diff --git a/src/com/redomar/game/entities/Mob.java b/src/com/redomar/game/entities/Mob.java index 01a2009..1c71e05 100644 --- a/src/com/redomar/game/entities/Mob.java +++ b/src/com/redomar/game/entities/Mob.java @@ -1,12 +1,18 @@ package com.redomar.game.entities; -import com.redomar.game.entities.projectiles.Medium; +import com.redomar.game.Game; +import com.redomar.game.entities.efx.Swim; +import com.redomar.game.entities.efx.WaterType; import com.redomar.game.entities.projectiles.Projectile; import com.redomar.game.entities.projectiles.Small; +import com.redomar.game.gfx.Colours; +import com.redomar.game.gfx.Screen; import com.redomar.game.level.LevelHandler; import com.redomar.game.level.Node; import com.redomar.game.level.tiles.Tile; +import com.redomar.game.lib.Font; import com.redomar.game.lib.utils.Vector2i; +import com.redomar.game.objects.Inventory; import java.util.ArrayList; import java.util.List; @@ -14,6 +20,8 @@ public abstract class Mob extends Entity { + protected final int shirtColour; + protected final int faceColour; protected Random random = new Random(); protected double speed; protected int numSteps = 0; @@ -24,24 +32,34 @@ public abstract class Mob extends Entity { protected boolean isMagma = false; protected boolean isMuddy = false; protected boolean changeLevels = false; - protected int ticker; + protected boolean showName = false; /** * [0] Contains the xMin
* [1] Contains the xMax
* [2] Contains the yMin
* [3] Contains the yMax */ - protected int[] collisionBoders = new int[4]; + protected int[] collisionBorders; - protected List projectiles = new ArrayList(); + protected List projectiles = new ArrayList<>(); + protected int colour; + protected int tickCount = 0; + protected Swim swim; + protected boolean[] swimType; - public Mob(LevelHandler level, String name, int x, int y, double speed, int[] collisionBoders) { + + public Mob(LevelHandler level, String name, int x, int y, int[] tile, double speed, int[] collisionBorders, int shirtColour, int faceColour) { super(level); this.name = name; this.setX(x); this.setY(y); + this.setXTile(tile[0]); + this.setYTile(tile[1]); this.speed = speed; - this.collisionBoders = collisionBoders; + this.collisionBorders = collisionBorders; + this.shirtColour = shirtColour; + this.faceColour = faceColour; + this.colour = Colours.get(-1, 111, shirtColour, faceColour); } public void move(double xa, double ya) { @@ -72,28 +90,28 @@ public void move(double xa, double ya) { movingDir = 3; } - while (xa != 0){ - if (Math.abs(xa) > 1){ - if (!hasCollided(abs(xa), ya)) { + while (xa != 0) { + if (Math.abs(xa) > 1) { + if (hasCollided(abs(xa), ya)) { this.x += abs(xa); } xa -= abs(xa); } else { - if (!hasCollided(abs(xa), ya)) { + if (hasCollided(abs(xa), ya)) { this.x += xa; } xa = 0; } } - while (ya != 0){ - if (Math.abs(ya) > 1){ - if (!hasCollided(xa, abs(ya))) { + while (ya != 0) { + if (Math.abs(ya) > 1) { + if (hasCollided(xa, abs(ya))) { this.y += abs(ya); } ya -= abs(ya); } else { - if (!hasCollided(xa, abs(ya))) { + if (hasCollided(xa, abs(ya))) { this.y += ya; } ya = 0; @@ -102,54 +120,35 @@ public void move(double xa, double ya) { } - public boolean hasCollided(double xa, double ya){ - int xMin = collisionBoders[0]; - int xMax = collisionBoders[1]; - int yMin = collisionBoders[2]; - int yMax = collisionBoders[3]; + public boolean hasCollided(double xa, double ya) { + int xMin = collisionBorders[0]; + int xMax = collisionBorders[1]; + int yMin = collisionBorders[2]; + int yMax = collisionBorders[3]; for (int x = xMin; x < xMax; x++) { if (isSolid((int) xa, (int) ya, x, yMin)) { - return true; + return false; } - } - - for (int x = xMin; x < xMax; x++) { if (isSolid((int) xa, (int) ya, x, yMax)) { - return true; + return false; } } for (int y = yMin; y < yMax; y++) { if (isSolid((int) xa, (int) ya, xMin, y)) { - return true; + return false; } - } - - for (int y = yMin; y < yMax; y++) { if (isSolid((int) xa, (int) ya, xMax, y)) { - return true; + return false; } } - return false; - } - - public boolean hasCollidedAlt(int xa, int ya){ - boolean solid = false; - for (int c = 0; c < 4; c++) { - double xt = ((x + xa) - c % 2 * 8) / 8; - double yt = ((y + ya) - c / 2 * 8) / 8; - int ix = (int) Math.ceil(xt); - int iy = (int) Math.ceil(yt); - if (c % 2 == 0) ix = (int) Math.floor(xt); - if (c / 2 == 0) iy = (int) Math.floor(yt); - if(level.getTile(ix, iy).isSolid()) solid = true; - } - return solid; + return true; } - private int abs(double i){ + @Deprecated + private int abs(double i) { if (i < 0) return -1; return 1; } @@ -160,46 +159,39 @@ protected boolean isSolid(int xa, int ya, int x, int y) { return false; } - Tile lastTile = level.getTile(((int) this.getX() + x) >> 3, - ((int) this.getY() + y) >> 3); - Tile newtTile = level.getTile(((int) this.getX() + x + xa) >> 3, ((int) this.getY() - + y + ya) >> 3); + Tile lastTile = level.getTile(((int) this.getX() + x) >> 3, ((int) this.getY() + y) >> 3); + Tile newtTile = level.getTile(((int) this.getX() + x + xa) >> 3, ((int) this.getY() + y + ya) >> 3); return !lastTile.equals(newtTile) && newtTile.isSolid(); } - protected void aStarMovementAI(int x, int y, int px, int py, double xa, - double ya, double speed, Mob mob, List path, int time) { - xa = 0; - ya = 0; + protected void aStarMovementAI(int x, int y, int px, int py, double speed, Mob mob) { + double xa = 0; + double ya = 0; Vector2i start = new Vector2i(x >> 3, y >> 3); Vector2i goal = new Vector2i(px >> 3, py >> 3); - path = level.findPath(start, goal); - if(path != null) { - if(path.size() > 0){ + List path = level.findPath(start, goal); + if (path != null) { + if (path.size() > 0) { Vector2i vector = path.get(path.size() - 1).tile; - if(x < vector.getX() << 3) xa =+ speed; - if(x > vector.getX() << 3) xa =- speed; - if(y < vector.getY() << 3) ya =+ speed; - if(y > vector.getY() << 3) ya =- speed; + if (x < vector.getX() << 3) xa = speed; + if (x > vector.getX() << 3) xa = -speed; + if (y < vector.getY() << 3) ya = speed; + if (y > vector.getY() << 3) ya = -speed; moveMob(xa, ya, mob); } } } - protected void followMovementAI(int x, int y, int px, int py, double xa, - double ya, double speed, Mob mob) { - ya = 0; - xa = 0; - if (px > x) - xa+=speed; - if (px < x) - xa-=speed; - if (py > y) - ya+=speed; - if (py < y) - ya-=speed; + @SuppressWarnings("unused") + protected void followMovementAI(int x, int y, int px, int py, double speed, Mob mob) { + double ya = 0; + double xa = 0; + if (px > x) xa += speed; + if (px < x) xa -= speed; + if (py > y) ya += speed; + if (py < y) ya -= speed; moveMob(xa, ya, mob); } @@ -212,11 +204,13 @@ protected double[] randomMovementAI(double x, double y, double xa, double ya, in ya = 0; } } - if(x <= 180){ + if (x <= 180) { xa = 1; + } + if (y <= 180) { ya = -1; } - double move[] = new double[2]; + double[] move = new double[2]; move[0] = xa; move[1] = ya; return move; @@ -231,16 +225,9 @@ protected void moveMob(double xa, double ya, Mob mob) { } } - protected void shoot(double x, double y, double dir, double buttonId, boolean secondry){ -// dir = dir * (180 /Math.PI); -// Printing print = new Printing(); -// print.print("Angle: "+ dir, PrintTypes.GAME); - if(buttonId == 1){ - Projectile p = new Small(level, (int) x,(int) y, dir); - projectiles.add(p); - level.addProjectileEntities(p); - } else if(buttonId == 3 && secondry == true){ - Projectile p = new Medium(level, (int) x,(int) y, dir); + protected void shoot(double x, double y, double dir, double buttonId) { + if (buttonId == 1) { + Projectile p = new Small(level, (int) x, (int) y, dir); projectiles.add(p); level.addProjectileEntities(p); } @@ -258,8 +245,8 @@ public void setNumSteps(int numSteps) { this.numSteps = numSteps; } - public boolean isMoving() { - return this.isMoving; + public void setSwim(Swim swim) { + this.swim = swim; } public void setMoving(boolean isMoving) { @@ -270,4 +257,89 @@ public void setMovingDir(int movingDir) { this.movingDir = movingDir; } + @Override + public void render(Screen screen) { + int xTile = getXTile(); + int yTile = getYTile(); + int modifier = 8 * scale; + int xOffset = (int) getX() - modifier / 2; + int yOffset = (int) getY() - modifier / 2 - 4; + int walkingSpeed = 4; + int flipTop = (numSteps >> walkingSpeed) & 1; + int flipBottom = (numSteps >> walkingSpeed) & 1; + + Inventory.activate(); + + if (movingDir == 1) { + xTile += 2; + if (!isMoving || swim.isActive(swimType)) { + yTile -= 2; + } + } else if (movingDir == 0 && !isMoving || movingDir == 0 && swim.isActive(swimType)) { + yTile -= 2; + } else if (movingDir > 1) { + xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2; + flipTop = (movingDir - 1) % 2; + if (!isMoving) { + xTile = 4; + } + } + + if (changeLevels) { + Game.setChangeLevel(true); + } + + WaterType type = isSwimming ? WaterType.SWIMMING : isMagma ? WaterType.MAGMA : isMuddy ? WaterType.SWAMP : null; + if (type != null) { + int[] swimColour = swim.waveCols(type); + + int waterColour; + yOffset += 4; + + colour = Colours.get(-1, 111, -1, faceColour); + + if (tickCount % 60 < 15) { + waterColour = Colours.get(-1, -1, swimColour[0], -1); + } else if (tickCount % 60 < 30) { + yOffset--; + waterColour = Colours.get(-1, swimColour[1], swimColour[2], -1); + } else if (tickCount % 60 < 45) { + waterColour = Colours.get(-1, swimColour[2], -1, swimColour[1]); + } else { + yOffset--; + waterColour = Colours.get(-1, -1, swimColour[1], swimColour[2]); + } + + screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour, 0x00, 1); + screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour, 0x01, 1); + } + + screen.render((xOffset + (modifier * flipTop)), yOffset, (xTile + yTile * 32), colour, flipTop, scale); + screen.render((xOffset + modifier - (modifier * flipTop)), yOffset, ((xTile + 1) + yTile * 32), colour, flipTop, scale); + if (!isSwimming && !isMagma && !isMuddy) { + screen.render((xOffset + (modifier * flipBottom)), (yOffset + modifier), (xTile + (yTile + 1) * 32), colour, flipBottom, scale); + screen.render((xOffset + modifier - (modifier * flipBottom)), (yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32), colour, flipBottom, scale); + colour = Colours.get(-1, 111, shirtColour, faceColour); + } + + if (name != null && showName) { + /* + * Improved name centering above player's sprite. + * Using player's own x value cast to int with an adjusted formula + * -posmicanomaly + */ + + int fontCharSize = 8; + int offsetUnit = ((name.length() & 1) == 0 ? fontCharSize / 2 : 0); + int nameOffset = (name.length() / 2) * fontCharSize - offsetUnit; + Font.render(name, screen, (int) x - nameOffset, yOffset - 10, Colours.get(-1, -1, -1, 111), 1); + + } + } + + public void render(Screen screen, int xTile, int yTile) { + setXTile(xTile); + setYTile(yTile); + render(screen); + } } diff --git a/src/com/redomar/game/entities/Player.java b/src/com/redomar/game/entities/Player.java index 5b9a0cf..0307749 100644 --- a/src/com/redomar/game/entities/Player.java +++ b/src/com/redomar/game/entities/Player.java @@ -1,98 +1,76 @@ package com.redomar.game.entities; import com.redomar.game.Game; -import com.redomar.game.InputHandler; import com.redomar.game.entities.efx.Swim; import com.redomar.game.entities.projectiles.Medium; import com.redomar.game.entities.projectiles.Projectile; import com.redomar.game.entities.projectiles.Small; -import com.redomar.game.gfx.Colours; -import com.redomar.game.gfx.Screen; +import com.redomar.game.event.InputHandler; import com.redomar.game.level.LevelHandler; -import com.redomar.game.lib.Font; -import com.redomar.game.lib.Name; -import com.redomar.game.objects.Inventory; +import com.redomar.game.lib.HashGen; + +import java.util.Objects; public class Player extends Mob { - private static Name customeName = new Name(); - public static String guestPlayerName = customeName.setName("Player "); + private static final String PLAYER_ID = new HashGen(false, 3).getHash(); + private static final int[] PLAYER_TILE = {0, 28}; + private static final int[] COLLISION_BORDERS = {-2, 8, 0, 7}; private static double speed = 1; - private static int[] collisionBoders = {-2, 8, 0, 7}; - private InputHandler input; - private Swim swim; - private int colour, shirtCol, faceCol; - private int tickCount = 0; - private String userName; - private boolean[] swimType; - private int[] swimColour; - private int fireRate = 0; - - // Need to add a class for constants - private final int fontCharSize = 8; - // "Cache" the division for the username length, no need for 60 divisions per second here. - private int nameOffset = 0; - - public Player(LevelHandler level, int x, int y, InputHandler input, - String userName, int shirtCol, int faceCol) { - super(level, "Player", x, y, speed, collisionBoders); - this.input = input; - this.userName = userName; - this.faceCol = faceCol; - this.shirtCol = shirtCol; - this.colour = Colours.get(-1, 111, shirtCol, faceCol); - fireRate = Small.FIRE_RATE; - } - - public static double getSpeed() { - return speed; - } + private final InputHandler inputHandler; + private int fireRate; - public static void setSpeed(double speed) { - Player.speed = speed; + public Player(LevelHandler level, int x, int y, InputHandler inputHandler, String name, int shirtColour, int faceColour) { + super(level, "Player", x, y, PLAYER_TILE, speed, COLLISION_BORDERS, shirtColour, faceColour); + this.inputHandler = inputHandler; + this.name = !Objects.equals(name, "") ? name : String.format("Player %s", PLAYER_ID); + fireRate = Small.FIRE_RATE; + showName = true; } public void tick() { double xa = 0; double ya = 0; - if (input != null) { - if (input.getUp().isPressed() && input.isIgnoreInput() == false) { + if (inputHandler != null) { + + speed = inputHandler.getSHIFTED().isPressed() ? 2 : 1; + + if (inputHandler.getUP_KEY().isPressed()) { ya -= speed; } - if (input.getDown().isPressed() && input.isIgnoreInput() == false) { + if (inputHandler.getDOWN_KEY().isPressed()) { ya += speed; } - if (input.getLeft().isPressed() && input.isIgnoreInput() == false) { + if (inputHandler.getLEFT_KEY().isPressed()) { xa -= speed; } - if (input.getRight().isPressed() && input.isIgnoreInput() == false) { + if (inputHandler.getRIGHT_KEY().isPressed()) { xa += speed; } } - if(fireRate > 0) fireRate--; + if (fireRate > 0) fireRate--; - if (Game.getMouse().getButton() == 1 || Game.getMouse().getButton() == 3){ - if(fireRate <= 0){ - if(Game.getMouse().getButton()== 1){ + if (Game.getMouse().getButton() == 1 || Game.getMouse().getButton() == 3) { + if (fireRate <= 0) { + if (Game.getMouse().getButton() == 1) { fireRate = Small.FIRE_RATE; - }else if(Game.getMouse().getButton() == 3){ + } else if (Game.getMouse().getButton() == 3) { fireRate = Medium.FIRE_RATE; } - if(!swim.isActive(swimType)){ - double dx = Game.getMouse().getX() - 480/2; - double dy = Game.getMouse().getY() - 320/2; + if (!swim.isActive(swimType)) { + double dx = Game.getMouse().getX() - 480 / 2d; + double dy = Game.getMouse().getY() - 320 / 2d; double dir = Math.atan2(dy, dx); - shoot(x, y, dir, Game.getMouse().getButton(), false); + shoot(x, y, dir, Game.getMouse().getButton()); } } } - for (int i = 0; i < projectiles.size(); i++) { - Projectile p = projectiles.get(i); - if(p.isRemoved()){ - projectiles.remove(i); + for (Projectile p : projectiles) { + if (p.isRemoved()) { + p.remove(); Game.getLevel().removeProjectileEntities(p); } } @@ -100,13 +78,11 @@ public void tick() { if (xa != 0 || ya != 0) { move(xa, ya); isMoving = true; - Game.getGame(); - } else { isMoving = false; } - setSwim(new Swim(level, (int) getX(), (int) getY())); + swim = new Swim(level, (int) getX(), (int) getY()); swimType = swim.swimming(isSwimming, isMagma, isMuddy); isSwimming = swimType[0]; isMagma = swimType[1]; @@ -119,128 +95,14 @@ public void tick() { tickCount++; } - public void render(Screen screen) { - int xTile = 0; - int yTile = 28; - int walkingSpeed = 4; - int flipTop = (numSteps >> walkingSpeed) & 1; - int flipBottom = (numSteps >> walkingSpeed) & 1; - - Inventory.activate(); - - if (movingDir == 1) { - xTile += 2; - if (!isMoving || swim.isActive(swimType)){ - yTile -= 2; - } - } else if (movingDir == 0 && !isMoving || movingDir == 0 && swim.isActive(swimType)) { - yTile -= 2; - } else if (movingDir > 1) { - xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2; - flipTop = (movingDir - 1) % 2; - if(!isMoving){ - xTile = 4; - } - } - - int modifier = 8 * scale; - int xOffset = (int) getX() - modifier / 2; - int yOffset = (int) getY() - modifier / 2 - 4; - - if (changeLevels) { - Game.setChangeLevel(true); - } - - if(isSwimming || isMagma || isMuddy){ - swimColour = swim.waveCols(isSwimming, isMagma, isMuddy); - - int waterColour = 0; - yOffset += 4; - - colour = Colours.get(-1, 111, -1, faceCol); - - if (tickCount % 60 < 15) { - waterColour = Colours.get(-1, -1, swimColour[0], -1); - } else if (15 <= tickCount % 60 && tickCount % 60 < 30) { - yOffset--; - waterColour = Colours.get(-1, swimColour[1], swimColour[2], -1); - } else if (30 <= tickCount % 60 && tickCount % 60 < 45) { - waterColour = Colours.get(-1, swimColour[2], -1, swimColour[1]); - } else { - yOffset--; - waterColour = Colours.get(-1, -1, swimColour[1], swimColour[2]); - } - - screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour, - 0x00, 1); - screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour, - 0x01, 1); - } - - screen.render((xOffset + (modifier * flipTop)), yOffset, - (xTile + yTile * 32), colour, flipTop, scale); - screen.render((xOffset + modifier - (modifier * flipTop)), yOffset, - ((xTile + 1) + yTile * 32), colour, flipTop, scale); - if (!isSwimming && !isMagma && !isMuddy) { - screen.render((xOffset + (modifier * flipBottom)), - (yOffset + modifier), (xTile + (yTile + 1) * 32), colour, - flipBottom, scale); - screen.render((xOffset + modifier - (modifier * flipBottom)), - (yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32), - colour, flipBottom, scale); - colour = Colours.get(-1, 111, shirtCol, faceCol); - } - - if (userName != null) { - /* - * Improved userName centering above player's sprite. - * Using player's own x value cast to int with an adjusted formula - * -posmicanomaly - */ - - Font.render(userName, - screen, - (int)x - nameOffset, - yOffset - 10, - Colours.get(-1, -1, -1, 111), 1); - - } - } - + @Deprecated public String getUsername() { - if (this.userName.isEmpty()) { - return guestPlayerName; - } - return this.userName; - } - - public void setUsername(String name) { - this.userName = name; + return this.name; } + @Deprecated public String getSanitisedUsername() { - if (this.getUsername() == null || this.userName.isEmpty()) { - setUsername(guestPlayerName); - - // Perfectly matches the text over the head - int offsetUnit = ((userName.length() & 1) == 0 ? fontCharSize / 2 : 0); - nameOffset = (userName.length() / 2) * fontCharSize - offsetUnit; - - return guestPlayerName; - } else - if(nameOffset == 0){ - int offsetUnit = ((userName.length() & 1) == 0 ? fontCharSize / 2 : 0); - nameOffset = (userName.length() / 2) * fontCharSize - offsetUnit; - } - return this.getUsername(); - } - - public Swim getSwim() { - return swim; - } - - public void setSwim(Swim swim) { - this.swim = swim; + return this.name; } } diff --git a/src/com/redomar/game/entities/PlayerMP.java b/src/com/redomar/game/entities/PlayerMP.java index 4707161..14d4cdd 100644 --- a/src/com/redomar/game/entities/PlayerMP.java +++ b/src/com/redomar/game/entities/PlayerMP.java @@ -1,26 +1,25 @@ package com.redomar.game.entities; -import com.redomar.game.InputHandler; +import com.redomar.game.event.InputHandler; import com.redomar.game.level.LevelHandler; import java.net.InetAddress; +@Deprecated public class PlayerMP extends Player { - public InetAddress ipAddess; + public InetAddress ipAddress; public int port; - public PlayerMP(LevelHandler level, int x, int y, InputHandler input, - String userName, InetAddress ipAddress, int port, int shirtCol, int faceCol) { + public PlayerMP(LevelHandler level, int x, int y, InputHandler input, String userName, InetAddress ipAddress, int port, int shirtCol, int faceCol) { super(level, x, y, input, userName, shirtCol, faceCol); - this.ipAddess = ipAddress; + this.ipAddress = ipAddress; this.port = port; } - public PlayerMP(LevelHandler level, int x, int y, String userName, - InetAddress ipAddress, int port, int shirtCol, int faceCol) { + public PlayerMP(LevelHandler level, int x, int y, String userName, InetAddress ipAddress, int port, int shirtCol, int faceCol) { super(level, x, y, null, userName, shirtCol, faceCol); - this.ipAddess = ipAddress; + this.ipAddress = ipAddress; this.port = port; } diff --git a/src/com/redomar/game/entities/Vendor.java b/src/com/redomar/game/entities/Vendor.java index 4e5be5c..e377ae6 100644 --- a/src/com/redomar/game/entities/Vendor.java +++ b/src/com/redomar/game/entities/Vendor.java @@ -1,41 +1,24 @@ package com.redomar.game.entities; import com.redomar.game.entities.efx.Swim; -import com.redomar.game.gfx.Colours; -import com.redomar.game.gfx.Screen; import com.redomar.game.level.LevelHandler; -import com.redomar.game.level.Node; - -import java.util.List; public class Vendor extends Mob { - private static double speed = 0.75; - private static int[] collisionBoders = {0, 7, 0, 7}; - private int colour, shirtCol, faceCol; // = Colours.get(-1, 111, 240, 310); - private int tickCount = 0; + private static final double SPEED = 0.75; + private static final int[] MOB_TILE = {0, 28}; + private static final int[] COLLISION_BORDERS = {0, 7, 0, 7}; private int tick = 0; private double xa = 0; private double ya = 0; - private double[] movement; - private boolean[] swimType; - private int[] swimColour; - private List path = null; - private int time = 0; - private Swim swim; - public Vendor(LevelHandler level, String name, int x, int y, int shirtCol, - int faceCol) { - super(level, name, x, y, speed, collisionBoders); - this.faceCol = faceCol; - this.shirtCol = shirtCol; - this.colour = Colours.get(-1, 111, shirtCol, faceCol); + public Vendor(LevelHandler level, String name, int x, int y, int shirtCol, int faceCol) { + super(level, name, x, y, MOB_TILE, SPEED, COLLISION_BORDERS, shirtCol, faceCol); } public void tick() { - tick++; - movement = randomMovementAI(x, y, xa, ya, tick); + double[] movement = randomMovementAI(x, y, xa, ya, tick); this.xa = movement[0]; this.ya = movement[1]; @@ -49,98 +32,6 @@ public void tick() { isMuddy = swimType[2]; tickCount++; - - } - - public void render(Screen screen) { - setTime(time + 1); - int xTile = 8; - int yTile = 28; - int walkingSpeed = 4; - int flipTop = (numSteps >> walkingSpeed) & 1; - int flipBottom = (numSteps >> walkingSpeed) & 1; - - if (movingDir == 1) { - xTile += 2; - if (!isMoving || swim.isActive(swimType)){ - yTile -= 2; - } - } else if (movingDir == 0 && !isMoving || movingDir == 0 && swim.isActive(swimType)) { - yTile -= 2; - } else if (movingDir > 1) { - xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2; - flipTop = (movingDir - 1) % 2; - if(!isMoving){ - xTile = 4; - } - } - - int modifier = 8 * scale; - int xOffset = (int) getX() - modifier / 2; - int yOffset = (int) getY() - modifier / 2 - 4; - - if (isSwimming || isMagma || isMuddy) { - swimColour = swim.waveCols(isSwimming, isMagma, isMuddy); - - int waterColour = 0; - yOffset += 4; - - colour = Colours.get(-1, 111, -1, faceCol); - - if (tickCount % 60 < 15) { - waterColour = Colours.get(-1, -1, swimColour[0], -1); - } else if (15 <= tickCount % 60 && tickCount % 60 < 30) { - yOffset--; - waterColour = Colours.get(-1, swimColour[1], swimColour[2], -1); - } else if (30 <= tickCount % 60 && tickCount % 60 < 45) { - waterColour = Colours.get(-1, swimColour[2], -1, swimColour[1]); - } else { - yOffset--; - waterColour = Colours.get(-1, -1, swimColour[1], swimColour[2]); - } - - screen.render(xOffset, yOffset + 3, 31 + 31 * 32, waterColour, - 0x00, 1); - screen.render(xOffset + 8, yOffset + 3, 31 + 31 * 32, waterColour, - 0x01, 1); - } - - screen.render((xOffset + (modifier * flipTop)), yOffset, - (xTile + yTile * 32), colour, flipTop, scale); - screen.render((xOffset + modifier - (modifier * flipTop)), yOffset, - ((xTile + 1) + yTile * 32), colour, flipTop, scale); - if (!isSwimming && !isMagma && !isMuddy) { - screen.render((xOffset + (modifier * flipBottom)), - (yOffset + modifier), (xTile + (yTile + 1) * 32), colour, - flipBottom, scale); - screen.render((xOffset + modifier - (modifier * flipBottom)), - (yOffset + modifier), ((xTile + 1) + (yTile + 1) * 32), - colour, flipBottom, scale); - colour = Colours.get(-1, 111, shirtCol, faceCol); - } - } - - public Swim getSwim() { - return swim; - } - - public void setSwim(Swim swim) { - this.swim = swim; - } - - public List getPath() { - return path; - } - - public void setPath(List path) { - this.path = path; } - public int getTime() { - return time; - } - - public void setTime(int time) { - this.time = time; - } } diff --git a/src/com/redomar/game/entities/efx/Swim.java b/src/com/redomar/game/entities/efx/Swim.java index b2b6399..354f173 100644 --- a/src/com/redomar/game/entities/efx/Swim.java +++ b/src/com/redomar/game/entities/efx/Swim.java @@ -2,12 +2,22 @@ import com.redomar.game.level.LevelHandler; +import java.util.HashMap; +import java.util.Map; + public class Swim { + private static final Map COLORS = new HashMap<>(); private static LevelHandler level; - private int x; - private int y; - private int[] swimCols = new int[3]; + + static { + COLORS.put(WaterType.SWIMMING, new int[]{255, 255, 115}); + COLORS.put(WaterType.MAGMA, new int[]{541, 521, 510}); + COLORS.put(WaterType.SWAMP, new int[]{422, 410, 321}); + } + + private final int x; + private final int y; public Swim(LevelHandler level, int x, int y) { Swim.level = level; @@ -15,24 +25,13 @@ public Swim(LevelHandler level, int x, int y) { this.y = y; } - public int[] waveCols(boolean isSwimming, boolean isMagma, boolean isMuddy){ + @Deprecated + public int[] waveCols(boolean isSwimming, boolean isMagma, boolean isMuddy) { + return COLORS.get(isSwimming ? WaterType.SWIMMING : isMagma ? WaterType.MAGMA : isMuddy ? WaterType.SWAMP : null); + } - if(isSwimming){ - swimCols[0] = 255; - swimCols[1] = 255; - swimCols[2] = 115; - } - if (isMagma) { - swimCols[0] = 541; - swimCols[1] = 521; - swimCols[2] = 510; - } - if (isMuddy) { - swimCols[0] = 422; - swimCols[1] = 410; - swimCols[2] = 321; - } - return swimCols; + public int[] waveCols(WaterType type) { + return COLORS.get(type); } public boolean water(boolean isSwimming) { @@ -71,19 +70,19 @@ public boolean mud(boolean isMuddy) { } public boolean[] swimming(boolean isSwimming, boolean isMagma, boolean isMuddy) { - boolean[] swimminhType; - swimminhType = new boolean[3]; - swimminhType[0] = water(isSwimming); - swimminhType[1] = magma(isMagma); - swimminhType[2] = mud(isMuddy); - return swimminhType; + boolean[] swimmingType; + swimmingType = new boolean[3]; + swimmingType[0] = water(isSwimming); + swimmingType[1] = magma(isMagma); + swimmingType[2] = mud(isMuddy); + return swimmingType; } - public boolean isActive(boolean[] swimmingType){ - if(swimmingType[0] == true){ + public boolean isActive(boolean[] swimmingType) { + if (swimmingType[0]) { return true; - }else if(swimmingType[1] == true){ + } else if (swimmingType[1]) { return true; - } else return swimmingType[2] == true; + } else return swimmingType[2]; } } diff --git a/src/com/redomar/game/entities/efx/WaterType.java b/src/com/redomar/game/entities/efx/WaterType.java new file mode 100644 index 0000000..5796eca --- /dev/null +++ b/src/com/redomar/game/entities/efx/WaterType.java @@ -0,0 +1,5 @@ +package com.redomar.game.entities.efx; + +public enum WaterType { + SWIMMING, MAGMA, SWAMP +} \ No newline at end of file diff --git a/src/com/redomar/game/entities/projectiles/Projectile.java b/src/com/redomar/game/entities/projectiles/Projectile.java index 9798d55..0633fe9 100644 --- a/src/com/redomar/game/entities/projectiles/Projectile.java +++ b/src/com/redomar/game/entities/projectiles/Projectile.java @@ -1,12 +1,13 @@ package com.redomar.game.entities.projectiles; import com.redomar.game.entities.Entity; +import com.redomar.game.gfx.Screen; import com.redomar.game.level.LevelHandler; import com.redomar.game.level.tiles.Tile; import java.util.Random; -public abstract class Projectile extends Entity{ +public abstract class Projectile extends Entity { protected final double xOrigin, yOrigin; protected double angle; @@ -27,7 +28,7 @@ public Projectile(LevelHandler level, int x, int y, double dir) { protected abstract void move(); - public boolean tileCollision(double xa, double ya, int nx, int ny){ + public boolean tileCollision(double xa, double ya, int nx, int ny) { int xMin = 0; int xMax = 7; int yMin = 0; @@ -60,21 +61,24 @@ public boolean tileCollision(double xa, double ya, int nx, int ny){ return false; } + @Override + public void render(Screen screen, int xTile, int yTile) { + + } + private boolean isSolid(int xa, int ya, int x, int y, int nx, int ny) { if (level == null) { return false; } - Tile lastTile = level.getTile((nx + x) >> 3, - (ny + y) >> 3); - Tile newtTile = level.getTile((nx + x + xa) >> 3, (ny - + y + ya) >> 3); + Tile lastTile = level.getTile((nx + x) >> 3, (ny + y) >> 3); + Tile newtTile = level.getTile((nx + x + xa) >> 3, (ny + y + ya) >> 3); return !lastTile.equals(newtTile) && newtTile.isSolid(); } - public void remove(){ + public void remove() { setRemoved(true); } diff --git a/src/com/redomar/game/entities/projectiles/Small.java b/src/com/redomar/game/entities/projectiles/Small.java index 92a4e1c..e1c8054 100644 --- a/src/com/redomar/game/entities/projectiles/Small.java +++ b/src/com/redomar/game/entities/projectiles/Small.java @@ -7,11 +7,10 @@ import java.io.File; -public class Small extends Projectile{ +public class Small extends Projectile { public static final int FIRE_RATE = 10; private static final File smallShot = new File("/music/small.wav"); - private static AudioHandler smallSound; public Small(LevelHandler level, int x, int y, double dir) { super(level, x, y, dir); @@ -22,27 +21,30 @@ public Small(LevelHandler level, int x, int y, double dir) { nx = speed * Math.cos(angle); ny = speed * Math.sin(angle); - smallSound = new AudioHandler(smallShot); - smallSound.setVolume(-15); - smallSound.play(); + try { + AudioHandler smallSound = new AudioHandler(smallShot); + smallSound.setVolume(-15); + smallSound.play(); + } catch (Exception e) { + entityPrinter.cast().exception(e.toString()).exception("Unable to load Audio file " + smallShot.getName()); + } } public void tick() { - if (tileCollision(x, y,(int) nx,(int) ny)) remove(); + if (tileCollision(x, y, (int) nx, (int) ny)) remove(); move(); } - protected void move(){ + protected void move() { x += nx; y += ny; - double distance = Math.sqrt(Math.abs((xOrigin - x)*(xOrigin - x)+(yOrigin - y)*(yOrigin - y))); - this.distance = distance; - if(this.distance > range) remove(); + this.distance = Math.sqrt(Math.abs((xOrigin - x) * (xOrigin - x) + (yOrigin - y) * (yOrigin - y))); + if (this.distance > range) remove(); } public void render(Screen screen) { - screen.render((int)x,(int)y, 8 * 32, Colours.get(-1, 522, 540, 555), 0x00, 1); + screen.render((int) x, (int) y, 8 * 32, Colours.get(-1, 522, 540, 555), 0x00, 1); } } diff --git a/src/com/redomar/game/entities/trees/Tree.java b/src/com/redomar/game/entities/trees/Tree.java index 6297246..575c8f4 100644 --- a/src/com/redomar/game/entities/trees/Tree.java +++ b/src/com/redomar/game/entities/trees/Tree.java @@ -1,14 +1,14 @@ package com.redomar.game.entities.trees; import com.redomar.game.entities.Entity; +import com.redomar.game.gfx.Screen; import com.redomar.game.level.LevelHandler; public abstract class Tree extends Entity { - private final LevelHandler level; /** * Tree abstract class - * -- Used by Spruce Class + * -- Used by Spruce Class */ //Position variables @@ -17,15 +17,19 @@ public abstract class Tree extends Entity { /** * Constructor for tree entities + * * @param level LevelHandler in which tree spawns - * @param x X co-ordinate - * @param y Y co-ordinate + * @param x X co-ordinate + * @param y Y co-ordinate */ Tree(LevelHandler level, double x, double y) { super(level); - this.level = level; this.x = x; this.y = y; } + @Override + public void render(Screen screen, int xTile, int yTile) { + + } } diff --git a/src/com/redomar/game/event/InputHandler.java b/src/com/redomar/game/event/InputHandler.java new file mode 100644 index 0000000..e5a6f4e --- /dev/null +++ b/src/com/redomar/game/event/InputHandler.java @@ -0,0 +1,174 @@ +package com.redomar.game.event; + +import com.redomar.game.Game; +import com.redomar.game.lib.Either; +import com.redomar.game.lib.SleepThread; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.im.InputContext; +import java.util.HashMap; +import java.util.Map; + +import static com.redomar.game.lib.Either.tryCatch; + +public class InputHandler implements KeyListener { + + private final boolean frenchKeyboardLayout; + private final Printer inputPrinter = new Printer(PrintTypes.INPUT); + private final KeyHandler UP_KEY = new KeyHandler(); + private final KeyHandler DOWN_KEY = new KeyHandler(); + private final KeyHandler LEFT_KEY = new KeyHandler(); + private final KeyHandler RIGHT_KEY = new KeyHandler(); + private final KeyHandler SHIFTED = new KeyHandler(); + private final KeyHandler M_KEY = new KeyHandler(); + + + public InputHandler(Game game) { + InputContext context = InputContext.getInstance(); + frenchKeyboardLayout = context.getLocale().getCountry().equals("BE") || context.getLocale().getCountry().equals("FR"); + game.addKeyListener(this); + } + + public void keyPressed(KeyEvent e) { + toggleKey(e.getKeyCode(), true); + } + + /** + * Toggles between two actions depending on the value of a boolean flag. + * + * @param key the key to be pressed to trigger the action + * @param isToggled the boolean flag that determines which action to activate + * @param actionA the function to execute the first action + * @param actionB the function to execute the second action + * @return an Either object with the result of the action on the right side and an Exception on the left side if an exception is thrown + */ + public Either toggleActionWithCheckedRunnable(KeyHandler key, boolean isToggled, Either.CheckedRunnable actionA, Either.CheckedRunnable actionB) { + boolean pressed = key.isPressed(); + if (pressed) { + if (!isToggled) { + Either result = tryCatch(actionA); + if (result.isLeft()) { + return result.map(__ -> false); + } + isToggled = true; + } else { + Either result = tryCatch(actionB); + if (result.isLeft()) { + return result.map(__ -> true); + } + isToggled = false; + } + key.reset(); + } + return Either.right(isToggled); + } + + public void keyReleased(KeyEvent e) { + int keyCode = e.getKeyCode(); + toggleKey(keyCode, false); + if (keyCode == KeyEvent.VK_BACK_QUOTE) { + if (!Game.isClosing()) { + Game.setDevMode(!Game.isDevMode()); + new Thread(new SleepThread()); + inputPrinter.print(String.format("Debug Mode %s", Game.isDevMode() ? "Enabled" : "Disabled")); + } + } + + if (keyCode == KeyEvent.VK_N) { + if (!Game.isNpc()) { + Game.setNpc(true); + Game.npcSpawn(); + inputPrinter.print("Dummy has been spawned", PrintTypes.GAME); + } + } + } + + public void keyTyped(KeyEvent e) { + + } + + private void toggleKey(int keyCode, boolean isPressed) { + Map keyCodeActions = new HashMap<>(); + + keyCodeActions.put(KeyEvent.VK_S, () -> DOWN_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_D, () -> RIGHT_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_UP, () -> UP_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_LEFT, () -> LEFT_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_DOWN, () -> DOWN_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_RIGHT, () -> RIGHT_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_SHIFT, () -> SHIFTED.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_M, () -> M_KEY.setPressedToggle(isPressed)); + + if (frenchKeyboardLayout) { + keyCodeActions.put(KeyEvent.VK_Q, () -> LEFT_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_Z, () -> UP_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_A, this::quitGame); + } else { + keyCodeActions.put(KeyEvent.VK_A, () -> LEFT_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_W, () -> UP_KEY.setPressed(isPressed)); + keyCodeActions.put(KeyEvent.VK_Q, this::quitGame); + } + + if (keyCodeActions.containsKey(keyCode)) keyCodeActions.get(keyCode).run(); + + if (keyCode == KeyEvent.VK_W && frenchKeyboardLayout || keyCode == KeyEvent.VK_Z && !frenchKeyboardLayout) { + if (Game.getMap() == 2) { + if (Game.isNpc()) { + Game.setNpc(false); + } + Game.setChangeLevel(true); + } + } + + if (keyCode == KeyEvent.VK_K) { + if (Game.isNpc()) { + Game.setNpc(false); + Game.npcKill(); + inputPrinter.print("Dummy has been removed", PrintTypes.GAME); + } + } + + } + + private void quitGame() { + Game.setClosing(true); + if (!inputPrinter.removeLog()) System.err.println("Could not delete Log file"); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Game.getLevel().removeEntity(Game.getPlayer().getName()); + Game.getGame().stop(); + Game.getFrame().dispose(); + System.exit(0); + } + + public KeyHandler getUP_KEY() { + return UP_KEY; + } + + public KeyHandler getDOWN_KEY() { + return DOWN_KEY; + } + + public KeyHandler getLEFT_KEY() { + return LEFT_KEY; + } + + public KeyHandler getRIGHT_KEY() { + return RIGHT_KEY; + } + + public KeyHandler getSHIFTED() { + return SHIFTED; + } + + public KeyHandler getM_KEY() { + return M_KEY; + } + +} diff --git a/src/com/redomar/game/event/KeyHandler.java b/src/com/redomar/game/event/KeyHandler.java new file mode 100644 index 0000000..56b0d06 --- /dev/null +++ b/src/com/redomar/game/event/KeyHandler.java @@ -0,0 +1,35 @@ +package com.redomar.game.event; + +@SuppressWarnings("unused") +public class KeyHandler { + private int numTimesPressed = 0; + private boolean pressed = false; + + @Deprecated + public int getNumTimesPressed() { + return numTimesPressed; + } + + public boolean isPressed() { + return pressed; + } + + void setPressed(boolean isPressed) { + pressed = isPressed; + if (isPressed) numTimesPressed++; + } + + void setPressedToggle(boolean isPressed) { + if (isPressed) return; + pressed = !pressed; + } + + public void on() { + pressed = true; + } + + public void reset() { + pressed = false; + numTimesPressed = 0; + } +} diff --git a/src/com/redomar/game/lib/Mouse.java b/src/com/redomar/game/event/Mouse.java similarity index 81% rename from src/com/redomar/game/lib/Mouse.java rename to src/com/redomar/game/event/Mouse.java index 6a334bd..f05807a 100644 --- a/src/com/redomar/game/lib/Mouse.java +++ b/src/com/redomar/game/event/Mouse.java @@ -1,12 +1,12 @@ -package com.redomar.game.lib; +package com.redomar.game.event; + +import com.redomar.game.Game; +import com.redomar.game.menu.Menu; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; -import com.redomar.game.Game; -import com.redomar.game.menu.Menu; - public class Mouse implements MouseListener, MouseMotionListener { public void mouseDragged(MouseEvent e) { @@ -16,17 +16,9 @@ public void mouseDragged(MouseEvent e) { public void mouseMoved(MouseEvent e) { if (e.getX() > 35 && e.getX() < 455) { // START is being selected - if (e.getY() > 38 && e.getY() < 150) { - Menu.setSelectedStart(true); - } else { - Menu.setSelectedStart(false); - } + Menu.setSelectedStart(e.getY() > 38 && e.getY() < 150); // EXIT is being selected - if (e.getY() > 170 && e.getY() < 280) { - Menu.setSelectedExit(true); - } else { - Menu.setSelectedExit(false); - } + Menu.setSelectedExit(e.getY() > 170 && e.getY() < 280); } else { Menu.setSelectedStart(false); Menu.setSelectedExit(false); diff --git a/src/com/redomar/game/MouseHandler.java b/src/com/redomar/game/event/MouseHandler.java similarity index 89% rename from src/com/redomar/game/MouseHandler.java rename to src/com/redomar/game/event/MouseHandler.java index 38e5e36..3cb3c2a 100644 --- a/src/com/redomar/game/MouseHandler.java +++ b/src/com/redomar/game/event/MouseHandler.java @@ -1,16 +1,18 @@ -package com.redomar.game; +package com.redomar.game.event; + +import com.redomar.game.Game; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; -public class MouseHandler implements MouseListener, MouseMotionListener{ +public class MouseHandler implements MouseListener, MouseMotionListener { private static int mouseX = -1; private static int mouseY = -1; private static int mouseB = -1; - public MouseHandler(Game game){ + public MouseHandler(Game game) { game.addMouseListener(this); game.addMouseMotionListener(this); } diff --git a/src/com/redomar/game/WindowHandler.java b/src/com/redomar/game/event/WindowHandler.java similarity index 86% rename from src/com/redomar/game/WindowHandler.java rename to src/com/redomar/game/event/WindowHandler.java index d04bf42..2180662 100644 --- a/src/com/redomar/game/WindowHandler.java +++ b/src/com/redomar/game/event/WindowHandler.java @@ -1,15 +1,15 @@ -package com.redomar.game; +package com.redomar.game.event; + +import com.redomar.game.Game; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; +@Deprecated +@SuppressWarnings("unused") public class WindowHandler implements WindowListener { - @SuppressWarnings("unused") - private final Game game; - public WindowHandler(Game game) { - this.game = game; Game.getFrame().addWindowListener(this); } diff --git a/src/com/redomar/game/gfx/Colours.java b/src/com/redomar/game/gfx/Colours.java index 630ff16..c345423 100644 --- a/src/com/redomar/game/gfx/Colours.java +++ b/src/com/redomar/game/gfx/Colours.java @@ -3,8 +3,7 @@ public class Colours { public static int get(int colour1, int colour2, int colour3, int colour4) { - return (get(colour4) << 24) + (get(colour3) << 16) - + (get(colour2) << 8) + (get(colour1)); + return (get(colour4) << 24) + (get(colour3) << 16) + (get(colour2) << 8) + (get(colour1)); } private static int get(int colour) { diff --git a/src/com/redomar/game/gfx/Screen.java b/src/com/redomar/game/gfx/Screen.java index 50d7614..19a501b 100644 --- a/src/com/redomar/game/gfx/Screen.java +++ b/src/com/redomar/game/gfx/Screen.java @@ -7,22 +7,19 @@ public class Screen { private static final byte BIT_MIRROR_X = 0x01; private static final byte BIT_MIRROR_Y = 0x02; - + private final SpriteSheet sheet; private int[] pixels; - private int xOffset = 0; private int yOffset = 0; - private int width; private int height; - private SpriteSheet sheet; - /** * Constructs the draw area - * @param width width of the screen + * + * @param width width of the screen * @param height height of the screen - * @param sheet Sprite-sheet selector. Constructed Spritesheet needs to be here, + * @param sheet Sprite-sheet selector. Constructed sprite sheet needs to be here, * Sprite-sheet cp requires path only. */ public Screen(int width, int height, SpriteSheet sheet) { @@ -34,23 +31,24 @@ public Screen(int width, int height, SpriteSheet sheet) { setPixels(new int[width * height]); } + @SuppressWarnings("unused") public static int getMapWidthMask() { return MAP_WIDTH_MASK; } /** - * Rendering sprites from Spritesheet onto the game world. - * Render contstucter requires - * @param xPos X Postion of subject - * @param yPos Y Postion of subject - * @param tile tile location. e.g 7 * 32 + 1 is the oblong bullet on the 7th row 2nd colomn - * @param colour Using established colouring nomanclature. i.e. use com.redomar.game.gfx.Colours - * @param mirrorDir flip Direction: 0x01 flip verticle, 0x02 flip horizontal. - * @param scale Scale + * Rendering sprites from sprite sheet onto the game world. + * Render constructor requires + * + * @param xPos X Position of subject + * @param yPos Y Position of subject + * @param tile tile location. e.g 7 * 32 + 1 is the oblong bullet on the 7th row 2nd column + * @param colour Using established colouring nomenclature. i.e. use com.redomar.game.gfx.Colours + * @param mirrorDir flip Direction: 0x01 flip vertical, 0x02 flip horizontal. + * @param scale Scale */ - public void render(int xPos, int yPos, int tile, int colour, int mirrorDir, - int scale) { + public void render(int xPos, int yPos, int tile, int colour, int mirrorDir, int scale) { xPos -= xOffset; yPos -= yOffset; @@ -80,26 +78,22 @@ public void render(int xPos, int yPos, int tile, int colour, int mirrorDir, int xPixel = x + xPos + (x * scaleMap) - ((scaleMap << 3) / 2); - int col = (colour >> (sheet.pixels[xSheet + ySheet - * sheet.getWidth() + tileOffset] * 8)) & 255; + int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.getWidth() + tileOffset] * 8)) & 255; if (col < 255) { for (int yScale = 0; yScale < scale; yScale++) { - if (yPixel + yScale < 0 - | yPixel + yScale >= getHeight()) { + if (yPixel + yScale < 0 | yPixel + yScale >= getHeight()) { continue; } for (int xScale = 0; xScale < scale; xScale++) { - if (xPixel + xScale < 0 - | xPixel + xScale >= getWidth()) { + if (xPixel + xScale < 0 | xPixel + xScale >= getWidth()) { continue; } - getPixels()[(xPixel + xScale) + (yPixel + yScale) - * getWidth()] = col; + getPixels()[(xPixel + xScale) + (yPixel + yScale) * getWidth()] = col; } } diff --git a/src/com/redomar/game/gfx/SpriteSheet.java b/src/com/redomar/game/gfx/SpriteSheet.java index f6f34c9..6d0faa4 100644 --- a/src/com/redomar/game/gfx/SpriteSheet.java +++ b/src/com/redomar/game/gfx/SpriteSheet.java @@ -1,23 +1,21 @@ package com.redomar.game.gfx; +import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; - -import javax.imageio.ImageIO; +import java.util.Objects; public class SpriteSheet { + public int[] pixels; private String path; private int width; - private int height; - - public int[] pixels; public SpriteSheet(String path) { BufferedImage image = null; try { - image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); + image = ImageIO.read(Objects.requireNonNull(SpriteSheet.class.getResourceAsStream(path))); } catch (IOException e) { e.printStackTrace(); } @@ -28,7 +26,7 @@ public SpriteSheet(String path) { this.setPath(path); this.setWidth(image.getWidth()); - this.height = image.getHeight(); + int height = image.getHeight(); pixels = image.getRGB(0, 0, width, height, null, 0, width); @@ -36,11 +34,9 @@ public SpriteSheet(String path) { pixels[i] = (pixels[i] & 0xff) / 64; // removes alpha (transparency) } - for (int i = 0; i < 8; i++) { - // System.out.println(pixels[i]); - } } + @SuppressWarnings("unused") public String getPath() { return path; } diff --git a/src/com/redomar/game/level/LevelHandler.java b/src/com/redomar/game/level/LevelHandler.java index 5b4d1c4..f25e61d 100644 --- a/src/com/redomar/game/level/LevelHandler.java +++ b/src/com/redomar/game/level/LevelHandler.java @@ -2,36 +2,31 @@ import com.redomar.game.entities.Entity; import com.redomar.game.entities.Player; -import com.redomar.game.entities.PlayerMP; import com.redomar.game.gfx.Screen; import com.redomar.game.level.tiles.Tile; import com.redomar.game.lib.utils.Vector2i; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; import com.redomar.game.scenes.Scene; -import com.redomar.game.script.PrintTypes; -import com.redomar.game.script.Printing; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.util.*; import java.util.logging.Level; public class LevelHandler { + private final List entities; + private final List entities_p; + private final Printer print; + private final Comparator nodeSorter; private byte[] tiles; private int width; private int height; - private List entities; - private List entities_p; private String imagePath; private BufferedImage image; - private Printing print; - - private Comparator nodeSorter; public LevelHandler(String imagePath) { @@ -45,40 +40,30 @@ public LevelHandler(String imagePath) { this.generateLevel(); } - print = new Printing(); - entities = new ArrayList(); - entities_p = new ArrayList(); - nodeSorter = new Comparator() { - - public int compare(Node n0, Node n1) { - if(n1.fCost < n0.fCost) return +1; - if(n1.fCost > n0.fCost) return -1; - return 0; - } - - }; + print = new Printer(); + entities = new ArrayList<>(); + entities_p = new ArrayList<>(); + nodeSorter = Comparator.comparingDouble(n0 -> n0.fCost); } private void loadLevelFromFile() { try { - this.image = ImageIO.read(LevelHandler.class.getResource(this.imagePath)); + this.image = ImageIO.read(Objects.requireNonNull(LevelHandler.class.getResource(this.imagePath))); this.setWidth(image.getWidth()); this.setHeight(image.getHeight()); tiles = new byte[getWidth() * getHeight()]; this.loadTiles(); - } catch (IOException e ){ + } catch (IOException e) { e.printStackTrace(); } } private void loadTiles() { - int[] tileColours = this.image.getRGB(0, 0, getWidth(), getHeight(), null, 0, - getWidth()); + int[] tileColours = this.image.getRGB(0, 0, getWidth(), getHeight(), null, 0, getWidth()); for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < getWidth(); x++) { for (Tile t : Tile.getTiles()) - if (t != null - && t.getLevelColour() == tileColours[x + y * getWidth()]) { + if (t != null && t.getLevelColour() == tileColours[x + y * getWidth()]) { this.tiles[x + y * getWidth()] = t.getId(); break; } @@ -89,8 +74,7 @@ private void loadTiles() { @SuppressWarnings("unused") private void saveLevelToFile() { try { - ImageIO.write(image, "png", - new File(Level.class.getResource(this.imagePath).getFile())); + ImageIO.write(image, "png", new File(Objects.requireNonNull(Level.class.getResource(this.imagePath)).getFile())); } catch (IOException e) { e.printStackTrace(); } @@ -162,12 +146,12 @@ public void renderTiles(Screen screen, int xOffset, int yOffset) { public void renderEntities(Screen screen) { for (Entity e : entities) { - e.render(screen); + e.render(screen, e.getXTile(), e.getYTile()); } } - public void renderProjectileEntities(Screen screen){ - for (Entity e : getProjectileEntities()){ + public void renderProjectileEntities(Screen screen) { + for (Entity e : getProjectileEntities()) { e.render(screen); } } @@ -181,7 +165,7 @@ public Tile getTile(int x, int y) { public void addEntity(Entity entity) { this.entities.add(entity); - print.print("Added "+entity.getName()+" Entity", PrintTypes.LEVEL); + print.print("Added " + entity.getName() + " Entity", PrintTypes.LEVEL); try { Thread.sleep(100); } catch (InterruptedException e) { @@ -196,16 +180,17 @@ public void addProjectileEntities(Entity entity) { /** * Removes the entity that is passed through + * * @param entity Specifies which entity needs to be removed */ public void removeEntity(Entity entity) { - if(entity == null) return; - print.print("Removed "+entity.getName()+" Entity", PrintTypes.LEVEL); + if (entity == null) return; + print.print("Removed " + entity.getName() + " Entity", PrintTypes.LEVEL); this.entities.remove(entity); try { Thread.sleep(100); } catch (InterruptedException e) { - print.print("Interruption error: "+ e, PrintTypes.ERROR); + print.print("Interruption error: " + e, PrintTypes.ERROR); e.printStackTrace(); } } @@ -217,8 +202,7 @@ public void removeProjectileEntities(Entity entity) { public void removeEntity(String username) { int index = 0; for (Entity e : entities) { - if (e instanceof Player - && ((Player) e).getUsername().equalsIgnoreCase(username)) { + if (e instanceof Player && e.getName().equalsIgnoreCase(username)) { break; } index++; @@ -226,39 +210,17 @@ public void removeEntity(String username) { this.entities.remove(index); } - private int getPlayerMPIndex(String username) { - int index = 0; - for (Entity e : entities) { - if (e instanceof PlayerMP - && ((PlayerMP) e).getUsername().equalsIgnoreCase(username)) { - break; - } - index++; - } - return index; - } - - public void movePlayer(String username, int x, int y, int numSteps, - boolean isMoving, int movingDir) { - int index = getPlayerMPIndex(username); - PlayerMP player = (PlayerMP) this.entities.get(index); - player.setX(x); - player.setY(y); - player.setNumSteps(numSteps); - player.setMoving(isMoving); - player.setMovingDir(movingDir); - } - public List findPath(Vector2i start, Vector2i goal){ - List openList = new ArrayList(); - List closedList = new ArrayList(); + public List findPath(Vector2i start, Vector2i goal) { + List openList = new ArrayList<>(); + List closedList = new ArrayList<>(); Node current = new Node(start, null, 0, getDistance(start, goal)); openList.add(current); - while(openList.size() > 0){ - Collections.sort(openList, nodeSorter); + while (openList.size() > 0) { + openList.sort(nodeSorter); current = openList.get(0); - if(current.tile.equals(goal)){ - List path = new ArrayList(); + if (current.tile.equals(goal)) { + List path = new ArrayList<>(); while (current.parent != null) { path.add(current); current = current.parent; @@ -269,70 +231,60 @@ public List findPath(Vector2i start, Vector2i goal){ } openList.remove(current); closedList.add(current); - for(int i = 0; i < 9; i++){ - if(i == 4) continue; + for (int i = 0; i < 9; i++) { + if (i == 4) continue; int x = current.tile.getX(); int y = current.tile.getY(); int xi = (i % 3) - 1; int yi = (i / 3) - 1; - Tile at = getTile(x + xi,y + yi); - if(at == null) continue; - if(at.isSolid()) continue; + Tile at = getTile(x + xi, y + yi); + if (at == null) continue; + if (at.isSolid()) continue; Vector2i a = new Vector2i(x + xi, y + yi); double gCost = current.gCost + (getDistance(current.tile, a) == 1 ? 1 : 0.95); double hCost = getDistance(a, goal); Node node = new Node(a, current, gCost, hCost); - if(isVectorInList(closedList, a) && gCost >= node.gCost) continue; - if(!isVectorInList(openList, a) || gCost < node.gCost) openList.add(node); + if (isVectorInList(closedList, a) && gCost >= node.gCost) continue; + if (!isVectorInList(openList, a) || gCost < node.gCost) openList.add(node); } } closedList.clear(); return null; } - private boolean isVectorInList(List list, Vector2i vector){ - for(Node n : list){ - if(n.tile.equals(vector)) return true; + private boolean isVectorInList(List list, Vector2i vector) { + for (Node n : list) { + if (n.tile.equals(vector)) return true; } return false; } - private double getDistance(Vector2i tile, Vector2i goal){ + private double getDistance(Vector2i tile, Vector2i goal) { double dx = tile.getX() - goal.getX(); double dy = tile.getY() - goal.getY(); return Math.sqrt(dx * dx + dy * dy); } - public List getEntities(Entity e, int radius){ - List result = new ArrayList(); + @Deprecated + public List getEntities(Entity e, int radius) { + List result = new ArrayList<>(); int ex = (int) e.getX(); int ey = (int) e.getY(); - for (int i = 0; i < entities.size(); i++) { - Entity entity = entities.get(i); + for (Entity entity : entities) { int x = (int) entity.getX(); int y = (int) entity.getY(); int dx = Math.abs(x - ex); int dy = Math.abs(y - ey); - double distance = Math.sqrt((dx*2) + (dy*2)); - if(distance <= radius){ + double distance = Math.sqrt((dx * 2) + (dy * 2)); + if (distance <= radius) { result.add(entity); } } return result; } - public List getPlayers(Entity e, int radius){ - List entities = getEntities(e, radius); - List result = new ArrayList(); - for (int i = 0; i < entities.size(); i++) { - if (entities.get(i) instanceof Player) { - result.add((Player) entities.get(i)); - } - } - return result; - } public int getWidth() { return width; diff --git a/src/com/redomar/game/level/Node.java b/src/com/redomar/game/level/Node.java index 1f4dc3a..02ed733 100644 --- a/src/com/redomar/game/level/Node.java +++ b/src/com/redomar/game/level/Node.java @@ -8,7 +8,7 @@ public class Node { public Node parent; public double fCost, gCost, hCost; - public Node(Vector2i tile, Node parent, double gCost, double hCost){ + public Node(Vector2i tile, Node parent, double gCost, double hCost) { this.tile = tile; this.parent = parent; this.gCost = gCost; diff --git a/src/com/redomar/game/level/tiles/AnimatedTile.java b/src/com/redomar/game/level/tiles/AnimatedTile.java index fa839f3..db3a4dd 100644 --- a/src/com/redomar/game/level/tiles/AnimatedTile.java +++ b/src/com/redomar/game/level/tiles/AnimatedTile.java @@ -2,23 +2,20 @@ public class AnimatedTile extends BasicTile { - private int[][] animationTileCoords; + private final int[][] animationTileCoords; private int currentAnimationIndex; private long lastIterationTime; - private int animationSwitchDelay; + private final int animationSwitchDelay; /** - * - * @param id Unique ID for the Tile - * @param animationCoords A 2D array of all x,y-coordinates in Integers. - * @param tileColour Colours from the SpriteSheet. - * @param levelColour Colours to be displayed in the Game World. + * @param id Unique ID for the Tile + * @param animationCoords A 2D array of all x,y-coordinates in Integers. + * @param tileColour Colours from the SpriteSheet. + * @param levelColour Colours to be displayed in the Game World. * @param animationSwitchDelay Length of time to be delayed to the next frame from the 2D array. */ - public AnimatedTile(int id, int[][] animationCoords, int tileColour, - int levelColour, int animationSwitchDelay) { - super(id, animationCoords[0][0], animationCoords[0][1], tileColour, - levelColour); + public AnimatedTile(int id, int[][] animationCoords, int tileColour, int levelColour, int animationSwitchDelay) { + super(id, animationCoords[0][0], animationCoords[0][1], tileColour, levelColour); this.animationTileCoords = animationCoords; this.currentAnimationIndex = 0; this.lastIterationTime = System.currentTimeMillis(); @@ -28,8 +25,7 @@ public AnimatedTile(int id, int[][] animationCoords, int tileColour, public void tick() { if ((System.currentTimeMillis() - lastIterationTime) >= (animationSwitchDelay)) { lastIterationTime = System.currentTimeMillis(); - currentAnimationIndex = (currentAnimationIndex + 1) - % animationTileCoords.length; + currentAnimationIndex = (currentAnimationIndex + 1) % animationTileCoords.length; this.tileId = (animationTileCoords[currentAnimationIndex][0] + (animationTileCoords[currentAnimationIndex][1] * 32)); } } diff --git a/src/com/redomar/game/level/tiles/BasicSolidTile.java b/src/com/redomar/game/level/tiles/BasicSolidTile.java index a8545fa..220e154 100644 --- a/src/com/redomar/game/level/tiles/BasicSolidTile.java +++ b/src/com/redomar/game/level/tiles/BasicSolidTile.java @@ -5,10 +5,10 @@ public class BasicSolidTile extends BasicTile { /** * This is a solid tile, but does not emit any light. This tile does not have any animations. * - * @param id Unique ID for the Tile - * @param x Specifies the x-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 - * @param y Specifies the y-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 - * @param tileColour Colours from the SpriteSheet. + * @param id Unique ID for the Tile + * @param x Specifies the x-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 + * @param y Specifies the y-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 + * @param tileColour Colours from the SpriteSheet. * @param levelColour Colours to be displayed in the Game World. */ public BasicSolidTile(int id, int x, int y, int tileColour, int levelColour) { diff --git a/src/com/redomar/game/level/tiles/BasicTile.java b/src/com/redomar/game/level/tiles/BasicTile.java index c1bb1c4..99f9e44 100644 --- a/src/com/redomar/game/level/tiles/BasicTile.java +++ b/src/com/redomar/game/level/tiles/BasicTile.java @@ -11,10 +11,10 @@ public class BasicTile extends Tile { /** * This is a tile that does not emit light, nor is sold. This tile does not have any animations. * - * @param id Unique ID for the Tile - * @param x Specifies the x-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 - * @param y Specifies the y-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 - * @param tileColour Colours from the SpriteSheet. + * @param id Unique ID for the Tile + * @param x Specifies the x-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 + * @param y Specifies the y-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31 + * @param tileColour Colours from the SpriteSheet. * @param levelColour Colours to be displayed in the Game World. */ public BasicTile(int id, int x, int y, int tileColour, int levelColour) { diff --git a/src/com/redomar/game/lib/Either.java b/src/com/redomar/game/lib/Either.java new file mode 100644 index 0000000..edf658f --- /dev/null +++ b/src/com/redomar/game/lib/Either.java @@ -0,0 +1,107 @@ +package com.redomar.game.lib; + +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; +import java.util.function.Consumer; +import java.util.function.Function; + +@SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "OptionalGetWithoutIsPresent", "unused"}) +public class Either { + + private final Optional left; + private final Optional right; + + public Either(final Optional left, final Optional right) { + this.left = left; + this.right = right; + } + + @Contract("_ -> new") + public static @NotNull Either left(final L value) { + return new Either<>(Optional.of(value), Optional.empty()); + } + + @Contract("_ -> new") + public static @NotNull Either right(final R value) { + return new Either<>(Optional.empty(), Optional.ofNullable(value)); + } + + public static Either tryCatch(CheckedRunnable runnable) { + try { + runnable.run(); + return Either.right(null); + } catch (Exception e) { + return Either.left(e); + } + } + + public static Either tryCatch(CheckedFunction function) { + try { + return Either.right(function.apply()); + } catch (Exception e) { + return Either.left(e); + } + } + + public static Either tryCatch(CheckedSupplier supplier) { + try { + return Either.right(supplier.get()); + } catch (Exception e) { + return Either.left(e); + } + } + + public boolean isLeft() { + return left.isPresent(); + } + + public L getLeft() { + return left.get(); + } + + public R getRight() { + return right.get(); + } + + public void either(Consumer leftConsumer, Consumer rightConsumer) { + if (isLeft()) { + leftConsumer.accept(getLeft()); + } else { + rightConsumer.accept(getRight()); + } + } + + public T either(Function leftFunction, Function rightFunction) { + if (isLeft()) { + return leftFunction.apply(getLeft()); + } else { + return rightFunction.apply(getRight()); + } + } + + public Either map(Function mapper) { + if (isLeft()) { + return Either.left(getLeft()); + } else { + return Either.right(mapper.apply(getRight())); + } + } + + @FunctionalInterface + public interface CheckedRunnable { + void run() throws Exception; + } + + @FunctionalInterface + public interface CheckedSupplier { + T get() throws Exception; + } + + @FunctionalInterface + public interface CheckedFunction { + T apply() throws Exception; + } + +} diff --git a/src/com/redomar/game/lib/Font.java b/src/com/redomar/game/lib/Font.java index cd659ba..b17de5d 100644 --- a/src/com/redomar/game/lib/Font.java +++ b/src/com/redomar/game/lib/Font.java @@ -6,23 +6,20 @@ public class Font { private static java.awt.Font arial; private static java.awt.Font segoe; - private static String chars = "" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ " - + "0123456789.,:;'\"!?$%()-=+/ "; + private static final String chars = "" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ " + "0123456789.,:;'\"!?$%()-=+/ "; public Font() { Font.setArial(new java.awt.Font("Arial", java.awt.Font.BOLD, 14)); Font.setSegoe(new java.awt.Font("Segoe UI", java.awt.Font.BOLD, 14)); } - public static void render(String msg, Screen screen, int x, int y, - int colour, int scale) { + public static void render(String msg, Screen screen, int x, int y, int colour, int scale) { msg = msg.toUpperCase(); for (int i = 0; i < msg.length(); i++) { int charIndex = chars.indexOf(msg.charAt(i)); if (charIndex >= 0) { - screen.render(x + (i * 8), y, charIndex + 30 * 32, colour, - 0x00, scale); + screen.render(x + (i * 8), y, charIndex + 30 * 32, colour, 0x00, scale); } } } diff --git a/src/com/redomar/game/lib/HashGen.java b/src/com/redomar/game/lib/HashGen.java index 3c36248..3044a40 100644 --- a/src/com/redomar/game/lib/HashGen.java +++ b/src/com/redomar/game/lib/HashGen.java @@ -11,18 +11,19 @@ public class HashGen { /** * Use for generating a hex Hash. Requires two parameters; + * * @param showPrefix to show 0x prefix. - * @param length Length of hash. + * @param length Length of hash. */ - public HashGen(boolean showPrefix, int length){ + public HashGen(boolean showPrefix, int length) { setPrefix(showPrefix); setHexLength(length); init(); } //Remove null char or prepend 0x - private void init(){ - if(prefix){ + private void init() { + if (prefix) { hexHash = "0x"; } else { hexHash = ""; @@ -31,29 +32,29 @@ private void init(){ /** * Retrieve hash + * * @return String containing hash */ - public String getHash(){ + public String getHash() { setHash(hexLength); return hexHash; } - private void setHash(int hexLength){ + private void setHash(int hexLength) { String hex; - for (int i = 0; i < hexLength; i++){ + for (int i = 0; i < hexLength; i++) { hex = Integer.toHexString(randNumGen()); hex = StringUtils.capitalize(hex); - hexHash = hexHash + hex; + hexHash = String.format("%s%s", hexHash, hex); } previousHash = hexHash; } - private int randNumGen(){ - int rand = (int)(Math.random() * 16); - return rand; + private int randNumGen() { + return (int) (Math.random() * 16); } //getters and setters @@ -66,8 +67,8 @@ public void setHexLength(int hexLength) { this.hexLength = hexLength; } - public String getPreviousHash(){ - if(previousHash == null) return null; + public String getPreviousHash() { + if (previousHash == null) return null; return previousHash; } } diff --git a/src/com/redomar/game/lib/Keys.java b/src/com/redomar/game/lib/Keys.java deleted file mode 100644 index 3533e40..0000000 --- a/src/com/redomar/game/lib/Keys.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.redomar.game.lib; - -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -import com.redomar.game.Game; - -public class Keys implements KeyListener { - public Keys(Game game) { - game.addKeyListener(this); - } - - public class Key { - private int numTimesPressed = 0; - private boolean pressed = false; - - public int getNumTimesPressed() { - return numTimesPressed; - } - - public boolean isPressed() { - return pressed; - } - - public void toggle(boolean isPressed) { - pressed = isPressed; - if (isPressed) { - numTimesPressed++; - } - } - } - - private Key up = new Key(); - private Key down = new Key(); - - public void keyPressed(KeyEvent arg0) { - - } - - public void keyReleased(KeyEvent arg0) { - - } - - public void keyTyped(KeyEvent arg0) { - - } - - public void toggleKey(int keyCode, boolean isPressed) { - if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP) { - up.toggle(isPressed); - } - if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) { - getDown().toggle(isPressed); - } - } - - public Key getUp() { - return up; - } - - public void setUp(Key up) { - this.up = up; - } - - public Key getDown() { - return down; - } - - public void setDown(Key down) { - this.down = down; - } - -} diff --git a/src/com/redomar/game/lib/Name.java b/src/com/redomar/game/lib/Name.java index a216932..7133819 100644 --- a/src/com/redomar/game/lib/Name.java +++ b/src/com/redomar/game/lib/Name.java @@ -2,19 +2,20 @@ import java.util.Random; +@Deprecated public class Name { - private Random rand = new Random(); private static int name_ID; public Name() { + Random rand = new Random(); setRand(rand); } + @Deprecated public String setName(String name) { - String finalName = name + name_ID; - return finalName; + return name + name_ID; } public static void setRand(Random rand) { diff --git a/src/com/redomar/game/lib/SleepThread.java b/src/com/redomar/game/lib/SleepThread.java index 7e807eb..7a9ada6 100644 --- a/src/com/redomar/game/lib/SleepThread.java +++ b/src/com/redomar/game/lib/SleepThread.java @@ -2,7 +2,7 @@ import com.redomar.game.Game; -public class SleepThread implements Runnable{ +public class SleepThread implements Runnable { public void run() { try { diff --git a/src/com/redomar/game/lib/utils/Vector2i.java b/src/com/redomar/game/lib/utils/Vector2i.java index 791e232..908301d 100644 --- a/src/com/redomar/game/lib/utils/Vector2i.java +++ b/src/com/redomar/game/lib/utils/Vector2i.java @@ -4,37 +4,31 @@ public class Vector2i { private int x, y; - public Vector2i(){ - set(0, 0); - } - public Vector2i(int x, int y){ + public Vector2i(int x, int y) { set(x, y); } - public Vector2i(Vector2i vector){ - set(vector.x, vector.y); - } - - public Vector2i add(Vector2i vector){ + public Vector2i add(Vector2i vector) { this.x += vector.x; this.y += vector.y; return this; } - public Vector2i subtract(Vector2i vector){ + @SuppressWarnings("unused") + public Vector2i subtract(Vector2i vector) { this.x -= vector.x; this.y -= vector.y; return this; } - public boolean equals(Object object){ + public boolean equals(Object object) { if (!(object instanceof Vector2i)) return false; Vector2i vector = (Vector2i) object; return vector.getX() == this.getX() && vector.getY() == this.getY(); } - public void set(int x, int y){ + public void set(int x, int y) { this.x = x; this.y = y; } diff --git a/src/com/redomar/game/script/PrintToLog.java b/src/com/redomar/game/log/PrintToLog.java similarity index 82% rename from src/com/redomar/game/script/PrintToLog.java rename to src/com/redomar/game/log/PrintToLog.java index 6927fdf..e3622e4 100644 --- a/src/com/redomar/game/script/PrintToLog.java +++ b/src/com/redomar/game/log/PrintToLog.java @@ -1,4 +1,4 @@ -package com.redomar.game.script; +package com.redomar.game.log; import java.io.File; import java.io.FileNotFoundException; @@ -8,7 +8,7 @@ public class PrintToLog { private static PrintWriter printer; - private File url; + private final File url; public PrintToLog(String url) { this.url = new File(url); @@ -25,11 +25,11 @@ private void initiate() { printer = new PrintWriter(new FileOutputStream(url, true)); } catch (FileNotFoundException e) { //throw new FileNotFoundException(); - System.err.println(e); + System.err.println(e.getMessage()); } } - public File getUrl(){ + public File getUrl() { return this.url; } } diff --git a/src/com/redomar/game/log/PrintTypes.java b/src/com/redomar/game/log/PrintTypes.java new file mode 100644 index 0000000..d0ea642 --- /dev/null +++ b/src/com/redomar/game/log/PrintTypes.java @@ -0,0 +1,5 @@ +package com.redomar.game.log; + +public enum PrintTypes { + GAME, LEVEL, ENTITY, INPUT, MUSIC, NETWORK, SERVER, ERROR, TEST +} diff --git a/src/com/redomar/game/log/Printer.java b/src/com/redomar/game/log/Printer.java new file mode 100644 index 0000000..73d2c3b --- /dev/null +++ b/src/com/redomar/game/log/Printer.java @@ -0,0 +1,114 @@ +package com.redomar.game.log; + +import com.redomar.game.lib.Time; + +import java.io.File; +import java.util.Arrays; + +public class Printer { + + private static int lineNumber = 0; + private final Time time = new Time(); + private PrintTypes type; + private String message; + private boolean evalTypeAsException = false; + private boolean mute = false; + + public Printer() { + this.type = PrintTypes.GAME; + } + + public Printer(PrintTypes type) { + this.type = type; + } + + public void print(String message, PrintTypes type) { + this.type = type; + this.message = message; + printOut(); + } + + public void print(String message) { + this.message = message; + printOut(); + } + + private void printOut() { + String msgTime = "[" + time.getTime() + "]"; + String msgType = "[" + type.toString() + "]"; + + PrintToLog logFile = printToLogType(type); + if (lineNumber == 0) { + + String dashes = ""; + String title = ("[" + time.getTimeDate() + "]"); + char dash = '-'; + int number = Math.max((title.length() / 3), 10); + + char[] repeat = new char[number]; + Arrays.fill(repeat, dash); + dashes += new String(repeat); + + logFile.log(String.format("%s%s%s", dashes, title, dashes)); + lineNumber++; + } + logFile.log(String.format("%s%s\t%s", msgTime, msgType, message)); + + if (mute) return; + String formattedStringForConsole = String.format("%s%s %s%n", msgType, msgTime, message); + + if (type.equals(PrintTypes.ERROR) || evalTypeAsException) { + System.err.printf(formattedStringForConsole); + evalTypeAsException = false; + } else { + System.out.printf(formattedStringForConsole); + } + } + + private PrintToLog printToLogType(PrintTypes type) { + if (type == PrintTypes.TEST) { + return new PrintToLog(".PrintType-TEST.txt"); + } else { + return new PrintToLog(".log.txt"); + } + } + + public boolean removeLog() { + return new File(".log.txt").delete(); + } + + public String getMessage() { + return message; + } + + public void mute() { + this.mute = true; + } + + @SuppressWarnings("unused") + public void unmute() { + this.mute = false; + } + + /** + * Error that keeps the current {@link PrintTypes PrintType } instead of using {@link PrintTypes PrintTypes.ERROR} + * + * @return Printer + */ + public Printer cast() { + this.evalTypeAsException = true; + return this; + } + + /** + * Print exception as message, cast to keep {@link PrintTypes PrintType } + * + * @param exceptionMessage message to print + * @return Printer + */ + public Printer exception(String exceptionMessage) { + if (!this.evalTypeAsException) this.type = PrintTypes.ERROR; + print(exceptionMessage); + return this; + } +} diff --git a/src/com/redomar/game/HelpMenu.java b/src/com/redomar/game/menu/HelpMenu.java similarity index 56% rename from src/com/redomar/game/HelpMenu.java rename to src/com/redomar/game/menu/HelpMenu.java index da9709a..0979621 100644 --- a/src/com/redomar/game/HelpMenu.java +++ b/src/com/redomar/game/menu/HelpMenu.java @@ -1,23 +1,29 @@ -package com.redomar.game; +package com.redomar.game.menu; +import javax.imageio.ImageIO; +import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; -import javax.imageio.ImageIO; -import javax.swing.*; +import java.net.URL; +import java.util.concurrent.atomic.AtomicReference; /** * Credit to Gagandeep Bali @ stackoverflow */ public class HelpMenu { - private MyPanel contentPane; + private final JFrame frame = new JFrame("Help Menu"); + + private static void run() { + Runnable runnable = () -> new HelpMenu().displayGUI(); + EventQueue.invokeLater(runnable); + } private void displayGUI() { - JFrame frame = new JFrame("Help Menu"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - contentPane = new MyPanel(); + MyPanel contentPane = new MyPanel(); frame.setContentPane(contentPane); frame.setLocationRelativeTo(null); @@ -26,13 +32,23 @@ private void displayGUI() { frame.setVisible(true); } - private class MyPanel extends JPanel { + public void helpMenuLaunch() { + run(); + } + + public void helpMenuClose() { + frame.setVisible(false); + frame.dispose(); + } + + private static class MyPanel extends JPanel { private BufferedImage image; private MyPanel() { try { - image = ImageIO.read(MyPanel.class.getResource("/controls/controls.png")); + AtomicReference controlsImageResource = new AtomicReference<>(MyPanel.class.getResource("/controls/controls.png")); + image = ImageIO.read(controlsImageResource.get()); } catch (IOException ioe) { ioe.printStackTrace(); } @@ -40,7 +56,7 @@ private MyPanel() { @Override public Dimension getPreferredSize() { - return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight()); + return image == null ? new Dimension(400, 300) : new Dimension(image.getWidth(), image.getHeight()); } @Override @@ -49,9 +65,4 @@ protected void paintComponent(Graphics g) { g.drawImage(image, 0, 0, this); } } - - public static void run() { - Runnable runnable = () -> new HelpMenu().displayGUI(); - EventQueue.invokeLater(runnable); - } } diff --git a/src/com/redomar/game/menu/Menu.java b/src/com/redomar/game/menu/Menu.java index 6726d68..c78044e 100644 --- a/src/com/redomar/game/menu/Menu.java +++ b/src/com/redomar/game/menu/Menu.java @@ -2,10 +2,10 @@ import com.redomar.game.Game; import com.redomar.game.audio.AudioHandler; +import com.redomar.game.event.Mouse; import com.redomar.game.lib.Font; -import com.redomar.game.lib.Mouse; +import com.redomar.game.log.Printer; import com.thehowtotutorial.splashscreen.JSplash; -import org.apache.commons.text.WordUtils; import javax.swing.*; import java.awt.*; @@ -13,96 +13,59 @@ import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferStrategy; +import java.net.URL; +import java.util.concurrent.atomic.AtomicReference; public class Menu implements Runnable { + private static final int VOLUME_IN_DB = -20; + private static final String NAME = "Menu"; private static final int WIDTH = 160; private static final int HEIGHT = (WIDTH / 3 * 2); private static final int SCALE = 3; - private static final String NAME = "Menu"; - private static boolean running = false; private static boolean selectedStart = true; private static boolean selectedExit = false; - private static boolean gameOver = false; + private static DedicatedJFrame frame; - private static DedicatedJFrame frame;// = new DedicatedJFrame(WIDTH, HEIGHT, - private static final JDialog dialog = new JDialog(); - // SCALE, NAME); - private Font font = new Font(); - private MouseListener Mouse = new Mouse(); - private KeyListener Key = new MenuInput(); + private final Font font = new Font(); + private final MouseListener menuMouseListener = new Mouse(); + private final KeyListener menuKeyListener = new MenuInput(); - private Color selected = new Color(0xFFFF8800); - private Color deSelected = new Color(0xFFCC5500); - - public static synchronized void stop() { - running = false; - } + private final Color SELECTED_COLOUR = new Color(0xFFFF8800); + private final Color UNSELECTED_COLOUR = new Color(0xFFCC5500); public static void play() { try { - JSplash splash = new JSplash( - Game.class.getResource("/splash/splash.png"), true, true, - false, Game.getGameVersion(), null, Color.RED, Color.ORANGE); + // Splash screen + AtomicReference splashImageResource = new AtomicReference<>(Game.class.getResource("/splash/splash.png")); + JSplash splash = new JSplash(splashImageResource.get(), true, true, false, Game.getGameVersion(), null, Color.RED, Color.ORANGE); splash.toFront(); splash.requestFocus(); splash.splashOn(); - splash.setProgress(20, "Loading Music"); - Game.setBackgroundMusic(new AudioHandler("/music/Towards The End.wav")); - splash.setProgress(50, "Setting Volume"); - Game.getBackgroundMusic().setVolume(-20); - splash.setProgress(60, "Acquiring data: Multiplayer"); - Thread.sleep(125); - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - String multiMsg = "Sorry but multiplayer has been disabled on this version.\nIf you would like multiplayer checkout Alpha 1.6"; - dialog.setAlwaysOnTop(true); - JOptionPane.showMessageDialog(dialog, multiMsg, - "Multiplayer Warning", JOptionPane.WARNING_MESSAGE); - // Game.setJdata_Host(JOptionPane.showConfirmDialog(Game.getGame(), - // "Do you want to be the HOST?")); - Game.setJdata_Host(1); - if (Game.getJdata_Host() != 1) { // Game.getJdata_Host() == 1 - Game.setJdata_IP(JOptionPane.showInputDialog(dialog, - "Enter the name \nleave blank for local")); - } - Thread.sleep(125); - splash.setProgress(70, "Acquiring data: Username"); - String s = JOptionPane.showInputDialog(dialog, - "Enter a name"); - if (s != null) { - Game.setJdata_UserName(s); - } - Thread.sleep(125); - splash.setProgress(90, "Collecting Player Data"); - Object[] options = {"African", "Caucasian"}; - int n = JOptionPane.showOptionDialog(dialog, - "Choose a race for the character to be", "Choose a race", - JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, - null, options, options[0]); - if (n == 0) { - Game.setAlternateColsR(true); - } else { - Game.setAlternateColsR(false); - } - Thread.sleep(250); - Object[] options1 = {"Orange", "Black"}; - int n1 = JOptionPane.showOptionDialog(dialog, - "Which Colour do you want the shirt to be?", - "Choose a shirt Colour", JOptionPane.YES_NO_OPTION, - JOptionPane.QUESTION_MESSAGE, null, options1, options1[0]); - if (n1 == 0) { - Game.setAlternateColsS(true); - } else { - Game.setAlternateColsS(false); + + // Background tasks + try { + Game.setBackgroundMusic(new AudioHandler("/music/Towards The End.wav", true)); + Game.getBackgroundMusic().setVolume(VOLUME_IN_DB); + } catch (Exception e) { + Printer printer = new Printer(); + printer.exception(e.getMessage()); } - splash.setProgress(100, "Connecting as" + Game.getJdata_UserName()); - Thread.sleep(250); - splash.splashOff(); + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + Game.setAlternateColsR(true); + Game.setAlternateColsS(true); + splash.setProgress(100, "Starting Game..."); + Thread.sleep(650); + + // Frame Init frame = new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME); - frame.getFrame(); frame.getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.requestFocus(); + + // Hide splash + splash.splashOff(); + splash.removeAll(); } catch (Exception e) { e.printStackTrace(); } @@ -112,10 +75,6 @@ public static DedicatedJFrame getFrame() { return Menu.frame; } - public static void setFrame(DedicatedJFrame frame) { - Menu.frame = frame; - } - public static boolean isRunning() { return running; } @@ -140,20 +99,8 @@ public static void setSelectedExit(boolean selectedExit) { Menu.selectedExit = selectedExit; } - public static int getWidth() { - return WIDTH; - } - - public static int getHeight() { - return HEIGHT; - } - - public static boolean isGameOver() { - return gameOver; - } - - public static void setGameOver(boolean gameOver) { - Menu.gameOver = gameOver; + public static synchronized void stop() { + running = false; } public synchronized void start() { @@ -184,12 +131,6 @@ public void run() { shouldRender = true; } - try { - Thread.sleep(2); - } catch (InterruptedException e) { - e.printStackTrace(); - } - if (shouldRender) { frames++; render(); @@ -197,8 +138,7 @@ public void run() { if (System.currentTimeMillis() - lastTimer >= 1000) { lastTimer += 1000; - frame.getFrame().setTitle( - "Frames: " + frames + " Ticks: " + ticks); + frame.getFrame().setTitle("Frames: " + frames + " Ticks: " + ticks); frames = 0; ticks = 0; } @@ -207,9 +147,9 @@ public void run() { private void render() { // frame.getFrame().getContentPane().setBackground(Color.GREEN); - frame.addMouseMotionListener((MouseMotionListener) Mouse); - frame.addMouseListener(Mouse); - frame.addKeyListener(Key); + frame.addMouseMotionListener((MouseMotionListener) menuMouseListener); + frame.addMouseListener(menuMouseListener); + frame.addKeyListener(menuKeyListener); BufferStrategy bs = frame.getBufferStrategy(); if (bs == null) { frame.createBufferStrategy(3); @@ -222,14 +162,7 @@ private void render() { g.fillRect(0, 0, WIDTH * 3, HEIGHT * 3); g.setColor(new Color(0xFFFF9900)); g.setFont(font.getArial()); - if (isGameOver()) { - g.drawString("GAME OVER... What will you do now?", 35, 30); - } else { - String name = (Game.getJdata_UserName().length() >= 1) ? WordUtils - .capitalizeFully(Game.getJdata_UserName()).toString() - : "Player"; - g.drawString("Welcome to JavaGame " + name, 35, 30); - } + g.drawString("Welcome to JavaGame", 35, 30); g.drawLine(0, HEIGHT * 3, 0, 0); g.drawLine(0, 0, (WIDTH * 3), 0); g.drawLine((WIDTH * 3), 0, (WIDTH * 3), (HEIGHT * 3)); @@ -246,11 +179,11 @@ private void paintButtons(boolean start, boolean exit, Graphics g) { if (!start) { g.setColor(new Color(0xFFBB4400)); g.fillRect(35, 40, (frame.getWidth() - 67), 113); - g.setColor(getDeSelected()); + g.setColor(UNSELECTED_COLOUR); } else { g.setColor(new Color(0xFFDD6600)); g.fillRect(35, 40, (frame.getWidth() - 67), 113); - g.setColor(getSelected()); + g.setColor(SELECTED_COLOUR); } g.fillRect(35, 40, (frame.getWidth() - 70), 110); g.setColor(Color.BLACK); @@ -259,31 +192,15 @@ private void paintButtons(boolean start, boolean exit, Graphics g) { if (!exit) { g.setColor(new Color(0xFFBB4400)); g.fillRect(35, 170, (frame.getWidth() - 67), 113); - g.setColor(getDeSelected()); + g.setColor(UNSELECTED_COLOUR); } else { g.setColor(new Color(0xFFDD6600)); g.fillRect(35, 170, (frame.getWidth() - 67), 113); - g.setColor(getSelected()); + g.setColor(SELECTED_COLOUR); } g.fillRect(35, 170, (frame.getWidth() - 70), 110); g.setColor(Color.BLACK); g.drawString("Exit", 220, 220); } - public Color getSelected() { - return selected; - } - - public void setSelected(Color selected) { - this.selected = selected; - } - - public Color getDeSelected() { - return deSelected; - } - - public void setDeSelected(Color deSelected) { - this.deSelected = deSelected; - } - } diff --git a/src/com/redomar/game/menu/MenuInput.java b/src/com/redomar/game/menu/MenuInput.java index 59642ff..ea4f60b 100644 --- a/src/com/redomar/game/menu/MenuInput.java +++ b/src/com/redomar/game/menu/MenuInput.java @@ -1,13 +1,13 @@ package com.redomar.game.menu; import com.redomar.game.Game; -import com.redomar.game.HelpMenu; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class MenuInput implements KeyListener { + private static final HelpMenu HELP_MENU = new HelpMenu(); private boolean ticket = false; private boolean help = false; @@ -35,14 +35,15 @@ private void toggleKey(int keyCode) { Menu.setSelectedStart(false); } - if(!ticket){ + if (!ticket) { if (keyCode == KeyEvent.VK_ENTER || keyCode == KeyEvent.VK_SPACE) { if (Menu.isSelectedStart()) { this.ticket = true; + new Game().start(); Menu.setRunning(false); Menu.getFrame().setVisible(false); Menu.getFrame().stopFrame(); - new Game().start(); + Menu.stop(); } if (Menu.isSelectedExit()) { @@ -50,18 +51,19 @@ private void toggleKey(int keyCode) { Menu.setRunning(false); Menu.getFrame().setVisible(false); Menu.getFrame().stopFrame(); + Menu.stop(); } } } if (keyCode == KeyEvent.VK_ESCAPE) { - System.exit(1); + HELP_MENU.helpMenuClose(); + help = false; + System.exit(0); } - if (keyCode == KeyEvent.VK_H){ - HelpMenu h = new HelpMenu(); - if (!help) - h.run(); + if (keyCode == KeyEvent.VK_H) { + if (!help) HELP_MENU.helpMenuLaunch(); help = true; } } diff --git a/src/com/redomar/game/menu/PopUp.java b/src/com/redomar/game/menu/PopUp.java new file mode 100644 index 0000000..3f35171 --- /dev/null +++ b/src/com/redomar/game/menu/PopUp.java @@ -0,0 +1,22 @@ +package com.redomar.game.menu; + +import com.redomar.game.Game; + +import javax.swing.*; + +public class PopUp { + + public boolean active; + + public PopUp() { + active = true; + } + + public int Warn(String msg) { + Object[] options = {"Continue"}; + if (active) { + JFrame frame = Game.getFrame(); + return JOptionPane.showOptionDialog(frame, msg, "Notice", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + } else return 1; + } +} diff --git a/src/com/redomar/game/net/GameClient.java b/src/com/redomar/game/net/GameClient.java index 05614fd..6fb0f2f 100644 --- a/src/com/redomar/game/net/GameClient.java +++ b/src/com/redomar/game/net/GameClient.java @@ -7,50 +7,34 @@ import com.redomar.game.net.packets.Packet00Login; import com.redomar.game.net.packets.Packet01Disconnect; import com.redomar.game.net.packets.Packet02Move; -import com.redomar.game.script.PrintTypes; -import com.redomar.game.script.Printing; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; import java.io.IOException; import java.net.*; +@Deprecated public class GameClient extends Thread { + private final Printer print = new Printer(); private InetAddress ipAddress; private DatagramSocket socket; private Game game; - private Printing print = new Printing(); public GameClient(Game game, String ipAddress) { this.setGame(game); try { this.socket = new DatagramSocket(); this.ipAddress = InetAddress.getByName(ipAddress); - } catch (SocketException e) { + } catch (SocketException | UnknownHostException e) { e.printStackTrace(); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - } - - public void run() { - while (true) { - byte[] data = new byte[1024]; - DatagramPacket packet = new DatagramPacket(data, data.length); - try { - socket.receive(packet); - } catch (IOException e) { - e.printStackTrace(); - } - this.parsePacket(packet.getData(), packet.getAddress(), - packet.getPort()); - // System.out.println("SERVER > "+new String(packet.getData())); } } private void parsePacket(byte[] data, InetAddress address, int port) { String message = new String(data).trim(); PacketTypes type = Packet.lookupPacket(message.substring(0, 2)); - Packet packet = null; + Packet packet; switch (type) { default: case INVALID: @@ -61,36 +45,23 @@ private void parsePacket(byte[] data, InetAddress address, int port) { break; case DISCONNECT: packet = new Packet01Disconnect(data); - print.print("[" + address.getHostAddress() + ":" + port - + "] " + ((Packet01Disconnect) packet).getUsername() - + " has disconnected...", PrintTypes.NETWORK); - Game.getLevel().removeEntity( - ((Packet01Disconnect) packet).getUsername()); + print.print("[" + address.getHostAddress() + ":" + port + "] " + ((Packet01Disconnect) packet).getUsername() + " has disconnected...", PrintTypes.NETWORK); + Game.getLevel().removeEntity(((Packet01Disconnect) packet).getUsername()); break; case MOVE: packet = new Packet02Move(data); - this.handleMove((Packet02Move) packet); break; } } private void handleLogin(Packet00Login packet, InetAddress address, int port) { - print.print("[" + address.getHostAddress() + ":" + port + "] " - + packet.getUsername() + " has joined...", PrintTypes.NETWORK); - PlayerMP player = new PlayerMP(Game.getLevel(), packet.getX(), - packet.getY(), packet.getUsername(), address, port, Game.getShirtCol(), Game.getFaceCol()); + print.print("[" + address.getHostAddress() + ":" + port + "] " + packet.getUsername() + " has joined...", PrintTypes.NETWORK); + PlayerMP player = new PlayerMP(Game.getLevel(), packet.getX(), packet.getY(), packet.getUsername(), address, port, Game.getShirtCol(), Game.getFaceCol()); Game.getLevel().addEntity(player); } - private void handleMove(Packet02Move packet) { - Game.getLevel().movePlayer(packet.getUsername(), packet.getX(), - packet.getY(), packet.getNumSteps(), packet.isMoving(), - packet.getMovingDir()); - } - public void sendData(byte[] data) { - DatagramPacket packet = new DatagramPacket(data, data.length, - ipAddress, 1331); + DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 1331); try { socket.send(packet); } catch (IOException e) { diff --git a/src/com/redomar/game/net/GameServer.java b/src/com/redomar/game/net/GameServer.java index 9d27a3f..bd96e00 100644 --- a/src/com/redomar/game/net/GameServer.java +++ b/src/com/redomar/game/net/GameServer.java @@ -2,13 +2,13 @@ import com.redomar.game.Game; import com.redomar.game.entities.PlayerMP; +import com.redomar.game.log.PrintTypes; +import com.redomar.game.log.Printer; import com.redomar.game.net.packets.Packet; import com.redomar.game.net.packets.Packet.PacketTypes; import com.redomar.game.net.packets.Packet00Login; import com.redomar.game.net.packets.Packet01Disconnect; import com.redomar.game.net.packets.Packet02Move; -import com.redomar.game.script.PrintTypes; -import com.redomar.game.script.Printing; import java.io.IOException; import java.net.DatagramPacket; @@ -18,12 +18,13 @@ import java.util.ArrayList; import java.util.List; +@Deprecated public class GameServer extends Thread { + private final List connectedPlayers = new ArrayList<>(); + private final Printer print = new Printer(); private DatagramSocket socket; private Game game; - private List connectedPlayers = new ArrayList(); - private Printing print = new Printing(); public GameServer(Game game) { this.setGame(game); @@ -42,42 +43,36 @@ public void run() { socket.receive(packet); } catch (IOException e) { e.printStackTrace(); + break; } - this.parsePacket(packet.getData(), packet.getAddress(), - packet.getPort()); + this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort()); - // String message = new String(packet.getData()); - // System.out.println("CLIENT ["+packet.getAddress().getHostAddress()+":"+packet.getPort()+"] "+message); - // if(message.trim().equalsIgnoreCase("ping")){ - // sendData("pong".getBytes(), packet.getAddress(), - // packet.getPort()); - // } + String message = new String(packet.getData()); + System.out.println("CLIENT [" + packet.getAddress().getHostAddress() + ":" + packet.getPort() + "] " + message); + if (message.trim().equalsIgnoreCase("ping")) { + sendData("pong".getBytes(), packet.getAddress(), packet.getPort()); + } } } private void parsePacket(byte[] data, InetAddress address, int port) { String message = new String(data).trim(); PacketTypes type = Packet.lookupPacket(message.substring(0, 2)); - Packet packet = null; + Packet packet; switch (type) { default: case INVALID: break; case LOGIN: packet = new Packet00Login(data); - print.print("[" + address.getHostAddress() + ":" + port - + "] " + ((Packet00Login) packet).getUsername() - + " has connected...", PrintTypes.SERVER); - PlayerMP player = new PlayerMP(Game.getLevel(), 10, 10, - ((Packet00Login) packet).getUsername(), address, port, Game.getShirtCol(), Game.getFaceCol()); + print.print("[" + address.getHostAddress() + ":" + port + "] " + ((Packet00Login) packet).getUsername() + " has connected...", PrintTypes.SERVER); + PlayerMP player = new PlayerMP(Game.getLevel(), 10, 10, ((Packet00Login) packet).getUsername(), address, port, Game.getShirtCol(), Game.getFaceCol()); this.addConnection(player, (Packet00Login) packet); break; case DISCONNECT: packet = new Packet01Disconnect(data); - print.print("[" + address.getHostAddress() + ":" + port - + "] " + ((Packet01Disconnect) packet).getUsername() - + " has disconnected...", PrintTypes.SERVER); + print.print("[" + address.getHostAddress() + ":" + port + "] " + ((Packet01Disconnect) packet).getUsername() + " has disconnected...", PrintTypes.SERVER); this.removeConnection((Packet01Disconnect) packet); break; case MOVE: @@ -103,8 +98,8 @@ public void addConnection(PlayerMP player, Packet00Login packet) { boolean alreadyConnected = false; for (PlayerMP p : this.connectedPlayers) { if (player.getUsername().equalsIgnoreCase(p.getUsername())) { - if (p.ipAddess == null) { - p.ipAddess = player.ipAddess; + if (p.ipAddress == null) { + p.ipAddress = player.ipAddress; } if (p.port == -1) { @@ -113,10 +108,10 @@ public void addConnection(PlayerMP player, Packet00Login packet) { alreadyConnected = true; } else { - sendData(packet.getData(), p.ipAddess, p.port); + sendData(packet.getData(), p.ipAddress, p.port); packet = new Packet00Login(p.getUsername(), (int) p.getX(), (int) p.getY()); - sendData(packet.getData(), player.ipAddess, player.port); + sendData(packet.getData(), player.ipAddress, player.port); } } if (!alreadyConnected) { @@ -151,8 +146,7 @@ public int getPlayerMPIndex(String username) { } public void sendData(byte[] data, InetAddress ipAddress, int port) { - DatagramPacket packet = new DatagramPacket(data, data.length, - ipAddress, port); + DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, port); try { this.socket.send(packet); } catch (IOException e) { @@ -162,7 +156,7 @@ public void sendData(byte[] data, InetAddress ipAddress, int port) { public void sendDataToAllClients(byte[] data) { for (PlayerMP p : connectedPlayers) { - sendData(data, p.ipAddess, p.port); + sendData(data, p.ipAddress, p.port); } } diff --git a/src/com/redomar/game/net/packets/Packet.java b/src/com/redomar/game/net/packets/Packet.java index 743784f..938c085 100644 --- a/src/com/redomar/game/net/packets/Packet.java +++ b/src/com/redomar/game/net/packets/Packet.java @@ -3,39 +3,15 @@ import com.redomar.game.net.GameClient; import com.redomar.game.net.GameServer; +@Deprecated public abstract class Packet { - public static enum PacketTypes { - INVALID(-1), LOGIN(00), DISCONNECT(01), MOVE(02); - - private int packetId; - - private PacketTypes(int packetId) { - this.packetId = packetId; - } - - public int getId() { - return packetId; - } - } - public byte packetId; public Packet(int packetId) { this.packetId = (byte) packetId; } - public abstract byte[] getData(); - - public abstract void writeData(GameClient client); - - public abstract void writeData(GameServer server); - - public String readData(byte[] data) { - String message = new String(data).trim(); - return message.substring(2); - } - public static PacketTypes lookupPacket(String packetId) { try { return lookupPacket(Integer.parseInt(packetId)); @@ -52,4 +28,29 @@ public static PacketTypes lookupPacket(int id) { } return PacketTypes.INVALID; } + + public abstract byte[] getData(); + + public abstract void writeData(GameClient client); + + public abstract void writeData(GameServer server); + + public String readData(byte[] data) { + String message = new String(data).trim(); + return message.substring(2); + } + + public enum PacketTypes { + INVALID(-1), LOGIN(0x00), DISCONNECT(0x01), MOVE(0x02); + + private final int packetId; + + PacketTypes(int packetId) { + this.packetId = packetId; + } + + public int getId() { + return packetId; + } + } } diff --git a/src/com/redomar/game/net/packets/Packet00Login.java b/src/com/redomar/game/net/packets/Packet00Login.java index 4e61dc4..6382b83 100644 --- a/src/com/redomar/game/net/packets/Packet00Login.java +++ b/src/com/redomar/game/net/packets/Packet00Login.java @@ -3,13 +3,15 @@ import com.redomar.game.net.GameClient; import com.redomar.game.net.GameServer; +@Deprecated public class Packet00Login extends Packet { - private String username; - private int x, y; + private final String username; + private final int x; + private final int y; public Packet00Login(byte[] data) { - super(00); + super(0x00); String[] dataArray = readData(data).split(","); this.username = dataArray[0]; this.x = Integer.parseInt(dataArray[1]); @@ -17,7 +19,7 @@ public Packet00Login(byte[] data) { } public Packet00Login(String username, int x, int y) { - super(00); + super(0x00); this.username = username; this.x = x; this.y = y; diff --git a/src/com/redomar/game/net/packets/Packet01Disconnect.java b/src/com/redomar/game/net/packets/Packet01Disconnect.java index d95eec5..4ac5e20 100644 --- a/src/com/redomar/game/net/packets/Packet01Disconnect.java +++ b/src/com/redomar/game/net/packets/Packet01Disconnect.java @@ -3,17 +3,18 @@ import com.redomar.game.net.GameClient; import com.redomar.game.net.GameServer; +@Deprecated public class Packet01Disconnect extends Packet { - private String username; + private final String username; public Packet01Disconnect(byte[] data) { - super(01); + super(0x01); this.username = readData(data); } public Packet01Disconnect(String username) { - super(01); + super(0x01); this.username = username; } diff --git a/src/com/redomar/game/net/packets/Packet02Move.java b/src/com/redomar/game/net/packets/Packet02Move.java index 8f1e47d..1336ee0 100644 --- a/src/com/redomar/game/net/packets/Packet02Move.java +++ b/src/com/redomar/game/net/packets/Packet02Move.java @@ -3,16 +3,18 @@ import com.redomar.game.net.GameClient; import com.redomar.game.net.GameServer; +@Deprecated public class Packet02Move extends Packet { - private String username; - private int x, y; - private int numSteps = 0; - private boolean isMoving; - private int movingDir = 1; + private final String username; + private final int x; + private final int y; + private final int numSteps; + private final boolean isMoving; + private final int movingDir; public Packet02Move(byte[] data) { - super(02); + super(0x02); String[] dataArray = readData(data).split(","); this.username = dataArray[0]; this.x = Integer.parseInt(dataArray[1]); @@ -22,9 +24,8 @@ public Packet02Move(byte[] data) { this.movingDir = Integer.parseInt(dataArray[5]); } - public Packet02Move(String username, int x, int y, int numSteps, - boolean isMoving, int movingDir) { - super(02); + public Packet02Move(String username, int x, int y, int numSteps, boolean isMoving, int movingDir) { + super(0x02); this.username = username; this.x = x; this.y = y; @@ -35,9 +36,7 @@ public Packet02Move(String username, int x, int y, int numSteps, @Override public byte[] getData() { - return ("02" + this.username + "," + this.x + "," + this.y + "," - + this.getNumSteps() + "," + (this.isMoving ? 1 : 0) + "," + this - .getMovingDir()).getBytes(); + return ("02" + this.username + "," + this.x + "," + this.y + "," + this.getNumSteps() + "," + (this.isMoving ? 1 : 0) + "," + this.getMovingDir()).getBytes(); } @Override diff --git a/src/com/redomar/game/objects/Inventory.java b/src/com/redomar/game/objects/Inventory.java index ad181a5..96a24e1 100644 --- a/src/com/redomar/game/objects/Inventory.java +++ b/src/com/redomar/game/objects/Inventory.java @@ -10,7 +10,7 @@ public class Inventory { public static boolean closing; public static boolean reset; public static boolean enabled; - private static InventoryWindow inv_window = new InventoryWindow(); + private static final InventoryWindow inv_window = new InventoryWindow(); public static void activate() { x = (int) Game.getPlayer().getX(); @@ -20,17 +20,14 @@ public static void activate() { if (enabled) { if (!open) { if (!closing) { - System.out.println("Opened\nInside this Bag their is:" - + inside()); + System.out.println("Opened\nInside this Bag their is:" + inside()); open = true; Game.getPlayer().setMoving(false); - Game.getInput().untoggle(true); inv_window.start(); } } else { if (closing) { Game.getPlayer().setMoving(true); - Game.getInput().untoggle(false); inv_window.stop(); inv_window.getFrame().setVisible(false); inv_window.getFrame().stopFrame(); @@ -42,7 +39,7 @@ public static void activate() { } } } else { - if (open == true || reset == true || closing == true) { + if (open || reset || closing) { reset = false; open = false; closing = false; @@ -53,7 +50,7 @@ public static void activate() { private static String inside() { String items = " "; for (Items item : Items.values()) { - items = items + item.toString() + ", "; + items = String.format("%s%s, ", items, item.toString()); } return items; } diff --git a/src/com/redomar/game/objects/InventoryHandler.java b/src/com/redomar/game/objects/InventoryHandler.java index 93b6128..e7b1cdd 100644 --- a/src/com/redomar/game/objects/InventoryHandler.java +++ b/src/com/redomar/game/objects/InventoryHandler.java @@ -7,11 +7,7 @@ public class InventoryHandler implements WindowListener { - @SuppressWarnings("unused") - private DedicatedJFrame frame; - public InventoryHandler(DedicatedJFrame frame) { - this.frame = frame; DedicatedJFrame.getFrameStatic().addWindowListener(this); } diff --git a/src/com/redomar/game/objects/InventoryWindow.java b/src/com/redomar/game/objects/InventoryWindow.java index 30afd00..94d7d45 100644 --- a/src/com/redomar/game/objects/InventoryWindow.java +++ b/src/com/redomar/game/objects/InventoryWindow.java @@ -5,7 +5,7 @@ import java.awt.*; import java.awt.image.BufferStrategy; -public class InventoryWindow implements Runnable{ +public class InventoryWindow implements Runnable { private static final int WIDTH = 160; private static final int HEIGHT = (WIDTH / 3 * 2); @@ -25,13 +25,13 @@ public static void setWindow(InventoryHandler inventoryHandler) { InventoryWindow.window = inventoryHandler; } - public synchronized void start(){ + public synchronized void start() { running = true; setFrame(new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME)); new Thread(this, NAME).start(); } - public synchronized void stop(){ + public synchronized void stop() { running = false; } @@ -72,8 +72,7 @@ public void run() { if (System.currentTimeMillis() - lastTimer >= 1000) { lastTimer += 1000; - frame.getFrame().setTitle( - "Frames: " + frames + " Ticks: " + ticks); + frame.getFrame().setTitle("Frames: " + frames + " Ticks: " + ticks); frames = 0; ticks = 0; } @@ -82,14 +81,14 @@ public void run() { private void render() { BufferStrategy bs = frame.getBufferStrategy(); - if(bs == null){ + if (bs == null) { frame.createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.BLACK); - g.fillRect(0, 0, WIDTH*SCALE+10, HEIGHT*SCALE+10); + g.fillRect(0, 0, WIDTH * SCALE + 10, HEIGHT * SCALE + 10); g.setColor(Color.WHITE); g.drawString(NAME, 50, 50); bs.show(); diff --git a/src/com/redomar/game/objects/Items.java b/src/com/redomar/game/objects/Items.java index 2bb4ffc..55f705a 100644 --- a/src/com/redomar/game/objects/Items.java +++ b/src/com/redomar/game/objects/Items.java @@ -1,8 +1,5 @@ package com.redomar.game.objects; public enum Items { - Stick, - Torch, - Sword, - Apple + Stick, Torch, Sword, Apple } diff --git a/src/com/redomar/game/scenes/Scene.java b/src/com/redomar/game/scenes/Scene.java index 0ca0f6d..2b869c6 100644 --- a/src/com/redomar/game/scenes/Scene.java +++ b/src/com/redomar/game/scenes/Scene.java @@ -6,17 +6,17 @@ public class Scene { private int xOffset, yOffset; - private Screen screen; - private LevelHandler level; + private final Screen screen; + private final LevelHandler level; - public Scene(int xOffset, int yOffset, Screen screen, LevelHandler level){ + public Scene(int xOffset, int yOffset, Screen screen, LevelHandler level) { this.xOffset = xOffset; this.yOffset = yOffset; this.screen = screen; this.level = level; } - public void playerScene(){ + public void playerScene() { if (xOffset < 0) { xOffset = 0; } diff --git a/src/com/redomar/game/script/PopUp.java b/src/com/redomar/game/script/PopUp.java deleted file mode 100644 index 2f69fa2..0000000 --- a/src/com/redomar/game/script/PopUp.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.redomar.game.script; - -import com.redomar.game.Game; - -import javax.swing.*; - -public class PopUp{ - - private JFrame frame; - public boolean active; - - public PopUp(){ - active = true; - } - - public int Warn(String msg){ - Object[] options = {"Continue"}; - if (active) { - frame = Game.getFrame(); - return JOptionPane.showOptionDialog(frame, msg, "Notice", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, - null, options, options[0]); - } - else - return 1; - } -} diff --git a/src/com/redomar/game/script/PrintTypes.java b/src/com/redomar/game/script/PrintTypes.java deleted file mode 100644 index 0a366a5..0000000 --- a/src/com/redomar/game/script/PrintTypes.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.redomar.game.script; - -public enum PrintTypes { - GAME, - LEVEL, - MUSIC, - ERROR, - TEST, - NETWORK, - SERVER -} diff --git a/src/com/redomar/game/script/Printing.java b/src/com/redomar/game/script/Printing.java deleted file mode 100644 index aba3de7..0000000 --- a/src/com/redomar/game/script/Printing.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.redomar.game.script; - -import com.redomar.game.lib.Time; - -import java.io.File; -import java.util.Arrays; - -public class Printing { - - private static int lineNumber = 0; - private PrintTypes type; - private Time time = new Time(); - private String message; - private String msgTime; - private String msgType; - private boolean errorMode = false; - private PrintToLog logFile; - - public Printing() { - - } - - public void print(String message, PrintTypes type) { - this.type = type; - setMessage(message); - readMessageType(type); - printOut(); - } - - private void printOut(){ - msgTime = "[" + time.getTime() + "]"; - msgType = "[" + type.toString() + "]"; - - logFile = printToLogType(type); - if (lineNumber == 0) { - - String dashes = ""; - String title = ("[" + time.getTimeDate() + "]"); - char dash = '-'; - int number = title.length() / 3; - - char[] repeat = new char[number]; - Arrays.fill(repeat, dash); - dashes += new String(repeat); - - logFile.log(dashes + title + dashes + "\n" + msgTime + msgType + this.getMessage()); - lineNumber++; - } else { - logFile.log(msgTime + msgType + this.getMessage()); - } - - - if(errorMode) { - System.err.println(msgType + msgTime + message); - }else{ - System.out.println(msgType + msgTime + message); - } - } - - private PrintToLog printToLogType(PrintTypes type){ - if (type == PrintTypes.TEST){ - return new PrintToLog(".PrintType-TEST.txt"); - } else { - return new PrintToLog(".log.txt"); - } - } - - public void removeLog(){ - new File(".log.txt").delete(); - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - private void readMessageType(PrintTypes type){ - this.errorMode = type == PrintTypes.ERROR; - } -} diff --git a/test/com/redomar/game/audio/AudioHandlerTest.java b/test/com/redomar/game/audio/AudioHandlerTest.java index 2177145..c17faa7 100644 --- a/test/com/redomar/game/audio/AudioHandlerTest.java +++ b/test/com/redomar/game/audio/AudioHandlerTest.java @@ -5,7 +5,7 @@ import java.io.File; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; /** * Created by Mohamed on 28/08/2016. @@ -13,27 +13,31 @@ */ public class AudioHandlerTest { + @Before + public void before() { + AudioHandler.musicPrinter.mute(); + } + @Test - public void bgMusicExists() throws Exception { + public void bgMusicExists() { File sfx = new File("res/music/Towards The End.mp3"); assertTrue(sfx.exists()); } @Test(expected = NullPointerException.class) - public void expectReturnExceptionFileEmptyDir(){ + public void expectReturnExceptionFileEmptyDir() { File empty = new File(""); - AudioHandler audio = new AudioHandler(empty); + new AudioHandler(empty); } @Test(expected = NullPointerException.class) - public void expectReturnExceptionFileEmptyPath(){ - AudioHandler audio = new AudioHandler(""); + public void expectReturnExceptionFileEmptyPath() { + new AudioHandler(""); } - @Test - public void tryInitiatingAndPlayingNonExistingFile(){ - AudioHandler audio = new AudioHandler("//"); - audio.play(); + @Test() + public void tryInitiatingAndPlayingNonExistingFile() { + new AudioHandler("//").play(); } } \ No newline at end of file diff --git a/test/com/redomar/game/log/PopUpTest.java b/test/com/redomar/game/log/PopUpTest.java new file mode 100644 index 0000000..3ccb68d --- /dev/null +++ b/test/com/redomar/game/log/PopUpTest.java @@ -0,0 +1,23 @@ +package com.redomar.game.log; + +import com.redomar.game.menu.PopUp; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PopUpTest { + private PopUp popUp; + + @Before + public void setUp() { + popUp = new PopUp(); + popUp.active = false; + } + + @Test + public void warnIntEfflux() { + assertEquals(1, popUp.Warn("TEST")); + } + +} diff --git a/test/com/redomar/game/script/PrintToLogTest.java b/test/com/redomar/game/log/PrintToLogTest.java similarity index 86% rename from test/com/redomar/game/script/PrintToLogTest.java rename to test/com/redomar/game/log/PrintToLogTest.java index ecc651e..f38dda5 100644 --- a/test/com/redomar/game/script/PrintToLogTest.java +++ b/test/com/redomar/game/log/PrintToLogTest.java @@ -1,13 +1,12 @@ -package com.redomar.game.script; +package com.redomar.game.log; import org.junit.Test; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** * Created by Mohamed on 23/08/2016. - * + *

* This file tests the com.redomar.game.script.PrintToLog class */ public class PrintToLogTest { diff --git a/test/com/redomar/game/log/PrinterTest.java b/test/com/redomar/game/log/PrinterTest.java new file mode 100644 index 0000000..187f524 --- /dev/null +++ b/test/com/redomar/game/log/PrinterTest.java @@ -0,0 +1,48 @@ +package com.redomar.game.log; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.*; + +/** + * Created by Mohamed on 28/08/2016. + * This file tests the com.redomar.game.script.Printing class + */ +public class PrinterTest { + + private Printer printer; + + @Before + public void setUp() { + printer = new Printer(); + } + + @Test + public void printToFileWorks() { + printer.print("TESTING STRING", PrintTypes.TEST); + File file = new File(".PrintType-TEST.txt"); + assertTrue(file.exists()); + assertTrue(file.delete()); + } + + @Test + public void getMessageIsNull() { + assertNull(printer.getMessage()); + } + + @Test + public void messageShouldBeNullAfterPrinting() { + printer.print("Not Null", PrintTypes.TEST); + assertNotNull(printer.getMessage()); + } + + @After + public void cleanUp() { + printer = null; + } + +} \ No newline at end of file diff --git a/test/com/redomar/game/script/PopUpTest.java b/test/com/redomar/game/script/PopUpTest.java deleted file mode 100644 index e4081ca..0000000 --- a/test/com/redomar/game/script/PopUpTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.redomar.game.script; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class PopUpTest { - private PopUp popUp; - - @Before - public void setUp() throws Exception { - popUp = new PopUp(); - popUp.active = false; - } - - @Test - public void warnIntEfflux() throws Exception { - assertEquals(1,popUp.Warn("TEST")); - } - -} diff --git a/test/com/redomar/game/script/PrintingTest.java b/test/com/redomar/game/script/PrintingTest.java deleted file mode 100644 index ab38469..0000000 --- a/test/com/redomar/game/script/PrintingTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.redomar.game.script; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.File; - -import static org.junit.Assert.*; - -/** - * Created by Mohamed on 28/08/2016. - * This file tests the com.redomar.game.script.Printing class - */ -public class PrintingTest { - - private Printing printing; - - @Before - public void setUp() throws Exception { - printing = new Printing(); - } - - @Test - public void printToFileWorks() throws Exception { - printing.print("TESTING STRING", PrintTypes.TEST); - File file = new File(".PrintType-TEST.txt"); - assertTrue(file.exists()); - assertTrue(file.delete()); - } - - @Test - public void getMessageIsNull() { - assertNull(printing.getMessage()); - } - - @Test - public void getMessageIsNotNull() { - printing.setMessage("Not Null"); - assertNotNull(printing.getMessage()); - } - - @After - public void cleanUp(){ - printing = null; - } - -} \ No newline at end of file