mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-16 09:16:44 +00:00
Universe warp around and maximum size #9
This commit is contained in:
parent
3ee9b4be95
commit
da7d050661
@ -71,7 +71,14 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
//Check if out of World bounds / collision
|
||||
if(newX < 0) {
|
||||
//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){
|
||||
world.getGameObjects().remove(this);
|
||||
@ -82,7 +89,13 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
}
|
||||
} else if(newX >= World.WORLD_SIZE) {
|
||||
//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){
|
||||
world.getGameObjects().remove(this);
|
||||
@ -92,25 +105,39 @@ public abstract class GameObject implements JSONSerialisable {
|
||||
x = 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)
|
||||
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){
|
||||
world.getGameObjects().remove(this);
|
||||
upWorld.getGameObjects().add(this);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ public class GameUniverse implements JSONSerialisable{
|
||||
|
||||
private int nextObjectId = 0;
|
||||
|
||||
private int maxWidth = 3; //0xFFFF
|
||||
|
||||
public GameUniverse(ServerConfiguration config) {
|
||||
|
||||
worlds = new ArrayList<>(32);
|
||||
@ -50,15 +52,18 @@ public class GameUniverse implements JSONSerialisable{
|
||||
}
|
||||
}
|
||||
|
||||
//World does not exist
|
||||
LogManager.LOGGER.severe("Trying to read a World that does not exist!");
|
||||
if (x >= 0 && x <= maxWidth && y >= 0 && y <= maxWidth) {
|
||||
//World does not exist
|
||||
LogManager.LOGGER.severe("Trying to read a World that does not exist!");
|
||||
|
||||
World world = createWorld(x,y);
|
||||
World world = createWorld(x, y);
|
||||
|
||||
worlds.add(world);
|
||||
|
||||
return world;
|
||||
worlds.add(world);
|
||||
|
||||
return world;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public World createWorld(int x, int y) {
|
||||
@ -256,4 +261,8 @@ public class GameUniverse implements JSONSerialisable{
|
||||
users.remove(user);
|
||||
|
||||
}
|
||||
|
||||
public int getMaxWidth() {
|
||||
return maxWidth;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package net.simon987.server.webserver;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
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 org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
@ -18,25 +18,28 @@ public class ObjectsRequestHandler implements MessageHandler {
|
||||
if (json.get("t").equals("object")) {
|
||||
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")) {
|
||||
int x = Long.valueOf((long) json.get("x")).intValue();
|
||||
int y = Long.valueOf((long) json.get("y")).intValue();
|
||||
World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y);
|
||||
|
||||
ArrayList<GameObject> gameObjects = GameServer.INSTANCE.getGameUniverse().getWorld(x, y).getGameObjects();
|
||||
if (world != null) {
|
||||
ArrayList<GameObject> gameObjects = world.getGameObjects();
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
JSONArray objects = new JSONArray();
|
||||
|
||||
|
||||
for (GameObject object : gameObjects) {
|
||||
|
||||
if (object instanceof JSONSerialisable) {
|
||||
objects.add(object.serialise());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
response.put("t", "object");
|
||||
response.put("objects", objects);
|
||||
|
||||
@ -44,8 +47,6 @@ public class ObjectsRequestHandler implements MessageHandler {
|
||||
if (user.getWebSocket().isOpen()) {
|
||||
user.getWebSocket().send(response.toJSONString());
|
||||
}
|
||||
} else {
|
||||
LogManager.LOGGER.info("(WS) Malformed Objects request from " + user.getUser().getUsername());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,13 +10,19 @@ public class TerrainRequestHandler implements MessageHandler {
|
||||
|
||||
@Override
|
||||
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());
|
||||
World world;
|
||||
try {
|
||||
world = GameServer.INSTANCE.getGameUniverse().getWorld(
|
||||
Long.valueOf((long) json.get("x")).intValue(),
|
||||
Long.valueOf((long) json.get("y")).intValue());
|
||||
} catch (NullPointerException e) {
|
||||
LogManager.LOGGER.severe("FIXME: handle TerrainRequestHandler");
|
||||
return;
|
||||
}
|
||||
|
||||
World world = GameServer.INSTANCE.getGameUniverse().getWorld(
|
||||
Long.valueOf((long) json.get("x")).intValue(),
|
||||
Long.valueOf((long) json.get("y")).intValue());
|
||||
|
||||
//todo It might be a good idea to cache this
|
||||
if (world != null) {
|
||||
|
@ -14,26 +14,22 @@ public class UserInfoRequestHandler implements MessageHandler {
|
||||
if (message.get("t").equals("userInfo")) {
|
||||
|
||||
LogManager.LOGGER.info("(WS) User info request from " + user.getUser().getUsername());
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
if(user.isGuest()) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("t", "userInfo");
|
||||
json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX"));
|
||||
json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY"));
|
||||
|
||||
user.getWebSocket().send(json.toJSONString());
|
||||
} else {
|
||||
GameObject object = (GameObject)user.getUser().getControlledUnit();
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("t", "userInfo");
|
||||
json.put("worldX", object.getWorld().getX());
|
||||
json.put("worldY", object.getWorld().getY());
|
||||
|
||||
user.getWebSocket().send(json.toJSONString());
|
||||
}
|
||||
|
||||
|
||||
json.put("t", "userInfo");
|
||||
json.put("maxWidth", GameServer.INSTANCE.getGameUniverse().getMaxWidth());
|
||||
user.getWebSocket().send(json.toJSONString());
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user