mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 22:59:02 +00:00
Improved World update performance. Decreased save file size. Added Harvester NPC #19.
This commit is contained in:
@@ -147,8 +147,12 @@ public class GameServer implements Runnable {
|
||||
//Process each worlds
|
||||
//Avoid concurrent modification
|
||||
ArrayList<World> worlds = new ArrayList<>(gameUniverse.getWorlds());
|
||||
int updatedWorlds = 0;
|
||||
for (World world : worlds) {
|
||||
world.update();
|
||||
if (world.shouldUpdate()) {
|
||||
world.update();
|
||||
updatedWorlds++;
|
||||
}
|
||||
}
|
||||
|
||||
//Save
|
||||
@@ -163,7 +167,8 @@ public class GameServer implements Runnable {
|
||||
|
||||
socketServer.tick();
|
||||
|
||||
LogManager.LOGGER.info("Processed " + gameUniverse.getWorlds().size() + " worlds");
|
||||
LogManager.LOGGER.info("Processed " + gameUniverse.getWorlds().size() + " worlds (" + updatedWorlds +
|
||||
") updated");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,7 @@ public class Main {
|
||||
//Load
|
||||
GameServer.INSTANCE.getGameUniverse().load(new File("save.json"));
|
||||
|
||||
|
||||
SocketServer socketServer = new SocketServer(new InetSocketAddress(config.getString("webSocket_host"),
|
||||
config.getInt("webSocket_port")), config);
|
||||
|
||||
|
||||
@@ -58,4 +58,26 @@ public enum Direction {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get direction to move from (x1, y1) to (x2, y2)
|
||||
*/
|
||||
public static Direction getDirectionTo(int x1, int y1, int x2, int y2) {
|
||||
|
||||
int dx = x2 - x1;
|
||||
int dy = y2 - y1;
|
||||
|
||||
if (dx > 0 && dx >= dy) {
|
||||
return Direction.EAST;
|
||||
} else if (dx < 0 && dx <= dy) {
|
||||
return Direction.WEST;
|
||||
} else if (dy > 0 && dy >= dx) {
|
||||
return Direction.NORTH;
|
||||
} else if (dy < 0 && dy <= dx) {
|
||||
return Direction.SOUTH;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,9 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
|
||||
if (leftWorld != null) {
|
||||
world.getGameObjects().remove(this);
|
||||
world.decUpdatable();
|
||||
leftWorld.getGameObjects().add(this);
|
||||
leftWorld.incUpdatable();
|
||||
setWorld(leftWorld);
|
||||
|
||||
x = World.WORLD_SIZE - 1;
|
||||
@@ -99,7 +101,9 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
|
||||
if (rightWorld != null) {
|
||||
world.getGameObjects().remove(this);
|
||||
world.decUpdatable();
|
||||
rightWorld.getGameObjects().add(this);
|
||||
rightWorld.incUpdatable();
|
||||
setWorld(rightWorld);
|
||||
|
||||
x = 0;
|
||||
@@ -117,7 +121,9 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
|
||||
if (upWorld != null) {
|
||||
world.getGameObjects().remove(this);
|
||||
world.decUpdatable();
|
||||
upWorld.getGameObjects().add(this);
|
||||
upWorld.incUpdatable();
|
||||
setWorld(upWorld);
|
||||
|
||||
y = World.WORLD_SIZE - 1;
|
||||
@@ -135,7 +141,9 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
|
||||
if (downWorld != null) {
|
||||
world.getGameObjects().remove(this);
|
||||
world.decUpdatable();
|
||||
downWorld.getGameObjects().add(this);
|
||||
downWorld.incUpdatable();
|
||||
setWorld(downWorld);
|
||||
|
||||
y = 0;
|
||||
|
||||
@@ -30,6 +30,11 @@ public class World implements JSONSerialisable {
|
||||
|
||||
private ArrayList<GameObject> gameObjects = new ArrayList<>(16);
|
||||
|
||||
/**
|
||||
* If this number is greater than 0, the World will be updated.
|
||||
*/
|
||||
private int updatable = 0;
|
||||
|
||||
public World(int x, int y, TileMap tileMap) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@@ -114,13 +119,15 @@ public class World implements JSONSerialisable {
|
||||
for (GameObject obj : gameObjects_) {
|
||||
objects.add(obj.serialise());
|
||||
}
|
||||
json.put("objects", objects);
|
||||
json.put("o", objects);
|
||||
|
||||
json.put("terrain", tileMap.serialise());
|
||||
json.put("t", tileMap.serialise());
|
||||
|
||||
json.put("x", x);
|
||||
json.put("y", y);
|
||||
|
||||
json.put("u", updatable);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -145,10 +152,11 @@ public class World implements JSONSerialisable {
|
||||
World world = new World();
|
||||
world.x = (int) (long) json.get("x");
|
||||
world.y = (int) (long) json.get("y");
|
||||
world.updatable = (int) (long) json.get("u");
|
||||
|
||||
world.tileMap = TileMap.deserialize((JSONObject) json.get("terrain"));
|
||||
world.tileMap = TileMap.deserialize((JSONObject) json.get("t"));
|
||||
|
||||
for (JSONObject objJson : (ArrayList<JSONObject>) json.get("objects")) {
|
||||
for (JSONObject objJson : (ArrayList<JSONObject>) json.get("o")) {
|
||||
|
||||
GameObject object = GameObject.deserialize(objJson);
|
||||
|
||||
@@ -258,4 +266,15 @@ public class World implements JSONSerialisable {
|
||||
return gameObjects;
|
||||
}
|
||||
|
||||
public void incUpdatable() {
|
||||
updatable++;
|
||||
}
|
||||
|
||||
public void decUpdatable() {
|
||||
updatable--;
|
||||
}
|
||||
|
||||
public boolean shouldUpdate() {
|
||||
return updatable > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user