package de.frajul.endlessroll.entities.tools;

import java.util.List;

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.DestroyEffect;
import de.frajul.endlessroll.entities.Obstacle;
import de.frajul.endlessroll.entities.Player;
import de.frajul.endlessroll.entities.collision.CollisionDetector;
import de.frajul.endlessroll.entities.collision.geometry.Geometry;
import de.frajul.endlessroll.entities.collision.geometry.Quad;
import de.frajul.endlessroll.main.game.Timer;

/**
 * Created by Julian on 20.02.2016.
 */
public class Bomb extends Tool {

    public final float RANGE = 1;
    private float delta;
    private boolean exploding = false;

    public Bomb(Vector position) {
        super(ToolType.BOMB, position, .29f, .29f, false, false);
        animation.setIndexSequence(new int[]{0, 1, 2});
        animation.setLooping(false);
        animation.setRequiredDelta(
                (int) (ToolType.BOMB.getCurrentUpgradeValue(ToolUpgradeType.DURATION) / 3));
    }

    @Override
    public void update(Timer timer) {
        super.update(timer);
        delta += timer.getFrameTimeSeconds();
        if (delta >= ToolType.BOMB.getCurrentUpgradeValue(ToolUpgradeType.DURATION))
            exploding = true;
    }

    @Override
    public void onPlayerCollision(Player player, Timer timer) {

    }

    @Override
    protected Geometry createWorldCollisionBounds() {
        return this;
    }

    @Override
    protected Geometry createPlayerCollisionBounds() {
        return this;
    }

    public boolean isExploding() {
        return exploding;
    }

    public void explode(List<Obstacle> obstacles, CollisionDetector detector) {
        float range = RANGE * ToolType.BOMB.getCurrentUpgradeValue(ToolUpgradeType.RANGE) / 100;
        Quad explosionRange = new Quad(new Vector(super.getPosition()), range, range);
        for (Obstacle obstacle : obstacles) {
            if (detector.isQuadQuadCollision(obstacle, explosionRange))
                obstacle.destroy(DestroyEffect.EXPLOSION);
        }
        super.destroy(DestroyEffect.EXPLOSION);
    }
}
