diff --git a/src/tanks/Bullet.java b/src/tanks/Bullet.java index b77d01fb..1ae16457 100644 --- a/src/tanks/Bullet.java +++ b/src/tanks/Bullet.java @@ -103,10 +103,10 @@ else if (dy >= 0 && dy < bound && horizontalDist < verticalDist) } - if (this.posX + this.size/2 > Screen.sizeX) + if (this.posX + this.size/2 > Window.sizeX) { collided = true; - this.posX = Screen.sizeX - this.size/2 - (this.posX + this.size/2 - Screen.sizeX); + this.posX = Window.sizeX - this.size/2 - (this.posX + this.size/2 - Window.sizeX); this.vX = -Math.abs(this.vX); } if (this.posX - this.size/2 < 0) @@ -115,10 +115,10 @@ else if (dy >= 0 && dy < bound && horizontalDist < verticalDist) this.posX = this.size/2 - (this.posX - this.size / 2); this.vX = Math.abs(this.vX); } - if (this.posY + this.size/2 > Screen.sizeY) + if (this.posY + this.size/2 > Window.sizeY) { collided = true; - this.posY = Screen.sizeY - this.size/2 - (this.posY + this.size/2 - Screen.sizeY); + this.posY = Window.sizeY - this.size/2 - (this.posY + this.size/2 - Window.sizeY); this.vY = -Math.abs(this.vY); } if (this.posY - this.size/2 < 0) @@ -266,7 +266,7 @@ public void draw(Graphics p) { double opacity = ((60 - destroyTimer) / 60.0); p.setColor(new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), (int)(opacity * opacity * opacity * 255.0))); - Screen.fillOval(p, posX, posY, size + destroyTimer * (size / Bullet.bullet_size), size + destroyTimer * (size / Bullet.bullet_size)); + Window.fillOval(p, posX, posY, size + destroyTimer * (size / Bullet.bullet_size), size + destroyTimer * (size / Bullet.bullet_size)); } } diff --git a/src/tanks/Button.java b/src/tanks/Button.java index e7858daf..b976b53d 100644 --- a/src/tanks/Button.java +++ b/src/tanks/Button.java @@ -39,16 +39,37 @@ public Button(double sX, double sY, String text, Runnable f, String hoverText) this.hoverText = hoverText.split("---"); } - public void drawUpdate(Graphics g, int x, int y) + public void draw(Graphics g, int x, int y) { this.posX = x; this.posY = y; - g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (24 * Screen.scale))); - - double mx = Game.gamescreen.getMouseX(); - double my = Game.gamescreen.getMouseY(); + g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (24 * Window.scale))); + + if (selected) + g.setColor(this.selectedCol); + else + g.setColor(this.unselectedCol); + + Window.fillRect(g, posX, posY, sizeX, sizeY); + + g.setColor(Color.black); + Window.drawText(g, posX, posY + 5, text); + + if (selected && enableHover) + { + Window.drawTooltip(g, this.hoverText); + } + } + + public void update(int x, int y) + { + this.posX = x; + this.posY = y; + double mx = Game.window.getMouseX(); + double my = Game.window.getMouseY(); + if (mx > posX - sizeX/2 && mx < posX + sizeX/2 && my > posY - sizeY/2 && my < posY + sizeY/2) selected = true; else @@ -63,20 +84,5 @@ public void drawUpdate(Graphics g, int x, int y) if (!(selected && MouseInputListener.lClick)) clicked = false; - - if (selected) - g.setColor(selectedCol); - else - g.setColor(unselectedCol); - - Screen.fillRect(g, posX, posY, sizeX, sizeY); - - g.setColor(Color.black); - Screen.drawText(g, posX, posY + 5, text); - - if (selected && enableHover) - { - Screen.drawTooltip(g, this.hoverText); - } } } diff --git a/src/tanks/Effect.java b/src/tanks/Effect.java index 8bb838d3..3b1dede5 100644 --- a/src/tanks/Effect.java +++ b/src/tanks/Effect.java @@ -31,7 +31,8 @@ public Effect(double x, double y, EffectType type) @Override public void checkCollision() {} - public void drawWithoutUpdate(Graphics p) + @Override + public void draw(Graphics p) { //p.setColor(Color.red); //Screen.fillRect(p, this.posX, this.posY, 4, 4); @@ -53,7 +54,7 @@ public void drawWithoutUpdate(Graphics p) Color col = new Color(255, green, 0, Math.min(255, Math.max(0, (int) (opacity * opacityMultiplier * ffOpacityMultiplier)))); p.setColor(col); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.smokeTrail) @@ -74,7 +75,7 @@ else if (this.type == EffectType.smokeTrail) } p.setColor(col); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.trail) { @@ -91,13 +92,13 @@ else if (this.type == EffectType.trail) Color col = new Color(127, 127, 127, Math.min(255, Math.max(0, (int) (opacity * opacityMultiplier * ffOpacityMultiplier)))); p.setColor(col); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.ray) { int size = 6; p.setColor(new Color(0, 0, 0, 50)); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); Game.removeEffects.add(this); } @@ -112,7 +113,7 @@ else if (this.type == EffectType.mineExplosion) int size = Game.tank_size * 4; int opacity = (int) (100 - this.age * 5); p.setColor(new Color(255, 0, 0, opacity)); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.laser) { @@ -126,7 +127,7 @@ else if (this.type == EffectType.laser) //double size = (int) (255 - this.age * 12); double size = Bullet.bullet_size - this.age / 2; p.setColor(new Color(255, 0, 0)); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.piece) { @@ -138,7 +139,7 @@ else if (this.type == EffectType.piece) int size = 1 + (int) (Bullet.bullet_size * (1 - this.age / this.maxAge)); p.setColor(col); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.obstaclePiece) { @@ -150,7 +151,7 @@ else if (this.type == EffectType.obstaclePiece) int size = 1 + (int) (Bullet.bullet_size * (1 - this.age / this.maxAge)); p.setColor(col); - Screen.fillRect(p, this.posX, this.posY, size, size); + Window.fillRect(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.charge) { @@ -162,7 +163,7 @@ else if (this.type == EffectType.charge) int size = 1 + (int) (Bullet.bullet_size * (this.age / this.maxAge)); p.setColor(col); - Screen.fillOval(p, this.posX, this.posY, size, size); + Window.fillOval(p, this.posX, this.posY, size, size); } else if (this.type == EffectType.tread) { @@ -183,14 +184,13 @@ else if (this.type == EffectType.tread) int opacity = (int) (255 - this.age / opacityFactor) / 4; p.setColor(new Color(0, 0, 0, opacity)); - Screen.fillRect(p, this.posX, this.posY, size * Obstacle.draw_size / Obstacle.obstacle_size, size * Obstacle.draw_size / Obstacle.obstacle_size); + Window.fillRect(p, this.posX, this.posY, size * Obstacle.draw_size / Obstacle.obstacle_size, size * Obstacle.draw_size / Obstacle.obstacle_size); } } @Override - public void draw(Graphics p) + public void update() { - this.drawWithoutUpdate(p); this.posX += this.vX * Panel.frameFrequency; this.posY += this.vY * Panel.frameFrequency; this.age += Panel.frameFrequency; diff --git a/src/tanks/EnemyTankWhite.java b/src/tanks/EnemyTankWhite.java index 4f0fb65c..96fbb392 100644 --- a/src/tanks/EnemyTankWhite.java +++ b/src/tanks/EnemyTankWhite.java @@ -40,7 +40,7 @@ public void draw(Graphics g) for (int i = 0; i < Game.tank_size * 2 - this.age; i++) { g.setColor(new Color(255, 255, 255, (int)((Game.tank_size * 2 - i - this.age) * 2.55))); - Screen.fillOval(g, this.posX, this.posY, i, i); + Window.fillOval(g, this.posX, this.posY, i, i); } if (this.drawTread) diff --git a/src/tanks/Firework.java b/src/tanks/Firework.java index 89d092a0..8351cc3d 100644 --- a/src/tanks/Firework.java +++ b/src/tanks/Firework.java @@ -85,7 +85,7 @@ public void drawUpdate(Graphics g) this.vY += 0.0625 * Panel.frameFrequency; g.setColor(this.color); - Screen.fillOval(g, posX, posY, this.size, this.size); + Window.fillOval(g, posX, posY, this.size, this.size); Firework f = new Firework(FireworkType.trail, this.posX, this.posY, this.list, this.removeList); f.maxAge = 30; @@ -115,7 +115,7 @@ public void drawUpdate(Graphics g) else if (type == FireworkType.trail) { g.setColor(new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), Math.max(0, Math.min(255, 255 - (int) (this.age * 255.0 / this.maxAge))))); - Screen.fillOval(g, posX, posY, this.size, this.size); + Window.fillOval(g, posX, posY, this.size, this.size); if (this.age >= this.maxAge) { @@ -128,7 +128,7 @@ else if (type == FireworkType.particle) int opacity = Math.min(255, Math.max(0, (int) (255 - this.age * 255.0 / this.maxAge))); g.setColor(new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), opacity)); - Screen.fillOval(g, posX, posY, this.size, this.size); + Window.fillOval(g, posX, posY, this.size, this.size); /*Firework f = new Firework(FireworkType.trail, this.posX, this.posY, this.list, this.removeList); f.maxAge = opacity / 50; diff --git a/src/tanks/Flame.java b/src/tanks/Flame.java index 233d5f4a..3f04921d 100644 --- a/src/tanks/Flame.java +++ b/src/tanks/Flame.java @@ -41,7 +41,7 @@ public void draw(Graphics g) Color col = new Color(255, green, 0, opacity); g.setColor(col); - Screen.fillOval(g, this.posX, this.posY, size, size); + Window.fillOval(g, this.posX, this.posY, size, size); } } diff --git a/src/tanks/Game.java b/src/tanks/Game.java index 7935be7d..2b2bacfc 100644 --- a/src/tanks/Game.java +++ b/src/tanks/Game.java @@ -14,9 +14,10 @@ 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(); @@ -29,27 +30,23 @@ 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 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 paused = true; + + public static Screen screen = new ScreenTitle(); + + //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; @@ -57,26 +54,24 @@ public enum Menu { 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 Window window; + + static String currentLevel = ""; + public static PrintStream logger = System.err; - + public static final String directoryPath = "/.tanks.d"; - 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 final String logPath = directoryPath + "/logfile.txt"; + public static final String registryPath = directoryPath + "/tank-registry.txt"; 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)); @@ -90,65 +85,81 @@ 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)); + + window = new Window(); + window.setTitle("Tanks"); + window.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 + //add things to movables - Panel screen = new Panel(); + Panel screen = new Panel(); - gamescreen.add(screen); - screen.startTimer(); + window.add(screen); + screen.startTimer(); + } } - }); + ); } - - public static void reset() { + + public static void reset() + { obstacles.clear(); belowEffects.clear(); movables.clear(); @@ -156,59 +167,63 @@ public static void reset() { System.gc(); start(); } - - public static void exit() { - Game.paused = true; - menu = Menu.interlevel; + + public static void exit() + { + screen = new ScreenInterlevel(); obstacles.clear(); belowEffects.clear(); movables.clear(); effects.clear(); System.gc(); } - - public static void exitToCrash() { - Game.paused = true; + + public static void exitToCrash() + { obstacles.clear(); belowEffects.clear(); movables.clear(); effects.clear(); System.gc(); - menu = Menu.crashed; + screen = new ScreenCrashed(); } - - 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); - Game.gamescreen.setScreenBounds(Game.tank_size * 28, Game.tank_size * 18); - Game.paused = true; - menu = Menu.title; + Game.window.setScreenBounds(Game.tank_size * 28, Game.tank_size * 18); + screen = new ScreenTitle(); obstacles.clear(); belowEffects.clear(); movables.clear(); 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()); + + Registry.loadRegistry(homedir); + 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 b2b07b27..911d37d9 100644 --- a/src/tanks/Level.java +++ b/src/tanks/Level.java @@ -2,105 +2,111 @@ 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() { - Registry.loadRegistry(Game.homedir); - + + public void loadLevel() + { 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++) { + + Game.window.setScreenBounds(Game.tank_size * sX, Game.tank_size * sY); + + 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])); @@ -108,15 +114,18 @@ 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/LevelBuilder.java b/src/tanks/LevelBuilder.java new file mode 100644 index 00000000..fc66f22a --- /dev/null +++ b/src/tanks/LevelBuilder.java @@ -0,0 +1,6 @@ +package tanks; + +public class LevelBuilder +{ + +} diff --git a/src/tanks/Mine.java b/src/tanks/Mine.java index c8f5ea5a..5ad36227 100644 --- a/src/tanks/Mine.java +++ b/src/tanks/Mine.java @@ -34,7 +34,7 @@ public void draw(Graphics p) if (timer < 150 && ((int) timer % 16) / 8 == 1) p.setColor(Color.yellow); - Screen.fillOval(p, this.posX, this.posY, s, s); + Window.fillOval(p, this.posX, this.posY, s, s); } @Override diff --git a/src/tanks/Obstacle.java b/src/tanks/Obstacle.java index 215cfddf..69cd8f0e 100644 --- a/src/tanks/Obstacle.java +++ b/src/tanks/Obstacle.java @@ -23,7 +23,7 @@ public void draw(Graphics g) g.setColor(color); //if (Screen.scale * 10 == Math.round(Screen.scale * 10)) - Screen.fillRect(g, this.posX, this.posY, draw_size, draw_size); + Window.fillRect(g, this.posX, this.posY, draw_size, draw_size); //else // Screen.fillRect(g, this.posX - 1, this.posY - 1, draw_size + 2, draw_size + 2); } diff --git a/src/tanks/Panel.java b/src/tanks/Panel.java index 1a6b57a8..2c7de283 100644 --- a/src/tanks/Panel.java +++ b/src/tanks/Panel.java @@ -12,25 +12,23 @@ 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; + int height = Window.sizeY; + int width = Window.sizeX; boolean resize = true; - boolean showMouseTarget = true; + static boolean showMouseTarget = true; ArrayList framesList = new ArrayList(); - String winlose = ""; - boolean win = false; + public static String winlose = ""; + public static boolean win = false; - double darkness = 0; + public static 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(); @@ -46,289 +44,86 @@ public class Panel extends JPanel { long startTime = System.currentTimeMillis(); - int lastFPS = (int) (100 / frameFrequency); + int lastFPS = 0; - ArrayList fireworks = new ArrayList(); - ArrayList removeFireworks = new ArrayList(); + public static boolean pausePressed = false; - Button resume = new Button(350, 40, "Continue playing", new Runnable() { - @Override - 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() { - @Override - public void run() { - Game.reset(); - Game.menu = Game.Menu.none; - Game.paused = false; - } - }); - - Button graphics = new Button(350, 40, "Graphics: fancy", new Runnable() { - @Override - public void run() { - Game.graphicalEffects = !Game.graphicalEffects; - - if (Game.graphicalEffects) - graphics.text = "Graphics: fancy"; - else - graphics.text = "Graphics: fast"; - } - }); - - Button mouseTarget = new Button(350, 40, "Mouse target: enabled", new Runnable() { - @Override - public void run() { - showMouseTarget = !showMouseTarget; - - if (showMouseTarget) - mouseTarget.text = "Mouse target: enabled"; - else - mouseTarget.text = "Mouse target: disabled"; - } - }); - - Button scale = new Button(350, 40, "Scale: 100%", new Runnable() { - @Override - public void run() { - if (KeyInputListener.keys.contains(KeyEvent.VK_SHIFT)) - Screen.scale -= 0.1; - else - Screen.scale += 0.1; - - if (Screen.scale < 0.45) - Screen.scale = 2; - - if (Screen.scale > 2.05) - Screen.scale = 0.5; - - 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)); - } - }, "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() { - @Override - public void run() { - Game.exitToTitle(); - // System.exit(0); - } - }); - - Button back = new Button(350, 40, "Back", new Runnable() { - @Override - public void run() { - Game.menu = Game.Menu.title; - } - }); - - Button exit = new Button(350, 40, "Exit the game", new Runnable() { - @Override - public void run() { - System.exit(0); - } - }); - - Button insanity = new Button(350, 40, "Insanity mode: disabled", new Runnable() { - @Override - public void run() { - Game.insanity = !Game.insanity; - - if (Game.insanity) - insanity.text = "Insanity mode: enabled"; - else - insanity.text = "Insanity mode: disabled"; - } - }); - - Button options = new Button(350, 40, "Options...", new Runnable() { - @Override - public void run() { - Game.menu = Game.Menu.options; - } - }); - - Button replay = new Button(350, 40, "Replay the level", new Runnable() { - @Override - public void run() { - Level level = new Level(Game.currentLevel); - level.loadLevel(); - Game.menu = Game.Menu.none; - Game.paused = false; - } - }); + public static double preGameTimer = 0; - 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 (Game.menu == Game.Menu.paused || Game.menu == Game.Menu.none) { - if (!pausePressed) - Game.paused = !Game.paused; - - if (Game.paused) - Game.menu = Game.Menu.paused; - else - Game.menu = Game.Menu.none; - } - - pausePressed = true; - } else - pausePressed = false; - - if (!Game.paused) { - if (preGameTimer > 0) { - preGameTimer -= frameFrequency; - if (Game.movables.contains(Game.player)) { - Obstacle.draw_size = Math.min(Game.tank_size, Obstacle.draw_size + frameFrequency); - } - } else { - Obstacle.draw_size = Math.min(Obstacle.obstacle_size, Obstacle.draw_size); - int tanks = 0; - 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++) { - Movable mo = Game.movables.get(m); - if (mo instanceof Bullet || mo instanceof Mine) - mo.destroy = true; - } - - 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) { - winlose = "You were destroyed!"; - win = false; - Game.exit(); - } - } - - } - - /* - * 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) { - Game.bulletLocked = true; - 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) { - Obstacle.draw_size = Math.max(0, Obstacle.draw_size - Panel.frameFrequency); - - if (Obstacle.draw_size <= 0) { - winlose = "Level Cleared!"; - win = true; - Game.exit(); - } - } - } else - Game.bulletLocked = false; - } - - for (int i = 0; i < Game.removeMovables.size(); i++) - Game.movables.remove(Game.removeMovables.get(i)); - - for (int i = 0; i < Game.removeObstacles.size(); i++) - Game.obstacles.remove(Game.removeObstacles.get(i)); - - for (int i = 0; i < Game.removeEffects.size(); i++) - Game.effects.remove(Game.removeEffects.get(i)); - - for (int i = 0; i < Game.removeBelowEffects.size(); i++) - Game.belowEffects.remove(Game.removeBelowEffects.get(i)); - - Game.removeMovables.clear(); - Game.removeObstacles.clear(); - Game.removeEffects.clear(); - Game.removeBelowEffects.clear(); - } + Game.screen.update(); + + 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() + " (syserr) something broke. below is the stack trace for what broke. good luck."); + Game.logger.println(new Date().toString() + " (syserr) the game has crashed! below is a crash report, good luck:"); exception.printStackTrace(Game.logger); Game.crashMessage = e.toString(); } @@ -337,260 +132,134 @@ 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); + Window.fillRect(g, Window.sizeX / 2, Window.sizeY / 2, Window.sizeX * 1.2, Window.sizeY * 1.2); + g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("loading.png")), 0, 0, null); } return; } + + Window.scale = Math.min(Game.window.getSize().getWidth() * 1.0 / Game.currentSizeX, (Game.window.getSize().getHeight() * 1.0 - 40 - Window.yOffset) / Game.currentSizeY) / 50.0; - 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); - - g.setColor(Color.white); - g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (100 * Screen.scale))); - Screen.drawText(g, 100, 100, ":("); - - g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (48 * Screen.scale))); - Screen.drawText(g, Screen.sizeX / 2, 100, "Oh noes! Tanks ran into a problem!"); - g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (24 * Screen.scale))); - Screen.drawText(g, Screen.sizeX / 2, 200, Game.crashMessage); - Screen.drawText(g, Screen.sizeX / 2, 280, "Check the log file for more information: "); - Screen.drawText(g, Screen.sizeX / 2, 320, Game.homedir.replace("\\", "/") + Game.logPath); - - 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 to definitelygoingtoreplytoyouremail@hotmail.com!"); - - this.quit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY - 160); - this.exit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY - 100); - - 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.window.getSize().getWidth()), 1+(int)(Game.window.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(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++) { - int extra; - if (Screen.scale * 10 == Math.round(Screen.scale * 10)) - extra = 0; - else - 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); - } - } - } - - if (!Game.paused) { - for (int i = 0; i < Game.belowEffects.size(); i++) - Game.belowEffects.get(i).draw(g); - } else { - for (int i = 0; i < Game.belowEffects.size(); i++) - ((Effect) Game.belowEffects.get(i)).drawWithoutUpdate(g); - } - - for (int n = 0; n < Game.movables.size(); n++) - Game.movables.get(n).draw(g); - - for (int i = 0; i < Game.obstacles.size(); i++) - Game.obstacles.get(i).draw(g); - - if (!Game.paused) { - for (int i = 0; i < Game.effects.size(); i++) - Game.effects.get(i).draw(g); - } else { - for (int i = 0; i < Game.effects.size(); i++) - ((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; - - // System.out.println(Game.gamescreen.getSize().getWidth() * 1.0 / - // Game.currentSizeX + " " + (Game.gamescreen.getSize().getHeight() * 1.0 - 40 - - // Screen.offset) / Game.currentSizeY); + //g.setColor(new Color(255, 227, 186)); + //g.fillRect(0, 0, (int) (Screen.sizeX * Screen.scale), (int) (Screen.sizeY * Screen.scale)); - // System.out.println(Screen.scale); + Game.screen.draw(g); - 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); + g.setColor(new Color(87, 46, 8)); + g.fillRect(0, (int) (Game.window.getSize().getHeight() - 40 - Window.yOffset), (int) (Game.window.getSize().getWidth()), 40); - 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))); - Screen.drawText(g, Screen.sizeX / 2, Screen.sizeY / 2 - 200, "Tanks"); - 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)) { - g.setColor(new Color(127, 178, 228, 64)); - 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)) { - 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); - f.setRandomColor(); - f.vY = -Math.random() * 3 - 6; - f.vX = Math.random() * 5 - 2.5; - fireworks.add(f); - } + g.setColor(new Color(255, 227, 186)); - for (int i = 0; i < fireworks.size(); i++) { - fireworks.get(i).drawUpdate(g); - } + g.setFont(g.getFont().deriveFont(Font.BOLD, 12)); - for (int i = 0; i < removeFireworks.size(); i++) { - fireworks.remove(removeFireworks.get(i)); - } - } + g.drawString("Tanks v0.3.6", 2, (int) (Game.window.getSize().getHeight() - 40 + 12 - Window.yOffset)); + g.drawString("FPS: " + lastFPS, 2, (int) (Game.window.getSize().getHeight() - 40 + 24 - Window.yOffset)); + g.drawString("Coins: " + Game.coins, 2, (int) (Game.window.getSize().getHeight() - 40 + 36 - Window.yOffset)); - newLevel.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 - 60); - replay.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2); - quit.drawUpdate(g, Screen.sizeX / 2, Screen.sizeY / 2 + 60); - if (win && Game.graphicalEffects) - g.setColor(Color.white); - Screen.drawText(g, Screen.sizeX / 2, Screen.sizeY / 2 - 150, winlose); - } + /*int obstacles = Game.obstacles.size(); + int movables = Game.movables.size(); + int effects = Game.effects.size(); - g.setColor(new Color(87, 46, 8)); - g.fillRect(0, (int) (Game.gamescreen.getSize().getHeight() - 40 - Screen.yOffset), - (int) (Game.gamescreen.getSize().getWidth()), 40); + int drawHeight = 23; + int drawSize = 10;*/ - g.setColor(new Color(255, 227, 186)); + /*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);*/ - g.setFont(g.getFont().deriveFont(Font.BOLD, 12)); + /*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; - 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) { + 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); + + double mx = Game.window.getMouseX(); + double my = Game.window.getMouseY(); + + if (showMouseTarget) + { g.setColor(Color.black); - Screen.drawOval(g, mx, my, 8, 8); - Screen.drawOval(g, mx, my, 4, 4); + Window.drawOval(g, mx, my, 8, 8); + Window.drawOval(g, mx, my, 4, 4); } - } catch (Exception e) { + } + catch (Exception e) + { Game.exitToCrash(); - Game.logger.println(new Date().toString() + " (syserr) something broke. below is the stack trace for what broke. good luck."); + Game.logger.println(new Date().toString() + " (syserr) the game has crashed! below is a crash report, good luck:"); e.printStackTrace(Game.logger); Game.crashMessage = e.toString(); } diff --git a/src/tanks/PlayerTank.java b/src/tanks/PlayerTank.java index 8566847c..df149c2e 100644 --- a/src/tanks/PlayerTank.java +++ b/src/tanks/PlayerTank.java @@ -78,7 +78,7 @@ else if (this.vY < 0) if (mine && this.cooldown <= 0 && this.liveMines < this.liveMinesMax) this.layMine(); - this.angle = this.getAngleInDirection(Screen.screen.getMouseX(), Screen.screen.getMouseY()); + this.angle = this.getAngleInDirection(Window.window.getMouseX(), Window.window.getMouseY()); super.update(); @@ -110,7 +110,7 @@ public void shoot() public void fireBullet(double speed, int bounces, Color color, Bullet.BulletEffect effect) { Bullet b = new Bullet(posX, posY, color, bounces, this); - b.setMotionInDirection(Screen.screen.getMouseX(), Screen.screen.getMouseY(), speed); + b.setMotionInDirection(Window.window.getMouseX(), Window.window.getMouseY(), speed); this.addPolarMotion(b.getPolarDirection() + Math.PI, 25.0 / 16.0); b.moveOut((int) (25.0 / speed * 2)); diff --git a/src/tanks/Ray.java b/src/tanks/Ray.java index 2fe4c19e..620d3b7b 100644 --- a/src/tanks/Ray.java +++ b/src/tanks/Ray.java @@ -122,10 +122,10 @@ else if (dy >= 0 && dy < bound && horizontalDist < verticalDist) } - if (this.posX + this.size/2 > Screen.sizeX) + if (this.posX + this.size/2 > Window.sizeX) { collided = true; - this.posX = Screen.sizeX - this.size/2 - (this.posX + this.size/2 - Screen.sizeX); + this.posX = Window.sizeX - this.size/2 - (this.posX + this.size/2 - Window.sizeX); this.vX = -Math.abs(this.vX); } if (this.posX - this.size/2 < 0) @@ -134,10 +134,10 @@ else if (dy >= 0 && dy < bound && horizontalDist < verticalDist) this.posX = this.size/2 - (this.posX - this.size / 2); this.vX = Math.abs(this.vX); } - if (this.posY + this.size/2 > Screen.sizeY) + if (this.posY + this.size/2 > Window.sizeY) { collided = true; - this.posY = Screen.sizeY - this.size/2 - (this.posY + this.size/2 - Screen.sizeY); + this.posY = Window.sizeY - this.size/2 - (this.posY + this.size/2 - Window.sizeY); this.vY = -Math.abs(this.vY); } if (this.posY - this.size/2 < 0) @@ -236,7 +236,7 @@ public int getDist() } - if (this.posX + this.size/2 > Screen.sizeX) + if (this.posX + this.size/2 > Window.sizeX) { return this.age; } @@ -244,7 +244,7 @@ else if (this.posX - this.size/2 < 0) { return this.age; } - else if (this.posY + this.size/2 > Screen.sizeY) + else if (this.posY + this.size/2 > Window.sizeY) { return this.age; } diff --git a/src/tanks/Registry.java b/src/tanks/Registry.java index e3985552..1952f007 100644 --- a/src/tanks/Registry.java +++ b/src/tanks/Registry.java @@ -10,75 +10,92 @@ 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() + " (syswarn) The default tank '" + tankLine[0] - + "' does not exist or cannot be loaded. ignoring."); - } else { - try { + Game.logger.println (new Date().toString() + " (syswarn) the default tank '" + tankLine[0] + "' does not exist!"); + } + 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() + " (syswarn) error loading custom tank '" - + tankLine[0] + "'. ignoring."); + Game.logger.println(new Date().toString() + " (syswarn) error loading custom tank '" + tankLine[0] + "'. try adding the path to your jvm classpath. ignoring."); } } } in.close(); - } catch (Exception e) { - Game.logger.println( - new Date().toString() + " (syswarn) tank registry file is nonexistent or broken, using default. below is stack trace."); + } + catch (Exception e) + { + Game.logger.println (new Date().toString() + " (syswarn) tank registry file is nonexistent or broken, using default:"); 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() + " (syserr) file permissions are screwed/broken! cannot initialize tank registry file."); + } + catch (IOException e) + { + Game.logger.println (new Date().toString() + " (syserr) file permissions are broken! cannot initialize tank registry."); 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"); @@ -90,104 +107,122 @@ 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() + " (syserr) something broke! cannot initialize tank registry. below is stack trace."); + } + catch (Exception e) + { + Game.logger.println(new Date().toString() + " (syserr) something broke! could not initialize tank registry:"); 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; } } diff --git a/src/tanks/Screen.java b/src/tanks/Screen.java index 36822c84..e066970e 100644 --- a/src/tanks/Screen.java +++ b/src/tanks/Screen.java @@ -1,182 +1,41 @@ -package tanks; -import java.awt.Color; -import java.awt.Container; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.MouseInfo; -import java.awt.font.FontRenderContext; - -import javax.swing.*; - -@SuppressWarnings("serial") -public class Screen extends JFrame -{ - public static int sizeX = 1400;//1920; - public static int sizeY = 900;//1100; - - public static double scale = 1; - - public Panel panel = new Panel(); - - public static Screen screen; - - public static int yOffset = 55; - public static int xOffset = 22; - public static int mouseXoffset = 0; - public static int mouseYoffset = -26; - - static FontRenderContext frc = new FontRenderContext(null, true, true); - - public Screen() - { - this.addMouseListener(new MouseInputListener()); - this.addKeyListener(new KeyInputListener()); - this.setSize((int)(sizeX * scale), (int) ((sizeY + yOffset) * scale )); - this.setVisible(true); - Container visiblePart = this.getContentPane(); - visiblePart.add(panel); - this.setResizable(true); - this.setMinimumSize(new Dimension(350, 265 + yOffset)); - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - screen = this; - - if (System.getProperties().get("os.name").toString().toLowerCase().contains("mac")) - { - Screen.yOffset = 22; - Screen.xOffset = 0; - Screen.mouseYoffset = -2; - } - } - - @Override - public void setSize(int x, int y) - { - super.setSize(x + xOffset, (int) (y + 40 + yOffset * (1 - scale))); - } - - @SuppressWarnings("static-access") - public int getSizeX() - { - return (int) (this.WIDTH * scale); - } - - @SuppressWarnings("static-access") - public int getSizeY() - { - return (int) (this.HEIGHT * scale); - } - - public static void fillOval(Graphics g, double x, double y, double sizeX, double sizeY) - { - int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.gamescreen.getSize().getWidth() - Screen.xOffset - Screen.sizeX * Screen.scale) / 2); - int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.gamescreen.getSize().getHeight() - Screen.yOffset - 40 - Screen.sizeY * Screen.scale) / 2); - int drawSizeX = (int) Math.round(sizeX * scale); - int drawSizeY = (int) Math.round(sizeY * scale); - - g.fillOval(drawX, drawY, drawSizeX, drawSizeY); - } - - public static void drawOval(Graphics g, double x, double y, double sizeX, double sizeY) - { - int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.gamescreen.getSize().getWidth() - Screen.xOffset - Screen.sizeX * Screen.scale) / 2); - int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.gamescreen.getSize().getHeight() - Screen.yOffset - 40 - Screen.sizeY * Screen.scale) / 2); - int drawSizeX = (int) Math.round(sizeX * scale); - int drawSizeY = (int) Math.round(sizeY * scale); - - g.drawOval(drawX, drawY, drawSizeX, drawSizeY); - } - - public static void fillRect(Graphics g, double x, double y, double sizeX, double sizeY) - { - int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.gamescreen.getSize().getWidth() - Screen.xOffset - Screen.sizeX * Screen.scale) / 2); - int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.gamescreen.getSize().getHeight() - Screen.yOffset - 40 - Screen.sizeY * Screen.scale) / 2); - int drawSizeX = (int) Math.round(sizeX * scale); - int drawSizeY = (int) Math.round(sizeY * scale); - - g.fillRect(drawX, drawY, drawSizeX, drawSizeY); - } - - public static void drawRect(Graphics g, double x, double y, double sizeX, double sizeY) - { - int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.gamescreen.getSize().getWidth() - Screen.xOffset - Screen.sizeX * Screen.scale) / 2); - int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.gamescreen.getSize().getHeight() - Screen.yOffset - 40 - Screen.sizeY * Screen.scale) / 2); - int drawSizeX = (int) Math.round(sizeX * scale); - int drawSizeY = (int) Math.round(sizeY * scale); - - g.drawRect(drawX , drawY, drawSizeX, drawSizeY); - } - - public static void drawText(Graphics g, double x, double y, String text) - { - double sizeX = Math.round(g.getFont().getStringBounds(text, frc).getWidth()); - - double sizeY = g.getFont().getSize() / 3 / Screen.scale; - //int size = text.length() * g.getFont().getSize() / 2; - int drawX = (int) (scale * x - sizeX / 2 + Math.max(0, Game.gamescreen.getSize().getWidth() - Screen.xOffset - Screen.sizeX * Screen.scale) / 2); - int drawY = (int) (scale * (y + sizeY / 2) + Math.max(0, Game.gamescreen.getSize().getHeight() - Screen.yOffset - 40 - Screen.sizeY * Screen.scale) / 2); - g.drawString(text, drawX, drawY); - - //g.setColor(Color.red); - //g.drawRect((int)((x - sizeX / 2) * scale), (int)((y - sizeY * 3 / 2) * scale), (int)g.getFont().getStringBounds(text, frc).getWidth(), (int)g.getFont().getStringBounds(text, frc).getHeight()); - } - - public static double drawTooltip(Graphics g, String[] text) - { - double x = Game.gamescreen.getMouseX(); - double y = Game.gamescreen.getMouseY(); - - int xPadding = (int) (16 * Screen.scale); - int yPadding = (int) (8 * Screen.scale); - - g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (Screen.scale * 14))); - - int sizeX = 0; - for (int i = 0; i < text.length; i++) - { - sizeX = Math.max(sizeX, (int) Math.round(g.getFont().getStringBounds(text[i], frc).getWidth())); - } - - int sizeY = g.getFont().getSize(); - - //int size = text.length() * g.getFont().getSize() / 2; - int drawX = (int) (scale * (x)); - int drawY = (int) (scale * (y)); - - g.setColor(new Color(0, 0, 0, 127)); - g.fillRect(drawX, drawY, sizeX + xPadding * 2, sizeY + yPadding * 2 * text.length); - - g.setColor(Color.WHITE); - for (int i = 0; i < text.length; i++) - { - g.drawString(text[i], drawX + xPadding, (int) (drawY + (i + 1) * (sizeY / 2 + yPadding * (1 + 1 / 2.0)))); - } - - return (y - (drawY / Screen.scale + sizeY + yPadding / Screen.scale * 2)); - } - - public double getMouseX() - { - return (MouseInfo.getPointerInfo().getLocation().getX() - this.getLocation().getX() - Math.max(0, Game.gamescreen.getSize().getWidth() - Screen.sizeX * Screen.scale) / 2) / scale + mouseXoffset / Screen.scale; - } - - public double getMouseY() - { - return ((MouseInfo.getPointerInfo().getLocation().getY() - this.getLocation().getY() - Math.max(0, Game.gamescreen.getSize().getHeight() - (yOffset + 1) - Screen.sizeY * Screen.scale) / 2)) / scale + mouseYoffset / Screen.scale; - } - - public void setScreenSize(int x, int y) - { - sizeX = x; - sizeY = y; - this.setSize((int) (x * scale), (int) ((y + yOffset) * scale)); - } - - public void setScreenBounds(int x, int y) - { - sizeX = x; - sizeY = y; - Game.currentSizeX = x / Game.tank_size; - Game.currentSizeY = y / Game.tank_size; - } -} +package tanks; + +import java.awt.Color; +import java.awt.Graphics; + +public abstract class Screen +{ + public abstract void update(); + + public abstract void draw(Graphics g); + + public void drawDefaultBackground(Graphics g) + { + if (!(Game.screen instanceof ScreenInterlevel)) + Panel.darkness = Math.max(Panel.darkness - Panel.frameFrequency * 3, 0); + + g.setColor(Level.currentColor); + Window.fillRect(g, Window.sizeX / 2, Window.sizeY / 2, Window.sizeX, Window.sizeY); + + if (Game.graphicalEffects) + { + for (int i = 0; i < Game.currentSizeX; i++) + { + for (int j = 0; j < Game.currentSizeY; j++) + { + int extra; + if (Window.scale * 10 == Math.round(Window.scale * 10)) + extra = 0; + else + extra = 0; + + g.setColor(Game.tiles[i][j]); + Window.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); + } + } + + g.setColor(new Color(0, 0, 0, (int) Panel.darkness)); + Window.fillRect(g, Window.sizeX / 2, Window.sizeY / 2, Window.sizeX, Window.sizeY); + } + } +} diff --git a/src/tanks/ScreenCrashed.java b/src/tanks/ScreenCrashed.java new file mode 100644 index 00000000..d7279b8d --- /dev/null +++ b/src/tanks/ScreenCrashed.java @@ -0,0 +1,65 @@ +package tanks; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; + +public class ScreenCrashed extends Screen +{ + Button exit = new Button(350, 40, "Exit the game", new Runnable() + { + @Override + public void run() + { + System.exit(0); + } + } + ); + + Button quit = new Button(350, 40, "Quit to title", new Runnable() + { + @Override + public void run() + { + Game.exitToTitle(); + } + } + ); + + @Override + public void update() + { + this.quit.update(Window.sizeX / 2, Window.sizeY - 160); + this.exit.update(Window.sizeX / 2, Window.sizeY - 100); + } + + @Override + public void draw(Graphics g) + { + g.setColor(Color.blue); + Window.fillRect(g, Window.sizeX / 2, Window.sizeY / 2, Window.sizeX * 1.2, Window.sizeY * 1.2); + + g.setColor(Color.white); + g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (100 * Window.scale))); + Window.drawText(g, 100, 100, ":("); + + g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (48 * Window.scale))); + Window.drawText(g, Window.sizeX / 2, 100, "Oh noes! Tanks ran into a problem!"); + + g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (24 * Window.scale))); + Window.drawText(g, Window.sizeX / 2, 200, Game.crashMessage); + Window.drawText(g, Window.sizeX / 2, 280, "Check the log file for more information: "); + Window.drawText(g, Window.sizeX / 2, 320, Game.homedir.replace("\\", "/") + Game.logPath); + + Window.drawText(g, Window.sizeX / 2, 400, "You may return to the game if you wish,"); + Window.drawText(g, Window.sizeX / 2, 440, "but be warned that things may become unstable."); + Window.drawText(g, Window.sizeX / 2, 480, "If you see this screen again, restart the game."); + Window.drawText(g, Window.sizeX / 2, 520, "Also, you may want to report this crash!"); + + this.quit.draw(g, Window.sizeX / 2, Window.sizeY - 160); + this.exit.draw(g, Window.sizeX / 2, Window.sizeY - 100); + + return; + } + +} diff --git a/src/tanks/ScreenGame.java b/src/tanks/ScreenGame.java new file mode 100644 index 00000000..b016d353 --- /dev/null +++ b/src/tanks/ScreenGame.java @@ -0,0 +1,146 @@ +package tanks; + +import java.awt.Graphics; +import java.awt.event.KeyEvent; + +public class ScreenGame extends Screen +{ + @Override + public void update() + { + if (Panel.preGameTimer > 0) + { + Panel.preGameTimer -= Panel.frameFrequency; + if (Game.movables.contains(Game.player)) + { + Obstacle.draw_size = Math.min(Game.tank_size, Obstacle.draw_size + Panel.frameFrequency); + } + } + else + { + Obstacle.draw_size = Math.min(Obstacle.obstacle_size, Obstacle.draw_size); + int tanks = 0; + for (int i = 0; i < Game.movables.size(); i++) + { + Movable m = Game.movables.get(i); + m.update(); + if (m instanceof Tank) + tanks++; + } + + for (int i = 0; i < Game.effects.size(); i++) + { + Game.effects.get(i).update(); + } + + for (int i = 0; i < Game.belowEffects.size(); i++) + { + Game.belowEffects.get(i).update(); + } + + 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) + { + 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) + { + Panel.winlose = "You were destroyed!"; + Panel.win = false; + Game.exit(); + } + } + + } + + /*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) + { + Game.bulletLocked = true; + 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) + { + Obstacle.draw_size = Math.max(0, Obstacle.draw_size - Panel.frameFrequency); + + if (Obstacle.draw_size <= 0) + { + Panel.winlose = "Level Cleared!"; + Panel.win = true; + Game.exit(); + } + } + } + else + Game.bulletLocked = false; + } + + for (int i = 0; i < Game.removeMovables.size(); i++) + Game.movables.remove(Game.removeMovables.get(i)); + + for (int i = 0; i < Game.removeObstacles.size(); i++) + Game.obstacles.remove(Game.removeObstacles.get(i)); + + for (int i = 0; i < Game.removeEffects.size(); i++) + Game.effects.remove(Game.removeEffects.get(i)); + + for (int i = 0; i < Game.removeBelowEffects.size(); i++) + Game.belowEffects.remove(Game.removeBelowEffects.get(i)); + + Game.removeMovables.clear(); + Game.removeObstacles.clear(); + Game.removeEffects.clear(); + Game.removeBelowEffects.clear(); + + if (KeyInputListener.keys.contains(KeyEvent.VK_ESCAPE)) + { + if (!Panel.pausePressed) + Game.screen = new ScreenPaused(); + + Panel.pausePressed = true; + } + else + Panel.pausePressed = false; + + } + + @Override + public void draw(Graphics g) + { + this.drawDefaultBackground(g); + + for (int i = 0; i < Game.belowEffects.size(); i++) + Game.belowEffects.get(i).draw(g); + + for (int n = 0; n < Game.movables.size(); n++) + Game.movables.get(n).draw(g); + + for (int i = 0; i < Game.obstacles.size(); i++) + Game.obstacles.get(i).draw(g); + + for (int i = 0; i < Game.effects.size(); i++) + ((Effect)Game.effects.get(i)).draw(g); + + } + +} diff --git a/src/tanks/ScreenInterlevel.java b/src/tanks/ScreenInterlevel.java new file mode 100644 index 00000000..1c4d0143 --- /dev/null +++ b/src/tanks/ScreenInterlevel.java @@ -0,0 +1,92 @@ +package tanks; + +import java.awt.Color; +import java.awt.Graphics; +import java.util.ArrayList; + +public class ScreenInterlevel extends Screen +{ + ArrayList fireworks = new ArrayList(); + ArrayList removeFireworks = new ArrayList(); + + Button replay = new Button(350, 40, "Replay the level", new Runnable() + { + @Override + public void run() + { + Level level = new Level(Game.currentLevel); + level.loadLevel(); + Game.screen = new ScreenGame(); + } + } + ); + + Button newLevel = new Button(350, 40, "Generate a new level", new Runnable() + { + @Override + public void run() + { + Game.reset(); + Game.screen = new ScreenGame(); + } + } + ); + + Button quit = new Button(350, 40, "Quit to title", new Runnable() + { + @Override + public void run() + { + Game.exitToTitle(); + } + } + ); + + @Override + public void update() + { + newLevel.update(Window.sizeX / 2, Window.sizeY / 2 - 60); + replay.update(Window.sizeX / 2, Window.sizeY / 2); + quit.update(Window.sizeX / 2, Window.sizeY / 2 + 60); + } + + @Override + public void draw(Graphics g) + { + this.drawDefaultBackground(g); + + if (Panel.win && Game.graphicalEffects) + { + if (Math.random() < 0.01) + { + Firework f = new Firework(Firework.FireworkType.rocket, (Math.random() * 0.6 + 0.2) * Window.sizeX, Window.sizeY, fireworks, removeFireworks); + f.setRandomColor(); + f.vY = - Math.random() * 3 - 6; + f.vX = Math.random() * 5 - 2.5; + fireworks.add(f); + } + + for (int i = 0; i < fireworks.size(); i++) + { + fireworks.get(i).drawUpdate(g); + } + + for (int i = 0; i < removeFireworks.size(); i++) + { + fireworks.remove(removeFireworks.get(i)); + } + } + + newLevel.draw(g, Window.sizeX / 2, Window.sizeY / 2 - 60); + replay.draw(g, Window.sizeX / 2, Window.sizeY / 2); + quit.draw(g, Window.sizeX / 2, Window.sizeY / 2 + 60); + + if (Panel.win && Game.graphicalEffects) + g.setColor(Color.white); + Window.drawText(g, Window.sizeX / 2, Window.sizeY / 2 - 150, Panel.winlose); + + if (Panel.win && Game.graphicalEffects) + Panel.darkness = Math.min(Panel.darkness + Panel.frameFrequency * 1.5, 191); + } + +} diff --git a/src/tanks/ScreenOptions.java b/src/tanks/ScreenOptions.java new file mode 100644 index 00000000..001f0ecd --- /dev/null +++ b/src/tanks/ScreenOptions.java @@ -0,0 +1,108 @@ +package tanks; + +import java.awt.Graphics; +import java.awt.event.KeyEvent; + +public class ScreenOptions extends Screen +{ + Button graphics = new Button(350, 40, "Graphics: fancy", new Runnable() + { + @Override + public void run() + { + Game.graphicalEffects = !Game.graphicalEffects; + + if (Game.graphicalEffects) + graphics.text = "Graphics: fancy"; + else + graphics.text = "Graphics: fast"; + } + } + ); + + Button mouseTarget = new Button(350, 40, "Mouse target: enabled", new Runnable() + { + @Override + public void run() + { + Panel.showMouseTarget = !Panel.showMouseTarget; + + if (Panel.showMouseTarget) + mouseTarget.text = "Mouse target: enabled"; + else + mouseTarget.text = "Mouse target: disabled"; + } + } + ); + + Button scale = new Button(350, 40, "Scale: 100%", new Runnable() + { + @Override + public void run() + { + if (KeyInputListener.keys.contains(KeyEvent.VK_SHIFT)) + Window.scale -= 0.1; + else + Window.scale += 0.1; + + if (Window.scale < 0.45) + Window.scale = 2; + + if (Window.scale > 2.05) + Window.scale = 0.5; + + Window.scale = Math.round(Window.scale * 10) / 10.0; + + scale.text = "Scale: " + (int)Math.round(Window.scale * 100) + "%"; + Game.window.setSize((int)(Window.sizeX * Window.scale), (int) ((Window.sizeY) * Window.scale )); + } + } + , "Click to increase scale by 10%---Hold shift while clicking to decrease scale by 10%"); + + + Button back = new Button(350, 40, "Back", new Runnable() + { + @Override + public void run() + { + Game.screen = new ScreenTitle(); + } + } + ); + + Button insanity = new Button(350, 40, "Insanity mode: disabled", new Runnable() + { + @Override + public void run() + { + Game.insanity = !Game.insanity; + + if (Game.insanity) + insanity.text = "Insanity mode: enabled"; + else + insanity.text = "Insanity mode: disabled"; + } + } + ); + + @Override + public void update() + { + insanity.update(Window.sizeX / 2, Window.sizeY / 2 + 30); + mouseTarget.update(Window.sizeX / 2, Window.sizeY / 2 - 30); + graphics.update(Window.sizeX / 2, Window.sizeY / 2 - 90); + back.update(Window.sizeX / 2, Window.sizeY / 2 + 90); + } + + @Override + public void draw(Graphics g) + { + this.drawDefaultBackground(g); + insanity.draw(g, Window.sizeX / 2, Window.sizeY / 2 + 30); + mouseTarget.draw(g, Window.sizeX / 2, Window.sizeY / 2 - 30); + graphics.draw(g, Window.sizeX / 2, Window.sizeY / 2 - 90); + back.draw(g, Window.sizeX / 2, Window.sizeY / 2 + 90); + Window.drawText(g, Window.sizeX / 2, Window.sizeY / 2 - 150, "Options"); + } + +} diff --git a/src/tanks/ScreenPaused.java b/src/tanks/ScreenPaused.java new file mode 100644 index 00000000..52583599 --- /dev/null +++ b/src/tanks/ScreenPaused.java @@ -0,0 +1,74 @@ +package tanks; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.KeyEvent; + +public class ScreenPaused extends ScreenGame +{ + + Button resume = new Button(350, 40, "Continue playing", new Runnable() + { + @Override + public void run() + { + Game.screen = new ScreenGame(); + Game.player.cooldown = 20; + } + } + ); + + Button newLevel = new Button(350, 40, "Generate a new level", new Runnable() + { + @Override + public void run() + { + Game.reset(); + Game.screen = new ScreenGame(); + } + } + ); + + Button quit = new Button(350, 40, "Quit to title", new Runnable() + { + @Override + public void run() + { + Game.exitToTitle(); + //System.exit(0); + } + } + ); + + @Override + public void update() + { + if (KeyInputListener.keys.contains(KeyEvent.VK_ESCAPE)) + { + if (!Panel.pausePressed) + Game.screen = new ScreenGame(); + + Panel.pausePressed = true; + } + else + Panel.pausePressed = false; + + newLevel.update(Window.sizeX / 2, Window.sizeY / 2); + quit.update(Window.sizeX / 2, Window.sizeY / 2 + 60); + resume.update(Window.sizeX / 2, Window.sizeY / 2 - 60); + } + + @Override + public void draw(Graphics g) + { + super.draw(g); + + g.setColor(new Color(127, 178, 228, 64)); + g.fillRect(0, 0, (int) (Game.window.getSize().getWidth()) + 1, (int) (Game.window.getSize().getHeight()) + 1); + newLevel.draw(g, Window.sizeX / 2, Window.sizeY / 2); + quit.draw(g, Window.sizeX / 2, Window.sizeY / 2 + 60); + resume.draw(g, Window.sizeX / 2, Window.sizeY / 2 - 60); + g.setColor(Color.black); + Window.drawText(g, Window.sizeX / 2, Window.sizeY / 2 - 150, "Game paused"); + } +} diff --git a/src/tanks/ScreenTitle.java b/src/tanks/ScreenTitle.java new file mode 100644 index 00000000..9fb9991b --- /dev/null +++ b/src/tanks/ScreenTitle.java @@ -0,0 +1,61 @@ +package tanks; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; + +public class ScreenTitle extends Screen +{ + Button exit = new Button(350, 40, "Exit the game", new Runnable() + { + @Override + public void run() + { + System.exit(0); + } + } + ); + + Button options = new Button(350, 40, "Options...", new Runnable() + { + @Override + public void run() + { + Game.screen = new ScreenOptions(); + } + } + ); + + Button newLevel = new Button(350, 40, "Generate a new level", new Runnable() + { + @Override + public void run() + { + Game.reset(); + Game.screen = new ScreenGame(); + } + } + ); + + @Override + public void update() + { + newLevel.update(Window.sizeX / 2, Window.sizeY / 2); + exit.update(Window.sizeX / 2, Window.sizeY / 2 + 120); + options.update(Window.sizeX / 2, Window.sizeY / 2 + 60); + } + + @Override + public void draw(Graphics g) + { + this.drawDefaultBackground(g); + + g.setColor(Color.black); + g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (60 * Window.scale))); + Window.drawText(g, Window.sizeX / 2, Window.sizeY / 2 - 200, "Tanks"); + newLevel.draw(g, Window.sizeX / 2, Window.sizeY / 2); + exit.draw(g, Window.sizeX / 2, Window.sizeY / 2 + 120); + options.draw(g, Window.sizeX / 2, Window.sizeY / 2 + 60); + } + +} diff --git a/src/tanks/Tank.java b/src/tanks/Tank.java index 50fb6ebd..dfd2b316 100644 --- a/src/tanks/Tank.java +++ b/src/tanks/Tank.java @@ -41,14 +41,14 @@ public void checkCollision() { hasCollided = false; - if (this.posX + this.size / 2 > Screen.sizeX) + if (this.posX + this.size / 2 > Window.sizeX) { - this.posX = Screen.sizeX - this.size / 2; + this.posX = Window.sizeX - this.size / 2; hasCollided = true; } - if (this.posY + this.size / 2 > Screen.sizeY) + if (this.posY + this.size / 2 > Window.sizeY) { - this.posY = Screen.sizeY - this.size / 2; + this.posY = Window.sizeY - this.size / 2; hasCollided = true; } if (this.posX - this.size / 2 < 0) @@ -181,12 +181,12 @@ public void draw(Graphics g) { drawAge += Panel.frameFrequency; g.setColor(new Color((int) ((this.color.getRed() * (1 - this.flashAnimation) + 255 * this.flashAnimation)), (int) (this.color.getGreen() * (1 - this.flashAnimation)), (int) (this.color.getBlue() * (1 - this.flashAnimation)))); - Screen.fillRect(g, this.posX, this.posY, (this.size * (Game.tank_size - destroyTimer) / Game.tank_size) * Math.min(this.drawAge / Game.tank_size, 1), (this.size * (Game.tank_size - destroyTimer) / Game.tank_size) * Math.min(this.drawAge / Game.tank_size, 1)); + Window.fillRect(g, this.posX, this.posY, (this.size * (Game.tank_size - destroyTimer) / Game.tank_size) * Math.min(this.drawAge / Game.tank_size, 1), (this.size * (Game.tank_size - destroyTimer) / Game.tank_size) * Math.min(this.drawAge / Game.tank_size, 1)); if (this.lives > 1) { for (int i = 1; i < lives; i++) { - Screen.drawRect(g, this.posX, this.posY, 8 * i + this.size * (Game.tank_size - destroyTimer) / Game.tank_size - Math.max(Game.tank_size - drawAge, 0), 8 * i + this.size * (Game.tank_size - destroyTimer) / Game.tank_size - Math.max(Game.tank_size - drawAge, 0)); + Window.drawRect(g, this.posX, this.posY, 8 * i + this.size * (Game.tank_size - destroyTimer) / Game.tank_size - Math.max(Game.tank_size - drawAge, 0), 8 * i + this.size * (Game.tank_size - destroyTimer) / Game.tank_size - Math.max(Game.tank_size - drawAge, 0)); } } this.turret.draw(g, angle); diff --git a/src/tanks/Turret.java b/src/tanks/Turret.java index bc450836..cfc3c936 100644 --- a/src/tanks/Turret.java +++ b/src/tanks/Turret.java @@ -35,7 +35,7 @@ public void draw(Graphics p, double angle) //p.setColor(new Color(75, 40, 0)); p.setColor(new Color((tank.color.getRed() + 64) / 2, (tank.color.getGreen() + 64) / 2, (tank.color.getBlue() + 64) / 2)); int s = (int) (size * (Game.tank_size - this.tank.destroyTimer - Math.max(Game.tank_size - tank.drawAge, 0)) / Game.tank_size); - Screen.fillOval(p, this.posX, this.posY, s, s); + Window.fillOval(p, this.posX, this.posY, s, s); this.posX += this.vX * amount; this.posY += this.vY * amount; } diff --git a/src/tanks/Window.java b/src/tanks/Window.java new file mode 100644 index 00000000..e01c7c22 --- /dev/null +++ b/src/tanks/Window.java @@ -0,0 +1,182 @@ +package tanks; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.MouseInfo; +import java.awt.font.FontRenderContext; + +import javax.swing.*; + +@SuppressWarnings("serial") +public class Window extends JFrame +{ + public static int sizeX = 1400;//1920; + public static int sizeY = 900;//1100; + + public static double scale = 1; + + public Panel panel = new Panel(); + + public static Window window; + + public static int yOffset = 55; + public static int xOffset = 22; + public static int mouseXoffset = 0; + public static int mouseYoffset = -26; + + static FontRenderContext frc = new FontRenderContext(null, true, true); + + public Window() + { + this.addMouseListener(new MouseInputListener()); + this.addKeyListener(new KeyInputListener()); + this.setSize((int)(sizeX * scale), (int) ((sizeY + yOffset) * scale )); + this.setVisible(true); + Container visiblePart = this.getContentPane(); + visiblePart.add(panel); + this.setResizable(true); + this.setMinimumSize(new Dimension(350, 265 + yOffset)); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window = this; + + if (System.getProperties().get("os.name").toString().toLowerCase().contains("mac")) + { + Window.yOffset = 22; + Window.xOffset = 0; + Window.mouseYoffset = -2; + } + } + + @Override + public void setSize(int x, int y) + { + super.setSize(x + xOffset, (int) (y + 40 + yOffset * (1 - scale))); + } + + @SuppressWarnings("static-access") + public int getSizeX() + { + return (int) (this.WIDTH * scale); + } + + @SuppressWarnings("static-access") + public int getSizeY() + { + return (int) (this.HEIGHT * scale); + } + + public static void fillOval(Graphics g, double x, double y, double sizeX, double sizeY) + { + int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.window.getSize().getWidth() - Window.xOffset - Window.sizeX * Window.scale) / 2); + int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.window.getSize().getHeight() - Window.yOffset - 40 - Window.sizeY * Window.scale) / 2); + int drawSizeX = (int) Math.round(sizeX * scale); + int drawSizeY = (int) Math.round(sizeY * scale); + + g.fillOval(drawX, drawY, drawSizeX, drawSizeY); + } + + public static void drawOval(Graphics g, double x, double y, double sizeX, double sizeY) + { + int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.window.getSize().getWidth() - Window.xOffset - Window.sizeX * Window.scale) / 2); + int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.window.getSize().getHeight() - Window.yOffset - 40 - Window.sizeY * Window.scale) / 2); + int drawSizeX = (int) Math.round(sizeX * scale); + int drawSizeY = (int) Math.round(sizeY * scale); + + g.drawOval(drawX, drawY, drawSizeX, drawSizeY); + } + + public static void fillRect(Graphics g, double x, double y, double sizeX, double sizeY) + { + int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.window.getSize().getWidth() - Window.xOffset - Window.sizeX * Window.scale) / 2); + int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.window.getSize().getHeight() - Window.yOffset - 40 - Window.sizeY * Window.scale) / 2); + int drawSizeX = (int) Math.round(sizeX * scale); + int drawSizeY = (int) Math.round(sizeY * scale); + + g.fillRect(drawX, drawY, drawSizeX, drawSizeY); + } + + public static void drawRect(Graphics g, double x, double y, double sizeX, double sizeY) + { + int drawX = (int) Math.round(scale * (x - sizeX / 2) + Math.max(0, Game.window.getSize().getWidth() - Window.xOffset - Window.sizeX * Window.scale) / 2); + int drawY = (int) Math.round(scale * (y - sizeY / 2) + Math.max(0, Game.window.getSize().getHeight() - Window.yOffset - 40 - Window.sizeY * Window.scale) / 2); + int drawSizeX = (int) Math.round(sizeX * scale); + int drawSizeY = (int) Math.round(sizeY * scale); + + g.drawRect(drawX , drawY, drawSizeX, drawSizeY); + } + + public static void drawText(Graphics g, double x, double y, String text) + { + double sizeX = Math.round(g.getFont().getStringBounds(text, frc).getWidth()); + + double sizeY = g.getFont().getSize() / 3 / Window.scale; + //int size = text.length() * g.getFont().getSize() / 2; + int drawX = (int) (scale * x - sizeX / 2 + Math.max(0, Game.window.getSize().getWidth() - Window.xOffset - Window.sizeX * Window.scale) / 2); + int drawY = (int) (scale * (y + sizeY / 2) + Math.max(0, Game.window.getSize().getHeight() - Window.yOffset - 40 - Window.sizeY * Window.scale) / 2); + g.drawString(text, drawX, drawY); + + //g.setColor(Color.red); + //g.drawRect((int)((x - sizeX / 2) * scale), (int)((y - sizeY * 3 / 2) * scale), (int)g.getFont().getStringBounds(text, frc).getWidth(), (int)g.getFont().getStringBounds(text, frc).getHeight()); + } + + public static double drawTooltip(Graphics g, String[] text) + { + double x = Game.window.getMouseX(); + double y = Game.window.getMouseY(); + + int xPadding = (int) (16 * Window.scale); + int yPadding = (int) (8 * Window.scale); + + g.setFont(g.getFont().deriveFont(Font.BOLD, (float) (Window.scale * 14))); + + int sizeX = 0; + for (int i = 0; i < text.length; i++) + { + sizeX = Math.max(sizeX, (int) Math.round(g.getFont().getStringBounds(text[i], frc).getWidth())); + } + + int sizeY = g.getFont().getSize(); + + //int size = text.length() * g.getFont().getSize() / 2; + int drawX = (int) (scale * (x)); + int drawY = (int) (scale * (y)); + + g.setColor(new Color(0, 0, 0, 127)); + g.fillRect(drawX, drawY, sizeX + xPadding * 2, sizeY + yPadding * 2 * text.length); + + g.setColor(Color.WHITE); + for (int i = 0; i < text.length; i++) + { + g.drawString(text[i], drawX + xPadding, (int) (drawY + (i + 1) * (sizeY / 2 + yPadding * (1 + 1 / 2.0)))); + } + + return (y - (drawY / Window.scale + sizeY + yPadding / Window.scale * 2)); + } + + public double getMouseX() + { + return (MouseInfo.getPointerInfo().getLocation().getX() - this.getLocation().getX() - Math.max(0, Game.window.getSize().getWidth() - Window.sizeX * Window.scale) / 2) / scale + mouseXoffset / Window.scale; + } + + public double getMouseY() + { + return ((MouseInfo.getPointerInfo().getLocation().getY() - this.getLocation().getY() - Math.max(0, Game.window.getSize().getHeight() - (yOffset + 1) - Window.sizeY * Window.scale) / 2)) / scale + mouseYoffset / Window.scale; + } + + public void setScreenSize(int x, int y) + { + sizeX = x; + sizeY = y; + this.setSize((int) (x * scale), (int) ((y + yOffset) * scale)); + } + + public void setScreenBounds(int x, int y) + { + sizeX = x; + sizeY = y; + Game.currentSizeX = x / Game.tank_size; + Game.currentSizeY = y / Game.tank_size; + } +} diff --git a/src/tanks/legacy/EnemyTankWhite.java b/src/tanks/legacy/EnemyTankWhite.java index e89c1557..5f46176f 100644 --- a/src/tanks/legacy/EnemyTankWhite.java +++ b/src/tanks/legacy/EnemyTankWhite.java @@ -396,7 +396,7 @@ public void draw(Graphics g) for (int i = 0; i < Game.tank_size * 2 - this.age; i++) { g.setColor(new Color(Game.tank_size * 2 - i - this.age, 255, Game.tank_size * 2 - i - this.age, Game.tank_size * 2 - i - this.age)); - Screen.drawOval(g, this.posX, this.posY, i, i); + Window.drawOval(g, this.posX, this.posY, i, i); } } }