Universe warp around and maximum size #9

This commit is contained in:
simon 2017-11-13 22:45:19 -05:00
parent 3ee9b4be95
commit da7d050661
5 changed files with 82 additions and 43 deletions

View File

@ -71,7 +71,14 @@ public abstract class GameObject implements JSONSerialisable {
//Check if out of World bounds / collision //Check if out of World bounds / collision
if(newX < 0) { if(newX < 0) {
//Move object to adjacent World (left) //Move object to adjacent World (left)
World leftWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() - 1, world.getY()); World leftWorld;
if (world.getX() == 0) {
//Warp around
leftWorld = GameServer.INSTANCE.getGameUniverse().getWorld(
GameServer.INSTANCE.getGameUniverse().getMaxWidth(), world.getY());
} else {
leftWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() - 1, world.getY());
}
if(leftWorld != null){ if(leftWorld != null){
world.getGameObjects().remove(this); world.getGameObjects().remove(this);
@ -82,7 +89,13 @@ public abstract class GameObject implements JSONSerialisable {
} }
} else if(newX >= World.WORLD_SIZE) { } else if(newX >= World.WORLD_SIZE) {
//Move object to adjacent World (right) //Move object to adjacent World (right)
World rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() + 1, world.getY()); World rightWorld;
if (world.getX() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
//Warp around
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(0, world.getY());
} else {
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() + 1, world.getY());
}
if(rightWorld != null){ if(rightWorld != null){
world.getGameObjects().remove(this); world.getGameObjects().remove(this);
@ -92,25 +105,39 @@ public abstract class GameObject implements JSONSerialisable {
x = 0; x = 0;
} }
} else if (newY < 0) { } else if (newY < 0) {
//Move object to adjacent World (down)
World downWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() - 1);
if(downWorld != null){
world.getGameObjects().remove(this);
downWorld.getGameObjects().add(this);
setWorld(downWorld);
y = World.WORLD_SIZE - 1;
}
} else if(newY >= World.WORLD_SIZE) {
//Move object to adjacent World (up) //Move object to adjacent World (up)
World upWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() + 1); World upWorld;
if (world.getY() == 0) {
//Warp around
upWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(),
GameServer.INSTANCE.getGameUniverse().getMaxWidth());
} else {
upWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() - 1);
}
if(upWorld != null){ if(upWorld != null){
world.getGameObjects().remove(this); world.getGameObjects().remove(this);
upWorld.getGameObjects().add(this); upWorld.getGameObjects().add(this);
setWorld(upWorld); setWorld(upWorld);
y = World.WORLD_SIZE - 1;
}
} else if (newY >= World.WORLD_SIZE) {
//Move object to adjacent World (down)
World downWorld;
if (world.getY() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
//Warp around
downWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), 0);
} else {
downWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() + 1);
}
if (downWorld != null) {
world.getGameObjects().remove(this);
downWorld.getGameObjects().add(this);
setWorld(downWorld);
y = 0; y = 0;
} }
} }

View File

@ -29,6 +29,8 @@ public class GameUniverse implements JSONSerialisable{
private int nextObjectId = 0; private int nextObjectId = 0;
private int maxWidth = 3; //0xFFFF
public GameUniverse(ServerConfiguration config) { public GameUniverse(ServerConfiguration config) {
worlds = new ArrayList<>(32); worlds = new ArrayList<>(32);
@ -50,6 +52,7 @@ public class GameUniverse implements JSONSerialisable{
} }
} }
if (x >= 0 && x <= maxWidth && y >= 0 && y <= maxWidth) {
//World does not exist //World does not exist
LogManager.LOGGER.severe("Trying to read a World that does not exist!"); LogManager.LOGGER.severe("Trying to read a World that does not exist!");
@ -58,7 +61,9 @@ public class GameUniverse implements JSONSerialisable{
worlds.add(world); worlds.add(world);
return world; return world;
} else {
return null;
}
} }
public World createWorld(int x, int y) { public World createWorld(int x, int y) {
@ -256,4 +261,8 @@ public class GameUniverse implements JSONSerialisable{
users.remove(user); users.remove(user);
} }
public int getMaxWidth() {
return maxWidth;
}
} }

View File

