Added json config
This commit is contained in:
@@ -4,11 +4,10 @@ eclipse.project.name = appName + '-core'
|
||||
dependencies {
|
||||
api "com.badlogicgames.gdx:gdx:$gdxVersion"
|
||||
api "com.github.crykn:kryonet:$kryoNetVersion"
|
||||
implementation project(':shared')
|
||||
implementation "com.google.code.gson:gson:$gsonVersion"
|
||||
|
||||
if(enableGraalNative == 'true') {
|
||||
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
|
||||
implementation(project(":shared"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.github.eldek0.config;
|
||||
|
||||
public class ClientConfig {
|
||||
public String serverIP;
|
||||
public int udpPort;
|
||||
public int tcpPort;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class MultiplayerScreen implements Screen {
|
||||
gameClient = new GameClient();
|
||||
|
||||
try {
|
||||
gameClient.connect("127.0.0.1", "Jugador");
|
||||
gameClient.connect("Jugador");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@ package io.github.eldek0.network;
|
||||
import com.esotericsoftware.kryonet.Client;
|
||||
import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.Listener;
|
||||
import com.google.gson.Gson;
|
||||
import io.github.eldek0.config.ClientConfig;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -13,8 +17,13 @@ public class GameClient {
|
||||
private final Client client;
|
||||
private final Map<Integer, NetworkPackets.PlayerState> players = new ConcurrentHashMap<>();
|
||||
private int myId = -1;
|
||||
private int udpPort = Network.DEFAULT_UDP_PORT;
|
||||
private int tcpPort = Network.DEFAULT_TCP_PORT;
|
||||
private String serverIP;
|
||||
|
||||
public GameClient() {
|
||||
this.loadClientConfig();
|
||||
|
||||
client = new Client();
|
||||
Network.register(client);
|
||||
|
||||
@@ -57,9 +66,9 @@ public class GameClient {
|
||||
});
|
||||
}
|
||||
|
||||
public void connect(String host, String name) throws IOException {
|
||||
public void connect(String name) throws IOException {
|
||||
client.start();
|
||||
client.connect(5000, host, Network.TCP_PORT, Network.UDP_PORT);
|
||||
client.connect(5000, this.serverIP, this.tcpPort, this.udpPort);
|
||||
|
||||
NetworkPackets.Login login = new NetworkPackets.Login();
|
||||
login.name = name;
|
||||
@@ -92,4 +101,17 @@ public class GameClient {
|
||||
public void stop() {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
private void loadClientConfig(){
|
||||
ClientConfig config = null;
|
||||
try {
|
||||
config = new Gson().fromJson(new FileReader("client-config.json"), ClientConfig.class);
|
||||
this.udpPort = config.udpPort;
|
||||
this.tcpPort = config.tcpPort;
|
||||
this.serverIP = config.serverIP;
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
23
core/src/main/java/io/github/eldek0/network/Network.java
Normal file
23
core/src/main/java/io/github/eldek0/network/Network.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package io.github.eldek0.network;
|
||||
|
||||
import com.esotericsoftware.kryo.Kryo;
|
||||
import com.esotericsoftware.kryonet.EndPoint;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Network {
|
||||
public static final int DEFAULT_TCP_PORT = 54555;
|
||||
public static final int DEFAULT_UDP_PORT = 54777;
|
||||
|
||||
public static void register(EndPoint endPoint) {
|
||||
Kryo kryo = endPoint.getKryo();
|
||||
|
||||
kryo.register(NetworkPackets.Login.class);
|
||||
kryo.register(NetworkPackets.PlayerConnected.class);
|
||||
kryo.register(NetworkPackets.PlayerDisconnected.class);
|
||||
kryo.register(NetworkPackets.PlayerMove.class);
|
||||
kryo.register(NetworkPackets.WorldState.class);
|
||||
kryo.register(NetworkPackets.PlayerState.class);
|
||||
|
||||
kryo.register(ArrayList.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package io.github.eldek0.network;
|
||||
|
||||
public class NetworkPackets {
|
||||
|
||||
public static class Login {
|
||||
public String name;
|
||||
}
|
||||
|
||||
public static class PlayerConnected {
|
||||
public int id;
|
||||
public String name;
|
||||
public float x;
|
||||
public float y;
|
||||
}
|
||||
|
||||
public static class PlayerDisconnected {
|
||||
public int id;
|
||||
}
|
||||
|
||||
public static class PlayerMove {
|
||||
public int id;
|
||||
public float x;
|
||||
public float y;
|
||||
}
|
||||
|
||||
public static class WorldState {
|
||||
public java.util.ArrayList<PlayerState> players = new java.util.ArrayList<>();
|
||||
}
|
||||
|
||||
public static class PlayerState {
|
||||
public int id;
|
||||
public String name;
|
||||
public float x;
|
||||
public float y;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user