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);
newY = y;
} else if (direction == Direction.SOUTH) {
newX = x;
newY = (y + 1);
} else if (direction == Direction.WEST) {
newX = (x - 1);
newY = y;
}
//Check if out of World bounds / collision
if (newX < 0) {
//Move object to adjacent World (left)
World leftWorld;
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) {
world.removeObject(this); world.removeObject(this);
world.decUpdatable(); world.decUpdatable();
leftWorld.addObject(this); nextWorld.addObject(this);
leftWorld.incUpdatable(); nextWorld.incUpdatable();
setWorld(leftWorld); setWorld(nextWorld);
x = leftWorld.getWorldSize() - 1; //Set position on next World according to direction
switch (direction) {
case NORTH:
setY(nextWorld.getWorldSize() - 1);
break;
case EAST:
setX(0);
break;
case SOUTH:
setY(0);
break;
case WEST:
setX(nextWorld.getWorldSize() - 1);
break;
} }
} else if (newX >= world.getWorldSize()) {
//Move object to adjacent World (right) return true;
World rightWorld;
if (world.getX() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
//Warp around
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(0, world.getY(), true);
} else { } else {
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() + 1, world.getY(), 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));
} }
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 //Check collision
else if (!world.isTileBlocked(newX, newY)) { if (!world.isTileBlocked(newX, newY)) { //Check for collision
//Tile is passable //Tile is passable
x = newX; x = newX;
y = newY; y = newY;
return true;
} else { } else {
return false; return false;
} }
return true; }
} }