Prechádzať zdrojové kódy

Update to use GameContext + some other tweaks

Tankernn 7 rokov pred
rodič
commit
a11c5bff5d

+ 28 - 0
src/main/java/eu/tankernn/game/server/GameServer.java

@@ -6,7 +6,9 @@ import java.util.concurrent.TimeUnit;
 
 import org.lwjgl.util.vector.Vector3f;
 
+import eu.tankernn.gameEngine.entities.GameContext;
 import eu.tankernn.gameEngine.entities.Light;
+import eu.tankernn.gameEngine.loader.models.AABB;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
@@ -30,6 +32,8 @@ public class GameServer {
 	private World world;
 	private ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
 
+	private long lastTickTimeStampNano;
+
 	public GameServer(int port) {
 		this.port = port;
 
@@ -45,6 +49,29 @@ public class GameServer {
 
 	private void update() {
 		try {
+			double tickLengthSeconds = (System.nanoTime() - lastTickTimeStampNano) / 1000000000.0;
+			lastTickTimeStampNano = System.nanoTime();
+			GameContext ctx = new GameContext(false, (float) tickLengthSeconds, world.getState().getEntities()) {
+
+				@Override
+				public float getTerrainHeight(float x, float z) {
+					// TODO Auto-generated method stub
+					return 1;
+				}
+
+				@Override
+				public float getHeight(int entityId) {
+					// TODO Auto-generated method stub
+					return 1;
+				}
+
+				@Override
+				public AABB getBoundingBox(int entityId) {
+					// TODO Auto-generated method stub
+					return new AABB(new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
+				}
+			};
+			world.update(ctx);
 			ChannelGroupFuture f = channelGroup.flushAndWrite(world.getState()).sync();
 			if (!f.isSuccess()) {
 				f.cause().printStackTrace();
@@ -53,6 +80,7 @@ public class GameServer {
 			e.printStackTrace();
 		} catch (Throwable t) {
 			t.printStackTrace();
+			throw t;
 		}
 	}
 

+ 13 - 7
src/main/java/eu/tankernn/game/server/World.java

@@ -6,26 +6,32 @@ 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;
 
 public class World {
-	private WorldState state;
+	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<>();
 	
 	public World(int seed) {
-		this.state = new WorldState(seed);
+		this.seed = seed;
 	}
 	
-	public void update() {
-		state.getEntities().stream().forEach(EntityState::update);
+	public void update(GameContext ctx) {
+		entities.stream().forEach(e -> e.update(ctx));
 	}
 	
 	public WorldState getState() {
-		return new WorldState(state.getSeed(), state.getLights(), entities.stream().map(ServerEntity::getState).collect(Collectors.toList()));
+		return new WorldState(seed, lights, entities.stream().map(ServerEntity::getState).collect(Collectors.toList()));
 	}
-
+	
+	public List<ServerEntity> getEntities() {
+		return entities;
+	}
+	
 	public List<ServerPlayer> getPlayers() {
 		return players;
 	}

+ 8 - 3
src/main/java/eu/tankernn/game/server/entities/ServerEntity.java

@@ -1,6 +1,7 @@
 package eu.tankernn.game.server.entities;
 
 import eu.tankernn.gameEngine.entities.EntityState;
+import eu.tankernn.gameEngine.entities.GameContext;
 
 public class ServerEntity {
 	protected EntityState state;
@@ -10,10 +11,14 @@ public class ServerEntity {
 	}
 
 	public void setState(EntityState state) {
-		this.state = 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());
 	}
 	
-	public void update() {
-		state.update();
+	public void update(GameContext ctx) {
+		state.update(ctx);
 	}
 }