Changed the walking function to enable interaction with Enterable objects

This commit is contained in:
simon 2018-01-17 21:11:16 -05:00
parent 3505a466bb
commit 815f3de234
2 changed files with 58 additions and 110 deletions

View File

@ -8,19 +8,27 @@ public enum Direction {
/** /**
* North, up * North, up
*/ */
NORTH, NORTH(0, -1),
/** /**
* East, right * East, right
*/ */
EAST, EAST(1, 0),
/** /**
* South, bottom * South, bottom
*/ */
SOUTH, SOUTH(0, 1),
/** /**
* West, left * West, left
*/ */
WEST; WEST(-1, 0);
public final int dX;
public final int dY;
Direction(int dX, int dY) {
this.dX = dX;
this.dY = dY;
}
public static Direction getDirection(int x) { public static Direction getDirection(int x) {
switch (x) { switch (x) {
@ -78,6 +86,5 @@ public enum Direction {
} else { } else {
return null; return null;
} }
} }
} }

View File

@ -9,6 +9,7 @@ import net.simon987.server.plugin.ServerPlugin;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.awt.*; import java.awt.*;
import java.util.ArrayList;
/** /**
* An INSTANCE of an object (e.g. a Tree, a character ...) inside the * An INSTANCE of an object (e.g. a Tree, a character ...) inside the
@ -43,125 +44,65 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
private World world; private World world;
//--------
/** /**
* Increment the location of the game object by 1 tile * Increment the location of the game object by 1 tile
* Collision checks happen here * Collision checks happen here
*/ */
public boolean incrementLocation() { public boolean incrementLocation() {
int newX = 0, newY = 0; int newX = getX() + direction.dX;
int newY = getY() + direction.dY;
if (direction == Direction.NORTH) { if (newX < 0 || newY < 0 || newX >= world.getWorldSize() || newY >= world.getWorldSize()) {
newX = x; //Next tile is out of world bounds, move to next world
newY = (y - 1); World nextWorld = GameServer.INSTANCE.getGameUniverse().getWorld(
world.getX() + direction.dX, world.getY() + direction.dY, true);
} else if (direction == Direction.EAST) { //Move object to next World
newX = (x + 1); world.removeObject(this);
newY = y; world.decUpdatable();
nextWorld.addObject(this);
nextWorld.incUpdatable();
setWorld(nextWorld);
} else if (direction == Direction.SOUTH) { //Set position on next World according to direction
newX = x; switch (direction) {
newY = (y + 1); case NORTH:
setY(nextWorld.getWorldSize() - 1);
} else if (direction == Direction.WEST) { break;
newX = (x - 1); case EAST:
newY = y; setX(0);
} break;
case SOUTH:
setY(0);
//Check if out of World bounds / collision break;
if (newX < 0) { case WEST:
//Move object to adjacent World (left) setX(nextWorld.getWorldSize() - 1);
World leftWorld; break;
if (world.getX() == 0) {
//Warp around
leftWorld = GameServer.INSTANCE.getGameUniverse().getWorld(
GameServer.INSTANCE.getGameUniverse().getMaxWidth(), world.getY(), true);
} else {
leftWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() - 1, world.getY(), true);
} }
if (leftWorld != null) { return true;
world.removeObject(this);
world.decUpdatable();
leftWorld.addObject(this);
leftWorld.incUpdatable();
setWorld(leftWorld);
x = leftWorld.getWorldSize() - 1;
}
} else if (newX >= world.getWorldSize()) {
//Move object to adjacent World (right)
World rightWorld;
if (world.getX() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
//Warp around
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(0, world.getY(), true);
} else {
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() + 1, world.getY(), true);
}
if (rightWorld != null) {
world.removeObject(this);
world.decUpdatable();
rightWorld.addObject(this);
rightWorld.incUpdatable();
setWorld(rightWorld);
x = 0;
}
} else if (newY < 0) {
//Move object to adjacent World (up)
World upWorld;
if (world.getY() == 0) {
//Warp around
upWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(),
GameServer.INSTANCE.getGameUniverse().getMaxWidth(), true);
} else {
upWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() - 1, true);
}
if (upWorld != null) {
world.removeObject(this);
world.decUpdatable();
upWorld.addObject(this);
upWorld.incUpdatable();
setWorld(upWorld);
y = upWorld.getWorldSize() - 1;
}
} else if (newY >= world.getWorldSize()) {
//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, true);
} else {
downWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() + 1, true);
}
if (downWorld != null) {
world.removeObject(this);
world.decUpdatable();
downWorld.addObject(this);
downWorld.incUpdatable();
setWorld(downWorld);
y = 0;
}
}
//Check collision
else if (!world.isTileBlocked(newX, newY)) {
//Tile is passable
x = newX;
y = newY;
} else { } else {
return false;
}
return true; ArrayList<GameObject> frontObjects = world.getGameObjectsAt(newX, newY);
//Check for enterable objects
if (frontObjects.size() > 0 && frontObjects.get(0) instanceof Enterable) {
return (((Enterable) frontObjects.get(0)).enter(this));
}
//Check collision
if (!world.isTileBlocked(newX, newY)) { //Check for collision
//Tile is passable
x = newX;
y = newY;
return true;
} else {
return false;
}
}
} }