Skip to content

Commit

Permalink
Tanks v0.7.2 - Added Versus Mode for parties, improved the party play…
Browse files Browse the repository at this point in the history
…er list, and fixed tank health not updating in multiplayer
  • Loading branch information
aehmttw committed Sep 23, 2019
1 parent 9687dd8 commit 36d7084
Show file tree
Hide file tree
Showing 17 changed files with 852 additions and 104 deletions.
40 changes: 35 additions & 5 deletions src/main/java/lwjglwindow/FontRenderer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lwjglwindow;

public class FontRenderer
import org.lwjgl.opengl.GL11;

public class FontRenderer
{
public LWJGLWindow home;
String chars;
Expand All @@ -23,7 +25,7 @@ public FontRenderer(LWJGLWindow h, String fontFile)
this.image = fontFile;
}

public int drawChar(double x, double y, double sX, double sY, char c)
public int drawChar(double x, double y, double z, double sX, double sY, char c)
{
int i = this.chars.indexOf(c);

Expand All @@ -33,12 +35,14 @@ public int drawChar(double x, double y, double sX, double sY, char c)
int col = i % 16;
int row = i / 16;
int width = charSizes[i];
this.home.drawImage(x, y, sX, sY - 0.0001, col / 16f, row / 16f, (col + width / 8f) / 16f, (row + 1) / 16f, image, true);
this.home.drawImage(x, y, z, sX, sY - 0.0001, col / 16f, row / 16f, (col + width / 8f) / 16f, (row + 1) / 16f, image, true, false);
return width;
}

public void drawString(double x, double y, double sX, double sY, String s)
public void drawString(double x, double y, double z, double sX, double sY, String s)
{
GL11.glEnable(GL11.GL_DEPTH_TEST);

double curX = x;
char[] c = s.toCharArray();

Expand All @@ -57,7 +61,33 @@ else if (c[i] == '\u00A7')
i += 12;
}
else
curX += (drawChar(curX, y, sX, sY, c[i]) + 1) * sX * 4;
curX += (drawChar(curX, y, z, sX, sY, c[i]) + 1) * sX * 4;
}

GL11.glDisable(GL11.GL_DEPTH_TEST);
}

public void drawString(double x, double y, double sX, double sY, String s)
{
double curX = x;
char[] c = s.toCharArray();

for (int i = 0; i < c.length; i++)
{
if (c[i] == '\u00C2')
continue;
else if (c[i] == '\u00A7')
{
int r = Integer.parseInt(c[i + 1] + "" + c[i + 2] + "" + c[i + 3]);
int g = Integer.parseInt(c[i + 4] + "" + c[i + 5] + "" + c[i + 6]);
int b = Integer.parseInt(c[i + 7] + "" + c[i + 8] + "" + c[i + 9]);
int a = Integer.parseInt(c[i + 10] + "" + c[i + 11] + "" + c[i + 12]);
this.home.setColor(r, g, b, a);

i += 12;
}
else
curX += (drawChar(curX, y, 0, sX, sY, c[i]) + 1) * sX * 4;
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/lwjglwindow/LWJGLWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -723,16 +723,22 @@ public void drawImage(double x, double y, double sX, double sY, double u1, doubl
glMatrixMode(GL_PROJECTION);
glDisable(GL_TEXTURE_2D);
}

public void drawImage(double x, double y, double z, double sX, double sY, double u1, double v1, double u2, double v2, String image, boolean scaled)
{
this.drawImage(x, y, z, sX, sY, u1, v1, u2, v2, image, scaled, true);
}

public void drawImage(double x, double y, double z, double sX, double sY, double u1, double v1, double u2, double v2, String image, boolean scaled, boolean depthtest)
{
if (!textures.containsKey(image))
createImage(image);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glEnable(GL_DEPTH_TEST);
if (depthtest)
glEnable(GL_DEPTH_TEST);

glFrustum(-absoluteWidth / (absoluteDepth * 2.0), absoluteWidth / (absoluteDepth * 2.0), absoluteHeight / (absoluteDepth * 2.0), -absoluteHeight / (absoluteDepth * 2.0), 1, absoluteDepth * 2);
glTranslated(-absoluteWidth / 2, -absoluteHeight / 2, -absoluteDepth);
Expand Down Expand Up @@ -768,6 +774,8 @@ public void drawImage(double x, double y, double z, double sX, double sY, double

glMatrixMode(GL_PROJECTION);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);

if (depthtest)
glDisable(GL_DEPTH_TEST);
}
}
11 changes: 11 additions & 0 deletions src/main/java/tanks/Drawing.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,17 @@ public void drawText(double x, double y, String text)
Game.game.window.fontRenderer.drawString(drawX, drawY, this.fontSize, this.fontSize, text);
}

