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.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 Stasis extends PlayerInfluenceTool {

    private ParticleSource particleSource;
    private boolean firstCollision = true;
    private final static float Y_SLOW_FACTOR = .4f; //Final .2f
    private final static float X_SLOW_FACTOR = .9f; //Final .8f

    public Stasis(Vector position, ParticleSystem particleSystem) {
        super(ToolType.STASIS, position,
                .7f * ToolType.STASIS.getCurrentUpgradeValue(ToolUpgradeType.SIZE) / 100,
                .7f * ToolType.STASIS.getCurrentUpgradeValue(ToolUpgradeType.SIZE) / 100, false,
                false);
        animation.setLooping(true);
        animation.setRequiredDelta(300);
        animation.setIndexSequence(new int[]{1, 1, 2, 3, 2, 4, 4, 3, 2, 2, 3, 3});
        super.setFloating(true);
        particleSource = new ParticleSource(new Vector(position), particleSystem.stasis);
        particleSource.setSpawnSize(new Vector(super.width, super.height));
        particleSource.start();
    }

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

    @Override
    public void onPlayerCollision(Player player, Timer timer) {
        player.addInfluenceTool(this);
    }

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

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

    @Override
    public void influencePlayerValues(Player player) {
        float forceValue = ToolType.STASIS.getCurrentUpgradeValue(ToolUpgradeType.FORCE);
        float finalXSlow = X_SLOW_FACTOR - forceValue / 20f;
        player.manipulateAllForces(Y_SLOW_FACTOR / forceValue);
        player.getMovement().setX(player.getMovement().getX() * finalXSlow);
        if (firstCollision) {
            player.getMovement().y *= Y_SLOW_FACTOR / forceValue;
            firstCollision = false;
        }

    }
}
