use of com.janfic.games.computercombat.network.client.ServerAPI in project computercombat by janfic.
the class LoadingScreen method show.
@Override
public void show() {
this.skin = new Skin(Gdx.files.internal(Assets.SKIN));
this.camera = new OrthographicCamera(1920 / 4, 1080 / 4);
this.stage = ComputerCombatGame.makeNewStage(camera);
Pixmap cursor = new Pixmap(Gdx.files.internal(Assets.CURSOR));
Gdx.graphics.setCursor(Gdx.graphics.newCursor(cursor, 0, 0));
this.progressBar = new ProgressBar(0, 1, 0.01f, false, skin.get("default-horizontal", ProgressBarStyle.class));
this.statusLabel = new Label("Loading Assets...", skin);
Table table = new Table(skin);
table.setFillParent(true);
table.add(statusLabel).row();
table.add(progressBar);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int index = 0;
int tries = 0;
JsonReader json = new JsonReader();
JsonValue parsed = json.parse(Gdx.files.internal("connections.json"));
List<String> connections = new ArrayList<>();
for (JsonValue jsonValue : parsed.child) {
connections.add(jsonValue.getString("ip") + " " + jsonValue.getString("port"));
}
System.out.println(connections);
boolean connected = false;
while (connected == false) {
try {
String[] connection = connections.get(index).split(" ");
game.setServerAPI(new ServerAPI(Gdx.net.newClientSocket(Net.Protocol.TCP, connection[0], Integer.parseInt(connection[1]), new SocketHints())));
connected = true;
Thread.sleep(5);
} catch (Exception e) {
tries++;
statusLabel.setText("Failed to Connect to server. Trying again. (" + tries + ")");
if (tries > 4) {
statusLabel.setText("Trying a different server...");
tries = 0;
index++;
index = index % connections.size();
}
}
}
game.getServerAPI().sendMessage(new Message(Type.CONNECTION_REQUEST, "CONNECTION_REQUEST"));
}
});
thread.start();
stage.addActor(table);
}
Aggregations