package com.mygame;
//set 0
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
// F3 D3
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.InputManager;
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.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
//3456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
/**
 * This is the Main Class of your Game. You should only do initialization here.
 * Move your Logic into AppStates or Controls
 * @author normenhansen
 */
public class Main extends SimpleApplication implements PhysicsCollisionListener, ActionListener {

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }
    private RigidBodyControl playerBody;
    private boolean IsGround = false;
    @Override
    public void simpleInitApp() {
        Box b = new Box(1, 1, 1);
        
        Geometry geom = new Geometry("Player", b);
        

        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        
        // Создаётся коллизия
        CollisionShape boxShape = new BoxCollisionShape(new Vector3f(1,1,1));
        
        // Создание физического тела
        playerBody= new RigidBodyControl(boxShape, 1f); 
        geom.addControl(playerBody);
       
        BulletAppState bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        PhysicsSpace physicsSpace = bulletAppState.getPhysicsSpace();
       
        
        // Игра "не читай комментарии в коде"
        // Ты проиграл

        rootNode.attachChild(geom);
        
        
     
         Box c = new Box(1, 1, 1);
        
        Geometry geom1 = new Geometry("Box1", c);
        geom1.setLocalTranslation(new Vector3f(0, -10, 0));

        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.Red);
        geom1.setMaterial(mat1);
        
        // Создаётся коллизия
        CollisionShape boxShape1 = new BoxCollisionShape(new Vector3f(1,1,1));
        
        // Создание физического тела
        RigidBodyControl boxRigidBody1 = new RigidBodyControl(boxShape1, 0f); 
        geom1.addControl(boxRigidBody1);
       
        
        stateManager.attach(bulletAppState);
        physicsSpace.add(playerBody);
        
        physicsSpace.add(boxRigidBody1);
        
        physicsSpace.addCollisionListener(this);
        
        
        rootNode.attachChild(geom1);
        
        inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(this, "Jump");
        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_H));
        inputManager.addListener(this, "Left");
        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_L));
        inputManager.addListener(this, "Right");
        inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_K));
        inputManager.addListener(this, "Up");
        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_J));
        inputManager.addListener(this, "Down");
        
    }
    
    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm /* -rf lol */) {
        //TODO: add render code
    }
    @Override
    public void collision(PhysicsCollisionEvent event) {
        
        if (event.getNodeA().getName().equals("Player") && event.getNodeB().getName().equals("Box1")) {
            IsGround = true;
        }            
    }

    @Override
    public void onAction(String name, boolean isPressed, float f) {
        if (name.equals("Jump") && isPressed && IsGround) {
            playerBody.applyImpulse(new Vector3f(0, 10, 0), Vector3f.ZERO);
            IsGround = false;
        }
        if (name.equals("Left") && isPressed) {
            playerBody.applyImpulse(new Vector3f(-5, 0, 0), Vector3f.ZERO);
        }
        if (name.equals("Right") && isPressed) {
            playerBody.applyImpulse(new Vector3f(5, 0, 0), Vector3f.ZERO);
        }
        if (name.equals("Up") && isPressed) {
            playerBody.applyImpulse(new Vector3f(0, 0, -5), Vector3f.ZERO);
        }
        if (name.equals("Down") && isPressed) {
            playerBody.applyImpulse(new Vector3f(0, 0, 5), Vector3f.ZERO);
        }
    }
}
/*
how to mKe a linu,x
nubmer o1 make a lindx
sucessfuly mde linoz
*/
