Search in sources :

Example 1 with ServerAPI

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);
}
Also used : Table(com.badlogic.gdx.scenes.scene2d.ui.Table) Message(com.janfic.games.computercombat.network.Message) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) JsonValue(com.badlogic.gdx.utils.JsonValue) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) JsonReader(com.badlogic.gdx.utils.JsonReader) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) ArrayList(java.util.ArrayList) List(java.util.List) ServerAPI(com.janfic.games.computercombat.network.client.ServerAPI) SocketHints(com.badlogic.gdx.net.SocketHints) ProgressBar(com.badlogic.gdx.scenes.scene2d.ui.ProgressBar) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 SocketHints (com.badlogic.gdx.net.SocketHints)1 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)1 ProgressBar (com.badlogic.gdx.scenes.scene2d.ui.ProgressBar)1 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)1 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)1 JsonReader (com.badlogic.gdx.utils.JsonReader)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 Message (com.janfic.games.computercombat.network.Message)1 ServerAPI (com.janfic.games.computercombat.network.client.ServerAPI)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1