use of com.agorria.shootandmove.math.CollisionInterface in project ShootAndRun by IonAgorria.
the class World method update.
/**
* Updates the world
*
* @param delta time since last update
*/
void update(float delta) {
// Update entities except player
List<Entity> entitiesCollision = new ArrayList<>();
for (Entity entity : entities) {
// This removes entity as is not added to next list
if (entity.isDestroyed()) {
removedEntity(entity);
continue;
}
// Update entity
entity.update(delta);
// Add to next
entitiesNext.add(entity);
if (entity.getCollision() != null) {
entitiesCollision.add(entity);
}
}
// Check if collides with world
for (Entity entity : entitiesCollision) {
for (Rectangle collision : collisions) {
if (!entity.ignoreCollisionWorld(collision) && entity.checkCollision(collision)) {
boolean unhandled = entity.handleCollisionWorld(collision);
if (unhandled) {
Vector2f vector = entity.resolveCollision(collision);
if (vector != null) {
entity.getPosition().add(vector.x, 0, vector.y);
entity.updateAfterPosition();
}
}
}
}
}
// Check if entities collide
for (Iterator<Entity> iterator = entitiesCollision.iterator(); iterator.hasNext(); ) {
Entity entity = iterator.next();
// Check other entities
for (Entity other : entitiesCollision) {
CollisionInterface otherCollision = other.getCollision();
if (entity != other && !(entity.ignoreCollisionEntity(other) || other.ignoreCollisionEntity(entity)) && entity.checkCollision(otherCollision)) {
boolean entityUnhandled = entity.handleCollisionEntity(other) && !other.ignoreCollisionResolution(entity);
boolean otherUnhandled = other.handleCollisionEntity(entity) && !entity.ignoreCollisionResolution(other);
if (entityUnhandled) {
Vector2f vector = entity.resolveCollision(otherCollision);
if (vector != null) {
if (otherUnhandled) {
// Handle both by distributing equally at opposing direction
vector.mul(0.5f);
other.getPosition().add(-vector.x, 0, -vector.y);
other.updateAfterPosition();
}
// Handle entity
entity.getPosition().add(vector.x, 0, vector.y);
entity.updateAfterPosition();
}
} else if (otherUnhandled) {
// Only handle other
Vector2f vector = other.resolveCollision(entity.getCollision());
if (vector != null) {
other.getPosition().add(vector.x, 0, vector.y);
other.updateAfterPosition();
}
}
}
}
// Remove entity as we have checked it
iterator.remove();
}
// Remove entities that are destroyed and get drawables
List<Drawable> entitiesDrawable = new ArrayList<>();
for (Iterator<Entity> iterator = entitiesNext.iterator(); iterator.hasNext(); ) {
Entity entity = iterator.next();
if (entity.isDestroyed()) {
// Remove entity
removedEntity(entity);
iterator.remove();
} else {
// Add to draw
Drawable drawable = entity.getDrawable();
if (drawable != null) {
entitiesDrawable.add(drawable);
}
}
}
// Set the current entities from next
entities.clear();
entities = entitiesNext;
entitiesNext = new ArrayList<>();
// Update camera
if (player != null && !player.isDestroyed()) {
updateCamera();
}
// Submit batch to draw entities
DrawableBatch batch = view.createBatch(DrawableBatch.SLOT.Entities, DrawableBatch.MODE.PerspectiveDepthBlend, entitiesDrawable, cameraPosition, cameraRotation, Color.WHITE);
view.submitBatch(batch);
}
Aggregations