/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.jme3.app.SimpleApplication
 *  com.jme3.collision.Collidable
 *  com.jme3.collision.CollisionResult
 *  com.jme3.collision.CollisionResults
 *  com.jme3.effect.ParticleEmitter
 *  com.jme3.effect.ParticleMesh$Type
 *  com.jme3.font.BitmapText
 *  com.jme3.input.controls.ActionListener
 *  com.jme3.input.controls.InputListener
 *  com.jme3.input.controls.KeyTrigger
 *  com.jme3.input.controls.MouseButtonTrigger
 *  com.jme3.input.controls.Trigger
 *  com.jme3.light.DirectionalLight
 *  com.jme3.light.Light
 *  com.jme3.material.Material
 *  com.jme3.math.ColorRGBA
 *  com.jme3.math.Ray
 *  com.jme3.math.Vector3f
 *  com.jme3.scene.Geometry
 *  com.jme3.scene.Mesh
 *  com.jme3.scene.Node
 *  com.jme3.scene.Spatial
 *  com.jme3.scene.shape.Box
 */
package jme3.hello;

import com.jme3.app.SimpleApplication;
import com.jme3.collision.Collidable;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.font.BitmapText;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.InputListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;

public class HelloPicking
extends SimpleApplication {
    Node shootables;
    Node inventory;
    boolean pickedUp;
    Geometry mark;
    private ActionListener actionListener = new ActionListener(){

        public void onAction(String name, boolean keyPressed, float tpf) {
            Ray ray;
            CollisionResults results;
            if (name.equals("Shoot") && !keyPressed) {
                results = new CollisionResults();
                ray = new Ray(HelloPicking.this.cam.getLocation(), HelloPicking.this.cam.getDirection());
                HelloPicking.this.shootables.collideWith((Collidable)ray, results);
                System.out.println("----- Collisions?" + results.size() + "-----");
                for (int i = 0; i < results.size(); ++i) {
                    float dist = results.getCollision(i).getDistance();
                    Vector3f pt = results.getCollision(i).getContactPoint();
                    String hit = results.getCollision(i).getGeometry().getName();
                    System.out.println("* Collision #" + i);
                    System.out.println(" You shot " + hit + "at" + pt + "," + dist + "wu away.");
                }
                if (results.size() > 0) {
                    CollisionResult closest = results.getClosestCollision();
                    HelloPicking.this.mark.setLocalTranslation(closest.getContactPoint());
                    Geometry geo = closest.getGeometry();
                    Boolean isModel = (Boolean)geo.getUserData("isModel");
                    if (isModel != null && !isModel.booleanValue()) {
                        Material mat = new Material(HelloPicking.this.assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
                        mat.setColor("Color", ColorRGBA.randomColor());
                        geo.setMaterial(mat);
                    }
                    HelloPicking.this.rootNode.attachChild((Spatial)HelloPicking.this.mark);
                } else {
                    HelloPicking.this.rootNode.detachChild((Spatial)HelloPicking.this.mark);
                }
            }
            if (name.equals("Inventar") && !keyPressed) {
                results = new CollisionResults();
                ray = new Ray(HelloPicking.this.cam.getLocation(), HelloPicking.this.cam.getDirection());
                HelloPicking.this.shootables.collideWith((Collidable)ray, results);
                if (results.size() > 0) {
                    CollisionResult closest = results.getClosestCollision();
                    Geometry inv = closest.getGeometry();
                    if (!HelloPicking.this.pickedUp) {
                        HelloPicking.this.pickedUp = true;
                        HelloPicking.this.shootables.detachChild((Spatial)inv);
                        inv.setLocalScale(100.0f);
                        HelloPicking.this.inventory.attachChild((Spatial)inv);
                    } else {
                        HelloPicking.this.pickedUp = false;
                        Geometry box = (Geometry)HelloPicking.this.inventory.getChild(0);
                        box.setLocalScale(1.0f);
                        box.setLocalTranslation(closest.getContactPoint());
                        HelloPicking.this.inventory.detachChild((Spatial)box);
                        HelloPicking.this.shootables.attachChild((Spatial)box);
                    }
                }
            }
        }
    };

    public static void main(String[] args) {
        HelloPicking app = new HelloPicking();
        app.start();
    }

    public void simpleInitApp() {
        this.initCrossHairs();
        this.initKeys();
        this.initMark();
        Spatial model = this.assetManager.loadModel("Models/Oto/Oto.mesh.xml");
        model.setLocalTranslation(new Vector3f(1.0f, 2.0f, -3.0f));
        model.setUserData("isModel", (Object)new Boolean(true));
        model.setLocalScale(0.5f);
        DirectionalLight light = new DirectionalLight();
        light.setDirection(new Vector3f(-0.1f, -1.0f, -1.0f).normalizeLocal());
        this.rootNode.addLight((Light)light);
        this.shootables = new Node("Shootables");
        this.inventory = new Node("Inventar");
        this.guiNode.attachChild((Spatial)this.inventory);
        this.rootNode.attachChild((Spatial)this.shootables);
        this.shootables.attachChild((Spatial)this.makeCube("a Dragon", -2.0f, 0.0f, 1.0f));
        this.shootables.attachChild((Spatial)this.makeCube("a tin can", 1.0f, -2.0f, 0.0f));
        this.shootables.attachChild((Spatial)this.makeCube("the Sheriff", 0.0f, 1.0f, -2.0f));
        this.shootables.attachChild((Spatial)this.makeCube("the Deputy", 1.0f, 0.0f, -4.0f));
        this.shootables.attachChild((Spatial)this.makeFloor());
        this.shootables.attachChild(model);
    }

    private void initKeys() {
        this.inputManager.addMapping("Shoot", new Trigger[]{new KeyTrigger(57), new MouseButtonTrigger(0)});
        this.inputManager.addMapping("Inventar", new Trigger[]{new MouseButtonTrigger(1)});
        this.inputManager.addListener((InputListener)this.actionListener, new String[]{"Shoot", "Inventar"});
    }

    protected Geometry makeCube(String name, float x, float y, float z) {
        Box box = new Box(new Vector3f(x, y, z), 1.0f, 1.0f, 1.0f);
        Geometry cube = new Geometry(name, (Mesh)box);
        cube.setUserData("isModel", (Object)new Boolean(false));
        Material mat1 = new Material(this.assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.randomColor());
        cube.setMaterial(mat1);
        return cube;
    }

    protected Geometry makeFloor() {
        Box box = new Box(new Vector3f(0.0f, -4.0f, -5.0f), 15.0f, 0.2f, 15.0f);
        Geometry floor = new Geometry("the Floor", (Mesh)box);
        Material mat1 = new Material(this.assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.LightGray);
        floor.setMaterial(mat1);
        return floor;
    }

    protected void initMark() {
        ParticleEmitter fire = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
        Material mat_red = new Material(this.assetManager, "Common/MatDefs/Misc/Particle.j3md");
        mat_red.setTexture("Texture", this.assetManager.loadTexture("Effects/Explosion/flame.png"));
        fire.setMaterial(mat_red);
        fire.setImagesX(2);
        fire.setImagesY(2);
        fire.setEndColor(new ColorRGBA(1.0f, 0.0f, 0.0f, 1.0f));
        fire.setStartColor(new ColorRGBA(1.0f, 1.0f, 0.0f, 0.5f));
        fire.getParticleInfluencer().setInitialVelocity(new Vector3f(0.0f, 2.0f, 0.0f));
        fire.setStartSize(1.5f);
        fire.setEndSize(0.1f);
        fire.setGravity(0.0f, 0.0f, 0.0f);
        fire.setLowLife(1.0f);
        fire.setHighLife(3.0f);
        fire.getParticleInfluencer().setVelocityVariation(0.3f);
        this.mark = fire;
    }

    protected void initCrossHairs() {
        this.guiNode.detachAllChildren();
        this.guiFont = this.assetManager.loadFont("Interface/Fonts/Default.fnt");
        BitmapText ch = new BitmapText(this.guiFont, false);
        ch.setSize((float)(this.guiFont.getCharSet().getRenderedSize() * 2));
        ch.setText("+");
        ch.setLocalTranslation((float)(this.settings.getWidth() / 2 - this.guiFont.getCharSet().getRenderedSize() / 3 * 2), (float)(this.settings.getHeight() / 2) + ch.getLineHeight() / 2.0f, 0.0f);
        this.guiNode.attachChild((Spatial)ch);
    }
}
