Add base for sector repository; clarify naming.

This commit is contained in:
Major-
2013-11-09 13:49:05 +00:00
parent 0544ad7e2c
commit fb422bcfe1
12 changed files with 123 additions and 88 deletions
+4
View File
@@ -101,4 +101,8 @@
<type>org.apollo.game.event.impl.FocusUpdateEvent</type> <type>org.apollo.game.event.impl.FocusUpdateEvent</type>
<chain /> <chain />
</event> </event>
<event>
<type>org.apollo.game.event.impl.TakeTileItemEvent</type>
<chain />
</event>
</events> </events>
@@ -33,4 +33,4 @@ public final class RegionChangeEvent extends Event {
return position; return position;
} }
} }
+25 -32
View File
@@ -60,48 +60,42 @@ public final class Position {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) { if (obj == null) {
return false; return false;
} }
if (this == obj) {
return true;
}
if (getClass() != obj.getClass()) { if (getClass() != obj.getClass()) {
return false; return false;
} }
Position other = (Position) obj; Position other = (Position) obj;
if (height != other.height) { if (height != other.height || x != other.x || y != other.y) {
return false;
}
if (x != other.x) {
return false;
}
if (y != other.y) {
return false; return false;
} }
return true; return true;
} }
/** /**
* Gets the x coordinate of the central region. * Gets the x coordinate of the central sector.
* *
* @return The x coordinate of the central region. * @return The x coordinate of the central sector.
*/ */
public int getCentralRegionX() { public int getCentralSectorX() {
return x / 8; return x / 8;
} }
/** /**
* Gets the y coordinate of the central region. * Gets the y coordinate of the central sector.
* *
* @return The y coordinate of the central region. * @return The y coordinate of the central sector.
*/ */
public int getCentralRegionY() { public int getCentralSectorY() {
return y / 8; return y / 8;
} }
/** /**
* Gets the distance between this position and another position. Only X and Y are considered (i.e. 2 dimensions). * Gets the distance between this position and another position. Only x and y are considered (i.e. 2 dimensions).
* *
* @param other The other position. * @param other The other position.
* @return The distance. * @return The distance.
@@ -109,7 +103,6 @@ public final class Position {
public int getDistance(Position other) { public int getDistance(Position other) {
int deltaX = x - other.x; int deltaX = x - other.x;
int deltaY = y - other.y; int deltaY = y - other.y;
// TODO will rounding up interfere with other stuff?
return (int) Math.ceil(Math.sqrt(deltaX * deltaX + deltaY * deltaY)); return (int) Math.ceil(Math.sqrt(deltaX * deltaX + deltaY * deltaY));
} }
@@ -123,7 +116,7 @@ public final class Position {
} }
/** /**
* Gets the x coordinate inside the region of this position. * Gets the x coordinate inside the sector of this position.
* *
* @return The local x coordinate. * @return The local x coordinate.
*/ */
@@ -132,17 +125,17 @@ public final class Position {
} }
/** /**
* Gets the local x coordinate inside the region of the {@code base} position. * Gets the local x coordinate inside the sector of the {@code base} position.
* *
* @param base The base position. * @param base The base position.
* @return The local x coordinate. * @return The local x coordinate.
*/ */
public int getLocalX(Position base) { public int getLocalX(Position base) {
return x - base.getTopLeftRegionX() * 8; return x - base.getTopLeftSectorX() * 8;
} }
/** /**
* Gets the y coordinate inside the region of this position. * Gets the y coordinate inside the sector of this position.
* *
* @return The local y coordinate. * @return The local y coordinate.
*/ */
@@ -151,13 +144,13 @@ public final class Position {
} }
/** /**
* Gets the local y coordinate inside the region of the {@code base} position. * Gets the local y coordinate inside the sector of the {@code base} position.
* *
* @param base The base position. * @param base The base position.
* @return The local y coordinate. * @return The local y coordinate.
*/ */
public int getLocalY(Position base) { public int getLocalY(Position base) {
return y - base.getTopLeftRegionY() * 8; return y - base.getTopLeftSectorY() * 8;
} }
/** /**
@@ -173,20 +166,20 @@ public final class Position {
} }
/** /**
* Gets the x coordinate of the region. * Gets the x coordinate of the sector.
* *
* @return The region x coordinate. * @return The sector x coordinate.
*/ */
public int getTopLeftRegionX() { public int getTopLeftSectorX() {
return x / 8 - 6; return x / 8 - 6;
} }
/** /**
* Gets the y coordinate of the region. * Gets the y coordinate of the sector.
* *
* @return The region y coordinate. * @return The sector y coordinate.
*/ */
public int getTopLeftRegionY() { public int getTopLeftSectorY() {
return y / 8 - 6; return y / 8 - 6;
} }
@@ -228,7 +221,7 @@ public final class Position {
@Override @Override
public String toString() { public String toString() {
return Position.class.getName() + " [x=" + x + ", y=" + y + ", height=" + height + "]"; return Position.class.getName() + " [x= " + x + ", y= " + y + ", height= " + height + "]";
} }
} }
@@ -1,15 +0,0 @@
package org.apollo.game.model.region;
/**
* Represents an 8x8 region.
*
* @author Graham
*/
public final class Region {
/**
* The width and height of the region in tiles.
*/
public static final int REGION_SIZE = 8;
}
@@ -1,10 +0,0 @@
package org.apollo.game.model.region;
/**
* Manages the repository of regions.
*
* @author Graham
*/
public class RegionRepository {
}
@@ -1,7 +0,0 @@
/**
* Contains classes which represent in-game regions, blocks of 8x8 tiles which
* are grouped, and can be used to store ground items, temporary objects, etc.
* efficiently.
*/
package org.apollo.game.model.region;
@@ -0,0 +1,57 @@
package org.apollo.game.model.sector;
/**
* Represents an 8x8 area of the map.
*
* @author Graham
*/
public final class Sector {
/**
* The width and height of the sector, in tiles.
*/
public static final int SECTOR_SIZE = 8;
/**
* The {@link SectorCoordinates} of this sector.
*/
private final SectorCoordinates coordinates;
/**
* Creates a new sector.
*
* @param x The x coordinate of the sector.
* @param y The y coordinate of the sector.
*/
public Sector(int x, int y) {
this(new SectorCoordinates(x, y));
}
/**
* Creates a new sector with the specified {@link SectorCoordinates}.
*
* @param coordinates The coordinates.
*/
public Sector(SectorCoordinates coordinates) {
this.coordinates = coordinates;
}
/**
* Gets the x coordinate of this sector.
*
* @return The x coordinate.
*/
public int getX() {
return coordinates.getX();
}
/**
* Gets the y coordinate of this sector.
*
* @return The y coordinate.
*/
public int getY() {
return coordinates.getY();
}
}
@@ -1,29 +1,29 @@
package org.apollo.game.model.region; package org.apollo.game.model.sector;
/** /**
* An immutable class which contains the coordinates of a region. * An immutable class representing the coordinates of a sector.
* *
* @author Graham * @author Graham
*/ */
public final class RegionCoordinates { public final class SectorCoordinates {
/** /**
* The X coordinate. * The x coordinate.
*/ */
private final int x; private final int x;
/** /**
* The Y coordinate. * The y coordinate.
*/ */
private final int y; private final int y;
/** /**
* Creates the region coordinates. * Creates the sector coordinates.
* *
* @param x The x coordinate. * @param x The x coordinate.
* @param y The y coordinate. * @param y The y coordinate.
*/ */
public RegionCoordinates(int x, int y) { public SectorCoordinates(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
@@ -36,29 +36,26 @@ public final class RegionCoordinates {
if (getClass() != obj.getClass()) { if (getClass() != obj.getClass()) {
return false; return false;
} }
final RegionCoordinates other = (RegionCoordinates) obj; final SectorCoordinates other = (SectorCoordinates) obj;
if (x != other.x) { if (x != other.x || y != other.y) {
return false;
}
if (y != other.y) {
return false; return false;
} }
return true; return true;
} }
/** /**
* Gets the X coordinate. * Gets the x coordinate.
* *
* @return The X coordinate. * @return The x coordinate.
*/ */
public int getX() { public int getX() {
return x; return x;
} }
/** /**
* Gets the Y coordinate. * Gets the y coordinate.
* *
* @return The Y coordinate. * @return The y coordinate.
*/ */
public int getY() { public int getY() {
return y; return y;
@@ -72,4 +69,4 @@ public final class RegionCoordinates {
return hash; return hash;
} }
} }
@@ -0,0 +1,10 @@
package org.apollo.game.model.sector;
/**
* A repository of sectors.
*
* @author Graham
*/
public class SectorRepository {
}
@@ -0,0 +1,5 @@
/**
* Contains classes which represent in-game sectors - blocks of 8x8 tiles used to store ground items, temporary objects, etc.
* efficiently.
*/
package org.apollo.game.model.sector;
@@ -16,10 +16,11 @@ public final class RegionChangeEventEncoder extends EventEncoder<RegionChangeEve
@Override @Override
public GamePacket encode(RegionChangeEvent event) { public GamePacket encode(RegionChangeEvent event) {
System.out.println("Sending region change event.");
GamePacketBuilder builder = new GamePacketBuilder(73); GamePacketBuilder builder = new GamePacketBuilder(73);
builder.put(DataType.SHORT, DataTransformation.ADD, event.getPosition().getCentralRegionX()); builder.put(DataType.SHORT, DataTransformation.ADD, event.getPosition().getCentralSectorX());
builder.put(DataType.SHORT, event.getPosition().getCentralRegionY()); builder.put(DataType.SHORT, event.getPosition().getCentralSectorY());
return builder.toGamePacket(); return builder.toGamePacket();
} }
} }
@@ -18,9 +18,9 @@ public final class RegionChangeEventEncoder extends EventEncoder<RegionChangeEve
@Override @Override
public GamePacket encode(RegionChangeEvent event) { public GamePacket encode(RegionChangeEvent event) {
GamePacketBuilder builder = new GamePacketBuilder(222); GamePacketBuilder builder = new GamePacketBuilder(222);
builder.put(DataType.SHORT, event.getPosition().getCentralRegionY()); builder.put(DataType.SHORT, event.getPosition().getCentralSectorY());
builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, event.getPosition().getCentralRegionX()); builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, event.getPosition().getCentralSectorX());
return builder.toGamePacket(); return builder.toGamePacket();
} }
} }