Strategy mouse area picking

package strategy;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.collision.Collidable;
import com.jme3.collision.CollisionResults;
import com.jme3.input.ChaseCamera;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Rectangle;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.RectangleMesh;
import java.time.Instant;

/**
 * This is the Main Class of your Game. It should boot up your game and do initial initialisation
 * Move your Logic into AppStates or Controls or other java classes
 */
public class Strategy extends SimpleApplication implements PhysicsCollisionListener{

    public static void main(String[] args) {
        Strategy app = new Strategy();
        app.setShowSettings(false); //Settings dialog not supported on mac
        app.start();
    }
    
    Node shootables;
    BulletAppState bulletAppState;
    Geometry collidable;
    @Override
    public void simpleInitApp() {
        setUpLight();
        inputManager.setCursorVisible(true);
        flyCam.setDragToRotate(true);
        shootables = new Node("Shootables");
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        
        Spatial model = assetManager.loadModel("Models/level.glb");
        shootables.attachChild(model);
        
      //  shootables.attachChild(makeCube("a Dragon", -2f, 0f, 1f));
       // shootables.attachChild(makeCube("a tin can", 1f, 0f, 0f));
       // shootables.attachChild(makeCube("the Sheriff", 0f, 0f, -2f));
       // shootables.attachChild(makeCube("the Deputy", 1f, 0f, -4f));
       
        
        collidable =  makeCube("dragon", -2f, 0f, 1f);
        
        GhostControl ghost = new GhostControl(CollisionShapeFactory.createDynamicMeshShape(collidable)); 
        collidable.addControl(ghost); 
        
        rootNode.attachChild(collidable);
        bulletAppState.getPhysicsSpace().add(ghost);
        
        

        
        cursor =  makeCube("cursor", 0, 0f, 0);
        

        
        shootables.attachChild(cursor);
        
        rootNode.attachChild(shootables);
        inputManager.addMapping("Target",new MouseButtonTrigger(MouseInput.BUTTON_RIGHT)); // trigger 2: left-button click
        
        inputManager.addListener(actionListener,"Target");
        cam.setLocation(new Vector3f(0,5f,0));

        cam.lookAtDirection(new Vector3f(0,-5,0), new Vector3f(0,0,0));
        //flyCam.setEnabled(false);
        //ChaseCamera chaseCam = new ChaseCamera(cam, cursor, inputManager);
        bulletAppState.getPhysicsSpace().addCollisionListener(this);
    bulletAppState.setDebugEnabled(true);
    }
    
    public void collision(PhysicsCollisionEvent event) {
         System.out.println(event.getNodeA().getName());
        
    }
     
    Geometry cursor;
  private Geometry makeCube(String name, float x, float y, float z) {
    Box box = new Box(1, 1, 1);
    Geometry cube = new Geometry(name, box);
    cube.setLocalTranslation(x, y, z);
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat1.setColor("Color", ColorRGBA.randomColor());
    cube.setMaterial(mat1);
    return cube;
  }
    Vector3f startPoint;
    Vector3f endPoint;
    private boolean press = false;
    Geometry area;
    long areaDeleteTime = 0;
        
