package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;

/**
 *
 * @author Rolf
 */
public class Cannonball implements ActionListener {

    public static final float CANNONBALLSPEED = 35.0f; // units per second
    Vector3f position, speed;
    boolean isFlying = false;
    Cannon cannon;
    Node parentNode;
    Geometry geomCannonBall;

    public Cannonball(SimpleApplication sa, Cannon cannon, Node parent) {
        parentNode = parent;
        this.cannon = cannon;
        //
        Material gold = new Material(sa.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        gold.setBoolean("UseMaterialColors", true);
        gold.setColor("Ambient", ColorRGBA.Red);
        gold.setColor("Diffuse", ColorRGBA.Green);
        gold.setColor("Specular", ColorRGBA.Gray);
        gold.setFloat("Shininess", 40f); // shininess from 1-128
        //
        // geometry cannonball
        Sphere ball = new Sphere(32, 32, 0.7f);
        geomCannonBall = new Geometry("ball", ball);
        geomCannonBall.setMaterial(gold);
        geomCannonBall.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);

        // init input        
        sa.getInputManager().addMapping("SHOOT", new KeyTrigger(KeyInput.KEY_SPACE));
        sa.getInputManager().addListener(this, new String[]{"SHOOT"});
    }

    // -------------------------------------------------------------------------
    public void onAction(String name, boolean isPressed, float tpf) {
        // shoot only if the cannonball is currently not flying
        if (isPressed && !isFlying) {
            isFlying = true;
            //
            // compute speed
            Quaternion q1 = new Quaternion();
            q1.fromAngleAxis(cannon.barrelRotation, Vector3f.UNIT_X);
            speed = q1.mult(Vector3f.UNIT_Z);
            
            Quaternion q2 = new Quaternion();
            q2.fromAngleAxis(cannon.baseRotation, Vector3f.UNIT_Y);
            speed = q2.mult(speed);
            
            
            speed = speed.mult(CANNONBALLSPEED);
            //
            // set position
            position = new Vector3f(0,2f,0);
            //
            // attach to tree
            parentNode.attachChild(geomCannonBall);

        }
    }

    // -------------------------------------------------------------------------
    // called from SimpleUpdate. This moves the cannon ball.
    public void fly(float tpf) {
        if (!isFlying) {
            return;
        } else {
            Vector3f posDiff = speed.mult(tpf);
            position.addLocal(posDiff);
            speed.y -= 9.81*tpf;
            geomCannonBall.setLocalTranslation(position);
            //
            // end cannonball flight if too low or too far away
            if (position.y <0 || position.length()>80){
                isFlying = false;
                parentNode.detachChild(geomCannonBall);
            }
            
        }
    }
}
