Advertisement
absorr

Main

Feb 13th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.29 KB | None | 0 0
  1. package com.lhsalliance.aquatic.core;
  2.  
  3. import com.jme3.animation.AnimChannel;
  4. import com.jme3.animation.AnimControl;
  5. import com.jme3.animation.AnimEventListener;
  6. import com.jme3.animation.LoopMode;
  7. import com.jme3.app.SimpleApplication;
  8. import com.jme3.export.binary.BinaryImporter;
  9. import com.jme3.font.BitmapFont;
  10. import com.jme3.font.BitmapText;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.MouseInput;
  13. import com.jme3.input.controls.ActionListener;
  14. import com.jme3.input.controls.AnalogListener;
  15. import com.jme3.input.controls.KeyTrigger;
  16. import com.jme3.input.controls.MouseButtonTrigger;
  17. import com.jme3.light.DirectionalLight;
  18. import com.jme3.material.Material;
  19. import com.jme3.math.ColorRGBA;
  20. import com.jme3.math.FastMath;
  21. import com.jme3.math.Quaternion;
  22. import com.jme3.math.Vector3f;
  23. import com.jme3.niftygui.NiftyJmeDisplay;
  24. import com.jme3.renderer.RenderManager;
  25. import com.jme3.renderer.ViewPort;
  26. import com.jme3.scene.CameraNode;
  27. import com.jme3.scene.Geometry;
  28. import com.jme3.scene.Node;
  29. import com.jme3.scene.Spatial;
  30. import com.jme3.scene.control.CameraControl.ControlDirection;
  31. import com.jme3.scene.shape.Box;
  32. import com.jme3.system.AppSettings;
  33. import com.jme3.ui.Picture;
  34. import com.lhsalliance.aquatic.entities.Anemone;
  35. import com.lhsalliance.aquatic.entities.Animal;
  36. import com.lhsalliance.aquatic.entities.AnimalRegistry;
  37. import com.lhsalliance.aquatic.entities.KillAI;
  38. import com.lhsalliance.aquatic.entities.Model;
  39. import com.lhsalliance.aquatic.entities.Updatable;
  40. import com.lhsalliance.aquatic.scene.Biome;
  41. import com.lhsalliance.aquatic.scene.BiomeRegistry;
  42. import com.lhsalliance.aquatic.scene.WorldGen;
  43. import de.lessvoid.nifty.Nifty;
  44. import de.lessvoid.nifty.screen.Screen;
  45. import de.lessvoid.nifty.screen.ScreenController;
  46. import java.awt.image.BufferedImage;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.Iterator;
  50. import java.util.logging.Level;
  51. import java.util.logging.Logger;
  52. import javax.imageio.ImageIO;
  53.  
  54. /**
  55.  * test
  56.  * @author normenhansen
  57.  */
  58. public class Main extends SimpleApplication implements AnimEventListener, ScreenController {
  59.  
  60.     public static Main game;
  61.    
  62.     public Animal player;
  63.     public HandlerPlayer playerHandler = new HandlerPlayer();
  64.    
  65.      //define age control bools
  66.     public boolean mate = false;
  67.     public Model heart;
  68.     public static boolean hide = false;
  69.    
  70.     public boolean isRunning=true;
  71.     public boolean isRight=false;
  72.     public boolean isLeft=false;
  73.     public boolean isUp=false;
  74.     public boolean isDown=false;
  75.     public boolean isInGame = false;
  76.    
  77.     private AnimChannel channel;
  78.     private AnimControl control;
  79.    
  80.     public long seed = 1209345223;
  81.     public Biome biome = BiomeRegistry.RIEF;
  82.    
  83.     public CameraNode camNode;
  84.     public Node movementNode = new Node();
  85.     public ViewPort bgvp;
  86.     public Picture bgpic;
  87.    
  88.     public int ticks = 0;
  89.    
  90.     public static void main(String[] args) {
  91.         Main app = new Main();
  92.         AppSettings cfg = new AppSettings(true);
  93.         cfg.setFrameRate(60); // set to less than or equal screen refresh rate
  94.         cfg.setVSync(true);   // prevents page tearing
  95.         cfg.setFrequency(60); // set to screen refresh rate
  96.         cfg.setResolution(1024, 768);  
  97.         cfg.setFullscreen(false);
  98.         cfg.setSamples(2);    // anti-aliasing
  99.         cfg.setTitle("Aquatic Adventure"); // branding: window name
  100.         cfg.setSettingsDialogImage("Interface/dialog.png");
  101.         try {
  102.           // Branding: window icon
  103.           cfg.setIcons(new BufferedImage[]{ImageIO.read(new File("assets/Interface/icon.png"))});
  104.         } catch (IOException ex) {
  105.           Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Icon missing.", ex);
  106.         }
  107.         // branding: load splashscreen from assets
  108.         //cfg.setSettingsDialogImage("Interface/MySplashscreen.png");
  109.         //app.setShowSettings(false); // or don't display splashscreen
  110.         app.setSettings(cfg);
  111.         app.start();
  112.        
  113.         game = app;
  114.     }
  115.  
  116.     @Override
  117.     public void simpleInitApp() {
  118.         AnimalRegistry.addAnimal(new Animal("Clownfish",
  119.                 new Model("assets/Models/clownfish/clownfish.j3o"),
  120.                 10));
  121.         AnimalRegistry.addAnimal(new Animal("Great White Shark",
  122.                 new Model("assets/Models/GreatWhiteShark/GreatWhiteShark.j3o"),
  123.                 50));
  124.         AnimalRegistry.addAnimal(new Animal("Parrot Fish",
  125.                 new Model("assets/Models/ParrotFish/ParrotFish.j3o"),
  126.                 50));
  127.         AnimalRegistry.addAnimal(new Animal("Angelfish",
  128.                 new Model("assets/Models/AngelFishRigged/AngelFishRigged.j3o"),
  129.                 50));
  130.         AnimalRegistry.addAnimal(new Animal("Tiger Shark",
  131.                 new Model("assets/Models/tiger shark/tiger shark.j3o"),
  132.                 50));
  133.         AnimalRegistry.addAnimal(new Animal("Hammerhead Shark",
  134.                 new Model("assets/Models/hammerhead shark/hammerhead shark.j3o"),
  135.                 50));
  136.         AnimalRegistry.addAnimal(new Animal("Octopus",
  137.                 new Model("assets/Models/octopus/octopus.j3o"),
  138.                 50));
  139.         AnimalRegistry.addAnimal(new Animal("Sea Turtle",
  140.                 new Model("assets/Models/Sea turtle/Sea turtle.j3o"),
  141.                 50));
  142.         AnimalRegistry.addAnimal(new Animal("Moray Eel",
  143.                 new Model("assets/Models/Moray eel/Moray eel.j3o"),
  144.                 50));
  145.         AnimalRegistry.addAnimal(new Animal("Manta Ray",
  146.                 new Model("assets/Models/MantaRay/MantaRay.j3o"),
  147.                 50));
  148.        
  149.         menuMain();
  150.         //btn_Start();
  151.        
  152.         initKeys(); // load my custom keybinding
  153.         inputManager.setCursorVisible(true);
  154.         flyCam.setEnabled(true);
  155.     }
  156.  
  157.     @Override
  158.     public void simpleUpdate(float tpf) {
  159.        
  160.         if (isInGame)
  161.         {
  162.             bgpic.updateLogicalState(1);
  163.             bgpic.updateGeometricState();
  164.             hide = false;
  165.             for (Updatable obj : Updatable.objects) {
  166.                 obj.onUpdate(tpf);
  167.             }
  168.            
  169.             Iterator itr = Animal.getAnimals();
  170.             while(itr.hasNext())
  171.             {
  172.                 Animal ani = (Animal) itr.next();
  173.                 ani.update(tpf);
  174.             }
  175.         }
  176.        
  177.         playerHandler.update(tpf);
  178.        
  179.         ticks++;
  180.     }
  181.  
  182.     @Override
  183.     public void simpleRender(RenderManager rm) {
  184.         //TODO: add render code
  185.     }
  186.    
  187.     /** Custom Keybinding: Map named actions to inputs. */
  188.     private void initKeys() {
  189.       // You can map one or several inputs to one named action
  190.       inputManager.addMapping("Pause",  new KeyTrigger(KeyInput.KEY_ESCAPE));
  191.       inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
  192.       inputManager.addMapping("Left",   new KeyTrigger(KeyInput.KEY_D));
  193.     inputManager.addMapping("Right",  new KeyTrigger(KeyInput.KEY_A));
  194.     inputManager.addMapping("Up",     new KeyTrigger(KeyInput.KEY_W));
  195.     inputManager.addMapping("Down",   new KeyTrigger(KeyInput.KEY_S));
  196.    
  197.     // Add the names to the action listener.
  198.     inputManager.addListener(analogListener,"Left", "Right", "Up", "Down", "Space");
  199.  
  200.     }
  201.  
  202.     private ActionListener actionListener = new ActionListener() {
  203.       public void onAction(String name, boolean keyPressed, float tpf) {
  204.         if (name.equals("Pause") && !keyPressed) {
  205.           isRunning = !isRunning;
  206.         }
  207.        
  208.       }
  209.     };
  210.  
  211.     private AnalogListener analogListener = new AnalogListener() {
  212.       public void onAnalog(String name, float value, float tpf) {
  213.         if (isRunning) {
  214.             if (isInGame)
  215.             {
  216.                 if (name.equals("Right")) {
  217.                     isRight = true;
  218.                     Vector3f v = player.model.node.getLocalTranslation();
  219.                     Vector3f c = camNode.getLocalTranslation();
  220.                     player.model.node.setLocalTranslation(v.x + value*8*speed, v.y, v.z);
  221.                     camNode.setLocalTranslation(c.x + value*8*speed, c.y, c.z);
  222.                     player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(90*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  223.                     channel.setAnim("Walk", 0.50f);
  224.                     channel.setLoopMode(LoopMode.Loop);
  225.                   }
  226.                   if (name.equals("Left")) {
  227.                     isLeft = true;
  228.                     Vector3f v = player.model.node.getLocalTranslation();
  229.                     Vector3f c = camNode.getLocalTranslation();
  230.                     player.model.node.setLocalTranslation(v.x - value*8*speed, v.y, v.z);
  231.                     camNode.setLocalTranslation(c.x - value*8*speed, c.y, c.z);
  232.                     player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(270*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  233.                   }
  234.                   if (name.equals("Up")) {
  235.                       if(isRight){
  236.                           player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(45*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  237.                       }
  238.                       if(isLeft){
  239.                           player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(315*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  240.                       }
  241.                     isUp = true;
  242.                     Vector3f v = player.model.node.getLocalTranslation();
  243.                     Vector3f c = camNode.getLocalTranslation();
  244.                     player.model.node.setLocalTranslation(v.x, v.y, v.z + value*8*speed);
  245.                     camNode.setLocalTranslation(c.x, c.y, c.z + value*8*speed);
  246.                     player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(0*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  247.                   }
  248.                   if (name.equals("Down")) {
  249.                       if(isRight){
  250.                           player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(135*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  251.                       }
  252.                       if(isLeft){
  253.                           player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(225*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  254.                       }
  255.                     isDown = true;
  256.                     Vector3f v = player.model.node.getLocalTranslation();
  257.                     Vector3f c = camNode.getLocalTranslation();
  258.                     player.model.node.setLocalTranslation(v.x, v.y, v.z - value*8*speed);
  259.                     camNode.setLocalTranslation(c.x, c.y, c.z - value*8*speed);
  260.                     player.model.node.setLocalRotation(new Quaternion().fromAngleAxis(180*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  261.                   }
  262.                  
  263.                   if(isLeft || isRight || isUp || isDown)
  264.                       HandlerPlayer.distTraveled++;
  265.             }
  266.             else
  267.             {
  268.                 if (name.equals("Space")){
  269.                     //openBiomeScreen();
  270.                     btn_Start();
  271.                 }
  272.             }
  273.         } else {
  274.           System.out.println("Press P to unpause.");
  275.         }
  276.       }
  277.     };
  278.    
  279.     public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
  280.       if (animName.equals("Walk")) {
  281.         channel.setAnim("stand", 0.50f);
  282.         channel.setLoopMode(LoopMode.DontLoop);
  283.         channel.setSpeed(1f);
  284.       }
  285.     }
  286.  
  287.     public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
  288.       // unused
  289.     }
  290.    
  291.     public NiftyJmeDisplay niftyDisplay;
  292.     public Nifty nifty;
  293.    
  294.     public void menuMain()
  295.     {
  296.         flyCam.setEnabled(false);
  297.         niftyDisplay = new NiftyJmeDisplay(assetManager,
  298.                                                           inputManager,
  299.                                                           audioRenderer,
  300.                                                           guiViewPort);
  301.         nifty = niftyDisplay.getNifty();
  302.         nifty.fromXml("Interface/Screen.xml", "start", this);
  303.         guiViewPort.addProcessor(niftyDisplay);
  304.        
  305.     }
  306.  
  307.     @Override
  308.     public void bind(Nifty nifty, Screen screen) {
  309.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  310.     }
  311.  
  312.     @Override
  313.     public void onStartScreen() {
  314.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  315.     }
  316.     public void openTutorialScreen() {
  317.  
  318.        nifty.addXml("Interface/Screen.xml");
  319.  
  320.        nifty.gotoScreen("tutorial");
  321.  
  322.     }
  323.     public void openBiomeScreen() {
  324.  
  325.        nifty.addXml("Interface/Screen.xml");
  326.  
  327.        nifty.gotoScreen("biome");
  328.        
  329.  
  330.     }
  331.     public void openAnimalScreen() {
  332.  
  333.        nifty.addXml("Interface/Screen.xml");
  334.  
  335.        nifty.gotoScreen("animal");
  336.     }
  337.     public void openFinScreen() {
  338.  
  339.        nifty.addXml("Interface/Screen.xml");
  340.  
  341.        nifty.gotoScreen("fin");
  342.        isRunning = false;
  343.     }
  344.     @Override
  345.     public void onEndScreen() {
  346.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  347.     }
  348.    
  349.     /*
  350.      * Button Functions
  351.      */
  352.    
  353.     public void btn_Start()
  354.     {
  355.         //Unload main menu
  356.         nifty.exit();
  357.        
  358.         bgpic = new Picture("Background Picture");
  359.         bgpic.setImage(assetManager, "Interface/newbg1.png", false);
  360.         bgpic.setWidth(settings.getWidth());
  361.         bgpic.setHeight(settings.getHeight());
  362.         bgpic.setPosition(0,0);
  363.         bgpic.updateGeometricState();
  364.        
  365.         bgvp = renderManager.createPreView("background", cam);
  366.         bgvp.setClearFlags(true, true, true);
  367.         bgvp.attachScene(bgpic);
  368.         viewPort.setClearFlags(false, true, true);
  369.        
  370.         //Make the player
  371.         player = AnimalRegistry.getAnimal("Clownfish");
  372.         player.model.loadModel();
  373.         control = player.model.node.getControl(AnimControl.class);
  374.         control.addListener(this);
  375.         channel = control.createChannel();
  376.         channel.setAnim("ArmatureAction");
  377.        
  378.         // You must add a light to make the model visible
  379.         DirectionalLight sun = new DirectionalLight();
  380.         sun.setDirection(new Vector3f(-0.8f, -0.7f, 2.0f));
  381.         rootNode.addLight(sun);
  382.        
  383.         //create the camera Node
  384.         camNode = new CameraNode("Camera Node", cam);
  385.         //This mode means that camera copies the movements of the target:
  386.         camNode.setControlDir(ControlDirection.SpatialToCamera);
  387.         //Attach the camNode to the target:
  388.         rootNode.attachChild(movementNode);
  389.         movementNode.attachChild(camNode);
  390.         movementNode.attachChild(player.model.node);
  391.         //Move camNode, e.g. behind and above the target:
  392.         camNode.setLocalTranslation(new Vector3f(0, 50, -50));
  393.         //Rotate the camNode to look at the target:
  394.         camNode.lookAt(player.model.node.getLocalTranslation(), Vector3f.UNIT_Y);
  395.        
  396.         WorldGen.generate();
  397.        
  398.         isInGame = true;
  399.     }
  400.    
  401.     public BitmapFont getFont()
  402.     {
  403.         return this.guiFont;
  404.     }
  405.    
  406.     public int getWidth()
  407.     {
  408.         return this.settings.getWidth();
  409.     }
  410.    
  411.     public int getHeight()
  412.     {
  413.         return this.settings.getHeight();
  414.     }
  415.    
  416.        //age level control methods
  417.    
  418.     public void mating()
  419.     {
  420.         if(!mate)
  421.         {
  422.             heart = new Model("assets/Models/heart/heart.j3o");
  423.             heart.loadModel();
  424.             heart.node.setLocalRotation(new Quaternion().fromAngleAxis(90*FastMath.DEG_TO_RAD, new Vector3f(0,1,0)));
  425.         }
  426.         heart.node.setLocalTranslation(player.model.node.getLocalTranslation().x,
  427.                 player.model.node.getLocalTranslation().y + 4,
  428.                 player.model.node.getLocalTranslation().z);
  429.         this.mate = true;
  430.     }
  431.     public void hiding()
  432.     {
  433.         hide = true;
  434.     }
  435.     public boolean isHiding()
  436.     {
  437.         boolean retVal;
  438.         if (hide == true)
  439.             retVal = true;
  440.         else
  441.             retVal = false;
  442.         return retVal;
  443.     }
  444.    
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement