Skip to content

Commit

Permalink
successfully implemented server/client, at least so that the client l…
Browse files Browse the repository at this point in the history
…oads the first few initial chunks
  • Loading branch information
chrisj42 committed Mar 1, 2018
1 parent b211d4c commit 05ed050
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 68 deletions.
15 changes: 5 additions & 10 deletions core/client/src/miniventure/client/ClientWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,28 @@ public class ClientWorld implements WorldManager {

private final GameScreen gameScreen;

private boolean worldLoaded = false;

private GameClient client;

private Player mainPlayer;
private float gameTime;

ClientWorld(GameScreen gameScreen) {
this.gameScreen = gameScreen;
worldLoaded = false;

client = new GameClient(); // doesn't automatically connect
}

@Override
public boolean worldLoaded() { return worldLoaded; }
public boolean worldLoaded() { return Level.hasLevels(); }

@Override
public void createWorld(int width, int height) {
worldLoaded = false;

LoadingScreen loadingScreen = new LoadingScreen();
GameCore.setScreen(loadingScreen);
gameTime = 0;

gameTime = 0;
Level.clearLevels();

new Thread(() -> {
ServerCore.initServer(width, height);

Expand All @@ -73,7 +69,7 @@ public void createWorld(int width, int height) {

@Override
public void update(float delta) {
if(!worldLoaded || mainPlayer == null) return;
if(!worldLoaded() || mainPlayer == null) return;

MenuScreen menu = GameCore.getScreen();

Expand All @@ -95,14 +91,13 @@ public void update(float delta) {
@Override
public void exitWorld(boolean save) { // returns to title screen
// set menu to main menu, and dispose of level/world resources
worldLoaded = false;
mainPlayer = null;
Level.clearLevels();
GameCore.setScreen(new MainMenu(this));
}

public void spawnPlayer(float x, float y) {
Level level = mainPlayer == null ? Level.getLevel(0) : mainPlayer.getLevel();
Level level = Level.getLevel(0);//mainPlayer == null ? : mainPlayer.getLevel();
if(mainPlayer != null)
mainPlayer.remove();

Expand Down
10 changes: 8 additions & 2 deletions core/client/src/miniventure/client/GameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import miniventure.game.GameProtocol.Login;
import miniventure.game.GameProtocol.SpawnData;
import miniventure.game.screen.LoadingScreen;
import miniventure.game.world.Chunk.ChunkData;
import miniventure.game.world.Level;

import com.esotericsoftware.kryonet.Client;
Expand All @@ -21,7 +22,7 @@ public class GameClient {
private Client client;

public GameClient() {
client = new Client();
client = new Client(16384*2, 16384);

GameProtocol.registerClasses(client.getKryo());

Expand All @@ -30,7 +31,12 @@ public GameClient() {
public void received (Connection connection, Object object) {
if(object instanceof LevelData) {
System.out.println("client received level");
Level.resetLevels(ClientCore.getWorld(), (LevelData)object);
Level.addLevel(ClientCore.getWorld(), (LevelData)object);
}

if(object instanceof ChunkData) {
//System.out.println("client received chunk");
Level.loadChunk((ChunkData)object);
}

if(object instanceof SpawnData) {
Expand Down
12 changes: 8 additions & 4 deletions core/game/src/miniventure/game/GameProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import miniventure.game.util.Version;
import miniventure.game.world.Chunk.ChunkData;
import miniventure.game.world.Level;
import miniventure.game.world.tile.Tile.TileData;

import com.esotericsoftware.kryo.Kryo;
Expand Down Expand Up @@ -36,7 +37,7 @@ class Login {
public final String username;
public final Version version;

public Login() { this(null, null); }
private Login() { this(null, null); }
public Login(String username, Version version) {
this.username = username;
this.version = version;
Expand All @@ -46,18 +47,21 @@ public Login(String username, Version version) {
class LevelData {
public final int width;
public final int height;
public final ChunkData[] chunkData;
public final int depth;

public LevelData(int width, int height, ChunkData[] data) {
private LevelData() { this(0, 0, 0); }
public LevelData(Level level) { this(level.getWidth(), level.getHeight(), level.getDepth()); }
public LevelData(int width, int height, int depth) {
this.width = width;
this.height = height;
chunkData = data;
this.depth = depth;
}
}

class SpawnData {
public final float x, y;

private SpawnData() { this(0, 0); }
public SpawnData(float x, float y) {
this.x = x;
this.y = y;
Expand Down
14 changes: 9 additions & 5 deletions core/game/src/miniventure/game/world/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Chunk(@NotNull Level level, @NotNull ChunkData data) {
this.tiles = new Tile[width][];
int height = 0;
for(int x = 0; x < tiles.length; x++) {
tiles[x] = new Tile[tiles[x].length];
tiles[x] = new Tile[data.tileData[x].length];
height = Math.max(height, tiles[x].length);
for(int y = 0; y < tiles[x].length; y++) {
TileData tileData = data.tileData[x][y];
Expand Down Expand Up @@ -69,21 +69,25 @@ public static int getCoord(float pos) {
return worldCoord / SIZE;
}

public Rectangle getBounds() { return new Rectangle(chunkX*SIZE, chunkY*SIZE, SIZE, SIZE); }
public Rectangle getBounds() { return new Rectangle(chunkX*SIZE, chunkY*SIZE, width, height); }


public static class ChunkData {
public final int chunkX, chunkY;
public final int chunkX, chunkY, levelDepth;
public final TileData[][] tileData;

public ChunkData(int chunkX, int chunkY, TileData[][] data) {
private ChunkData() { this(0, 0, 0, null); }
public ChunkData(int chunkX, int chunkY, int depth, TileData[][] data) {
this.chunkX = chunkX;
this.chunkY = chunkY;
this.levelDepth = depth;
this.tileData = data;
}
public ChunkData(Chunk chunk) {
public ChunkData(Chunk chunk, Level level) { this(chunk, level.getDepth()); }
public ChunkData(Chunk chunk, int levelDepth) {
chunkX = chunk.chunkX;
chunkY = chunk.chunkY;
this.levelDepth = levelDepth;

Tile[][] tiles = chunk.getTiles();
tileData = new TileData[tiles.length][];
Expand Down
49 changes: 18 additions & 31 deletions core/game/src/miniventure/game/world/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@
import java.util.HashMap;
import java.util.HashSet;

import miniventure.game.GameCore;
import miniventure.game.GameProtocol.LevelData;
import miniventure.game.WorldManager;
import miniventure.game.item.Item;
import miniventure.game.screen.LoadingScreen;
import miniventure.game.util.MyUtils;
import miniventure.game.world.Chunk.ChunkData;
import miniventure.game.world.entity.Entity;
import miniventure.game.world.entity.ItemEntity;
import miniventure.game.world.entity.Particle;
import miniventure.game.world.entity.mob.AiType;
import miniventure.game.world.entity.mob.Mob;
import miniventure.game.world.entity.mob.Player;
import miniventure.game.world.levelgen.LevelGenerator;
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
Expand Down Expand Up @@ -106,7 +96,6 @@ public void addEntity(Entity e, float x, float y, boolean center) {
y -= size.y/2;
}
e.moveTo(this, x, y);
addEntity(e);
}
public void addEntity(Entity e) {
entities.add(e);
Expand Down Expand Up @@ -315,37 +304,35 @@ public boolean chunkExists(int x, int y) {



private static final String[] levelNames = {"Surface"};
private static final int minDepth = 0;

private static Level[] levels = new Level[0];
private static final HashMap<Integer, Level> levels = new HashMap<>();
private static final HashMap<Entity, Level> entityLevels = new HashMap<>();

public static boolean hasLevels() { return levels.size() > 0; }

public static void clearLevels() {
entityLevels.clear();
for(Level level: levels)
for(Level level: levels.values())
level.entities.clear();
levels = new Level[0];
levels.clear();
}

public static void resetLevels(WorldManager world, LevelData... levelData) {
clearLevels();
levels = new Level[levelData.length];
for(int i = 0; i < levels.length; i++) {
LevelData data = levelData[i];
levels[i] = new Level(world, i, data.width, data.height);
for(ChunkData chunk: data.chunkData) {
Chunk newChunk = new Chunk(levels[i], chunk);
levels[i].loadedChunks.put(new Point(chunk.chunkX, chunk.chunkY), newChunk);
}
public static void addLevel(WorldManager world, LevelData data) {
levels.put(data.depth, new Level(world, data.depth, data.width, data.height));
}

public static void loadChunk(ChunkData data) {
Level level = levels.get(data.levelDepth);
if(level == null) {
System.err.println("client could not load chunk because level is null");
return;
}
Chunk newChunk = new Chunk(level, data);
level.tileCount += newChunk.width * newChunk.height;
level.loadedChunks.put(new Point(data.chunkX, data.chunkY), newChunk);
}

@Nullable
public static Level getLevel(int depth) {
int idx = depth-minDepth;
return idx >= 0 && idx < levels.length ? levels[idx] : null;
}
public static Level getLevel(int depth) { return levels.get(depth); }

@Nullable
public static Level getEntityLevel(Entity entity) { return entityLevels.get(entity); }
Expand Down
11 changes: 6 additions & 5 deletions core/game/src/miniventure/game/world/ServerLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.HashMap;

import miniventure.game.GameCore;
import miniventure.game.GameProtocol.LevelData;
import miniventure.game.WorldManager;
import miniventure.game.item.Item;
import miniventure.game.screen.LoadingScreen;
Expand Down Expand Up @@ -58,6 +57,8 @@ public void entityMoved(Entity entity) {
}

public void update(float delta) {
if(loadedChunks.size() == 0) return;

int tilesToUpdate = (int) (percentTilesUpdatedPerSecond * tileCount * delta);

Object[] chunks = loadedChunks.values().toArray();
Expand Down Expand Up @@ -185,7 +186,7 @@ public void spawnMob(Mob mob, Rectangle spawnArea, boolean loadedChunksOnly) {
}
}

public LevelData createClientLevel(Player client) {
public ChunkData[] createClientLevel(Player client) {
// creates a new level instance to send to the new client

Array<Point> points = getAreaChunks(client.getCenter(), 1, true, true);
Expand All @@ -196,10 +197,10 @@ public LevelData createClientLevel(Player client) {
for (int i = 0; i < points.size; i++) {
Point p = points.get(i);
Chunk chunk = loadedChunks.containsKey(p) ? loadedChunks.get(p) : new Chunk(p.x, p.y, this, levelGenerator.generateChunk(p.x, p.y));
chunks[i] = new ChunkData(chunk);
chunks[i] = new ChunkData(chunk, this);
}

return new LevelData(getWidth(), getHeight(), chunks);
return chunks;
}

@Override
Expand All @@ -226,7 +227,7 @@ public static ServerLevel getLevel(int depth) {
return idx >= 0 && idx < levels.length ? levels[idx] : null;
}

public static void resetLevels(WorldManager world, LevelGenerator levelGenerator) {
public static void generateLevels(WorldManager world, LevelGenerator levelGenerator) {
MenuScreen menu = GameCore.getScreen();
LoadingScreen display = null;
if(menu != null && menu instanceof LoadingScreen) {
Expand Down
7 changes: 6 additions & 1 deletion core/game/src/miniventure/game/world/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ private float moveAxis(@NotNull Level level, boolean xaxis, float amt, float oth

public void moveTo(@NotNull Level level, @NotNull Vector2 pos) { moveTo(level, pos.x, pos.y); }
public void moveTo(@NotNull Level level, float x, float y) {
if(level == getLevel() && x == this.x && y == this.y) return; // no action or updating required.

// this method doesn't care where you end up.
x = Math.max(x, 0);
y = Math.max(y, 0);
Expand All @@ -188,7 +190,10 @@ public void moveTo(@NotNull Level level, float x, float y) {
this.x = x;
this.y = y;

level.entityMoved(this);
if(level == getLevel())
level.entityMoved(this);
else
level.addEntity(this);
}
public void moveTo(@NotNull Tile tile) {
Vector2 pos = tile.getCenter();
Expand Down
1 change: 1 addition & 0 deletions core/game/src/miniventure/game/world/tile/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public static class TileData {
public final int[] typeOrdinals;
public final String[] data;

private TileData() { this(null, null); }
public TileData(int[] typeOrdinals, String[] data) {
this.typeOrdinals = typeOrdinals;
this.data = data;
Expand Down
11 changes: 6 additions & 5 deletions core/server/src/miniventure/server/GameServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import java.io.IOException;
import java.util.HashMap;

import miniventure.game.GameCore;
import miniventure.game.GameProtocol;
import miniventure.game.GameProtocol.LevelData;
import miniventure.game.GameProtocol.Login;
import miniventure.game.GameProtocol.SpawnData;
import miniventure.game.world.Level;
import miniventure.game.world.Chunk.ChunkData;
import miniventure.game.world.ServerLevel;
import miniventure.game.world.entity.mob.Player;

Expand All @@ -24,7 +23,7 @@ public class GameServer {
private Server server;

public GameServer() {
server = new Server();
server = new Server(16384*2, 16384);
GameProtocol.registerClasses(server.getKryo());

addListener(new Listener() {
Expand All @@ -38,8 +37,10 @@ public void received (Connection connection, Object object) {
if(level != null) {
Player player = ServerCore.getWorld().addPlayer();
playerConnections.put(connection, player);
LevelData playerLevel = level.createClientLevel(player);
connection.sendTCP(playerLevel);
ChunkData[] playerChunks = level.createClientLevel(player);
connection.sendTCP(new LevelData(level));
for(ChunkData chunk: playerChunks)
connection.sendTCP(chunk);
Vector2 pos = player.getPosition();
connection.sendTCP(new SpawnData(pos.x, pos.y));
}
Expand Down

0 comments on commit 05ed050

Please sign in to comment.