From d3c99df01dafb8075836cb54a97213c9079ed59d Mon Sep 17 00:00:00 2001 From: Karan Gurazada Date: Mon, 9 Jul 2018 22:07:27 -0700 Subject: [PATCH] fixed registry, added dynamic loading and cool error handler --- src/tanks/Game.java | 192 ++++++++-------- src/tanks/Level.java | 101 ++++----- src/tanks/Panel.java | 472 +++++++++++++++++----------------------- src/tanks/Registry.java | 194 +++++++---------- 4 files changed, 410 insertions(+), 549 deletions(-) diff --git a/src/tanks/Game.java b/src/tanks/Game.java index 0595364a..7935be7d 100644 --- a/src/tanks/Game.java +++ b/src/tanks/Game.java @@ -14,10 +14,9 @@ import javax.swing.SwingUtilities; -public class Game -{ +public class Game { public static final int tank_size = 50; - + public static ArrayList movables = new ArrayList(); public static ArrayList obstacles = new ArrayList(); public static ArrayList effects = new ArrayList(); @@ -30,24 +29,26 @@ public class Game static int currentSizeX = 28; static int currentSizeY = 18; - static double bgResMultiplier = 1; + static double bgResMultiplier = 1; public static Color[][] tiles = new Color[28][18]; - + public static double levelSize = 1; - + public static PlayerTank player; - + public static boolean bulletLocked = false; - - public enum Menu {none, paused, title, options, interlevel, crashed} - + + public enum Menu { + none, paused, title, options, interlevel, crashed + } + public static String crashMessage = "Yay! The game hasn't crashed yet!"; - + public static Menu menu = Menu.title; - - //public static boolean mainMenu = false; - //public static boolean optionsMenu = true; - //public static boolean pausedMenu = false; + + // public static boolean mainMenu = false; + // public static boolean optionsMenu = true; + // public static boolean pausedMenu = false; public static boolean paused = true; public static boolean graphicalEffects = true; @@ -56,24 +57,26 @@ public enum Menu {none, paused, title, options, interlevel, crashed} public static int coins = 0; public static Item[] items = new Item[5]; - + public static Registry registry = new Registry(); - + static Screen gamescreen; - - static String currentLevel = ""; - + + static String currentLevel = ""; + public static PrintStream logger = System.err; - + public static final String directoryPath = "/.tanks.d"; - public static final String logPath = directoryPath + "/logfile.txt"; - public static final String registryPath = directoryPath + "/tank-registry.txt"; + public static final String logPath = directoryPath + "/logfile.lmao"; + public static final String registryPath = directoryPath + "/tank-registry.lmao"; + public static final String initFilePath = directoryPath + "/init-options.lmao"; public static String homedir; - + public static ArrayList defaultTanks = new ArrayList(); - - public static void initScript() - { + + public static void initScript() { + + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankBrown.class, "brown", 1)); defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankGray.class, "gray", 1)); defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankMint.class, "mint", 1.0 / 2)); @@ -87,81 +90,65 @@ public static void initScript() defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankDarkGreen.class, "darkgreen", 1.0 / 9)); defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankBlack.class, "black", 1.0 / 10)); defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankPink.class, "pink", 1.0 / 15)); - + homedir = System.getProperty("user.home"); - if (!Files.exists(Paths.get(homedir + directoryPath))) - { + if (!Files.exists(Paths.get(homedir + directoryPath))) { new File(homedir + directoryPath).mkdir(); - try - { + try { new File(homedir + logPath).createNewFile(); - Game.logger = new PrintStream(new FileOutputStream (homedir + logPath, true)); - } - catch (IOException e) - { + Game.logger = new PrintStream(new FileOutputStream(homedir + logPath, true)); + } catch (IOException e) { e.printStackTrace(); System.exit(1); } } - - if (!Files.exists(Paths.get(homedir + registryPath))) - { + + if (!Files.exists(Paths.get(homedir + registryPath))) { Registry.initRegistry(homedir); } - - try - { - Game.logger = new PrintStream(new FileOutputStream (homedir + logPath, true)); - } - catch (FileNotFoundException e) - { + + try { + Game.logger = new PrintStream(new FileOutputStream(homedir + logPath, true)); + } catch (FileNotFoundException e) { Game.logger = System.err; - Game.logger.println(new Date().toString() + " (syswarn) logfile not found despite existence of tanks directory! using stderr instead."); + Game.logger.println(new Date().toString() + + " (syswarn) logfile not found despite existence of tanks directory! using stderr instead."); } - + Registry.loadRegistry(homedir); } - - public static void main(String[] args) - { + + public static void main(String[] args) { initScript(); - SwingUtilities.invokeLater - ( - new Runnable() - { - @Override - public void run() - { - for (int i = 0; i < currentSizeX; i++) - { - for (int j = 0; j < currentSizeY; j++) - { - Game.tiles[i][j] = new Color((int)(255 - Math.random() * 20), (int)(227 - Math.random() * 20), (int)(186 - Math.random() * 20)); - } + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + for (int i = 0; i < currentSizeX; i++) { + for (int j = 0; j < currentSizeY; j++) { + Game.tiles[i][j] = new Color((int) (255 - Math.random() * 20), (int) (227 - Math.random() * 20), + (int) (186 - Math.random() * 20)); } - - gamescreen = new Screen(); - gamescreen.setTitle("Tanks"); - gamescreen.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("icon64.png"))); - - - //movables.add(new EnemyTankStationary(120, 600, tank_size)); - //movables.add(new EnemyTankStationary(900, 700, tank_size)); + } - //add things to movables + gamescreen = new Screen(); + gamescreen.setTitle("Tanks"); + gamescreen.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("icon64.png"))); - Panel screen = new Panel(); + // movables.add(new EnemyTankStationary(120, 600, tank_size)); + // movables.add(new EnemyTankStationary(900, 700, tank_size)); - gamescreen.add(screen); - screen.startTimer(); - } + // add things to movables + + Panel screen = new Panel(); + + gamescreen.add(screen); + screen.startTimer(); } - ); + }); } - - public static void reset() - { + + public static void reset() { obstacles.clear(); belowEffects.clear(); movables.clear(); @@ -169,9 +156,8 @@ public static void reset() System.gc(); start(); } - - public static void exit() - { + + public static void exit() { Game.paused = true; menu = Menu.interlevel; obstacles.clear(); @@ -180,9 +166,8 @@ public static void exit() effects.clear(); System.gc(); } - - public static void exitToCrash() - { + + public static void exitToCrash() { Game.paused = true; obstacles.clear(); belowEffects.clear(); @@ -191,15 +176,13 @@ public static void exitToCrash() System.gc(); menu = Menu.crashed; } - - public static void exitToTitle() - { + + public static void exitToTitle() { Game.tiles = new Color[28][18]; - for (int i = 0; i < 28; i++) - { - for (int j = 0; j < 18; j++) - { - Game.tiles[i][j] = new Color((int)(255 - Math.random() * 20), (int)(227 - Math.random() * 20), (int)(186 - Math.random() * 20)); + for (int i = 0; i < 28; i++) { + for (int j = 0; j < 18; j++) { + Game.tiles[i][j] = new Color((int) (255 - Math.random() * 20), (int) (227 - Math.random() * 20), + (int) (186 - Math.random() * 20)); } } Level.currentColor = new Color(235, 207, 166); @@ -213,18 +196,19 @@ public static void exitToTitle() effects.clear(); System.gc(); } - - public static void start() - { - //Level level = new Level("{28,18|4...11-6,11-0...5,17...27-6,16-3...6,0...10-11,11-11...14,16...23-11,16-12...17|3-15-player,7-3-purple2-2,20-14-green,22-3-green-2,8-8.5-brown,19-8.5-mint-2,13.5-5-yellow-1}"); - - //System.out.println(LevelGenerator.generateLevelString()); + + public static void start() { + // Level level = new + // Level("{28,18|4...11-6,11-0...5,17...27-6,16-3...6,0...10-11,11-11...14,16...23-11,16-12...17|3-15-player,7-3-purple2-2,20-14-green,22-3-green-2,8-8.5-brown,19-8.5-mint-2,13.5-5-yellow-1}"); + + // System.out.println(LevelGenerator.generateLevelString()); Game.currentLevel = LevelGenerator.generateLevelString(); Level level = new Level(currentLevel); - //Level level = new Level("{28,18|3...6-3...4,3...4-5...6,10...19-13...14,18...19-4...12|22-14-player,14-10-brown}"); - //Level level = new Level("{28,18|0...27-9,0...27-7|2-8-player,26-8-purple2-2}"); + // Level level = new + // Level("{28,18|3...6-3...4,3...4-5...6,10...19-13...14,18...19-4...12|22-14-player,14-10-brown}"); + // Level level = new + // Level("{28,18|0...27-9,0...27-7|2-8-player,26-8-purple2-2}"); level.loadLevel(); } - } diff --git a/src/tanks/Level.java b/src/tanks/Level.java index 4db322bc..b2b07b27 100644 --- a/src/tanks/Level.java +++ b/src/tanks/Level.java @@ -2,111 +2,105 @@ import java.awt.Color; -public class Level -{ +public class Level { String[] screen; String[] obstaclesPos; String[] tanks; - + static Color currentColor = new Color(235, 207, 166); - - public Level(String level) - { + + public Level(String level) { String[] preset = level.split("\\{")[1].split("\\}")[0].split("\\|"); screen = preset[0].split(","); obstaclesPos = preset[1].split(","); tanks = preset[2].split(","); } - - public void loadLevel() - { + + public void loadLevel() { + Registry.loadRegistry(Game.homedir); + int sX = Integer.parseInt(screen[0]); int sY = Integer.parseInt(screen[1]); - + int r = 235; int g = 207; int b = 166; - + int dr = 20; int dg = 20; int db = 20; - - if (screen.length >= 5) - { + + if (screen.length >= 5) { r = Integer.parseInt(screen[2]); g = Integer.parseInt(screen[3]); b = Integer.parseInt(screen[4]); - - if (screen.length >= 8) - { + + if (screen.length >= 8) { dr = Integer.parseInt(screen[5]); dg = Integer.parseInt(screen[6]); db = Integer.parseInt(screen[7]); } } - + Game.currentSizeX = (int) (sX * Game.bgResMultiplier); Game.currentSizeY = (int) (sY * Game.bgResMultiplier); - + currentColor = new Color(r, g, b); - + Game.tiles = new Color[Game.currentSizeX][Game.currentSizeY]; - for (int i = 0; i < Game.currentSizeX; i++) - { - for (int j = 0; j < Game.currentSizeY; j++) - { - Game.tiles[i][j] = new Color((int)(r + Math.random() * dr), (int)(g + Math.random() * dg), (int)(b + Math.random() * db)); + for (int i = 0; i < Game.currentSizeX; i++) { + for (int j = 0; j < Game.currentSizeY; j++) { + Game.tiles[i][j] = new Color((int) (r + Math.random() * dr), (int) (g + Math.random() * dg), + (int) (b + Math.random() * db)); } } - + Game.gamescreen.setScreenBounds(Game.tank_size * sX, Game.tank_size * sY); - - for (int i = 0; i < obstaclesPos.length; i++) - { + + for (int i = 0; i < obstaclesPos.length; i++) { String[] obs = obstaclesPos[i].split("-"); - + String[] xPos = obs[0].split("\\.\\.\\."); - - double startX; + + double startX; double endX; - + startX = Double.parseDouble(xPos[0]); endX = startX; if (xPos.length > 1) endX = Double.parseDouble(xPos[1]); - + String[] yPos = obs[1].split("\\.\\.\\."); - - double startY; + + double startY; double endY; - + startY = Double.parseDouble(yPos[0]); endY = startY; if (yPos.length > 1) endY = Double.parseDouble(yPos[1]); - - for (double x = startX; x <= endX; x++) - { - for (double y = startY; y <= endY; y++) - { + + for (double x = startX; x <= endX; x++) { + for (double y = startY; y <= endY; y++) { double colorMul = Math.random() * 0.5 + 0.5; Color col; - + if (Game.graphicalEffects) - col = new Color((int) (colorMul * (176 - Math.random() * 70)), (int) (colorMul * (111 - Math.random() * 34)), (int) (colorMul * 14)); + col = new Color((int) (colorMul * (176 - Math.random() * 70)), + (int) (colorMul * (111 - Math.random() * 34)), (int) (colorMul * 14)); else col = new Color(87, 46, 8); Game.obstacles.add(new Obstacle(x, y, col)); - //Game.obstacles.add(new Obstacle(x, y, Color.getHSBColor((float) ((x + y) / 100), 1, 1))); + // Game.obstacles.add(new Obstacle(x, y, Color.getHSBColor((float) ((x + y) / + // 100), 1, 1))); } } } - - for (int i = 0; i < tanks.length; i++) - { + + for (int i = 0; i < tanks.length; i++) { String[] tank = tanks[i].split("-"); double x = Game.tank_size * (0.5 + Double.parseDouble(tank[0])); double y = Game.tank_size * (0.5 + Double.parseDouble(tank[1])); @@ -114,18 +108,15 @@ public void loadLevel() double angle = 0; if (tank.length == 4) angle = (Math.PI / 2 * Double.parseDouble(tank[3])); - - if (type.equals("player")) - { + + if (type.equals("player")) { Game.player = new PlayerTank(x, y, Game.tank_size, new Color(0, 150, 255)); Game.movables.add(Game.player); - } - else - { + } else { Game.movables.add(Game.registry.getRegistry(type).getTank(x, y, angle)); } } - + Panel.preGameTimer = 400; } } diff --git a/src/tanks/Panel.java b/src/tanks/Panel.java index 2a934876..1a6b57a8 100644 --- a/src/tanks/Panel.java +++ b/src/tanks/Panel.java @@ -12,8 +12,7 @@ import javax.swing.Timer; @SuppressWarnings("serial") -public class Panel extends JPanel -{ +public class Panel extends JPanel { Timer timer; int height = Screen.sizeY; int width = Screen.sizeX; @@ -28,7 +27,10 @@ public class Panel extends JPanel double darkness = 0; - /** Important value used in calculating game speed. Larger values are set when the frames are lower, and game speed is increased to compensate.*/ + /** + * Important value used in calculating game speed. Larger values are set when + * the frames are lower, and game speed is increased to compensate. + */ public static double frameFrequency = 1; ArrayList frameFrequencies = new ArrayList(); @@ -49,35 +51,27 @@ public class Panel extends JPanel ArrayList fireworks = new ArrayList(); ArrayList removeFireworks = new ArrayList(); - Button resume = new Button(350, 40, "Continue playing", new Runnable() - { + Button resume = new Button(350, 40, "Continue playing", new Runnable() { @Override - public void run() - { + public void run() { Game.menu = Game.Menu.none; Game.paused = false; Game.player.cooldown = 20; } - } - ); + }); - Button newLevel = new Button(350, 40, "Generate a new level", new Runnable() - { + Button newLevel = new Button(350, 40, "Generate a new level", new Runnable() { @Override - public void run() - { + public void run() { Game.reset(); Game.menu = Game.Menu.none; Game.paused = false; } - } - ); + }); - Button graphics = new Button(350, 40, "Graphics: fancy", new Runnable() - { + Button graphics = new Button(350, 40, "Graphics: fancy", new Runnable() { @Override - public void run() - { + public void run() { Game.graphicalEffects = !Game.graphicalEffects; if (Game.graphicalEffects) @@ -85,14 +79,11 @@ public void run() else graphics.text = "Graphics: fast"; } - } - ); + }); - Button mouseTarget = new Button(350, 40, "Mouse target: enabled", new Runnable() - { + Button mouseTarget = new Button(350, 40, "Mouse target: enabled", new Runnable() { @Override - public void run() - { + public void run() { showMouseTarget = !showMouseTarget; if (showMouseTarget) @@ -100,14 +91,11 @@ public void run() else mouseTarget.text = "Mouse target: disabled"; } - } - ); + }); - Button scale = new Button(350, 40, "Scale: 100%", new Runnable() - { + Button scale = new Button(350, 40, "Scale: 100%", new Runnable() { @Override - public void run() - { + public void run() { if (KeyInputListener.keys.contains(KeyEvent.VK_SHIFT)) Screen.scale -= 0.1; else @@ -121,48 +109,36 @@ public void run() Screen.scale = Math.round(Screen.scale * 10) / 10.0; - scale.text = "Scale: " + (int)Math.round(Screen.scale * 100) + "%"; - Game.gamescreen.setSize((int)(Screen.sizeX * Screen.scale), (int) ((Screen.sizeY) * Screen.scale )); + scale.text = "Scale: " + (int) Math.round(Screen.scale * 100) + "%"; + Game.gamescreen.setSize((int) (Screen.sizeX * Screen.scale), (int) ((Screen.sizeY) * Screen.scale)); } - } - , "Click to increase scale by 10%---Hold shift while clicking to decrease scale by 10%"); + }, "Click to increase scale by 10%---Hold shift while clicking to decrease scale by 10%"); - Button quit = new Button(350, 40, "Quit to title", new Runnable() - { + Button quit = new Button(350, 40, "Quit to title", new Runnable() { @Override - public void run() - { + public void run() { Game.exitToTitle(); - //System.exit(0); + // System.exit(0); } - } - ); + }); - Button back = new Button(350, 40, "Back", new Runnable() - { + Button back = new Button(350, 40, "Back", new Runnable() { @Override - public void run() - { + public void run() { Game.menu = Game.Menu.title; } - } - ); + }); - Button exit = new Button(350, 40, "Exit the game", new Runnable() - { + Button exit = new Button(350, 40, "Exit the game", new Runnable() { @Override - public void run() - { + public void run() { System.exit(0); } - } - ); + }); - Button insanity = new Button(350, 40, "Insanity mode: disabled", new Runnable() - { + Button insanity = new Button(350, 40, "Insanity mode: disabled", new Runnable() { @Override - public void run() - { + public void run() { Game.insanity = !Game.insanity; if (Game.insanity) @@ -170,74 +146,58 @@ public void run() else insanity.text = "Insanity mode: disabled"; } - } - ); + }); - Button options = new Button(350, 40, "Options...", new Runnable() - { + Button options = new Button(350, 40, "Options...", new Runnable() { @Override - public void run() - { + public void run() { Game.menu = Game.Menu.options; } - } - ); + }); - Button replay = new Button(350, 40, "Replay the level", new Runnable() - { + Button replay = new Button(350, 40, "Replay the level", new Runnable() { @Override - public void run() - { + public void run() { Level level = new Level(Game.currentLevel); level.loadLevel(); Game.menu = Game.Menu.none; Game.paused = false; } - } - ); + }); boolean pausePressed = false; static double preGameTimer = 0; - public Panel() - { - timer = new Timer(0, new ActionListener() - { + public Panel() { + timer = new Timer(0, new ActionListener() { @Override - public void actionPerformed(ActionEvent e) - { - //long start = System.nanoTime(); + public void actionPerformed(ActionEvent e) { + // long start = System.nanoTime(); - try - { + try { long milliTime = System.currentTimeMillis(); framesList.add(milliTime); ArrayList removeList = new ArrayList(); - for (int i = 0; i < framesList.size(); i++) - { + for (int i = 0; i < framesList.size(); i++) { if (milliTime - framesList.get(i) > 1000) removeList.add(framesList.get(i)); } - for (int i = 0; i < removeList.size(); i++) - { + for (int i = 0; i < removeList.size(); i++) { framesList.remove(removeList.get(i)); } if (Game.coins < 0) Game.coins = 0; + if (KeyInputListener.keys.contains(KeyEvent.VK_ESCAPE)) { - if (KeyInputListener.keys.contains(KeyEvent.VK_ESCAPE)) - { - - if (Game.menu == Game.Menu.paused || Game.menu == Game.Menu.none) - { + if (Game.menu == Game.Menu.paused || Game.menu == Game.Menu.none) { if (!pausePressed) Game.paused = !Game.paused; @@ -248,49 +208,38 @@ public void actionPerformed(ActionEvent e) } pausePressed = true; - } - else + } else pausePressed = false; - if (!Game.paused) - { - if (preGameTimer > 0) - { + if (!Game.paused) { + if (preGameTimer > 0) { preGameTimer -= frameFrequency; - if (Game.movables.contains(Game.player)) - { + if (Game.movables.contains(Game.player)) { Obstacle.draw_size = Math.min(Game.tank_size, Obstacle.draw_size + frameFrequency); } - } - else - { + } else { Obstacle.draw_size = Math.min(Obstacle.obstacle_size, Obstacle.draw_size); int tanks = 0; - for (int i = 0; i < Game.movables.size(); i++) - { + for (int i = 0; i < Game.movables.size(); i++) { Movable m = Game.movables.get(i); m.update(); if (m instanceof Tank) tanks++; } - if (!Game.movables.contains(Game.player)) - { - for (int m = 0; m < Game.movables.size(); m++) - { + if (!Game.movables.contains(Game.player)) { + for (int m = 0; m < Game.movables.size(); m++) { Movable mo = Game.movables.get(m); if (mo instanceof Bullet || mo instanceof Mine) mo.destroy = true; } - if (Game.effects.size() == 0) - { + if (Game.effects.size() == 0) { Obstacle.draw_size = Math.max(0, Obstacle.draw_size - Panel.frameFrequency); for (int i = 0; i < Game.movables.size(); i++) Game.movables.get(i).destroy = true; - if (Obstacle.draw_size <= 0) - { + if (Obstacle.draw_size <= 0) { winlose = "You were destroyed!"; win = false; Game.exit(); @@ -299,35 +248,31 @@ public void actionPerformed(ActionEvent e) } - /*for (int i = 0; i < Game.obstacles.size(); i++) - { - Game.obstacles.get(i).posX += (Game.obstacles.get(i).posX - Game.player.posX) / 1000; - Game.obstacles.get(i).posY += (Game.obstacles.get(i).posY - Game.player.posY) / 1000; - }*/ + /* + * for (int i = 0; i < Game.obstacles.size(); i++) { Game.obstacles.get(i).posX + * += (Game.obstacles.get(i).posX - Game.player.posX) / 1000; + * Game.obstacles.get(i).posY += (Game.obstacles.get(i).posY - Game.player.posY) + * / 1000; } + */ - if (tanks <= 1 && !Game.player.destroy) - { + if (tanks <= 1 && !Game.player.destroy) { Game.bulletLocked = true; - for (int m = 0; m < Game.movables.size(); m++) - { + for (int m = 0; m < Game.movables.size(); m++) { Movable mo = Game.movables.get(m); if (mo instanceof Bullet || mo instanceof Mine) mo.destroy = true; } - if (Game.effects.size() == 0) - { + if (Game.effects.size() == 0) { Obstacle.draw_size = Math.max(0, Obstacle.draw_size - Panel.frameFrequency); - if (Obstacle.draw_size <= 0) - { + if (Obstacle.draw_size <= 0) { winlose = "Level Cleared!"; win = true; Game.exit(); } } - } - else + } else Game.bulletLocked = false; } @@ -350,43 +295,40 @@ public void actionPerformed(ActionEvent e) } repaint(); - //frameFrequency = 100.0 / lastFPS; - //timer.setDelay((int) (frameFrequency * 10)); + // frameFrequency = 100.0 / lastFPS; + // timer.setDelay((int) (frameFrequency * 10)); - //long end = System.nanoTime(); - //System.out.println(end - start); + // long end = System.nanoTime(); + // System.out.println(end - start); - //int wait = (int) ((end - start)/1000); - //timer.setDelay(wait); + // int wait = (int) ((end - start)/1000); + // timer.setDelay(wait); long time = System.currentTimeMillis(); long lastFrameTime = lastFrame; lastFrame = time; - double freq = (time - lastFrameTime) / 10.0; + double freq = (time - lastFrameTime) / 10.0; frameFrequencies.add(freq); - if (frameFrequencies.size() > 5) - { + if (frameFrequencies.size() > 5) { frameFrequencies.remove(0); } double totalFrequency = 0; - for (int i = 0; i < frameFrequencies.size(); i++) - { + for (int i = 0; i < frameFrequencies.size(); i++) { totalFrequency += frameFrequencies.get(i); } frameFrequency = totalFrequency / frameFrequencies.size(); - //System.out.println(frameFrequency); - //frameFrequency = 100.0 / framesList.size(); - } - catch (Exception exception) - { + // System.out.println(frameFrequency); + // frameFrequency = 100.0 / framesList.size(); + } catch (Exception exception) { Game.exitToCrash(); - - Game.logger.println(new Date().toString() + " (Fatal) The game has crashed! Below is a crash report:"); + + Game.logger + .println(new Date().toString() + " (syserr) something broke. below is the stack trace for what broke. good luck."); exception.printStackTrace(Game.logger); Game.crashMessage = e.toString(); } @@ -395,55 +337,46 @@ public void actionPerformed(ActionEvent e) }); } - public void startTimer() - { + public void startTimer() { timer.start(); } @Override - public void paintComponent(Graphics g) - { + public void paintComponent(Graphics g) { super.paintComponent(g); - int verticalCenter = this.getHeight()/2; - int horizontalCenter = this.getWidth()/2; + int verticalCenter = this.getHeight() / 2; + int horizontalCenter = this.getWidth() / 2; - if(!resize) - { - int topLeftSquareCornerY = verticalCenter - (height/2); - int topLeftSquareCornerX = horizontalCenter - (width/2); + if (!resize) { + int topLeftSquareCornerY = verticalCenter - (height / 2); + int topLeftSquareCornerX = horizontalCenter - (width / 2); g.setColor(Color.BLUE); g.drawRect(topLeftSquareCornerX, topLeftSquareCornerY, width, height); - } - else - { + } else { g.setColor(Color.MAGENTA); - g.drawRect(15,15,(this.getWidth() - 30), this.getHeight() - 30); + g.drawRect(15, 15, (this.getWidth() - 30), this.getHeight() - 30); } } @Override - public void paint(Graphics g) - { - try - { - if (System.currentTimeMillis() - startTime < 1000) - { - for (int i = 0; i < Game.currentSizeX; i++) - { + public void paint(Graphics g) { + try { + if (System.currentTimeMillis() - startTime < 1000) { + for (int i = 0; i < Game.currentSizeX; i++) { g.setColor(Level.currentColor); - Screen.fillRect(g, Screen.sizeX / 2, Screen.sizeY / 2, Screen.sizeX * 1.2, Screen.sizeY * 1.2); - g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("loading.png")), 0, 0, null); + Screen.fillRect(g, Screen.sizeX / 2, Screen.sizeY / 2, Screen.sizeX * 1.2, Screen.sizeY * 1.2); + g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("loading.png")), 0, 0, + null); } return; } - if (Game.menu == Game.Menu.crashed) - { + if (Game.menu == Game.Menu.crashed) { g.setColor(Color.blue); - Screen.fillRect(g, Screen.sizeX / 2, Screen.sizeY / 2, Screen.sizeX * 1.2, Screen.sizeY * 1.2); + Screen.fillRect(g, Screen.sizeX / 2, Screen.sizeY / 2, Screen.sizeX * 1.2, Screen.sizeY * 1.2); g.setColor(Color.white); g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (100 * Screen.scale))); @@ -460,8 +393,7 @@ public void paint(Graphics g) Screen.drawText(g, Screen.sizeX / 2, 400, "You may return to the game if you wish,"); Screen.drawText(g, Screen.sizeX / 2, 440, "but be warned that things may become unstable."); Screen.drawText(g, Screen.sizeX / 2, 480, "If you see this screen again, restart the game."); - Screen.drawText(g, Screen.sizeX / 2, 520, "Also, you may want to report this crash!"); - + Screen.drawText(g, Screen.sizeX / 2, 520, "Also, you may want to report this crash to definitelygoingtoreplytoyouremail@hotmail.com!"); this.quit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY - 160); this.exit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY - 100); @@ -469,29 +401,27 @@ public void paint(Graphics g) return; } - g.fillRect(0, 0, 1 + (int)(Game.gamescreen.getSize().getWidth()), 1+(int)(Game.gamescreen.getSize().getHeight())); - long time = (long) (System.currentTimeMillis() * frameSampling / 1000 ); - if (lastFrameSec < time && lastFrameSec != firstFrameSec) - { + g.fillRect(0, 0, 1 + (int) (Game.gamescreen.getSize().getWidth()), + 1 + (int) (Game.gamescreen.getSize().getHeight())); + long time = (long) (System.currentTimeMillis() * frameSampling / 1000); + if (lastFrameSec < time && lastFrameSec != firstFrameSec) { lastFPS = (int) (frames * 1.0 * frameSampling); frames = 0; } - lastFrameSec = time; + lastFrameSec = time; frames++; - //g.setColor(new Color(255, 227, 186)); - //g.fillRect(0, 0, (int) (Screen.sizeX * Screen.scale), (int) (Screen.sizeY * Screen.scale)); + // g.setColor(new Color(255, 227, 186)); + // g.fillRect(0, 0, (int) (Screen.sizeX * Screen.scale), (int) (Screen.sizeY * + // Screen.scale)); g.setColor(Level.currentColor); Screen.fillRect(g, Screen.sizeX / 2, Screen.sizeY / 2, Screen.sizeX, Screen.sizeY); - if (Game.graphicalEffects) - { - for (int i = 0; i < Game.currentSizeX; i++) - { - for (int j = 0; j < Game.currentSizeY; j++) - { + if (Game.graphicalEffects) { + for (int i = 0; i < Game.currentSizeX; i++) { + for (int j = 0; j < Game.currentSizeY; j++) { int extra; if (Screen.scale * 10 == Math.round(Screen.scale * 10)) extra = 0; @@ -499,20 +429,20 @@ public void paint(Graphics g) extra = 0; g.setColor(Game.tiles[i][j]); - Screen.fillRect(g, (i + 0.5) / Game.bgResMultiplier * Obstacle.obstacle_size, (j + 0.5) / Game.bgResMultiplier * Obstacle.obstacle_size, extra + Obstacle.obstacle_size / Game.bgResMultiplier, extra + Obstacle.obstacle_size / Game.bgResMultiplier); + Screen.fillRect(g, (i + 0.5) / Game.bgResMultiplier * Obstacle.obstacle_size, + (j + 0.5) / Game.bgResMultiplier * Obstacle.obstacle_size, + extra + Obstacle.obstacle_size / Game.bgResMultiplier, + extra + Obstacle.obstacle_size / Game.bgResMultiplier); } } } - if (!Game.paused) - { + if (!Game.paused) { for (int i = 0; i < Game.belowEffects.size(); i++) Game.belowEffects.get(i).draw(g); - } - else - { + } else { for (int i = 0; i < Game.belowEffects.size(); i++) - ((Effect)Game.belowEffects.get(i)).drawWithoutUpdate(g); + ((Effect) Game.belowEffects.get(i)).drawWithoutUpdate(g); } for (int n = 0; n < Game.movables.size(); n++) @@ -521,37 +451,35 @@ public void paint(Graphics g) for (int i = 0; i < Game.obstacles.size(); i++) Game.obstacles.get(i).draw(g); - if (!Game.paused) - { + if (!Game.paused) { for (int i = 0; i < Game.effects.size(); i++) Game.effects.get(i).draw(g); - } - else - { + } else { for (int i = 0; i < Game.effects.size(); i++) - ((Effect)Game.effects.get(i)).drawWithoutUpdate(g); + ((Effect) Game.effects.get(i)).drawWithoutUpdate(g); } double mx = Screen.screen.getMouseX(); double my = Screen.screen.getMouseY(); - Screen.scale = Math.min(Game.gamescreen.getSize().getWidth() * 1.0 / Game.currentSizeX, (Game.gamescreen.getSize().getHeight() * 1.0 - 40 - Screen.yOffset) / Game.currentSizeY) / 50.0; + Screen.scale = Math.min(Game.gamescreen.getSize().getWidth() * 1.0 / Game.currentSizeX, + (Game.gamescreen.getSize().getHeight() * 1.0 - 40 - Screen.yOffset) / Game.currentSizeY) / 50.0; - //System.out.println(Game.gamescreen.getSize().getWidth() * 1.0 / Game.currentSizeX + " " + (Game.gamescreen.getSize().getHeight() * 1.0 - 40 - Screen.offset) / Game.currentSizeY); + // System.out.println(Game.gamescreen.getSize().getWidth() * 1.0 / + // Game.currentSizeX + " " + (Game.gamescreen.getSize().getHeight() * 1.0 - 40 - + // Screen.offset) / Game.currentSizeY); - //System.out.println(Screen.scale); + // System.out.println(Screen.scale); if (Game.menu.equals(Game.Menu.interlevel) && win && Game.graphicalEffects) darkness = Math.min(darkness + Panel.frameFrequency * 1.5, 191); else darkness = Math.max(darkness - Panel.frameFrequency * 3, 0); - g.setColor(new Color(0, 0, 0, (int) darkness)); Screen.fillRect(g, Screen.sizeX / 2, Screen.sizeY / 2, Screen.sizeX, Screen.sizeY); - if (Game.menu.equals(Game.Menu.title)) - { + if (Game.menu.equals(Game.Menu.title)) { Game.paused = true; g.setColor(Color.black); g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (60 * Screen.scale))); @@ -559,48 +487,40 @@ public void paint(Graphics g) newLevel.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2); exit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 120); options.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 60); - } - else if (Game.menu.equals(Game.Menu.paused)) - { + } else if (Game.menu.equals(Game.Menu.paused)) { g.setColor(new Color(127, 178, 228, 64)); - g.fillRect(0, 0, (int) (Game.gamescreen.getSize().getWidth()) + 1, (int) (Game.gamescreen.getSize().getHeight()) + 1); + g.fillRect(0, 0, (int) (Game.gamescreen.getSize().getWidth()) + 1, + (int) (Game.gamescreen.getSize().getHeight()) + 1); newLevel.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2); quit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 60); resume.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 - 60); g.setColor(Color.black); Screen.drawText(g, Screen.sizeX / 2, Screen.sizeY / 2 - 150, "Game paused"); - } - else if (Game.menu.equals(Game.Menu.options)) - { + } else if (Game.menu.equals(Game.Menu.options)) { insanity.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 30); mouseTarget.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 - 30); graphics.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 - 90); back.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 90); Screen.drawText(g, Screen.sizeX / 2, Screen.sizeY / 2 - 150, "Options"); - //scale.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 0); - } - else if (Game.menu.equals(Game.Menu.interlevel)) - { - if (win && Game.graphicalEffects) - { - if (Math.random() < 0.01) - { - Firework f = new Firework(Firework.FireworkType.rocket, (Math.random() * 0.6 + 0.2) * Screen.sizeX, Screen.sizeY, fireworks, removeFireworks); + // scale.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 0); + } else if (Game.menu.equals(Game.Menu.interlevel)) { + if (win && Game.graphicalEffects) { + if (Math.random() < 0.01) { + Firework f = new Firework(Firework.FireworkType.rocket, + (Math.random() * 0.6 + 0.2) * Screen.sizeX, Screen.sizeY, fireworks, removeFireworks); f.setRandomColor(); - f.vY = - Math.random() * 3 - 6; + f.vY = -Math.random() * 3 - 6; f.vX = Math.random() * 5 - 2.5; fireworks.add(f); } - for (int i = 0; i < fireworks.size(); i++) - { + for (int i = 0; i < fireworks.size(); i++) { fireworks.get(i).drawUpdate(g); } - for (int i = 0; i < removeFireworks.size(); i++) - { + for (int i = 0; i < removeFireworks.size(); i++) { fireworks.remove(removeFireworks.get(i)); - } + } } newLevel.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 - 60); @@ -611,68 +531,66 @@ else if (Game.menu.equals(Game.Menu.interlevel)) Screen.drawText(g, Screen.sizeX / 2, Screen.sizeY / 2 - 150, winlose); } - - g.setColor(new Color(87, 46, 8)); - g.fillRect(0, (int) (Game.gamescreen.getSize().getHeight() - 40 - Screen.yOffset), (int) (Game.gamescreen.getSize().getWidth()), 40); + g.fillRect(0, (int) (Game.gamescreen.getSize().getHeight() - 40 - Screen.yOffset), + (int) (Game.gamescreen.getSize().getWidth()), 40); g.setColor(new Color(255, 227, 186)); g.setFont(g.getFont().deriveFont(Font.BOLD, 12)); g.drawString("Tanks v0.3.5", 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 12 - Screen.yOffset)); - g.drawString("FPS: " + lastFPS, 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 24 - Screen.yOffset)); - g.drawString("Coins: " + Game.coins, 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 36 - Screen.yOffset)); - - /*int obstacles = Game.obstacles.size(); - int movables = Game.movables.size(); - int effects = Game.effects.size(); - - int drawHeight = 23; - int drawSize = 10;*/ - - /*g.setColor(Color.red); - g.fillRect(0, drawHeight, obstacles, drawSize); - g.setColor(Color.green); - g.fillRect(obstacles, drawHeight, movables, drawSize); - g.setColor(Color.blue); - g.fillRect(obstacles + movables, drawHeight, effects, drawSize);*/ - - /*for (int i = 0; i < Game.obstacles.size(); i++) - { - //Game.obstacles.get(i).posX += (Game.obstacles.get(i).posX - Game.player.posX) / 1000; - //Game.obstacles.get(i).posY += (Game.obstacles.get(i).posY - Game.player.posY) / 1000; - - Game.obstacles.get(i).posX += Math.random() * 4 - 2; - Game.obstacles.get(i).posY += Math.random() * 4 - 2; - } - for (int i = 0; i < Game.movables.size(); i++) - { - Game.movables.get(i).posX += Math.random() * 4 - 2; - Game.movables.get(i).posY += Math.random() * 4 - 2; - } - for (int i = 0; i < Game.effects.size(); i++) - { - Game.effects.get(i).posX += Math.random() * 4 - 2; - Game.effects.get(i).posY += Math.random() * 4 - 2; - }*/ - - //g.setColor(Color.red); - //g.fillRect(Game.gamescreen.getWidth() - 250, (int)(Game.gamescreen.getSize().getHeight() - 40 + 15 - Screen.offset), (int) (200 * (Runtime.getRuntime().totalMemory() * 1.0 / Runtime.getRuntime().maxMemory())), 10); - //g.drawRect(Game.gamescreen.getWidth() - 250, (int)(Game.gamescreen.getSize().getHeight() - 40 + 15 - Screen.offset), 200, 10); - - if (showMouseTarget) - { + g.drawString("FPS: " + lastFPS, 2, + (int) (Game.gamescreen.getSize().getHeight() - 40 + 24 - Screen.yOffset)); + g.drawString("Coins: " + Game.coins, 2, + (int) (Game.gamescreen.getSize().getHeight() - 40 + 36 - Screen.yOffset)); + + /* + * int obstacles = Game.obstacles.size(); int movables = Game.movables.size(); + * int effects = Game.effects.size(); + * + * int drawHeight = 23; int drawSize = 10; + */ + + /* + * g.setColor(Color.red); g.fillRect(0, drawHeight, obstacles, drawSize); + * g.setColor(Color.green); g.fillRect(obstacles, drawHeight, movables, + * drawSize); g.setColor(Color.blue); g.fillRect(obstacles + movables, + * drawHeight, effects, drawSize); + */ + + /* + * for (int i = 0; i < Game.obstacles.size(); i++) { + * //Game.obstacles.get(i).posX += (Game.obstacles.get(i).posX - + * Game.player.posX) / 1000; //Game.obstacles.get(i).posY += + * (Game.obstacles.get(i).posY - Game.player.posY) / 1000; + * + * Game.obstacles.get(i).posX += Math.random() * 4 - 2; + * Game.obstacles.get(i).posY += Math.random() * 4 - 2; } for (int i = 0; i < + * Game.movables.size(); i++) { Game.movables.get(i).posX += Math.random() * 4 - + * 2; Game.movables.get(i).posY += Math.random() * 4 - 2; } for (int i = 0; i < + * Game.effects.size(); i++) { Game.effects.get(i).posX += Math.random() * 4 - + * 2; Game.effects.get(i).posY += Math.random() * 4 - 2; } + */ + + // g.setColor(Color.red); + // g.fillRect(Game.gamescreen.getWidth() - 250, + // (int)(Game.gamescreen.getSize().getHeight() - 40 + 15 - Screen.offset), (int) + // (200 * (Runtime.getRuntime().totalMemory() * 1.0 / + // Runtime.getRuntime().maxMemory())), 10); + // g.drawRect(Game.gamescreen.getWidth() - 250, + // (int)(Game.gamescreen.getSize().getHeight() - 40 + 15 - Screen.offset), 200, + // 10); + + if (showMouseTarget) { g.setColor(Color.black); Screen.drawOval(g, mx, my, 8, 8); Screen.drawOval(g, mx, my, 4, 4); } - } - catch (Exception e) - { + } catch (Exception e) { Game.exitToCrash(); - - Game.logger.println(new Date().toString() + " (Fatal) The game has crashed! Below is a crash report:"); + + Game.logger.println(new Date().toString() + " (syserr) something broke. below is the stack trace for what broke. good luck."); e.printStackTrace(Game.logger); Game.crashMessage = e.toString(); } diff --git a/src/tanks/Registry.java b/src/tanks/Registry.java index 0399c863..e3985552 100644 --- a/src/tanks/Registry.java +++ b/src/tanks/Registry.java @@ -10,89 +10,75 @@ import java.util.Date; import java.util.Scanner; -public class Registry -{ - +public class Registry { + public ArrayList tankRegistries = new ArrayList(); protected double maxTankWeight = 0; - - public static void loadRegistry (String homedir) - { + + public static void loadRegistry(String homedir) { + Game.registry.tankRegistries.clear(); + Game.registry.maxTankWeight = 0; String path = homedir + Game.registryPath; - try - { - Scanner in = new Scanner (new File (path)); - while (in.hasNextLine()) - { + try { + Scanner in = new Scanner(new File(path)); + while (in.hasNextLine()) { String line = in.nextLine(); String[] tankLine = line.split(","); - - if (tankLine[0].charAt(0) == '#') - { - continue; + + if (tankLine[0].charAt(0) == '#') { + continue; } - if (tankLine[2].toLowerCase().equals("default")) - { + if (tankLine[2].toLowerCase().equals("default")) { boolean foundTank = false; - for (int i = 0; i < Game.defaultTanks.size(); i++) - { - if (tankLine[0].equals(Game.defaultTanks.get(i).name)) - { + for (int i = 0; i < Game.defaultTanks.size(); i++) { + if (tankLine[0].equals(Game.defaultTanks.get(i).name)) { Game.defaultTanks.get(i).registerEntry(Game.registry, Double.parseDouble(tankLine[1])); foundTank = true; break; } } - + if (!foundTank) - Game.logger.println (new Date().toString() + " (Warning) The default tank '" + tankLine[0] + "' does not exist!"); - } - else - { - try - { + Game.logger.println(new Date().toString() + " (syswarn) The default tank '" + tankLine[0] + + "' does not exist or cannot be loaded. ignoring."); + } else { + try { @SuppressWarnings("resource") - ClassLoader loader = new URLClassLoader( new URL[] { new File(tankLine[3]).toURI().toURL() }); // super messy + ClassLoader loader = new URLClassLoader(new URL[] { new File(tankLine[3]).toURI().toURL() }); // super + // messy @SuppressWarnings("unchecked") Class clasz = (Class) loader.loadClass(tankLine[4]); new Registry.TankEntry(Game.registry, clasz, tankLine[0], Double.parseDouble(tankLine[1])); - } - catch (Exception e) - { + } catch (Exception e) { e.printStackTrace(); - Game.logger.println(new Date().toString() + " (Warning) Error loading custom tank '" + tankLine[0] + "'. try adding the path to your jvm classpath. Ignoring."); + Game.logger.println(new Date().toString() + " (syswarn) error loading custom tank '" + + tankLine[0] + "'. ignoring."); } } } in.close(); - } - catch (Exception e) - { - Game.logger.println (new Date().toString() + " (Warning) Tank registry file is nonexistent or broken, using default:"); + } catch (Exception e) { + Game.logger.println( + new Date().toString() + " (syswarn) tank registry file is nonexistent or broken, using default. below is stack trace."); e.printStackTrace(Game.logger); - - for (int i = 0; i < Game.defaultTanks.size(); i++) - { + + for (int i = 0; i < Game.defaultTanks.size(); i++) { Game.defaultTanks.get(i).registerEntry(Game.registry); } } } - - public static void initRegistry(String homedir) - { + + public static void initRegistry(String homedir) { String path = homedir + Game.registryPath; - try - { + try { new File(path).createNewFile(); - } - catch (IOException e) - { - Game.logger.println (new Date().toString() + " (Error) File permissions are broken! Cannot initialize tank registry."); + } catch (IOException e) { + Game.logger.println( + new Date().toString() + " (syserr) file permissions are screwed/broken! cannot initialize tank registry file."); System.exit(1); } - try - { - PrintStream writer = new PrintStream (new File (path)); + try { + PrintStream writer = new PrintStream(new File(path)); writer.println("# This is the Tank Registry file!"); writer.println("# A registry entry is a line in the file"); writer.println("# The parameters are name, rarity, custom/default, jar location, and class"); @@ -104,122 +90,104 @@ public static void initRegistry(String homedir) writer.println("# put 'custom' for parameter 3"); writer.println("# and put its absolute file path as parameter 4 in this file."); writer.println("# Then, put a comma and write the Class name with package and all as parameter 5."); - writer.println("# Example custom tank entry: 'mytank,1,custom,C:\\Users\\potato\\.tanks.d\\MyTank.jar,com.potato.MyTank'"); + writer.println( + "# Example custom tank entry: 'mytank,1,custom,C:\\Users\\potato\\.tanks.d\\MyTank.jar,com.potato.MyTank'"); writer.println("# Don't leave any blank lines!"); - - for (int i = 0; i < Game.defaultTanks.size(); i++) - { + + for (int i = 0; i < Game.defaultTanks.size(); i++) { writer.println(Game.defaultTanks.get(i).getString()); } - } - catch (Exception e) - { - Game.logger.println(new Date().toString() + " (Error) Something broke! Cannot initialize tank registry:"); + } catch (Exception e) { + Game.logger.println(new Date().toString() + " (syserr) something broke! cannot initialize tank registry. below is stack trace."); e.printStackTrace(Game.logger); System.exit(1); } - + } - - static class TankEntry - { + + static class TankEntry { public final Class tank; public final String name; public final double weight; - + protected double startWeight; protected double endWeight; - - public TankEntry(Registry r, Class tank, String name, double weight) - { + + public TankEntry(Registry r, Class tank, String name, double weight) { this.tank = tank; this.name = name; this.weight = weight; - + this.startWeight = r.maxTankWeight; r.maxTankWeight += weight; this.endWeight = r.maxTankWeight; - + r.tankRegistries.add(this); } - - public Tank getTank(double x, double y, double a) - { - try - { + + public Tank getTank(double x, double y, double a) { + try { return tank.getConstructor(double.class, double.class, double.class).newInstance(x, y, a); - } - catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) - { + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { e.printStackTrace(); return null; } } } - - static class DefaultTankEntry - { + + static class DefaultTankEntry { public final Class tank; public final String name; public final double weight; - + protected double startWeight; protected double endWeight; - - public DefaultTankEntry(Class tank, String name, double weight) - { + + public DefaultTankEntry(Class tank, String name, double weight) { this.tank = tank; this.name = name; this.weight = weight; } - - public TankEntry registerEntry(Registry r) - { + + public TankEntry registerEntry(Registry r) { return new TankEntry(r, this.tank, this.name, this.weight); } - - public TankEntry registerEntry(Registry r, double weight) - { + + public TankEntry registerEntry(Registry r, double weight) { return new TankEntry(r, this.tank, this.name, weight); } - - public String getString() - { + + public String getString() { return this.name + "," + this.weight + ",default"; } } - - public TankEntry getRandomTank() - { + + public TankEntry getRandomTank() { if (this.tankRegistries.size() <= 0) - throw new RuntimeException("The tank registry file is empty. Please register some tanks!"); - + throw new RuntimeException("the tank registry file is empty. please register some tanks."); + double random = Math.random() * maxTankWeight; - for (int i = 0; i < tankRegistries.size(); i++) - { + for (int i = 0; i < tankRegistries.size(); i++) { TankEntry r = tankRegistries.get(i); - if (random >= r.startWeight && random < r.endWeight) - { + if (random >= r.startWeight && random < r.endWeight) { return r; } } - + return null; } - - public TankEntry getRegistry(String name) - { - for (int i = 0; i < tankRegistries.size(); i++) - { + + public TankEntry getRegistry(String name) { + for (int i = 0; i < tankRegistries.size(); i++) { TankEntry r = tankRegistries.get(i); - - if (r.name.equals(name)) - { + + if (r.name.equals(name)) { return r; } } - + return null; } }