CubeField.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) 2009-2012 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. package jme3test.games;
  33. import com.jme3.app.SimpleApplication;
  34. import com.jme3.bounding.BoundingVolume;
  35. import com.jme3.font.BitmapFont;
  36. import com.jme3.font.BitmapText;
  37. import com.jme3.input.KeyInput;
  38. import com.jme3.input.controls.AnalogListener;
  39. import com.jme3.input.controls.KeyTrigger;
  40. import com.jme3.material.Material;
  41. import com.jme3.math.ColorRGBA;
  42. import com.jme3.math.FastMath;
  43. import com.jme3.math.Quaternion;
  44. import com.jme3.math.Vector3f;
  45. import com.jme3.scene.Geometry;
  46. import com.jme3.scene.Node;
  47. import com.jme3.scene.shape.Box;
  48. import com.jme3.scene.shape.Dome;
  49. import java.util.ArrayList;
  50. import java.util.logging.Level;
  51. import java.util.logging.Logger;
  52. /**
  53. * @author Kyle "bonechilla" Williams
  54. */
  55. public class CubeField extends SimpleApplication implements AnalogListener {
  56. public static void main(String[] args) {
  57. CubeField app = new CubeField();
  58. app.start();
  59. }
  60. private BitmapFont defaultFont;
  61. private boolean START;
  62. private int difficulty, Score, colorInt, highCap, lowCap,diffHelp;
  63. private Node player;
  64. private Geometry fcube;
  65. private ArrayList<Geometry> cubeField;
  66. private ArrayList<ColorRGBA> obstacleColors;
  67. private float speed, coreTime,coreTime2;
  68. private float camAngle = 0;
  69. private BitmapText fpsScoreText, pressStart;
  70. private boolean solidBox = true;
  71. private Material playerMaterial;
  72. private Material floorMaterial;
  73. private float fpsRate = 1000f / 1f;
  74. /**
  75. * Initializes game
  76. */
  77. @Override
  78. public void simpleInitApp() {
  79. Logger.getLogger("com.jme3").setLevel(Level.WARNING);
  80. flyCam.setEnabled(false);
  81. setDisplayStatView(false);
  82. Keys();
  83. defaultFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
  84. pressStart = new BitmapText(defaultFont, false);
  85. fpsScoreText = new BitmapText(defaultFont, false);
  86. loadText(fpsScoreText, "Current Score: 0", defaultFont, 0, 2, 0);
  87. loadText(pressStart, "PRESS ENTER", defaultFont, 0, 5, 0);
  88. player = createPlayer();
  89. rootNode.attachChild(player);
  90. cubeField = new ArrayList<Geometry>();
  91. obstacleColors = new ArrayList<ColorRGBA>();
  92. gameReset();
  93. }
  94. /**
  95. * Used to reset cubeField
  96. */
  97. private void gameReset(){
  98. Score = 0;
  99. lowCap = 10;
  100. colorInt = 0;
  101. highCap = 40;
  102. difficulty = highCap;
  103. for (Geometry cube : cubeField){
  104. cube.removeFromParent();
  105. }
  106. cubeField.clear();
  107. if (fcube != null){
  108. fcube.removeFromParent();
  109. }
  110. fcube = createFirstCube();
  111. obstacleColors.clear();
  112. obstacleColors.add(ColorRGBA.Orange);
  113. obstacleColors.add(ColorRGBA.Red);
  114. obstacleColors.add(ColorRGBA.Yellow);
  115. renderer.setBackgroundColor(ColorRGBA.White);
  116. speed = lowCap / 400f;
  117. coreTime = 20.0f;
  118. coreTime2 = 10.0f;
  119. diffHelp=lowCap;
  120. player.setLocalTranslation(0,0,0);
  121. }
  122. @Override
  123. public void simpleUpdate(float tpf) {
  124. camTakeOver(tpf);
  125. if (START){
  126. gameLogic(tpf);
  127. }
  128. colorLogic();
  129. }
  130. /**
  131. * Forcefully takes over Camera adding functionality and placing it behind the character
  132. * @param tpf Tickes Per Frame
  133. */
  134. private void camTakeOver(float tpf) {
  135. cam.setLocation(player.getLocalTranslation().add(-8, 2, 0));
  136. cam.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
  137. Quaternion rot = new Quaternion();
  138. rot.fromAngleNormalAxis(camAngle, Vector3f.UNIT_Z);
  139. cam.setRotation(cam.getRotation().mult(rot));
  140. camAngle *= FastMath.pow(.99f, fpsRate * tpf);
  141. }
  142. @Override
  143. public void requestClose(boolean esc) {
  144. if (!esc){
  145. System.out.println("The game was quit.");
  146. }else{
  147. System.out.println("Player has Collided. Final Score is " + Score);
  148. }
  149. context.destroy(false);
  150. }
  151. /**
  152. * Randomly Places a cube on the map between 30 and 90 paces away from player
  153. */
  154. private void randomizeCube() {
  155. Geometry cube = fcube.clone();
  156. int playerX = (int) player.getLocalTranslation().getX();
  157. int playerZ = (int) player.getLocalTranslation().getZ();
  158. // float x = FastMath.nextRandomInt(playerX + difficulty + 10, playerX + difficulty + 150);
  159. float x = FastMath.nextRandomInt(playerX + difficulty + 30, playerX + difficulty + 90);
  160. float z = FastMath.nextRandomInt(playerZ - difficulty - 50, playerZ + difficulty + 50);
  161. cube.getLocalTranslation().set(x, 0, z);
  162. // playerX+difficulty+30,playerX+difficulty+90
  163. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  164. if (!solidBox){
  165. mat.getAdditionalRenderState().setWireframe(true);
  166. }
  167. mat.setColor("Color", obstacleColors.get(FastMath.nextRandomInt(0, obstacleColors.size() - 1)));
  168. cube.setMaterial(mat);
  169. rootNode.attachChild(cube);
  170. cubeField.add(cube);
  171. }
  172. private Geometry createFirstCube() {
  173. Vector3f loc = player.getLocalTranslation();
  174. loc.addLocal(4, 0, 0);
  175. Box b = new Box(loc, 1, 1, 1);
  176. Geometry geom = new Geometry("Box", b);
  177. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  178. mat.setColor("Color", ColorRGBA.Blue);
  179. geom.setMaterial(mat);
  180. return geom;
  181. }
  182. private Node createPlayer() {
  183. Dome b = new Dome(Vector3f.ZERO, 10, 100, 1);
  184. Geometry playerMesh = new Geometry("Box", b);
  185. playerMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  186. playerMaterial.setColor("Color", ColorRGBA.Red);
  187. playerMesh.setMaterial(playerMaterial);
  188. playerMesh.setName("player");
  189. Box floor = new Box(Vector3f.ZERO.add(playerMesh.getLocalTranslation().getX(),
  190. playerMesh.getLocalTranslation().getY() - 1, 0), 100, 0, 100);
  191. Geometry floorMesh = new Geometry("Box", floor);
  192. floorMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  193. floorMaterial.setColor("Color", ColorRGBA.LightGray);
  194. floorMesh.setMaterial(floorMaterial);
  195. floorMesh.setName("floor");
  196. Node playerNode = new Node();
  197. playerNode.attachChild(playerMesh);
  198. playerNode.attachChild(floorMesh);
  199. return playerNode;
  200. }
  201. /**
  202. * If Game is Lost display Score and Reset the Game
  203. */
  204. private void gameLost(){
  205. START = false;
  206. loadText(pressStart, "You lost! Press enter to try again.", defaultFont, 0, 5, 0);
  207. gameReset();
  208. }
  209. /**
  210. * Core Game Logic
  211. */
  212. private void gameLogic(float tpf){
  213. //Subtract difficulty level in accordance to speed every 10 seconds
  214. if(timer.getTimeInSeconds()>=coreTime2){
  215. coreTime2=timer.getTimeInSeconds()+10;
  216. if(difficulty<=lowCap){
  217. difficulty=lowCap;
  218. }
  219. else if(difficulty>lowCap){
  220. difficulty-=5;
  221. diffHelp+=1;
  222. }
  223. }
  224. if(speed<.1f){
  225. speed+=.000001f*tpf*fpsRate;
  226. }
  227. player.move(speed * tpf * fpsRate, 0, 0);
  228. if (cubeField.size() > difficulty){
  229. cubeField.remove(0);
  230. }else if (cubeField.size() != difficulty){
  231. randomizeCube();
  232. }
  233. if (cubeField.isEmpty()){
  234. requestClose(false);
  235. }else{
  236. for (int i = 0; i < cubeField.size(); i++){
  237. //better way to check collision
  238. Geometry playerModel = (Geometry) player.getChild(0);
  239. Geometry cubeModel = cubeField.get(i);
  240. cubeModel.updateGeometricState();
  241. BoundingVolume pVol = playerModel.getWorldBound();
  242. BoundingVolume vVol = cubeModel.getWorldBound();
  243. if (pVol.intersects(vVol)){
  244. gameLost();
  245. return;
  246. }
  247. //Remove cube if 10 world units behind player
  248. if (cubeField.get(i).getLocalTranslation().getX() + 10 < player.getLocalTranslation().getX()){
  249. cubeField.get(i).removeFromParent();
  250. cubeField.remove(cubeField.get(i));
  251. }
  252. }
  253. }
  254. Score += fpsRate * tpf;
  255. fpsScoreText.setText("Current Score: "+Score);
  256. }
  257. /**
  258. * Sets up the keyboard bindings
  259. */
  260. private void Keys() {
  261. inputManager.addMapping("START", new KeyTrigger(KeyInput.KEY_RETURN));
  262. inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT));
  263. inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT));
  264. inputManager.addListener(this, "START", "Left", "Right");
  265. }
  266. public void onAnalog(String binding, float value, float tpf) {
  267. if (binding.equals("START") && !START){
  268. START = true;
  269. guiNode.detachChild(pressStart);
  270. System.out.println("START");
  271. }else if (START == true && binding.equals("Left")){
  272. player.move(0, 0, -(speed / 2f) * value * fpsRate);
  273. camAngle -= value*tpf;
  274. }else if (START == true && binding.equals("Right")){
  275. player.move(0, 0, (speed / 2f) * value * fpsRate);
  276. camAngle += value*tpf;
  277. }
  278. }
  279. /**
  280. * Determines the colors of the player, floor, obstacle and background
  281. */
  282. private void colorLogic() {
  283. if (timer.getTimeInSeconds() >= coreTime){
  284. colorInt++;
  285. coreTime = timer.getTimeInSeconds() + 20;
  286. switch (colorInt){
  287. case 1:
  288. obstacleColors.clear();
  289. solidBox = false;
  290. obstacleColors.add(ColorRGBA.Green);
  291. renderer.setBackgroundColor(ColorRGBA.Black);
  292. playerMaterial.setColor("Color", ColorRGBA.White);
  293. floorMaterial.setColor("Color", ColorRGBA.Black);
  294. break;
  295. case 2:
  296. obstacleColors.set(0, ColorRGBA.Black);
  297. solidBox = true;
  298. renderer.setBackgroundColor(ColorRGBA.White);
  299. playerMaterial.setColor("Color", ColorRGBA.Gray);
  300. floorMaterial.setColor("Color", ColorRGBA.LightGray);
  301. break;
  302. case 3:
  303. obstacleColors.set(0, ColorRGBA.Pink);
  304. break;
  305. case 4:
  306. obstacleColors.set(0, ColorRGBA.Cyan);
  307. obstacleColors.add(ColorRGBA.Magenta);
  308. renderer.setBackgroundColor(ColorRGBA.Gray);
  309. floorMaterial.setColor("Color", ColorRGBA.Gray);
  310. playerMaterial.setColor("Color", ColorRGBA.White);
  311. break;
  312. case 5:
  313. obstacleColors.remove(0);
  314. renderer.setBackgroundColor(ColorRGBA.Pink);
  315. solidBox = false;
  316. playerMaterial.setColor("Color", ColorRGBA.White);
  317. break;
  318. case 6:
  319. obstacleColors.set(0, ColorRGBA.White);
  320. solidBox = true;
  321. renderer.setBackgroundColor(ColorRGBA.Black);
  322. playerMaterial.setColor("Color", ColorRGBA.Gray);
  323. floorMaterial.setColor("Color", ColorRGBA.LightGray);
  324. break;
  325. case 7:
  326. obstacleColors.set(0, ColorRGBA.Green);
  327. renderer.setBackgroundColor(ColorRGBA.Gray);
  328. playerMaterial.setColor("Color", ColorRGBA.Black);
  329. floorMaterial.setColor("Color", ColorRGBA.Orange);
  330. break;
  331. case 8:
  332. obstacleColors.set(0, ColorRGBA.Red);
  333. floorMaterial.setColor("Color", ColorRGBA.Pink);
  334. break;
  335. case 9:
  336. obstacleColors.set(0, ColorRGBA.Orange);
  337. obstacleColors.add(ColorRGBA.Red);
  338. obstacleColors.add(ColorRGBA.Yellow);
  339. renderer.setBackgroundColor(ColorRGBA.White);
  340. playerMaterial.setColor("Color", ColorRGBA.Red);
  341. floorMaterial.setColor("Color", ColorRGBA.Gray);
  342. colorInt=0;
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. }
  349. /**
  350. * Sets up a BitmapText to be displayed
  351. * @param txt the Bitmap Text
  352. * @param text the
  353. * @param font the font of the text
  354. * @param x
  355. * @param y
  356. * @param z
  357. */
  358. private void loadText(BitmapText txt, String text, BitmapFont font, float x, float y, float z) {
  359. txt.setSize(font.getCharSet().getRenderedSize());
  360. txt.setLocalTranslation(txt.getLineWidth() * x, txt.getLineHeight() * y, z);
  361. txt.setText(text);
  362. guiNode.attachChild(txt);
  363. }
  364. }