Browse Source

It is now possible to see Projectiles created by other players

Tankernn 7 years ago
parent
commit
3d849fa7d5

+ 11 - 9
src/main/java/eu/tankernn/game/server/GameServer.java

@@ -6,6 +6,7 @@ import java.util.concurrent.TimeUnit;
 
 import org.lwjgl.util.vector.Vector3f;
 
+import eu.tankernn.gameEngine.entities.EntityState;
 import eu.tankernn.gameEngine.entities.GameContext;
 import eu.tankernn.gameEngine.entities.Light;
 import eu.tankernn.gameEngine.loader.models.AABB;
@@ -14,7 +15,6 @@ import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.group.ChannelGroup;
-import io.netty.channel.group.ChannelGroupFuture;
 import io.netty.channel.group.DefaultChannelGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
@@ -40,7 +40,7 @@ public class GameServer {
 		executor = Executors.newSingleThreadScheduledExecutor();
 
 		this.world = new World(1337);
-		world.getState().getLights().add(new Light(new Vector3f(1000, 1000, 0), new Vector3f(1, 1, 1)));
+		world.addLight(new Light(new Vector3f(1000, 1000, 0), new Vector3f(1, 1, 1)));
 		startListening();
 		executor.scheduleAtFixedRate(this::update, 0, 1000 / 16, TimeUnit.MILLISECONDS); // 64
 																							// tick
@@ -55,8 +55,7 @@ public class GameServer {
 
 				@Override
 				public float getTerrainHeight(float x, float z) {
-					// TODO Auto-generated method stub
-					return 1;
+					return world.getTerrainHeight(x, z);
 				}
 
 				@Override
@@ -68,14 +67,17 @@ public class GameServer {
 				@Override
 				public AABB getBoundingBox(int entityId) {
 					// TODO Auto-generated method stub
-					return new AABB(new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
+					return new AABB(new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)).updatePosition(world.getEntities().get(entityId).getState().getPosition());
+				}
+
+				@Override
+				public EntityState getEntity(int id) {
+					System.out.println("Request: " + id + ". Available: " + world.getEntities().values().toString());
+					return world.getEntities().get(id).getState();
 				}
 			};
 			world.update(ctx);
-			ChannelGroupFuture f = channelGroup.flushAndWrite(world.getState()).sync();
-			if (!f.isSuccess()) {
-				f.cause().printStackTrace();
-			}
+			channelGroup.flushAndWrite(world.getState()).sync();
 		} catch (InterruptedException e) {
 			e.printStackTrace();
 		} catch (Throwable t) {

+ 7 - 1
src/main/java/eu/tankernn/game/server/GameServerHandler.java

@@ -35,7 +35,13 @@ public class GameServerHandler extends ChannelInboundHandlerAdapter {
 			ctx.writeAndFlush(new LoginResponse(true, player.getState())).sync();
 		} else if (msg instanceof EntityState) {
 			EntityState state = (EntityState) msg;
-			player.setState(state);
+			if (state.getId() == player.getId())
+				player.setState(state);
+			else {
+				// Spawn new entity
+				state.resetId();
+				world.setEntityState(state);
+			}
 		} else {
 			System.err.println("Unknown message: " + msg.toString());
 		}

+ 32 - 19
src/main/java/eu/tankernn/game/server/World.java

@@ -2,10 +2,13 @@ package eu.tankernn.game.server;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 import eu.tankernn.game.server.entities.ServerEntity;
 import eu.tankernn.game.server.entities.player.ServerPlayer;
+import eu.tankernn.gameEngine.entities.EntityState;
 import eu.tankernn.gameEngine.entities.GameContext;
 import eu.tankernn.gameEngine.entities.ILight;
 import eu.tankernn.gameEngine.multiplayer.WorldState;
@@ -13,38 +16,48 @@ import eu.tankernn.gameEngine.multiplayer.WorldState;
 public class World {
 	private final int seed;
 	private final List<ILight> lights = new ArrayList<>();
-	private final List<ServerEntity> entities = new ArrayList<>();
-	private final List<ServerPlayer> players = new ArrayList<>();
-	
+	private final Map<Integer, ServerEntity> entities = new ConcurrentHashMap<>();
+
 	public World(int seed) {
 		this.seed = seed;
 	}
-	
+
 	public void update(GameContext ctx) {
-		entities.stream().forEach(e -> e.update(ctx));
+		entities.values().forEach(e -> e.update(ctx));
+		entities.values().removeIf(e -> e.getState().isDead());
 	}
-	
+
 	public WorldState getState() {
-		return new WorldState(seed, lights, entities.stream().map(ServerEntity::getState).collect(Collectors.toList()));
+		return new WorldState(seed, lights,
+				entities.values().stream().map(ServerEntity::getState).collect(Collectors.toList()));
 	}
-	
-	public List<ServerEntity> getEntities() {
+
+	public Map<Integer, ServerEntity> getEntities() {
 		return entities;
 	}
-	
-	public List<ServerPlayer> getPlayers() {
-		return players;
-	}
 
 	public void addPlayer(ServerPlayer player) {
-		players.add(player);
-		entities.add(player);
+		entities.put(player.getId(), player);
 	}
 
 	public void removePlayer(ServerPlayer player) {
-		players.remove(player);
-		entities.remove(player);
+		entities.remove(player.getId());
+	}
+
+	public void setEntityState(EntityState state) {
+		if (entities.containsKey(state.getId()))
+			entities.get(state.getId()).setState(state);
+		else
+			entities.put(state.getId(), new ServerEntity(state));
+	}
+
+	public float getTerrainHeight(float x, float z) {
+		// TODO
+		return 0;
 	}
-	
-	
+
+	public void addLight(ILight light) {
+		lights.add(light);
+	}
+
 }

+ 4 - 1
src/main/java/eu/tankernn/game/server/entities/ServerEntity.java

@@ -6,6 +6,10 @@ import eu.tankernn.gameEngine.entities.GameContext;
 public class ServerEntity {
 	protected EntityState state;
 
+	public ServerEntity(EntityState state) {
+		this.state = state;
+	}
+
 	public EntityState getState() {
 		return state;
 	}
@@ -13,7 +17,6 @@ public class ServerEntity {
 	public void setState(EntityState state) {
 		//this.state = state;
 		this.state.getVelocity().set(state.getVelocity());
-		this.state.getPosition().set(state.getPosition());
 		this.state.getRotation().set(state.getRotation());
 		this.state.setBehaviors(state.getBehaviors());
 	}

+ 1 - 1
src/main/java/eu/tankernn/game/server/entities/player/ServerPlayer.java

@@ -10,7 +10,7 @@ public class ServerPlayer extends ServerEntity {
 	private String username;
 	
 	public ServerPlayer(Channel channel) {
-		this.state = new EntityState();
+		super(new EntityState());
 		state.setModelId(0);
 		this.channel = channel;
 	}