Эх сурвалжийг харах

Simple online multiplayer started

Tankernn 7 жил өмнө
parent
commit
5099d35260

+ 6 - 6
pom.xml

@@ -20,12 +20,6 @@
 			<artifactId>tankernn-game-engine</artifactId>
 			<version>1.2</version>
 		</dependency>
-		<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
-		<dependency>
-			<groupId>io.netty</groupId>
-			<artifactId>netty-all</artifactId>
-			<version>4.1.6.Final</version>
-		</dependency>
 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
@@ -33,6 +27,12 @@
 			<scope>test</scope>
 		</dependency>
 
+		<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
+		<dependency>
+			<groupId>io.netty</groupId>
+			<artifactId>netty-all</artifactId>
+			<version>4.0.4.Final</version>
+		</dependency>
 	</dependencies>
 
 	<build>

+ 51 - 0
src/main/java/eu/tankernn/game/Game.java

@@ -4,6 +4,7 @@ import static eu.tankernn.game.Settings.DUDV_MAP;
 import static eu.tankernn.game.Settings.GAME_NAME;
 import static eu.tankernn.game.Settings.NIGHT_TEXTURE_FILES;
 import static eu.tankernn.game.Settings.NORMAL_MAP;
+import static eu.tankernn.game.Settings.ONLINE;
 import static eu.tankernn.game.Settings.TEXTURE_FILES;
 
 import java.io.FileNotFoundException;
@@ -14,6 +15,7 @@ import org.lwjgl.input.Keyboard;
 import org.lwjgl.util.vector.Vector2f;
 import org.lwjgl.util.vector.Vector3f;
 
+import eu.tankernn.game.networking.GameClientHandler;
 import eu.tankernn.gameEngine.GameLauncher;
 import eu.tankernn.gameEngine.TankernnGame3D;
 import eu.tankernn.gameEngine.entities.Entity3D;
@@ -47,6 +49,19 @@ import eu.tankernn.gameEngine.terrains.Terrain;
 import eu.tankernn.gameEngine.terrains.TerrainPack;
 import eu.tankernn.gameEngine.util.InternalFile;
 import eu.tankernn.gameEngine.util.MousePicker;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.serialization.ClassResolvers;
