Add support for checking the bounds of entities

This commit is contained in:
Steve Soltys
2018-07-22 15:14:03 -04:00
parent c44a942976
commit 84f05ef51a
6 changed files with 129 additions and 1 deletions
@@ -20,6 +20,11 @@ public abstract class Entity {
*/
protected final World world;
/**
* The EntityBounds for this Entity.
*/
private EntityBounds bounds;
/**
* Creates the Entity.
*
@@ -34,6 +39,20 @@ public abstract class Entity {
@Override
public abstract boolean equals(Object obj);
/**
* Gets the {@link EntityBounds} for this Entity.
*
* @return The EntityBounds.
*/
public EntityBounds getBounds() {
if(bounds == null) {
bounds = new EntityBounds(this);
}
return bounds;
}
/**
* Gets the {@link Position} of this Entity.
*
@@ -59,6 +78,20 @@ public abstract class Entity {
*/
public abstract EntityType getEntityType();
/**
* Gets the length of this Entity.
*
* @return The length.
*/
public abstract int getLength();
/**
* Gets the width of this Entity.
*
* @return The width.
*/
public abstract int getWidth();
@Override
public abstract int hashCode();
@@ -0,0 +1,48 @@
package org.apollo.game.model.entity;
import org.apollo.game.model.Position;
/**
* The bounds of an {@link Entity}.
*
* @author Steve Soltys
*/
public class EntityBounds {
/**
* The {@link Entity}.
*/
private final Entity entity;
/**
* Creates an EntityBounds.
*
* @param entity The entity.
*/
EntityBounds(Entity entity) {
this.entity = entity;
}
/**
* Checks whether the given position is within the Entity's bounds.
*
* @param position The position.
* @return A flag indicating whether or not the position exists within the Entity's bounds.
*/
public boolean contains(Position position) {
int positionX = position.getX();
int positionY = position.getY();
int positionHeight = position.getHeight();
int entityX = entity.getPosition().getX();
int entityY = entity.getPosition().getY();
int entityHeight = entity.getPosition().getHeight();
int width = entity.getWidth();
int length = entity.getLength();
return positionX >= entityX && positionX < entityX + width &&
positionY >= entityY && positionY < entityY + length &&
positionHeight == entityHeight;
}
}
@@ -80,6 +80,16 @@ public final class GroundItem extends Entity implements GroupableEntity {
return EntityType.GROUND_ITEM;
}
@Override
public int getLength() {
return 1;
}
@Override
public int getWidth() {
return 1;
}
/**
* Gets the {@link Item} displayed on the ground.
*
@@ -327,6 +327,16 @@ public abstract class Mob extends Entity {
return walkingQueue;
}
@Override
public int getLength() {
return definition.map(NpcDefinition::getSize).orElse(1);
}
@Override
public int getWidth() {
return definition.map(NpcDefinition::getSize).orElse(1);
}
/**
* Returns whether or not this mob has an {@link NpcDefinition}.
*
@@ -414,6 +414,16 @@ public final class Projectile extends Entity implements GroupableEntity {
return EntityType.PROJECTILE;
}
@Override
public int getLength() {
return 1;
}
@Override
public int getWidth() {
return 1;
}
@Override
public int hashCode() {
return Objects.hashCode(position, destination, delay, lifetime, target, startHeight,
@@ -2,6 +2,7 @@ package org.apollo.game.model.entity.obj;
import org.apollo.cache.def.ObjectDefinition;
import org.apollo.game.model.Position;
import org.apollo.game.model.Direction;
import org.apollo.game.model.World;
import org.apollo.game.model.area.EntityUpdateType;
import org.apollo.game.model.area.Region;
@@ -85,6 +86,23 @@ public abstract class GameObject extends Entity implements GroupableEntity {
return packed >> 2 & 0x3F;
}
@Override
public int getLength() {
Direction direction = Direction.WNES[getOrientation()];
return direction == Direction.WEST || direction == Direction.EAST ?
getDefinition().getWidth() : getDefinition().getLength();
}
@Override
public int getWidth() {
Direction direction = Direction.WNES[getOrientation()];
return direction == Direction.WEST || direction == Direction.EAST ?
getDefinition().getLength() : getDefinition().getWidth();
}
@Override
public int hashCode() {
return packed;
@@ -109,5 +127,4 @@ public abstract class GameObject extends Entity implements GroupableEntity {
* @return {@code true} if the Player can see this GameObject, {@code false} if not.
*/
public abstract boolean viewableBy(Player player, World world);
}