public void drawText(double x, double y, double z, String text)
{
double sizeX = Game.game.window.fontRenderer.getStringSizeX(this.fontSize, text) / scale;
double sizeY = Game.game.window.fontRenderer.getStringSizeY(this.fontSize, text) / scale;

double drawX = (scale * (x + getPlayerOffsetX() - sizeX / 2) + Math.max(0, Panel.windowWidth - this.sizeX * scale) / 2);
double drawY = (scale * (y + getPlayerOffsetY() - sizeY / 2) + Math.max(0, Panel.windowHeight - statsHeight - this.sizeY * scale) / 2);

Game.game.window.fontRenderer.drawString(drawX, drawY, z, this.fontSize, this.fontSize, text);
}

public void drawInterfaceText(double x, double y, String text)
{
double sizeX = Game.game.window.fontRenderer.getStringSizeX(this.fontSize, text);
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class Game

public static double[][] tilesDepth = new double[28][18];

public static final int network_protocol = 4;
public static final String version = "Tanks 0.7.1";
public static final int network_protocol = 5;
public static final String version = "Tanks 0.7.2";

public static int port = 8080;

Expand Down Expand Up @@ -146,7 +146,7 @@ public static void initScript()
/* 15*/ NetworkEventMap.register(EventCreatePlayer.class);
/* 16*/ NetworkEventMap.register(EventCreateTank.class);
/* 17*/ NetworkEventMap.register(EventCreateCustomTank.class);
/* 18*/ NetworkEventMap.register(EventTankDestroyed.class);
/* 18*/ NetworkEventMap.register(EventTankUpdateHealth.class);
/* 19*/ NetworkEventMap.register(EventShootBullet.class);
/* 20*/ NetworkEventMap.register(EventLayMine.class);
/* 21*/ NetworkEventMap.register(EventTankTeleport.class);
Expand Down Expand Up @@ -324,7 +324,9 @@ public static void exit(String name)

public static void exitToCrash(Exception e)
{
if (ScreenPartyHost.isServer)
e.printStackTrace();

if (ScreenPartyHost.isServer && ScreenPartyHost.server != null)
ScreenPartyHost.server.close("The party has ended because the host crashed");

if (ScreenPartyLobby.isClient)
Expand All @@ -337,7 +339,6 @@ public static void exitToCrash(Exception e)
belowEffects.clear();
movables.clear();
effects.clear();
e.printStackTrace();
Game.crashMessage = e.toString();
Game.logger.println(new Date().toString() + " (syserr) the game has crashed! below is a crash report, good luck:");
e.printStackTrace(Game.logger);
Expand Down Expand Up @@ -406,7 +407,7 @@ public static void loadLevel(File f)

public static void loadLevel(File f, ScreenLevelBuilder s)
{
Scanner in;
Scanner in = null;
try
{
in = new Scanner(f);
Expand All @@ -422,6 +423,9 @@ public static void loadLevel(File f, ScreenLevelBuilder s)
{
Game.exitToCrash(e);
}

if (in != null)
in.close();
}

public static void start()
Expand Down
98 changes: 72 additions & 26 deletions src/main/java/tanks/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import tanks.gui.Button;
import tanks.gui.screen.ScreenGame;
import tanks.gui.screen.ScreenLevelBuilder;
import tanks.gui.screen.ScreenParty;
import tanks.gui.screen.ScreenPartyHost;
import tanks.network.Server;
import tanks.network.ServerHandler;
import tanks.obstacle.Obstacle;
import tanks.registry.RegistryObstacle;
Expand Down Expand Up @@ -39,8 +41,16 @@ public class Level
public boolean remote = false;

public HashMap<String, Team> teamsMap = new HashMap<String, Team>();

public ArrayList<Team> teamsList = new ArrayList<Team>();

public ArrayList<Integer> availablePlayerSpawns = new ArrayList<Integer>();

public ArrayList<Double> playerSpawnsX = new ArrayList<Double>();
public ArrayList<Double> playerSpawnsY = new ArrayList<Double>();
public ArrayList<Double> playerSpawnsAngle = new ArrayList<Double>();
public ArrayList<Team> playerSpawnsTeam = new ArrayList<Team>();

/**
* A level string is structured like this:
* (parentheses signify required parameters, and square brackets signify optional parameters.
Expand All @@ -49,7 +59,7 @@ public class Level
* {(SizeX),(SizeY),[(Red),(Green),(Blue)],[(RedNoise),(GreenNoise),(BlueNoise)]|[(ObstacleX)-(ObstacleY)]*|[(TankX)-(TankY)-(TankType)-[TankAngle]-[TeamName]]*|[(TeamName)-[FriendlyFire]-[(Red)-(Green)-(Blue)]]*}
*/
public Level(String level)
{
{
this.levelString = level.replaceAll("\u0000", "");

preset = this.levelString.split("\\{")[1].split("}")[0].split("\\|");
Expand Down Expand Up @@ -339,32 +349,12 @@ public void run()
if (team == Game.enemyTeam)
team = Game.playerTeam;

if (ScreenPartyHost.isServer)
{
EventCreatePlayer local = new EventCreatePlayer(Game.clientID, Game.username, x, y, angle, team);
playerEvents.add(local);
Game.eventsOut.add(local);

synchronized(ScreenPartyHost.server.connections)
{
for (ServerHandler c: ScreenPartyHost.server.connections)
{
if (c.clientID == null)
continue;

EventCreatePlayer e = new EventCreatePlayer(c.clientID, c.rawUsername, x, y, angle, team);
playerEvents.add(e);
Game.eventsOut.add(e);
}
}

continue;
}
else if (remote)
continue;
this.playerSpawnsX.add(x);
this.playerSpawnsY.add(y);
this.playerSpawnsAngle.add(angle);
this.playerSpawnsTeam.add(team);

t = new TankPlayer(x, y, angle, Game.clientID);
Game.player = (TankPlayer) t;
continue;
}
else
{
Expand All @@ -380,6 +370,62 @@ else if (remote)
}
}

this.availablePlayerSpawns.clear();

int playerCount = 1;
if (ScreenPartyHost.isServer && ScreenPartyHost.server != null)
playerCount += ScreenPartyHost.server.connections.size();

for (int i = 0; i < playerCount; i++)
{
if (this.availablePlayerSpawns.size() == 0)
{
for (int j = 0; j < this.playerSpawnsTeam.size(); j++)
{
this.availablePlayerSpawns.add(j);
}
}

int spawn = this.availablePlayerSpawns.remove((int) (Math.random() * this.availablePlayerSpawns.size()));

double x = this.playerSpawnsX.get(spawn);
double y = this.playerSpawnsY.get(spawn);
double angle = this.playerSpawnsAngle.get(spawn);
Team team = this.playerSpawnsTeam.get(spawn);

if (ScreenPartyHost.isServer)
{
if (i == 0)
{
EventCreatePlayer local = new EventCreatePlayer(Game.clientID, Game.username, x, y, angle, team);
playerEvents.add(local);
Game.eventsOut.add(local);
}
else
{
synchronized (ScreenPartyHost.server.connections)
{
ServerHandler c = ScreenPartyHost.server.connections.get(i - 1);

EventCreatePlayer e = new EventCreatePlayer(c.clientID, c.rawUsername, x, y, angle, team);
playerEvents.add(e);
Game.eventsOut.add(e);
}
}

continue;
}
else if (remote)
continue;
else
{
Tank tank = new TankPlayer(x, y, angle, Game.clientID);
Game.player = (TankPlayer) tank;
tank.team = team;
Game.movables.add(tank);
}
}

for (EventCreatePlayer e: playerEvents)
e.execute();

Expand Down
Loading

0 comments on commit 36d7084

Please sign in to comment.