    final private ActionListener actionListener = new ActionListener() {
            @Override
            public void onAction(String name, boolean keyPressed, float tpf) {
                if (name.equals("Target") ) {
                    if(keyPressed){
                       
                        Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

                        Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);

                        direction.subtractLocal(origin).normalizeLocal();


                        CollisionResults results = new CollisionResults();
                        //create ray
                        Ray ray = new Ray( cam.getLocation() , direction );


                        //collide mouse click wit npc node
                        shootables.collideWith( ray, results );

                        if(results.size() > 0){
                           press = true;
                           startPoint = results.getCollision(0).getContactPoint();
                        }
                    
                    }
                    
                    if(!keyPressed && press){
                        Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

                        Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);

                        direction.subtractLocal(origin).normalizeLocal();


                        CollisionResults results = new CollisionResults();
                        //create ray
                        Ray ray = new Ray( cam.getLocation() , direction );
                        

                        //collide mouse click wit npc node
                        shootables.collideWith( ray, results );

                        if(results.size() > 0){
                            
                            endPoint = results.getCollision(0).getContactPoint();
                            Rectangle rectangle = new Rectangle();
                            
                            if(startPoint.z > endPoint.z){
                                rectangle.setA(new Vector3f(startPoint.x,startPoint.y+0.3f,startPoint.z));
                                rectangle.setB(new Vector3f(endPoint.x,startPoint.y+0.3f ,startPoint.z));
                                rectangle.setC(new Vector3f(startPoint.x,endPoint.y+0.3f ,endPoint.z));
                            }else{
                                rectangle.setA(new Vector3f(startPoint.x,startPoint.y+0.3f,startPoint.z));
                                rectangle.setC(new Vector3f(endPoint.x,startPoint.y +0.3f,startPoint.z));
                                rectangle.setB(new Vector3f(startPoint.x,endPoint.y +0.3f,endPoint.z));
                            }
                            //moveCamera(1f,true);
                            RectangleMesh mesh = new RectangleMesh(rectangle);

                            
                            area = new Geometry("OurMesh", mesh); // using our custom mesh object
                            Material mat = new Material(assetManager,
                                "Common/MatDefs/Misc/Unshaded.j3md");
                           
                            mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
                            mat.setColor("Color", ColorRGBA.Blue);
                            area.setQueueBucket(Bucket.Transparent);

                            area.setMaterial(mat);
                            
                            GhostControl ghostArea = new GhostControl(CollisionShapeFactory.createDynamicMeshShape(area));  // a box-shaped ghost
                            area.addControl(ghostArea); 
                            bulletAppState.getPhysicsSpace().add(ghostArea);
                            rootNode.attachChild(area);
                            
                            areaDeleteTime = Instant.now().getEpochSecond();
                        }
                    }
                }
            }
        };
    @Override
    public void simpleUpdate(float tpf) {
        if(area != null && areaDeleteTime < Instant.now().getEpochSecond()){
            GhostControl ghost = area.getControl(GhostControl.class);
             bulletAppState.getPhysicsSpace().remove(ghost);
            rootNode.detachChild(area);
            
        }
        cameraControl();
        
    }
    private void  cameraControl(){
        Vector2f click2d = inputManager.getCursorPosition();
        float width   = getContext().getSettings().getWidth();
        float height =getContext().getSettings().getHeight();
        Vector3f position = cursor.getLocalTranslation();
        
        //System.out.println(click2d);
        if((0 < click2d.x) && (10 > click2d.x)){
            cursor.setLocalTranslation(position.x + 0.1f, position.y, position.z);
        }
        if((width- 10f < click2d.x) && (width > click2d.x)){
            cursor.setLocalTranslation(position.x - 0.1f, position.y, position.z);
        }
        if((height - 10f < click2d.y)){
            cursor.setLocalTranslation(position.x, position.y, position.z+0.1f);
        
        }
        if((10f > click2d.y) && (0 < click2d.x)){
            cursor.setLocalTranslation(position.x, position.y, position.z-0.1f);
        
        }
        Vector3f direction = cursor.getLocalTranslation().subtract(cam.getLocation()).normalize();

        cam.lookAtDirection(direction, new Vector3f(0,0,0));
        cam.setLocation(new Vector3f(position.x-0.5f,position.y+8f,position.z-4.5f));
    }
    @Override
    public void simpleRender(RenderManager rm) {
        //add render code here (if any)
    }
    
    private void setUpLight() {


        // We add light so we see the scene
        AmbientLight al = new AmbientLight();
        al.setColor(ColorRGBA.White.mult(5.3f));
        rootNode.addLight(al);

        DirectionalLight dl = new DirectionalLight();
        dl.setColor(ColorRGBA.White);
        dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
        rootNode.addLight(dl);

        DirectionalLight dl2 = new DirectionalLight();
        dl2.setColor(ColorRGBA.White);
       dl2.setDirection(new Vector3f(100,3,26).normalizeLocal());
       rootNode.addLight(dl2);

        DirectionalLight dl3 = new DirectionalLight();
        dl3.setColor(ColorRGBA.White);
       dl3.setDirection(new Vector3f(-100,3,26).normalizeLocal());
       rootNode.addLight(dl3);

    }
    
}