package de.thdeg.greiner.superpangworld.graphics.base;

import de.thdeg.greiner.superpangworld.gameview.GameView;

import java.util.List;

/**
 * An object that can actively collide with other objects, e.g. a shot.
 */
public abstract class CollidingGameObject extends CollidableGameObject {

    /** The list of game objects that this object may collide with */
    protected final List<CollidableGameObject> objectsToCollideWith;

    /**
     * Create a colliding game object.
     *
     * @param gameView the game view
     * @param objectsToCollideWith the objects that this object can collide with
     */
    protected CollidingGameObject(GameView gameView, List<CollidableGameObject> objectsToCollideWith) {
        super(gameView);
        this.objectsToCollideWith = objectsToCollideWith;
    }

    @Override
    public void update() {
        super.update();
        checkCollisions();
    }

    /**
     * Used to check possible collisions that are actively caused by this game object. Both parties are notified
     * about the collision.
     */
    private void checkCollisions() {
        for (CollidableGameObject collidableGameObject : objectsToCollideWith) {
            if (collidesWith(collidableGameObject)) {
                reactToCollision(collidableGameObject);
                collidableGameObject.reactToCollision(this);
            }
        }
    }

    /**
     * Determines if this game object is collided with the other game object.
     *
     * @param other The other game object.
     * @return <code>true</code> if the there was a collision.
     */
    protected final boolean collidesWith(CollidableGameObject other) {
        return this.hitBox.intersects(other.hitBox);
    }

    /**
     * Add collideable game objects to the list.
     *
     * @param objects the collideable game objects
     */
    public void addCollidableGameObject(List<CollidableGameObject> objects){
        objectsToCollideWith.addAll(objects);
    }
}
