Search in sources :

Example 1 with CollisionInterface

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);
}
Also used : Entity(com.agorria.shootandmove.entities.Entity) Vector2f(com.agorria.shootandmove.math.Vector2f) ArrayList(java.util.ArrayList) Rectangle(com.agorria.shootandmove.math.Rectangle) CollisionInterface(com.agorria.shootandmove.math.CollisionInterface) Drawable(com.agorria.shootandmove.graphics.Drawable) DrawableBatch(com.agorria.shootandmove.graphics.DrawableBatch)

Aggregations

Entity (com.agorria.shootandmove.entities.Entity)1 Drawable (com.agorria.shootandmove.graphics.Drawable)1 DrawableBatch (com.agorria.shootandmove.graphics.DrawableBatch)1 CollisionInterface (com.agorria.shootandmove.math.CollisionInterface)1 Rectangle (com.agorria.shootandmove.math.Rectangle)1 Vector2f (com.agorria.shootandmove.math.Vector2f)1 ArrayList (java.util.ArrayList)1