/*
 * Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.shootingstars.controls;

import com.jme3.math.FastMath;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import org.wyrez.shootingstars.data.AudioDataManager;
import org.wyrez.shootingstars.helper.MathHelper;
import org.wyrez.shootingstars.helper.UserDataKeys;

/**
 *
 * @author Darth Affe
 */
public class PlayerMoveControl extends BaseControl {

    private static final float BPM_TO_SPEED_FACTOR = 1f / 4f;
    private static final float speedFactor = 0.25f;
    /**/
    private AudioDataManager audioDataManager;
    private float radius = 666f;
    private float angle = 0f;
    private float speed;

    public PlayerMoveControl(AudioDataManager audioDataManager) {
        this.audioDataManager = audioDataManager;
        this.speed = audioDataManager.getBPM() * BPM_TO_SPEED_FACTOR;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public float getRadius() {
        return radius;
    }
    long lastDash = 0;

    @Override
    protected void controlUpdate(float tpf) {
        if (spatial.getUserData(UserDataKeys.RUNNING)) {
            if (angle > FastMath.TWO_PI) {
                angle -= FastMath.TWO_PI;
            }

            spatial.setLocalTranslation(MathHelper.calcPointOnCircle(angle, radius,
                    200f + (600f * audioDataManager.getMovementData())));
            angle += MathHelper.degreeToRadian((speed / 4f + (speed * audioDataManager.getSpeedData())) * speedFactor * tpf);
        }
    }

    public Control cloneForSpatial(Spatial spatial) {
        PlayerMoveControl control = new PlayerMoveControl(audioDataManager);
        control.setSpatial(spatial);
        return control;
    }
}
