Fix GameObject decoding.

This commit is contained in:
Major-
2015-08-30 22:05:05 +01:00
parent b08d9e73b2
commit a4186587df
3 changed files with 39 additions and 37 deletions
@@ -206,7 +206,7 @@ public final class GameObjectDecoder implements Runnable {
Position position = new Position(x, y, height);
if (!previous.contains(position)) {
previous = regions.fromPosition(new Position(x, y, height));
previous = regions.fromPosition(position);
}
previous.getMatrix(height).block(localX, localY);
@@ -270,9 +270,11 @@ public final class GameObjectDecoder implements Runnable {
while (true) {
int attributeId = buffer.get() & 0xFF;
if (attributeId == 1) {
if (attributeId == 0) {
decodeAttributes(attributes, x + localX, y + localY, height);
break;
} else if (attributeId == 1) {
buffer.get();
} else if (attributeId == 0) {
decodeAttributes(attributes, x + localX, y + localY, height);
break;
} else if (attributeId <= 49) {
@@ -103,7 +103,7 @@ public final class Region {
/**
* Creates a new Region with the specified {@link RegionCoordinates}.
*
* @param coordinates The coordinates.
* @param coordinates The RegionCoordinates.
*/
public Region(RegionCoordinates coordinates) {
this.coordinates = coordinates;
@@ -120,7 +120,7 @@ public final class Region {
* register it to this Region.
*
* @param entity The Entity.
* @param notify A flag indicating whether the {@link RegionListener}s for this Region should be notified.
* @param notify Whether or not the {@link RegionListener}s for this Region should be notified.
* @throws IllegalArgumentException If the Entity does not belong in this Region.
*/
public void addEntity(Entity entity, boolean notify) {
@@ -138,7 +138,7 @@ public final class Region {
/**
* Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
* register it to this Region.
* <p/>
*
* By default, this method notifies RegionListeners for this region of the addition.
*
* @param entity The Entity.
@@ -150,7 +150,7 @@ public final class Region {
/**
* Checks if this Region contains the specified Entity.
* <p/>
*
* This method operates in constant time.
*
* @param entity The Entity.
@@ -193,7 +193,7 @@ public final class Region {
/**
* Gets this Region's {@link RegionCoordinates}.
*
* @return The Region coordinates.
* @return The RegionCoordinates.
*/
public RegionCoordinates getCoordinates() {
return coordinates;
@@ -203,8 +203,8 @@ public final class Region {
* Gets a shallow copy of the {@link Set} of {@link Entity} objects at the specified {@link Position}. The returned
* type will be immutable.
*
* @param position The position containing the entities.
* @return The list.
* @param position The Position containing the entities.
* @return The Set. Will be immutable.
*/
public Set<Entity> getEntities(Position position) {
Set<Entity> set = entities.get(position);
@@ -218,7 +218,7 @@ public final class Region {
*
* @param position The {@link Position} containing the entities.
* @param types The {@link EntityType}s.
* @return The set of entities.
* @return The Set of Entity objects.
*/
public <T extends Entity> Set<T> getEntities(Position position, EntityType... types) {
Set<Entity> local = entities.get(position);
@@ -271,12 +271,12 @@ public final class Region {
}
/**
* Removes a {@link Entity} from this Region.
* Removes an {@link Entity} from this Region.
*
* @param entity The Entity.
* @throws IllegalArgumentException If the Entity does not belong in this Region, or if it was never added.
*/
public void removeEntity(Entity entity) { // TODO entity update stuff
public void removeEntity(Entity entity) {
Position position = entity.getPosition();
checkPosition(position);
@@ -73,7 +73,7 @@ public final class CollisionMatrix {
* @param x The x coordinate.
* @param y The y coordinate.
* @param flags The CollisionFlags.
* @return {@code true} if all of the CollisionFlags are set, otherwise {@code false}.
* @return {@code true} iff all of the CollisionFlags are set.
*/
public boolean all(int x, int y, CollisionFlag... flags) {
for (CollisionFlag flag : flags) {
@@ -92,7 +92,7 @@ public final class CollisionMatrix {
* @param x The x coordinate.
* @param y The y coordinate.
* @param flags The CollisionFlags.
* @return {@code true} if any of the CollisionFlags are set, otherwise {@code false}.
* @return {@code true} iff any of the CollisionFlags are set.
*/
public boolean any(int x, int y, CollisionFlag... flags) {
for (CollisionFlag flag : flags) {
@@ -115,8 +115,8 @@ public final class CollisionMatrix {
}
/**
* Clears (i.e. sets to {@code false}) the value of the specified {@link CollisionFlag} for the specified coordinate
* pair.
* Clears (i.e. sets to {@code false}) the value of the specified {@link CollisionFlag} for the specified
* coordinate pair.
*
* @param x The x coordinate.
* @param y The y coordinate.
@@ -132,7 +132,7 @@ public final class CollisionMatrix {
* @param x The x coordinate.
* @param y The y coordinate.
* @param flag The CollisionFlag.
* @return {@code true} if the CollisionFlag is set, {@code false} if not.
* @return {@code true} iff the CollisionFlag is set.
*/
public boolean flagged(int x, int y, CollisionFlag flag) {
return (get(x, y) & flag.asByte()) != 0;
@@ -159,17 +159,6 @@ public final class CollisionMatrix {
set(x, y, ALL_ALLOWED);
}
/**
* Sets the appropriate index for the specified coordinate pair to the specified value.
*
* @param x The x coordinate.
* @param y The y coordinate.
* @param value The value.
*/
private void set(int x, int y, byte value) {
matrix[indexOf(x, y)] = value;
}
/**
* Sets (i.e. sets to {@code true}) the value of the specified {@link CollisionFlag} for the specified coordinate
* pair.
@@ -184,7 +173,8 @@ public final class CollisionMatrix {
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("width", width).add("length", length).add("matrix", Arrays.toString(matrix)).toString();
return MoreObjects.toStringHelper(this).add("width", width).add("length", length)
.add("matrix", Arrays.toString(matrix)).toString();
}
/**
@@ -195,7 +185,7 @@ public final class CollisionMatrix {
* @param y The y coordinate.
* @param entity The {@link EntityType}.
* @param direction The {@link Direction} the Entity is approaching from.
* @return {@code true} if the tile at the specified coordinate pair is not traversable, {@code false} if not.
* @return {@code true} iff the tile at the specified coordinate pair is not traversable.
*/
public boolean untraversable(int x, int y, EntityType entity, Direction direction) {
CollisionFlag[] flags = CollisionFlag.forType(entity);
@@ -203,19 +193,19 @@ public final class CollisionMatrix {
switch (direction) {
case NORTH_WEST:
return any(x, y, flags[south], flags[east]);
return flagged(x, y, flags[south]) || flagged(x, y, flags[east]);
case NORTH:
return flagged(x, y, flags[south]);
case NORTH_EAST:
return any(x, y, flags[south], flags[west]);
return flagged(x, y, flags[south]) || flagged(x, y, flags[west]);
case EAST:
return flagged(x, y, flags[west]);
case SOUTH_EAST:
return any(x, y, flags[north], flags[west]);
return flagged(x, y, flags[north]) || flagged(x, y, flags[west]);
case SOUTH:
return flagged(x, y, flags[north]);
case SOUTH_WEST:
return any(x, y, flags[north], flags[east]);
return flagged(x, y, flags[north]) || flagged(x, y, flags[east]);
case WEST:
return flagged(x, y, flags[east]);
default:
@@ -234,8 +224,18 @@ public final class CollisionMatrix {
private int indexOf(int x, int y) {
Preconditions.checkElementIndex(x, width, "X coordinate must be [0, " + width + "), received " + x + ".");
Preconditions.checkElementIndex(y, length, "Y coordinate must be [0, " + length + "), received " + y + ".");
int index = y * width + x;
return index;
return y * width + x;
}
/**
* Sets the appropriate index for the specified coordinate pair to the specified value.
*
* @param x The x coordinate.
* @param y The y coordinate.
* @param value The value.
*/
private void set(int x, int y, byte value) {
matrix[indexOf(x, y)] = value;
}
}