Made Point3D, Quaternion, and Location classes Encodables removing the need for getLocation and addLocation methods in SWGPacket class

This commit is contained in:
Waverunner
2015-07-24 18:07:58 -04:00
parent d11f93fc75
commit 1c2dbfd6a3
8 changed files with 85 additions and 46 deletions
+26 -2
View File
@@ -27,10 +27,15 @@
***********************************************************************************/
package resources;
import network.packets.Packet;
import resources.encodables.Encodable;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Location implements Serializable {
public class Location implements Encodable, Serializable {
private static final long serialVersionUID = 1L;
@@ -238,7 +243,26 @@ public class Location implements Serializable {
return false;
return x == y;
}
@Override
public byte[] encode() {
ByteBuffer bb = ByteBuffer.allocate(28).order(ByteOrder.LITTLE_ENDIAN);
Packet.addFloat(bb, (float) (Double.isNaN(orientation.getX()) ? 0 : orientation.getX()));
Packet.addFloat(bb, (float) (Double.isNaN(orientation.getY()) ? 0 : orientation.getY()));
Packet.addFloat(bb, (float) (Double.isNaN(orientation.getZ()) ? 0 : orientation.getZ()));
Packet.addFloat(bb, (float) (Double.isNaN(orientation.getW()) ? 1 : orientation.getW()));
Packet.addFloat(bb, (float) (Double.isNaN(point.getX()) ? 0 : point.getX()));
Packet.addFloat(bb, (float) (Double.isNaN(point.getY()) ? 0 : point.getY()));
Packet.addFloat(bb, (float) (Double.isNaN(point.getZ()) ? 0 : point.getZ()));
return bb.array();
}
@Override
public void decode(ByteBuffer data) {
orientation.decode(data);
point.decode(data);
}
public String toString() {
return String.format("Location[TRN=%s, %s %s]", terrain, point, orientation);
}