package de.frajul.endlessroll.entities.tools;

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.DestroyEffect;
import de.frajul.endlessroll.entities.Player;
import de.frajul.endlessroll.entities.collision.geometry.Circle;
import de.frajul.endlessroll.entities.collision.geometry.Geometry;
import de.frajul.endlessroll.entities.particles.ParticleSource;
import de.frajul.endlessroll.entities.particles.ParticleSystem;
import de.frajul.endlessroll.main.game.Timer;

/**
 * Created by Julian on 11.02.2016.
 */
public class Magnet extends Tool {

    private ParticleSource particleSource;

    public Magnet(Vector position, ParticleSystem particleSystem) {
        super(ToolType.MAGNET, position, .24f, .24f, false, false);
        animation.setRequiredDelta(300);
        animation.setIndexSequence(new int[]{1, 1, 0});
        animation.setLooping(true);
        super.setFloating(true);
        particleSource = new ParticleSource(new Vector(position), particleSystem.magnet);
        particleSource.start();
    }

    @Override
    public void destroy(DestroyEffect destroyEffect) {
        super.destroy(destroyEffect);
        particleSource.kill();
    }

    @Override
    public void onPlayerCollision(Player player, Timer timer) {
        float fromPlayerDistance = player.getPosition().vectorTo(super.getPosition()).length();
        float fromPlayerDistanceGreaterFour = Math.max(fromPlayerDistance, 0.4f);
        float fromPlayerYDistance = super.getPosition().y - player.getPosition().y;
        float force = 0.0000012f / (fromPlayerDistanceGreaterFour * fromPlayerDistanceGreaterFour);
        force *= ToolType.MAGNET.getCurrentUpgradeValue(ToolUpgradeType.FORCE) / 100;
        force *= timer.getFrameTimeSeconds();

        if (fromPlayerYDistance < 0) {
            force = -force;
        }
        player.addForce(force);
    }

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

    @Override
    protected Geometry createPlayerCollisionBounds() {
        return new Circle(super.getPosition(), 2);
    }
}