@ -2,7 +2,7 @@ package net.simon987.server.webserver;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject; import net.simon987.server.game.GameObject;
import net.simon987.server.io.JSONSerialisable; import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -18,25 +18,28 @@ public class ObjectsRequestHandler implements MessageHandler {
if (json.get("t").equals("object")) { if (json.get("t").equals("object")) {
LogManager.LOGGER.info("(WS) Objects request from " + user.getUser().getUsername()); LogManager.LOGGER.info("(WS) Objects request from " + user.getUser().getUsername());
int x, y;
try {
x = Long.valueOf((long) json.get("x")).intValue();
y = Long.valueOf((long) json.get("y")).intValue();
} catch (Exception e) {
LogManager.LOGGER.info("(WS) Malformed Objects request from " + user.getUser().getUsername());
return;
}
if (json.containsKey("x") && json.containsKey("y")) { World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y);
int x = Long.valueOf((long) json.get("x")).intValue();
int y = Long.valueOf((long) json.get("y")).intValue();
ArrayList<GameObject> gameObjects = GameServer.INSTANCE.getGameUniverse().getWorld(x, y).getGameObjects(); if (world != null) {
ArrayList<GameObject> gameObjects = world.getGameObjects();
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
JSONArray objects = new JSONArray(); JSONArray objects = new JSONArray();
for (GameObject object : gameObjects) { for (GameObject object : gameObjects) {
if (object instanceof JSONSerialisable) {
objects.add(object.serialise()); objects.add(object.serialise());
} }
}
response.put("t", "object"); response.put("t", "object");
response.put("objects", objects); response.put("objects", objects);
@ -44,8 +47,6 @@ public class ObjectsRequestHandler implements MessageHandler {
if (user.getWebSocket().isOpen()) { if (user.getWebSocket().isOpen()) {
user.getWebSocket().send(response.toJSONString()); user.getWebSocket().send(response.toJSONString());
} }
} else {
LogManager.LOGGER.info("(WS) Malformed Objects request from " + user.getUser().getUsername());
} }
} }
} }

View File

@ -10,13 +10,19 @@ public class TerrainRequestHandler implements MessageHandler {
@Override @Override
public void handle(OnlineUser user, JSONObject json) { public void handle(OnlineUser user, JSONObject json) {
if (json.get("t").equals("terrain")) { if (json.get("t").equals("terrain") && json.containsKey("x") && json.containsKey("y")) {
LogManager.LOGGER.info("Terrain request from " + user.getUser().getUsername()); LogManager.LOGGER.info("Terrain request from " + user.getUser().getUsername());
World world;
World world = GameServer.INSTANCE.getGameUniverse().getWorld( try {
world = GameServer.INSTANCE.getGameUniverse().getWorld(
Long.valueOf((long) json.get("x")).intValue(), Long.valueOf((long) json.get("x")).intValue(),
Long.valueOf((long) json.get("y")).intValue()); Long.valueOf((long) json.get("y")).intValue());
} catch (NullPointerException e) {
LogManager.LOGGER.severe("FIXME: handle TerrainRequestHandler");
return;
}
//todo It might be a good idea to cache this //todo It might be a good idea to cache this
if (world != null) { if (world != null) {

View File

@ -14,26 +14,22 @@ public class UserInfoRequestHandler implements MessageHandler {
if (message.get("t").equals("userInfo")) { if (message.get("t").equals("userInfo")) {
LogManager.LOGGER.info("(WS) User info request from " + user.getUser().getUsername()); LogManager.LOGGER.info("(WS) User info request from " + user.getUser().getUsername());
JSONObject json = new JSONObject();
if(user.isGuest()) { if(user.isGuest()) {
JSONObject json = new JSONObject();
json.put("t", "userInfo");
json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX")); json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX"));
json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY")); json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY"));
user.getWebSocket().send(json.toJSONString());
} else { } else {
GameObject object = (GameObject)user.getUser().getControlledUnit(); GameObject object = (GameObject)user.getUser().getControlledUnit();
JSONObject json = new JSONObject();
json.put("t", "userInfo");
json.put("worldX", object.getWorld().getX()); json.put("worldX", object.getWorld().getX());
json.put("worldY", object.getWorld().getY()); json.put("worldY", object.getWorld().getY());
}
json.put("t", "userInfo");
json.put("maxWidth", GameServer.INSTANCE.getGameUniverse().getMaxWidth());
user.getWebSocket().send(json.toJSONString()); user.getWebSocket().send(json.toJSONString());
}
} }