Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1.  
  2. import com.jme3.app.SimpleApplication;
  3. import com.jme3.asset.plugins.ZipLocator;
  4. import com.jme3.bullet.BulletAppState;
  5. import com.jme3.bullet.collision.PhysicsCollisionEvent;
  6. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  7. import com.jme3.bullet.collision.shapes.CollisionShape;
  8. import com.jme3.bullet.control.CharacterControl;
  9. import com.jme3.bullet.control.RigidBodyControl;
  10. import com.jme3.bullet.util.CollisionShapeFactory;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.controls.ActionListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.light.AmbientLight;
  15. import com.jme3.light.DirectionalLight;
  16. import com.jme3.math.ColorRGBA;
  17. import com.jme3.math.Vector3f;
  18. import com.jme3.scene.Node;
  19. import com.jme3.scene.Spatial;
  20. import com.jme3.*;
  21. import com.jme3.bullet.collision.PhysicsCollisionListener;
  22. import com.jme3.bullet.collision.PhysicsCollisionObject;
  23. import com.jme3.bullet.collision.PhysicsRayTestResult;
  24. import com.jme3.bullet.collision.shapes.BoxCollisionShape;
  25. import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
  26. import com.jme3.collision.CollisionResults;
  27. import com.jme3.font.BitmapText;
  28. import com.jme3.material.Material;
  29. import com.jme3.math.FastMath;
  30. import com.jme3.math.Quaternion;
  31. import com.jme3.math.Ray;
  32. import com.jme3.scene.Geometry;
  33. import com.jme3.scene.shape.Box;
  34. import com.jme3.scene.shape.Cylinder;
  35. import java.io.File;
  36. import java.io.FileNotFoundException;
  37. import java.util.ArrayList;
  38. import java.util.Collection;
  39. import java.util.List;
  40. import java.util.Scanner;
  41. import jme3tools.optimize.GeometryBatchFactory;
  42.  
  43. public class TrapFactory {
  44.  
  45. private ArrayList<Node> traplist;
  46. private ArrayList<ArrayList<Spatial>> mapTrapList;
  47. private File file;
  48. private Material trapmat;
  49. private BulletAppState bulletAppState;
  50. private String TrapNodeName, TrapName, PlayerNodeName;
  51. private Node rootNode;
  52.  
  53. public TrapFactory(ArrayList<Node> traplist1, Material mat, BulletAppState appstate, String trn, String tn, String pn, Node rootnode) {
  54. traplist = traplist1;
  55. mapTrapList = new ArrayList<ArrayList<Spatial>>();
  56. trapmat = mat;
  57. bulletAppState = appstate;
  58. TrapNodeName = trn;
  59. TrapName = tn;
  60. PlayerNodeName = pn;
  61. rootNode = rootnode;
  62. }
  63.  
  64. public ArrayList<Node> gettraplist() {
  65. return traplist;
  66. }
  67.  
  68. public ArrayList<ArrayList<Spatial>> parseFile(String filename) {
  69.  
  70. Scanner sc = null;
  71. Scanner lc = null;
  72. if (!filename.equals("")) {
  73. file = new File(filename);
  74. }
  75.  
  76. try {
  77. sc = new Scanner(file);
  78. lc = new Scanner(file);
  79. } catch (FileNotFoundException e) {
  80. System.out.println("FILE NOT FOUND");
  81. }
  82.  
  83. int linecount = 0;
  84.  
  85. while (lc.hasNextLine()) {
  86. String trash = lc.nextLine();
  87. linecount++;
  88. }
  89.  
  90. ArrayList<Spatial> trapList = null;
  91. int counter = 0;
  92. while (sc.hasNextLine()) {
  93.  
  94. ArrayList<String> stringList = new ArrayList<String>();
  95. String s = sc.nextLine();
  96.  
  97. for (int i = 0; i < s.length(); i++) {
  98. char ch = s.charAt(i);
  99. String sh = Character.toString(ch);
  100. stringList.add(sh);
  101. }
  102. Node nottrashnode = new Node("nottrash!");
  103. trapList = new ArrayList<Spatial>();
  104. for (int i = 0; i < stringList.size(); i++) {
  105. if(stringList.get(i).equals("t")){
  106. trapList.add(nottrashnode);
  107. }else{
  108. trapList.add(makeTrapRepresentation());
  109. }
  110.  
  111. }
  112.  
  113. mapTrapList.add(trapList);
  114. counter++;
  115.  
  116. }
  117. System.out.println();
  118. return mapTrapList;
  119. }
  120.  
  121. private Node makeATrap() {
  122. TrapControl trapcontrol = new TrapControl(TrapNodeName, TrapName, PlayerNodeName);
  123.  
  124. Node trapnode = new Node(TrapNodeName);
  125.  
  126. Box box = new Box(Vector3f.ZERO, 3, 0.2f, 3);
  127. Geometry trap = new Geometry(TrapName, box);
  128. trap.setMaterial(trapmat);
  129. trap.setLocalTranslation(0, 1, 0);
  130.  
  131. trapnode.attachChild(trap);
  132. //rootNode.attachChild(trapnode);
  133.  
  134. RigidBodyControl myControl = new RigidBodyControl(0f);
  135. //myControl.setEnabled(true);
  136.  
  137. trapnode.addControl(myControl);
  138. trapnode.addControl(trapcontrol);
  139.  
  140. trapcontrol.setSpatial();
  141.  
  142. bulletAppState.getPhysicsSpace().addCollisionListener(trapcontrol);
  143. bulletAppState.getPhysicsSpace().add(myControl);
  144.  
  145. traplist.add(trapnode);
  146. return trapnode;
  147. }
  148.  
  149. public Spatial makeTrapRepresentation() {
  150. Node n = new Node("trash");
  151. return n;
  152. }
  153.  
  154. public void attachMapToNode(Node n) {
  155. for (int j = 0; j < mapTrapList.size(); j++) {
  156. for (int i = 0; i < mapTrapList.size(); i++) {
  157. if (!mapTrapList.get(j).get(i).getName().equals("trash")) {
  158. float sizing = 5f;//Add a way to get this from square class
  159. Node trapnode = makeATrap();
  160. trapnode.setLocalTranslation((sizing * i * 2), 1, (sizing * j * 2));
  161. trapnode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f((sizing * i * 2), 5, (sizing * j * 2)));
  162. n.attachChild(trapnode);
  163. }
  164. }
  165. }
  166. }
  167.  
  168. public void checkForUsedTraps() {
  169. for (Node s : traplist) {
  170. if (!s.getControl(TrapControl.class).getarmed()) {
  171. s.getControl(RigidBodyControl.class).getPhysicsSpace().removeCollisionListener(s.getControl(TrapControl.class));
  172. }
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement