diff --git a/src/tanks/EnemyTankMini.java b/src/tanks/EnemyTankMini.java index 14bcaec0..c143cadc 100644 --- a/src/tanks/EnemyTankMini.java +++ b/src/tanks/EnemyTankMini.java @@ -18,14 +18,14 @@ public EnemyTankMini(double x, double y, double angle) this.liveBulletMax = 1; this.cooldownRandom = 60; this.cooldownBase = 120; - this.idleTurretSpeed = 0.02; + this.aimTurretSpeed = 0.02; this.bulletBounces = 0; this.bulletColor = Color.blue; this.bulletEffect = Bullet.BulletEffect.none; this.bulletSpeed = 25.0 / 4; this.bulletDamage = 0.25; this.bulletSize /= 2; - this.enableLookingAtPlayer = false; + this.enableLookingAtPlayer = true; this.motionChangeChance = 0.001; this.enableBulletAvoidance = false; } diff --git a/src/tanks/EnemyTankMint.java b/src/tanks/EnemyTankMint.java index 8be6b17e..0c6356d8 100644 --- a/src/tanks/EnemyTankMint.java +++ b/src/tanks/EnemyTankMint.java @@ -15,7 +15,7 @@ public EnemyTankMint(double x, double y, double angle) this.liveBulletMax = 1; this.cooldownRandom = 60; this.cooldownBase = 240; - this.idleTurretSpeed = 0.02; + this.aimTurretSpeed = 0.02; this.bulletBounces = 0; this.bulletColor = Color.red; this.bulletEffect = Bullet.BulletEffect.fire; diff --git a/src/tanks/EnemyTankOrange.java b/src/tanks/EnemyTankOrange.java index b6e1efc6..ca0b2442 100644 --- a/src/tanks/EnemyTankOrange.java +++ b/src/tanks/EnemyTankOrange.java @@ -13,7 +13,7 @@ public EnemyTankOrange(double x, double y, double angle) this.enableMineLaying = false; this.enablePredictiveFiring = false; - this.idleTurretSpeed = 0.01; + this.aimTurretSpeed = 0.01; this.aimAccuracyOffset = 0; this.motionChangeChance = 0.0005; diff --git a/src/tanks/EnemyTankRed.java b/src/tanks/EnemyTankRed.java index ee4603a2..3a019227 100644 --- a/src/tanks/EnemyTankRed.java +++ b/src/tanks/EnemyTankRed.java @@ -15,7 +15,7 @@ public EnemyTankRed(double x, double y, double angle) this.enableMineLaying = false; this.enablePredictiveFiring = false; this.liveBulletMax = 1; - this.idleTurretSpeed = 0.001; + this.aimTurretSpeed = 0.02; this.enableLookingAtPlayer = false; this.cooldown = 250; diff --git a/src/tanks/Game.java b/src/tanks/Game.java index fb40222d..aa00c5df 100644 --- a/src/tanks/Game.java +++ b/src/tanks/Game.java @@ -47,12 +47,28 @@ public enum Menu {none, paused, title, options, interlevel} public static int coins = 0; public static Item[] items = new Item[5]; + public static Registry registry = new Registry(); + static Screen gamescreen; static String currentLevel = ""; public static void main(String[] args) { + new Registry.TankRegistry(registry, EnemyTankBrown.class, "brown", 1); + new Registry.TankRegistry(registry, EnemyTankGray.class, "gray", 1); + new Registry.TankRegistry(registry, EnemyTankMint.class, "mint", 1.0 / 2); + new Registry.TankRegistry(registry, EnemyTankYellow.class, "yellow", 1.0 / 2); + new Registry.TankRegistry(registry, EnemyTankMagenta.class, "magenta", 1.0 / 3); + new Registry.TankRegistry(registry, EnemyTankRed.class, "red", 1.0 / 3); + new Registry.TankRegistry(registry, EnemyTankGreen.class, "green", 1.0 / 4); + new Registry.TankRegistry(registry, EnemyTankPurple.class, "purple", 1.0 / 4); + new Registry.TankRegistry(registry, EnemyTankWhite.class, "white", 1.0 / 4); + new Registry.TankRegistry(registry, EnemyTankOrange.class, "orange", 1.0 / 6); + new Registry.TankRegistry(registry, EnemyTankDarkGreen.class, "darkgreen", 1.0 / 9); + new Registry.TankRegistry(registry, EnemyTankBlack.class, "black", 1.0 / 10); + new Registry.TankRegistry(registry, EnemyTankPink.class, "pink", 1.0 / 15); + SwingUtilities.invokeLater ( new Runnable() diff --git a/src/tanks/Level.java b/src/tanks/Level.java index 369a0309..4db322bc 100644 --- a/src/tanks/Level.java +++ b/src/tanks/Level.java @@ -115,39 +115,15 @@ public void loadLevel() if (tank.length == 4) angle = (Math.PI / 2 * Double.parseDouble(tank[3])); - if (type.equals("brown")) - Game.movables.add(new EnemyTankBrown(x, y, angle)); - else if (type.equals("mint")) - Game.movables.add(new EnemyTankMint(x, y, angle)); - else if (type.equals("yellow")) - Game.movables.add(new EnemyTankYellow(x, y, angle)); - else if (type.equals("green")) - Game.movables.add(new EnemyTankGreen(x, y, angle)); - else if (type.equals("purple")) - Game.movables.add(new EnemyTankPurple(x, y, angle)); - else if (type.equals("magenta")) - Game.movables.add(new EnemyTankMagenta(x, y, angle)); - else if (type.equals("white")) - Game.movables.add(new EnemyTankWhite(x, y, angle)); - else if (type.equals("gray")) - Game.movables.add(new EnemyTankGray(x, y, angle)); - else if (type.equals("black")) - Game.movables.add(new EnemyTankBlack(x, y, angle)); - else if (type.equals("red")) - Game.movables.add(new EnemyTankRed(x, y, angle)); - else if (type.equals("orange")) - Game.movables.add(new EnemyTankOrange(x, y, angle)); - else if (type.equals("mini")) - Game.movables.add(new EnemyTankMini(x, y, angle)); - else if (type.equals("pink")) - Game.movables.add(new EnemyTankPink(x, y, angle)); - else if (type.equals("darkgreen")) - Game.movables.add(new EnemyTankDarkGreen(x, y, angle)); - else 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 + { + Game.movables.add(Game.registry.getRegistry(type).getTank(x, y, angle)); + } } Panel.preGameTimer = 400; diff --git a/src/tanks/LevelGenerator.java b/src/tanks/LevelGenerator.java index 82740101..247363d3 100644 --- a/src/tanks/LevelGenerator.java +++ b/src/tanks/LevelGenerator.java @@ -1,8 +1,10 @@ package tanks; // kgurazada -public class LevelGenerator { - public static String generateLevelString() { +public class LevelGenerator +{ + public static String generateLevelString() + { //int type = (int) (Math.random() * 13); //test ^ @@ -25,32 +27,42 @@ public static String generateLevelString() { String s = "{" + width + "," + height + "," + r + "," + g + "," + b + ",20,20,20|"; boolean[][] cells = new boolean[width][height]; - for (int i = 0; i < cells.length; i++) { - for (int j = 0; j < cells[i].length; j++) { + for (int i = 0; i < cells.length; i++) + { + for (int j = 0; j < cells[i].length; j++) + { cells[i][j] = false; } } - for (int i = 0; i < walls; i++) { + for (int i = 0; i < walls; i++) + { int l = (int) (Math.random() * Math.min(height, width) - 3); // max 14 int x = (int) (Math.random() * (width - l)); int y = (int) (Math.random() * (height - l)); boolean o = false; double z = Math.random(); - if (z > 0.5) { + if (z > 0.5) + { o = true; } - if (o) { + if (o) + { s += x + "..." + (x + l) + "-" + y; - for (int j = x; j <= x + l; j++) { + for (int j = x; j <= x + l; j++) + { cells[j][y] = true; } - } else { - for (int j = y; j <= y + l; j++) { + } + else + { + for (int j = y; j <= y + l; j++) + { cells[x][j] = true; } s += x + "-" + y + "..." + (y + l); } - if (i == walls - 1) { + if (i == walls - 1) + { s += "|"; } else { s += ","; @@ -63,7 +75,8 @@ public static String generateLevelString() { int x = (int) (Math.random() * (width)); int y = (int) (Math.random() * (height)); - while (cells[x][y]) { + while (cells[x][y]) + { x = (int) (Math.random() * (width)); y = (int) (Math.random() * (height)); } @@ -73,7 +86,6 @@ public static String generateLevelString() { s += x + "-" + y + "-" + "player,"; for (int i = 0; i < numTanks; i++) { - int type = (int) (Math.random() * 13); int angle = (int) (Math.random() * 4); x = (int) (Math.random() * (width)); y = (int) (Math.random() * (height)); @@ -86,36 +98,15 @@ public static String generateLevelString() { cells[Math.max(0, Math.min(width - 1, x+a))][Math.max(0, Math.min(height - 1, y+j))] = true; s += x + "-" + y + "-"; - if (type == 0) { - s += "brown-" + angle; - } else if (type == 1) { - s += "green-" + angle; - } else if (type == 2) { - s += "mint-" + angle; - } else if (type == 3) { - s += "yellow-" + angle; - } else if (type == 4) { - s += "purple-" + angle; - } else if (type == 5) { - s += "magenta-" + angle; - } else if (type == 6) { - s += "white-" + angle; - } else if (type == 7) { - s += "gray-" + angle; - } else if (type == 8) { - s += "black-" + angle; - } else if (type == 9) { - s += "red-" + angle; - } else if (type == 10) { - s += "orange-" + angle; - } else if (type == 11) { - s += "pink-" + angle; - } else if (type == 12) { - s += "darkgreen-" + angle; - } - if (i == numTanks - 1) { + s += Game.registry.getRandomTank().name; + s += "-" + angle; + + if (i == numTanks - 1) + { s += "}"; - } else { + } + else + { s += ","; } } diff --git a/src/tanks/Panel.java b/src/tanks/Panel.java index 077624df..acd829c3 100644 --- a/src/tanks/Panel.java +++ b/src/tanks/Panel.java @@ -577,7 +577,7 @@ else if (Game.menu.equals(Game.Menu.interlevel)) g.setFont(g.getFont().deriveFont(Font.BOLD, 12)); - g.drawString("Tanks v0.3.3d", 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 12 - Screen.yOffset)); + g.drawString("Tanks v0.3.4", 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)); diff --git a/src/tanks/Registry.java b/src/tanks/Registry.java index 37e0f9bf..68ff6220 100644 --- a/src/tanks/Registry.java +++ b/src/tanks/Registry.java @@ -1,18 +1,78 @@ package tanks; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + public class Registry { + public ArrayList tankRegistries = new ArrayList(); + protected double maxTankWeight = 0; + static class TankRegistry { - Class tank; - String name; - int weight; + public final Class tank; + public final String name; + public final double weight; + + protected double startWeight; + protected double endWeight; - public TankRegistry(Class tank, String name, int weight) + public TankRegistry(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 + { + return tank.getConstructor(double.class, double.class, double.class).newInstance(x, y, a); + } + catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) + { + e.printStackTrace(); + return null; + } + } + } + + public TankRegistry getRandomTank() + { + double random = Math.random() * maxTankWeight; + + for (int i = 0; i < tankRegistries.size(); i++) + { + TankRegistry r = tankRegistries.get(i); + + if (random >= r.startWeight && random < r.endWeight) + { + return r; + } } + + return null; + } + + public TankRegistry getRegistry(String name) + { + for (int i = 0; i < tankRegistries.size(); i++) + { + TankRegistry r = tankRegistries.get(i); + + if (r.name.equals(name)) + { + return r; + } + } + + return null; } } diff --git a/src/tanks/legacy/EnemyTankMint.java b/src/tanks/legacy/EnemyTankMint.java index 4027cb1b..f8617827 100644 --- a/src/tanks/legacy/EnemyTankMint.java +++ b/src/tanks/legacy/EnemyTankMint.java @@ -3,6 +3,7 @@ import java.awt.Color; import tanks.*; +@Deprecated public class EnemyTankMint extends Tank { int moveTime = 0; diff --git a/src/tanks/legacy/EnemyTankRed.java b/src/tanks/legacy/EnemyTankRed.java index 01a405cd..6fd8571b 100644 --- a/src/tanks/legacy/EnemyTankRed.java +++ b/src/tanks/legacy/EnemyTankRed.java @@ -3,6 +3,7 @@ import java.awt.Color; import tanks.*; +@Deprecated public class EnemyTankRed extends Tank { int moveTime = 0;