Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to retrieve info on last collided entities #270

Open
wants to merge 7 commits into
base: master
from
@@ -2,6 +2,8 @@

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import de.gurkenlabs.litiengine.Align;
import de.gurkenlabs.litiengine.Game;
@@ -36,6 +38,8 @@
private Collision collisionType;

private Rectangle2D collisionBox;

private List<ICollisionEntity> collidedEntities;

protected CollisionEntity() {
super();
@@ -47,6 +51,7 @@ protected CollisionEntity() {
this.align = info.align();
this.collisionType = info.collisionType();
this.collisionBox = this.getCollisionBox(this.getLocation());
this.collidedEntities = new ArrayList<ICollisionEntity> ();
}

public static Rectangle2D getCollisionBox(final Point2D location, final double entityWidth, final double entityHeight, final double collisionBoxWidth, final double collisionBoxHeight, final Align align, final Valign valign) {
@@ -114,6 +119,14 @@ public Valign getCollisionBoxValign() {
public Collision getCollisionType() {
return this.collisionType;
}

/**
* {@inheritDoc}
*/
@Override
public List<ICollisionEntity> getLastCollidedEntities() {
return this.collidedEntities;
}

/**
* Checks for collision.
@@ -199,4 +212,12 @@ public void setCollisionType(Collision type) {
this.collisionType = type;
}
}

/**
* {@inheritDoc}
*/
@Override
public void addCollidedEntities(ICollisionEntity entity) {
this.collidedEntities.add(entity);
}
}
@@ -2,6 +2,7 @@

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import de.gurkenlabs.litiengine.Align;
import de.gurkenlabs.litiengine.Valign;
@@ -42,6 +43,13 @@
public double getCollisionBoxHeight();

public double getCollisionBoxWidth();

/**
* Gets a list of all last collided entities.
*
* @return a list of all last collided entities; null otherwise
*/
public List<ICollisionEntity> getLastCollidedEntities();

/**
* Checks for collision.
@@ -67,4 +75,12 @@
public void setCollisionBoxValign(final Valign valign);

public void setCollisionType(Collision collisionType);

/**
* Adds a collided entity at the end of a list of collided entities.
*
* @param entity
* a collided entity
*/
public void addCollidedEntities(ICollisionEntity entity);
}
@@ -182,8 +182,8 @@ public boolean collides(final Rectangle2D rect) {
*
* @param rect
* The rectangle to check the collision for.
* @param collisionEntity
* The entity on which this collision check is based on.
* @param collisionEntity
* The entity on which this collision check is based on.
* @return Returns true if the specified rectangle collides with any collision
* box of the specified type(s); otherwise false.
*/
@@ -415,17 +415,26 @@ private Rectangle2D getIntersection(final ICollisionEntity entity, final Rectang
}

private boolean collides(final ICollisionEntity entity, Collision type, Predicate<ICollisionEntity> check) {
boolean result = false;

for (final ICollisionEntity otherEntity : this.getCollisionEntities(type)) {
if (!canCollide(entity, otherEntity)) {
continue;
}

if (check.test(otherEntity)) {
return true;
if (entity != null) {
otherEntity.getLastCollidedEntities().clear();
entity.getLastCollidedEntities().clear();
otherEntity.addCollidedEntities(entity);
entity.addCollidedEntities(otherEntity);
}

result = true;
}
}

return false;
return result;
}

/**
@@ -520,4 +529,4 @@ private boolean resolveCollisionForRaycastToNewPosition(ICollisionEntity entity,
final Line2D line = new Line2D.Double(entity.getCollisionBox().getCenterX(), entity.getCollisionBox().getCenterY(), entity.getCollisionBox(newPosition).getCenterX(), entity.getCollisionBox(newPosition).getCenterY());
return this.collides(line, Collision.ANY, entity);
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.