buildcraft/api/buildcraft/api/core/Position.java

196 lines
3.8 KiB
Java
Raw Normal View History

/**
2014-02-15 09:21:40 +01:00
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
2012-05-09 22:43:05 +02:00
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
2012-05-09 22:43:05 +02:00
*/
2012-07-25 12:45:15 +02:00
package buildcraft.api.core;
2012-05-09 22:43:05 +02:00
import io.netty.buffer.ByteBuf;
2012-12-17 23:29:42 +01:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
2012-05-09 22:43:05 +02:00
public class Position implements ISerializable {
2012-06-08 02:13:31 +02:00
2012-05-09 22:43:05 +02:00
public double x, y, z;
public ForgeDirection orientation;
2012-06-04 22:48:18 +02:00
public Position() {
x = 0;
y = 0;
z = 0;
orientation = ForgeDirection.UNKNOWN;
}
2012-06-04 22:48:18 +02:00
public Position(double ci, double cj, double ck) {
2012-05-09 22:43:05 +02:00
x = ci;
y = cj;
z = ck;
orientation = ForgeDirection.UNKNOWN;
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public Position(double ci, double cj, double ck, ForgeDirection corientation) {
2012-05-09 22:43:05 +02:00
x = ci;
y = cj;
z = ck;
orientation = corientation;
if (orientation == null) {
orientation = ForgeDirection.UNKNOWN;
}
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public Position(Position p) {
2012-05-09 22:43:05 +02:00
x = p.x;
y = p.y;
z = p.z;
orientation = p.orientation;
}
2012-06-04 22:48:18 +02:00
public Position(NBTTagCompound nbttagcompound) {
2014-02-16 22:29:46 +01:00
readFromNBT(nbttagcompound);
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public Position(TileEntity tile) {
2012-05-09 22:43:05 +02:00
x = tile.xCoord;
y = tile.yCoord;
z = tile.zCoord;
orientation = ForgeDirection.UNKNOWN;
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public Position(BlockIndex index) {
x = index.x;
y = index.y;
z = index.z;
orientation = ForgeDirection.UNKNOWN;
}
2012-06-04 22:48:18 +02:00
public void moveRight(double step) {
2012-05-09 22:43:05 +02:00
switch (orientation) {
case SOUTH:
2012-05-09 22:43:05 +02:00
x = x - step;
break;
case NORTH:
2012-06-04 22:48:18 +02:00
x = x + step;
2012-05-09 22:43:05 +02:00
break;
case EAST:
2012-05-09 22:43:05 +02:00
z = z + step;
break;
case WEST:
2012-05-09 22:43:05 +02:00
z = z - step;
break;
default:
2012-05-09 22:43:05 +02:00
}
}
2012-06-04 22:48:18 +02:00
public void moveLeft(double step) {
2012-05-09 22:43:05 +02:00
moveRight(-step);
}
2012-06-04 22:48:18 +02:00
public void moveForwards(double step) {
2012-05-09 22:43:05 +02:00
switch (orientation) {
case UP:
2012-05-09 22:43:05 +02:00
y = y + step;
break;
case DOWN:
2012-05-09 22:43:05 +02:00
y = y - step;
break;
case SOUTH:
2012-05-09 22:43:05 +02:00
z = z + step;
break;
case NORTH:
2012-06-04 22:48:18 +02:00
z = z - step;
2012-05-09 22:43:05 +02:00
break;
case EAST:
2012-05-09 22:43:05 +02:00
x = x + step;
2012-06-04 22:48:18 +02:00
break;
case WEST:
2012-05-09 22:43:05 +02:00
x = x - step;
break;
default:
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
}
public void moveBackwards(double step) {
2012-05-09 22:43:05 +02:00
moveForwards(-step);
}
2012-06-04 22:48:18 +02:00
public void moveUp(double step) {
2012-05-09 22:43:05 +02:00
switch (orientation) {
case SOUTH:
case NORTH:
case EAST:
case WEST:
2012-05-09 22:43:05 +02:00
y = y + step;
break;
default:
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public void moveDown(double step) {
moveUp(-step);
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
2012-05-09 22:43:05 +02:00
public void writeToNBT(NBTTagCompound nbttagcompound) {
if (orientation == null) {
orientation = ForgeDirection.UNKNOWN;
}
2012-05-09 22:43:05 +02:00
nbttagcompound.setDouble("i", x);
nbttagcompound.setDouble("j", y);
nbttagcompound.setDouble("k", z);
nbttagcompound.setByte("orientation", (byte) orientation.ordinal());
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
2014-02-16 22:29:46 +01:00
public void readFromNBT(NBTTagCompound nbttagcompound) {
x = nbttagcompound.getDouble("i");
y = nbttagcompound.getDouble("j");
z = nbttagcompound.getDouble("k");
orientation = ForgeDirection.values() [nbttagcompound.getByte("orientation")];
2014-02-16 22:29:46 +01:00
}
2012-05-09 22:43:05 +02:00
@Override
2012-06-04 22:48:18 +02:00
public String toString() {
2012-05-09 22:43:05 +02:00
return "{" + x + ", " + y + ", " + z + "}";
}
2012-06-04 22:48:18 +02:00
public Position min(Position p) {
2012-06-08 02:13:31 +02:00
return new Position(p.x > x ? x : p.x, p.y > y ? y : p.y, p.z > z ? z : p.z);
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public Position max(Position p) {
2012-06-08 02:13:31 +02:00
return new Position(p.x < x ? x : p.x, p.y < y ? y : p.y, p.z < z ? z : p.z);
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public boolean isClose(Position newPosition, float f) {
double dx = x - newPosition.x;
double dy = y - newPosition.y;
double dz = z - newPosition.z;
double sqrDis = dx * dx + dy * dy + dz * dz;
return !(sqrDis > f * f);
}
@Override
public void readData(ByteBuf stream) {
x = stream.readDouble();
y = stream.readDouble();
z = stream.readDouble();
orientation = ForgeDirection.getOrientation(stream.readByte());
}
@Override
public void writeData(ByteBuf stream) {
stream.writeDouble(x);
stream.writeDouble(y);
stream.writeDouble(z);
stream.writeByte(orientation.ordinal());
}
2012-12-17 23:29:42 +01:00
}