+import io.netty.handler.codec.serialization.ObjectDecoder;
+import io.netty.handler.codec.serialization.ObjectEncoder;
+import io.netty.handler.timeout.IdleStateHandler;
+import io.netty.handler.timeout.ReadTimeoutHandler;
 
 public class Game extends TankernnGame3D {
 	private float cooldown;
@@ -57,6 +72,8 @@ public class Game extends TankernnGame3D {
 	private FlareManager flareManager;
 	private Sun sun;
 
+	private Channel channel;
+
 	public Game() {
 		super(GAME_NAME, TEXTURE_FILES, NIGHT_TEXTURE_FILES);
 
@@ -123,6 +140,35 @@ public class Game extends TankernnGame3D {
 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		}
+
+		if (ONLINE)
+			connectOnline();
+	}
+
+	private void connectOnline() {
+		EventLoopGroup workerGroup = new NioEventLoopGroup();
+
+		try {
+			Bootstrap b = new Bootstrap();
+			b.group(workerGroup);
+			b.channel(NioSocketChannel.class);
+			b.option(ChannelOption.SO_KEEPALIVE, true);
+			b.handler(new ChannelInitializer<SocketChannel>() {
+				@Override
+				public void initChannel(SocketChannel ch) throws Exception {
+					ch.pipeline().addLast("objectDecoder", new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
+					ch.pipeline().addLast("objectEncoder", new ObjectEncoder());
+					ch.pipeline().addLast("timeouthandler", new ReadTimeoutHandler(30));
+					ch.pipeline().addLast(new IdleStateHandler(0, 0, 29));
+					ch.pipeline().addLast(new GameClientHandler(Game.this));
+				}
+			});
+
+			// Start the client.
+			channel = b.connect("localhost", 25566).sync().channel();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
 	}
 
 	private void setupFlares() {
@@ -202,6 +248,11 @@ public class Game extends TankernnGame3D {
 
 		if (cooldown > 0)
 			cooldown -= DisplayManager.getFrameTimeSeconds();
+
+		if (channel != null) { // Send player pos to server
+			channel.writeAndFlush(player.getPosition());
+		}
+
 	}
 
 	@Override

+ 0 - 57
src/main/java/eu/tankernn/game/Launcher.java

@@ -1,57 +0,0 @@
-package eu.tankernn.game;
-
-import static eu.tankernn.game.Settings.GAME_NAME;
-
-import org.lwjgl.opengl.Display;
-
-import eu.tankernn.gameEngine.TankernnGame;
-import eu.tankernn.gameEngine.renderEngine.DisplayManager;
-import eu.tankernn.gameEngine.util.NativesExporter;
-
-public class Launcher {
-	
-	public static TankernnGame instance;
-	
-	public static void main(String[] args) {
-		init(args[0], Integer.parseInt(args[1]));
-		
-		while (!Display.isCloseRequested()) {
-			instance.update();
-			instance.fullRender();
-		}
-		
-		instance.cleanUp();
-	}
-	
-	private static void init(String host, int port) {
-		NativesExporter.exportNatives();
-		DisplayManager.createDisplay(GAME_NAME);
-		instance = new Game();
-		
-//		EventLoopGroup workerGroup = new NioEventLoopGroup();
-//
-//        try {
-//            Bootstrap b = new Bootstrap(); // (1)
-//            b.group(workerGroup); // (2)
-//            b.channel(NioSocketChannel.class); // (3)
-//            b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
-//            b.handler(new ChannelInitializer<SocketChannel>() {
-//                @Override
-//                public void initChannel(SocketChannel ch) throws Exception {
-//                    ch.pipeline().addLast(new GameClientHandler());
-//                }
-//            });
-//
-//            // Start the client.
-//            ChannelFuture f = b.connect(host, port).sync(); // (5)
-//
-//            // Wait until the connection is closed.
-//            f.channel().closeFuture().sync();
-//        } catch (InterruptedException e) {
-//			e.printStackTrace();
-//		} finally {
-//            workerGroup.shutdownGracefully();
-//        }
-	}
-	
-}

+ 1 - 0
src/main/java/eu/tankernn/game/Settings.java

@@ -1,6 +1,7 @@
 package eu.tankernn.game;
 
 public class Settings {
+	public static final boolean ONLINE = true;
 	public static final boolean DEBUG = false;
 	public static final String GAME_NAME = "Tankernn game engine tester";
 

+ 35 - 4
src/main/java/eu/tankernn/game/networking/GameClientHandler.java

@@ -1,15 +1,46 @@
 package eu.tankernn.game.networking;
 
-import eu.tankernn.gameEngine.Chunk;
+import eu.tankernn.gameEngine.TankernnGame3D;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.handler.timeout.IdleStateEvent;
 
 public class GameClientHandler extends ChannelInboundHandlerAdapter {
+
+	private TankernnGame3D game;
+
+	public GameClientHandler(TankernnGame3D instance) {
+		this.game = instance;
+	}
+
+	@Override
+	public void channelActive(ChannelHandlerContext ctx) throws Exception {
+		ctx.writeAndFlush("Username");
+	}
+
 	@Override
 	public void channelRead(ChannelHandlerContext ctx, Object msg) {
-		Chunk w = (Chunk) msg;
-		
-		
+		if (msg instanceof Integer) {
+			System.out.println("Seed: " + msg);
+		}
+
+		// else if (msg instanceof Pair<Integer, Vector3f>) {
+		// ByteBuf buf = (ByteBuf) msg;
+		// int entityId = buf.readInt();
+		// float x = buf.readFloat();
+		// float y = buf.readFloat();
+		// float z = buf.readFloat();
+		// game.getEntities().stream().filter(e -> e.getId() ==
+		// entityId).findFirst()
+		// .ifPresent(e -> e.getPosition().set(x, y, z));
+		// }
+	}
+
+	@Override
+	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
+		if (evt instanceof IdleStateEvent) {
+			ctx.writeAndFlush(new Object());
+		}
 	}
 
 	@Override