Advertisement
charlesg

CubeField.java from jMonkey jme3test

Dec 15th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.18 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2009-2010 jMonkeyEngine
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are
  7.  * met:
  8.  *
  9.  * * Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  *
  12.  * * Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  *
  16.  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17.  *   may be used to endorse or promote products derived from this software
  18.  *   without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22.  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32.  
  33. package jme3test.games;
  34.  
  35. import com.jme3.app.SimpleApplication;
  36. import com.jme3.bounding.BoundingVolume;
  37. import com.jme3.font.BitmapFont;
  38. import com.jme3.font.BitmapText;
  39. import com.jme3.input.KeyInput;
  40. import com.jme3.input.controls.AnalogListener;
  41. import com.jme3.input.controls.KeyTrigger;
  42. import com.jme3.material.Material;
  43. import com.jme3.math.ColorRGBA;
  44. import com.jme3.math.FastMath;
  45. import com.jme3.math.Quaternion;
  46. import com.jme3.math.Vector3f;
  47. import com.jme3.scene.Geometry;
  48. import com.jme3.scene.Node;
  49. import com.jme3.scene.shape.Box;
  50. import com.jme3.scene.shape.Dome;
  51. import java.util.ArrayList;
  52. import java.util.logging.Level;
  53. import java.util.logging.Logger;
  54.  
  55. /**
  56.  * @author Kyle "bonechilla" Williams
  57.  */
  58. public class CubeField extends SimpleApplication implements AnalogListener {
  59.  
  60.     public static void main(String[] args) {
  61.         CubeField app = new CubeField();
  62.         app.start();
  63.     }
  64.  
  65.     private BitmapFont defaultFont;
  66.  
  67.     private boolean START;
  68.     private int difficulty, Score, colorInt, highCap, lowCap, diffHelp;
  69.     private Node player;
  70.     private Geometry fcube;
  71.     private ArrayList cubeField;
  72.     private ArrayList obstacleColors;
  73.     private float gameSpeed, coreTime, coreTime2;
  74.     private float camAngle = 0;
  75.     private BitmapText fpsScoreText, pressStart;
  76.  
  77.     private boolean solidBox = true;
  78.     private Material playerMaterial;
  79.     private Material floorMaterial;
  80.  
  81.     private float fpsRate = 1000f / 1f;
  82.  
  83.     /**
  84.      * Initializes game
  85.      */
  86.     @Override
  87.     public void simpleInitApp() {
  88.         Logger.getLogger("com.jme3").setLevel(Level.WARNING);
  89.  
  90.         flyCam.setEnabled(false);
  91.         setDisplayStatView(false);
  92.  
  93.         Keys();
  94.  
  95.         defaultFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
  96.         pressStart = new BitmapText(defaultFont, false);
  97.         fpsScoreText = new BitmapText(defaultFont, false);
  98.  
  99.         loadText(fpsScoreText, "Current Score: 0", defaultFont, 0, 2, 0);
  100.         loadText(pressStart, "PRESS ENTER", defaultFont, 0, 5, 0);
  101.        
  102.         player = createPlayer();
  103.         rootNode.attachChild(player);
  104.         cubeField = new ArrayList();
  105.         obstacleColors = new ArrayList();
  106.  
  107.         gameReset();
  108.     }
  109.     /**
  110.      * Used to reset cubeField
  111.      */
  112.     private void gameReset(){
  113.         Score = 0;
  114.         lowCap = 10;
  115.         colorInt = 8;
  116.         solidBox = true;
  117.         highCap = 40;
  118.         difficulty = highCap;
  119.  
  120.         for (Geometry cube : cubeField){
  121.             cube.removeFromParent();
  122.         }
  123.         cubeField.clear();
  124.  
  125.         if (fcube != null){
  126.             fcube.removeFromParent();
  127.         }
  128.         fcube = createFirstCube();
  129.  
  130.         obstacleColors.clear();
  131.         obstacleColors.add(ColorRGBA.Orange);
  132.         obstacleColors.add(ColorRGBA.Red);
  133.         obstacleColors.add(ColorRGBA.Yellow);
  134.         renderer.setBackgroundColor(ColorRGBA.White);
  135.         gameSpeed = lowCap / 400f;
  136.         coreTime = timer.getTimeInSeconds();
  137.         coreTime2 = coreTime;
  138.         diffHelp=lowCap;
  139.         player.setLocalTranslation(0,0,0);
  140.     }
  141.  
  142.     @Override
  143.     public void simpleUpdate(float tpf) {
  144.         camTakeOver(tpf);
  145.         if (START){
  146.             gameLogic(tpf);
  147.         }
  148.         colorLogic();
  149.     }
  150.     /**
  151.      * Forcefully takes over Camera adding functionality and placing it behind the character
  152.      * @param tpf Tickes Per Frame
  153.      */
  154.     private void camTakeOver(float tpf) {
  155.         cam.setLocation(player.getLocalTranslation().add(-8, 2, 0));
  156.         cam.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
  157.        
  158.         Quaternion rot = new Quaternion();
  159.         rot.fromAngleNormalAxis(camAngle, Vector3f.UNIT_Z);
  160.         cam.setRotation(cam.getRotation().mult(rot));
  161.         camAngle *= FastMath.pow(.99f, fpsRate * tpf);
  162.     }
  163.  
  164.     @Override
  165.     public void requestClose(boolean esc) {
  166.         if (!esc){
  167.             System.out.println("The game was quit.");
  168.         }else{
  169.             System.out.println("Player has Collided. Final Score is " + Score);
  170.         }
  171.         context.destroy(false);
  172.     }
  173.     /**
  174.      * Randomly Places a cube on the map between 30 and 90 paces away from player
  175.      */
  176.     private void randomizeCube() {
  177.         Geometry cube = fcube.clone();
  178.         int playerX = (int) player.getLocalTranslation().getX();
  179.         int playerZ = (int) player.getLocalTranslation().getZ();
  180. //        float x = FastMath.nextRandomInt(playerX + difficulty + 10, playerX + difficulty + 150);
  181.         float x = FastMath.nextRandomInt(playerX + difficulty + 30, playerX + difficulty + 90);
  182.         float z = FastMath.nextRandomInt(playerZ - difficulty - 50, playerZ + difficulty + 50);
  183.         cube.getLocalTranslation().set(x, 0, z);
  184.  
  185. //        playerX+difficulty+30,playerX+difficulty+90
  186.  
  187.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  188.         if (!solidBox){
  189.             mat.getAdditionalRenderState().setWireframe(true);
  190.         }
  191.         mat.setColor("Color", obstacleColors.get(FastMath.nextRandomInt(0, obstacleColors.size() - 1)));
  192.         cube.setMaterial(mat);
  193.  
  194.         rootNode.attachChild(cube);
  195.         cubeField.add(cube);
  196.     }
  197.  
  198.     private Geometry createFirstCube() {
  199.         Vector3f loc = player.getLocalTranslation();
  200.         loc.addLocal(4, 0, 0);
  201.         Box b = new Box(loc, 1, 1, 1);
  202.  
  203.         Geometry geom = new Geometry("Box", b);
  204.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  205.         mat.setColor("Color", ColorRGBA.Blue);
  206.         geom.setMaterial(mat);
  207.  
  208.         return geom;
  209.     }
  210.  
  211.     private Node createPlayer() {
  212.         Dome b = new Dome(Vector3f.ZERO, 10, 100, 1);
  213.         Geometry playerMesh = new Geometry("Box", b);
  214.  
  215.         playerMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  216.         playerMaterial.setColor("Color", ColorRGBA.Red);
  217.         playerMesh.setMaterial(playerMaterial);
  218.         playerMesh.setName("player");
  219.  
  220.         Box floor = new Box(Vector3f.ZERO.add(playerMesh.getLocalTranslation().getX(),
  221.                 playerMesh.getLocalTranslation().getY() - 1, 0), 100, 0, 100);
  222.         Geometry floorMesh = new Geometry("Box", floor);
  223.  
  224.         floorMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  225.         floorMaterial.setColor("Color", ColorRGBA.LightGray);
  226.         floorMesh.setMaterial(floorMaterial);
  227.         floorMesh.setName("floor");
  228.  
  229.         Node playerNode = new Node();
  230.         playerNode.attachChild(playerMesh);
  231.         playerNode.attachChild(floorMesh);
  232.  
  233.         return playerNode;
  234.     }
  235.  
  236.     /**
  237.      * If Game is Lost display Score and Reset the Game
  238.      */
  239.     private void gameLost(){
  240.         START = false;
  241.         loadText(pressStart, "You lost! Press enter to try again.", defaultFont, 0, 5, 0);
  242.         gameReset();
  243.     }
  244.    
  245.     /**
  246.      * Core Game Logic
  247.      */
  248.     private void gameLogic(float tpf){
  249.         //Subtract difficulty level in accordance to speed every 10 seconds
  250.         if(timer.getTimeInSeconds()>=coreTime2){
  251.             coreTime2=timer.getTimeInSeconds()+10;
  252.             if(difficultylowCap){
  253.                 difficulty-=5;
  254.                 diffHelp+=1;
  255.             }
  256.         }
  257.        
  258.         if(gameSpeed difficulty){
  259.             Geometry g;
  260.             g = cubeField.get(0);
  261.             g.removeFromParent();
  262.             cubeField.remove(g);
  263.         }else if (cubeField.size() != difficulty){
  264.             randomizeCube();
  265.         }
  266.  
  267.         if (cubeField.isEmpty()){
  268.             requestClose(false);
  269.         }else{
  270.             for (int i = 0; i < cubeField.size(); i++){
  271.                
  272.                 //better way to check collision
  273.                 Geometry playerModel = (Geometry) player.getChild(0);
  274.                 Geometry cubeModel = cubeField.get(i);
  275.                 cubeModel.updateGeometricState();
  276.  
  277.                 BoundingVolume pVol = playerModel.getWorldBound();
  278.                 BoundingVolume vVol = cubeModel.getWorldBound();
  279.  
  280.                 if (pVol.intersects(vVol)){
  281.                     gameLost();
  282.                     return;
  283.                 }
  284.                 //Remove cube if 10 world units behind player
  285.                 if (cubeField.get(i).getLocalTranslation().getX() + 10 = coreTime){
  286.            
  287.             colorInt++;
  288.             coreTime = timer.getTimeInSeconds() + 20;
  289.        
  290.  
  291.             switch (colorInt){
  292.                 case 1:
  293.                     obstacleColors.clear();
  294.                     solidBox = false;
  295.                     obstacleColors.add(ColorRGBA.Green);
  296.                     renderer.setBackgroundColor(ColorRGBA.Black);
  297.                     playerMaterial.setColor("Color", ColorRGBA.White);
  298.             floorMaterial.setColor("Color", ColorRGBA.Black);
  299.                     break;
  300.                 case 2:
  301.                     obstacleColors.set(0, ColorRGBA.Black);
  302.                     solidBox = true;
  303.                     renderer.setBackgroundColor(ColorRGBA.White);
  304.                     playerMaterial.setColor("Color", ColorRGBA.Gray);
  305.                         floorMaterial.setColor("Color", ColorRGBA.LightGray);
  306.                     break;
  307.                 case 3:
  308.                     obstacleColors.set(0, ColorRGBA.Pink);
  309.                     break;
  310.                 case 4:
  311.                     obstacleColors.set(0, ColorRGBA.Cyan);
  312.                     obstacleColors.add(ColorRGBA.Magenta);
  313.                     renderer.setBackgroundColor(ColorRGBA.Gray);
  314.                         floorMaterial.setColor("Color", ColorRGBA.Gray);
  315.                     playerMaterial.setColor("Color", ColorRGBA.White);
  316.                     break;
  317.                 case 5:
  318.                     obstacleColors.remove(0);
  319.                     renderer.setBackgroundColor(ColorRGBA.Pink);
  320.                     solidBox = false;
  321.                     playerMaterial.setColor("Color", ColorRGBA.White);
  322.                     break;
  323.                 case 6:
  324.                     obstacleColors.set(0, ColorRGBA.White);
  325.                     solidBox = true;
  326.                     renderer.setBackgroundColor(ColorRGBA.Black);
  327.                     playerMaterial.setColor("Color", ColorRGBA.Gray);
  328.                         floorMaterial.setColor("Color", ColorRGBA.LightGray);
  329.                     break;
  330.                 case 7:
  331.                     obstacleColors.set(0, ColorRGBA.Green);
  332.                     renderer.setBackgroundColor(ColorRGBA.Gray);
  333.                     playerMaterial.setColor("Color", ColorRGBA.Black);
  334.                         floorMaterial.setColor("Color", ColorRGBA.Orange);
  335.                     break;
  336.                 case 8:
  337.                     obstacleColors.set(0, ColorRGBA.Red);
  338.                         floorMaterial.setColor("Color", ColorRGBA.Pink);
  339.                     break;
  340.                 case 9:
  341.                     obstacleColors.set(0, ColorRGBA.Orange);
  342.                     obstacleColors.add(ColorRGBA.Red);
  343.                     obstacleColors.add(ColorRGBA.Yellow);
  344.                     renderer.setBackgroundColor(ColorRGBA.White);
  345.                     playerMaterial.setColor("Color", ColorRGBA.Red);
  346.                     floorMaterial.setColor("Color", ColorRGBA.Gray);
  347.                     colorInt=0;
  348.                     break;
  349.                 default:
  350.                     break;
  351.             }
  352.         }
  353.     }
  354.     /**
  355.      * Sets up a BitmapText to be displayed
  356.      * @param txt the Bitmap Text
  357.      * @param text the
  358.      * @param font the font of the text
  359.      * @param x    
  360.      * @param y
  361.      * @param z
  362.      */
  363.     private void loadText(BitmapText txt, String text, BitmapFont font, float x, float y, float z) {
  364.         txt.setSize(font.getCharSet().getRenderedSize());
  365.         txt.setLocalTranslation(txt.getLineWidth() * x, txt.getLineHeight() * y, z);
  366.         txt.setText(text);
  367.         guiNode.attachChild(txt);
  368.     }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement