Skip to content

Commit

Permalink
Tanks v0.3.4 - Added Tank Registry class, and made harder tanks appea…
Browse files Browse the repository at this point in the history
…r rarer
  • Loading branch information
aehmttw committed Jul 8, 2018
1 parent 4d8f0dc commit 1058daa
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/tanks/EnemyTankMini.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tanks/EnemyTankMint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tanks/EnemyTankOrange.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tanks/EnemyTankRed.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 16 additions & 0 deletions src/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 5 additions & 29 deletions src/tanks/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
75 changes: 33 additions & 42 deletions src/tanks/LevelGenerator.java
Original file line number Diff line number Diff line change
@@ -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 ^
Expand All @@ -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 += ",";
Expand All @@ -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));
}
Expand All @@ -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));
Expand All @@ -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 += ",";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tanks/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
68 changes: 64 additions & 4 deletions src/tanks/Registry.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,78 @@
package tanks;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

public class Registry
{
public ArrayList<TankRegistry> tankRegistries = new ArrayList<TankRegistry>();
protected double maxTankWeight = 0;

static class TankRegistry
{
Class<? extends Tank> tank;
String name;
int weight;
public final Class<? extends Tank> tank;
public final String name;
public final double weight;

protected double startWeight;
protected double endWeight;

public TankRegistry(Class<? extends Tank> tank, String name, int weight)
public TankRegistry(Registry r, Class<? extends Tank> 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;
}
}
1 change: 1 addition & 0 deletions src/tanks/legacy/EnemyTankMint.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.Color;
import tanks.*;

@Deprecated
public class EnemyTankMint extends Tank
{
int moveTime = 0;
Expand Down
1 change: 1 addition & 0 deletions src/tanks/legacy/EnemyTankRed.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.Color;
import tanks.*;

@Deprecated
public class EnemyTankRed extends Tank
{
int moveTime = 0;
Expand Down

0 comments on commit 1058daa

Please sign in to comment.