Commits

Anonymous committed 28a27d1

Daily

Signed-off-by: Meitian Huang <kid85783388@gmail.com>

  • Participants
  • Parent commits ba468d8

Comments (0)

Files changed (11)

File src/Game/Ball.java

  *   This class is a wrap of the RigidBodyControl offered by the engine
  */
 
-public class Ball {
-    private float x, y, z;
+public class Ball extends RigidBody {
+    //    private Vector3f location;
+    //    private float x, y, z;
     private float radius;
-    private Maze maze;
-    private Node ballNode;
-    protected RigidBodyControl ball;
-
-    public Ball(float x, float y, float z, float radius, Maze maze) {
-        this.x = x;
-        this.y = y;
-        this.z = z;
+    private RigidBodyControl ball;
+
+    public Ball(Handler handler) {
+        super(handler);
+    }
+
+    public void setRadius(float radius) {
         this.radius = radius;
-        this.maze = maze;
     }
 
     public void attach() {
-        ballNode = new Node("ball");
-        ballNode.setLocalTranslation(x, y, z);
+        Node ballNode = new Node("ball");
+        ballNode.setLocalTranslation(location);
         ball = new RigidBodyControl(new SphereCollisionShape(radius));
         ballNode.addControl(ball);
-        maze.attachNodeToRoot(ballNode);
-        maze.attachPhysicsControl(ball);
+        handler.attachNodeToRoot(ballNode);
+        handler.attachPhysicsControl(ball);
     }
 
     public Vector3f getPhysicsLocation() {
     public Vector3f getLinearVelocity() {
         return ball.getLinearVelocity();
     }
+
+    public float getRadius() {
+        return radius;
+    }
 }

File src/Game/EndPoint.java

 
 package Game;
 
-import com.jme3.math.Vector3f;
-
 /**
- * EndPoint - This class represents the end of a game.
+ * EndPoint - This class represents the endPosition of a game.
  * An EndPoint is similar to a Hole but it has a different behaviour on
  * collision detection.
  */
 public class EndPoint extends Hole {
-    public EndPoint(Vector3f centre, Maze maze) {
-        super(centre, maze);
+    public EndPoint(Handler handler) {
+        super(handler);
     }
 
     @Override
     public boolean collideWith(Ball ball) {
 //        The ball collides with the end if the distance between their centres
 //        is less than a certain value.
-        return centre.distance(ball.getPhysicsLocation()) < 0.18f;
+        return location.distance(ball.getPhysicsLocation()) < threshold;
     }
 }

File src/Game/Handler.java

+package Game;
+
+import com.jme3.bullet.collision.shapes.BoxCollisionShape;
+import com.jme3.bullet.collision.shapes.CylinderCollisionShape;
+import com.jme3.bullet.control.RigidBodyControl;
+import com.jme3.math.Vector3f;
+import com.jme3.scene.Node;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: Meitian Huang, u4700480
+ * Date: 18/10/11
+ * Time: 10:44 PM
+ */
+public interface Handler {
+    public void rotateBoard(float x, float y, float z);
+
+    public void attachNodeToRoot(Node node);
+
+    public void attachPhysicsControl(RigidBodyControl rigidBodyControl);
+
+    public void addShapeToBase(BoxCollisionShape boxCollisionShape,
+                               Vector3f location);
+
+    public void addShapeToBase(CylinderCollisionShape cylinderCollisionShape,
+                               Vector3f location);
+
+    public float getBallRadius();
+
+    public void resetGame();
+
+    public void stopGame();
+
+    public void loseGame();
+}

File src/Game/Hole.java

  */
 
 public class Hole extends RigidBody {
-    protected CylinderCollisionShape cylinderCollisionShape;
-    protected Vector3f centre;
-    protected float radius;
+    private CylinderCollisionShape cylinderCollisionShape;
+    private float radius;
+    protected float threshold;
 
-    public Hole(Vector3f centre, Maze maze) {
-        super(maze);
-        this.centre = centre;
+    public Hole(Handler handler) {
+        super(handler);
     }
 
     public void setRadius(float radius) {
         this.radius = radius;
+        threshold = (float) Math.sqrt(Math.pow(radius, 2)
+                + Math.pow(handler.getBallRadius(), 2));
     }
 
     @Override
     public void attach() {
         cylinderCollisionShape =
                 new CylinderCollisionShape(new Vector3f(radius, 0.02f, 0.1f), 1);
-        maze.base.addChildShape(cylinderCollisionShape, centre);
+        handler.addShapeToBase(cylinderCollisionShape, location);
     }
 
     public boolean collideWith(Ball ball) {
-//        The ball collides with the end if the distance between their centres
-//        is less than a certain value AND the velocity of the ball is less than
-//        a certain value.
-        return centre.distance(ball.getPhysicsLocation()) < 0.07f
+        /**
+         * The ball collides with the end if the distance between their centres
+         * is less than a certain value AND the velocity of the ball is less than
+         * a certain value.
+         */
+        return location.distance(ball.getPhysicsLocation()) < threshold
                 && ball.getLinearVelocity().length() < 1.1f;
     }
 }

File src/Game/Key.java

  */
 
 public class Key {
-    private Maze maze;
+    private Handler handler;
 
-    public Key(Maze maze) {
-        this.maze = maze;
+    public Key(Handler handler) {
+        this.handler = handler;
     }
 
     private ActionListener actionListener = new ActionListener() {
         public void onAction(String name, boolean pressed, float tpf) {
             if (name.equals("Reset") && !pressed) {
-                maze.pref.init();
-                maze.reset();
+                handler.resetGame();
             } else if (name.equals("Quit") && !pressed) {
-                maze.stop();
+                handler.stopGame();
             } else if (name.equals("Change") && !pressed) {
-                maze.pref.init();
-                maze.reset();
+                handler.resetGame();
             }
         }
     };
     private AnalogListener analogListener = new AnalogListener() {
         public void onAnalog(String name, float value, float tpf) {
             if (name.equals("Lefts")) {
-                maze.rotate(0, 0, 0.0003f * maze.pref.sensitivity);
+                handler.rotateBoard(0, 0, 0.0003f);
             } else if (name.equals("Rights")) {
-                maze.rotate(0, 0, -0.0003f * maze.pref.sensitivity);
+                handler.rotateBoard(0, 0, -0.0003f);
             } else if (name.equals("Ups")) {
-                maze.rotate(-0.0003f * maze.pref.sensitivity, 0, 0);
+                handler.rotateBoard(-0.0003f, 0, 0);
             } else if (name.equals("Downs")) {
-                maze.rotate(0.0003f * maze.pref.sensitivity, 0, 0);
+                handler.rotateBoard(0.0003f, 0, 0);
             }
         }
     };
         inputManager.addListener(actionListener, "Quit");
         inputManager.addListener(actionListener, "Change");
     }
-
-
 }

File src/Game/Maze.java

 import com.jme3.bullet.BulletAppState;
 import com.jme3.bullet.collision.shapes.BoxCollisionShape;
 import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
+import com.jme3.bullet.collision.shapes.CylinderCollisionShape;
 import com.jme3.bullet.control.RigidBodyControl;
 import com.jme3.font.BitmapText;
 import com.jme3.math.Vector3f;
 
 public class Maze extends SimpleApplication {
 
-    BulletAppState bulletAppState;
-    float time;
-    Score score;
-    Pref pref;
-
-    RigidBodyControl board;
-    Ball ball;
-    Node boardNode;
-    CompoundCollisionShape base;
-    ArrayList<Hole> holes;
-    EndPoint end;
-    ArrayList<Wall> walls;
-    BitmapText bitmapText;
-    int timeLeft;
-    boolean isLost;
+    private BulletAppState bulletAppState;
+    private float time;
+    private Score score;
+    private Handler handler;
+    private Pref pref;
+
+    private RigidBodyControl board;
+    private Ball ball;
+    private Node boardNode;
+    private CompoundCollisionShape base;
+    private ArrayList<Hole> holes;
+    private EndPoint end;
+    private ArrayList<Wall> walls;
+    private BitmapText bitmapText;
+    private int timeLeft;
+    private boolean isLost;
 
     @Override
     public void simpleInitApp() {
         time = 0;
-        score = new Score(this);
         bulletAppState = new BulletAppState();
         stateManager.attach(bulletAppState);
         bulletAppState.getPhysicsSpace().enableDebug(assetManager);
         pref = new Pref();
 
+        initHandler();
+        score = new Score(handler);
 
         initWorld();
         setupKeys();
+    }
+
+    private void initHandler() {
+        handler = new Handler() {
+            public void rotateBoard(float x, float y, float z) {
+                if (!isLost) {
+                    boardNode.rotate(x * pref.sensitivity, y * pref.sensitivity, z * pref.sensitivity);
+                }
+            }
+
+            public void attachNodeToRoot(Node node) {
+                rootNode.attachChild(node);
+            }
+
+            public void attachPhysicsControl(RigidBodyControl rigidBodyControl) {
+                bulletAppState.getPhysicsSpace().add(rigidBodyControl);
+            }
 
+            public void addShapeToBase(BoxCollisionShape boxCollisionShape, Vector3f location) {
+                base.addChildShape(boxCollisionShape, location);
+            }
+
+            public void addShapeToBase(CylinderCollisionShape cylinderCollisionShape, Vector3f location) {
+                base.addChildShape(cylinderCollisionShape, location);
+            }
+
+            public float getBallRadius() {
+                return ball.getRadius();
+            }
+
+            public void resetGame() {
+                reset();
+            }
+
+            public void stopGame() {
+                stop();
+            }
+
+            public void loseGame() {
+                System.out.println("You lose!");
+                stop();
+            }
+        };
     }
 
     private void initWorld() {
 
         boardNode.addControl(board);
 
-        attachNodeToRoot(boardNode);
-        attachPhysicsControl(board);
+        handler.attachNodeToRoot(boardNode);
+        handler.attachPhysicsControl(board);
     }
 
 
         walls = new ArrayList<Wall>();
 
         for (Vector3f location : pref.vWallLocations) {
-            walls.add(new Wall(pref.vWallShape, location, this));
+            Wall wall = new Wall(handler);
+            wall.setLocation(location);
+            wall.setWall(pref.vWallShape);
+            wall.attach();
+            walls.add(wall);
         }
 
         for (Vector3f location : pref.hWallLocations) {
-            walls.add(new Wall(pref.hWallShape, location, this));
+            Wall wall = new Wall(handler);
+            wall.setLocation(location);
+            wall.setWall(pref.hWallShape);
+            wall.attach();
+            walls.add(wall);
         }
     }
 
     private void initHoles() {
         holes = new ArrayList<Hole>();
-        for (Vector3f center : pref.holeCentres) {
-            Hole hole = new Hole(center, this);
-            hole.setRadius(pref.dfs.getUnit() * 0.5f);
+        for (Vector3f centre : pref.holeCentres) {
+            Hole hole = new Hole(handler);
+            hole.setLocation(centre);
+            hole.setRadius(pref.getUnit() * 0.5f);
             hole.attach();
             holes.add(hole);
         }
     }
 
     private void initEnd() {
-        end = new EndPoint(pref.end, this);
-        end.setRadius(pref.dfs.getUnit() * 0.9f);
+        end = new EndPoint(handler);
+        end.setLocation(pref.endPosition);
+        end.setRadius(pref.getUnit() * 0.9f);
         end.attach();
     }
 
     private void initBall() {
-        ball = new Ball(0, 0.4f, 0, pref.ballRadius, this);
+        ball = new Ball(handler);
+        ball.setLocation(pref.ballPosition);
+        ball.setRadius(pref.ballRadius);
         ball.attach();
-
     }
 
     private void finalise() {
-//        if (!this.getRenderer().equals(AppSettings.LWJGL_OPENGL1)) {
-//            rootNode.attachChild(SkyFactory.createSky(
-//                    assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
-//        }
         this.setDisplayStatView(false);
         this.setDisplayFps(false);
     }
 
     private void setupKeys() {
-        Key key = new Key(this);
+        Key key = new Key(handler);
         key.associateKey(inputManager);
     }
 
         bitmapText = new BitmapText(guiFont, false);
         bitmapText.setSize(guiFont.getCharSet().getRenderedSize());
         updateText();
-        bitmapText.setLocalTranslation(0, bitmapText.getLineHeight(), 0);
+        bitmapText.setLocalTranslation(300, bitmapText.getLineHeight(), 0);
         guiNode.attachChild(bitmapText);
     }
 
     public void reset() {
+        pref.init();
         bulletAppState.getPhysicsSpace().remove(board);
         boardNode.removeFromParent();
 
         initEnd();
 
         ball.setPhysicsLocation(
-                new Vector3f(pref.ballX, pref.ballY, pref.ballZ));
-    }
-
-    public void rotate(float x, float y, float z) {
-        if (!isLost) {
-            boardNode.rotate(x, y, z);
-        }
-    }
-
-    public void attachNodeToRoot(Node node) {
-        this.rootNode.attachChild(node);
-    }
-
-    public void attachPhysicsControl(RigidBodyControl rigidBodyControl) {
-        bulletAppState.getPhysicsSpace().add(rigidBodyControl);
-    }
-
-    public void lose() {
-        System.out.println("You lose!");
-        this.stop();
+                pref.ballPosition);
     }
 }

File src/Game/Pref.java

     protected int level;
     protected float sensitivity;
 
-    public float ballRadius;
-    public float ballX, ballY, ballZ;
-    public float ballMass;
+    protected float ballRadius;
+    protected Vector3f ballPosition;
+    protected float ballMass;
 
     private float epsilon = 0.001f;
 
 
     protected float boardLength, boardWidth, boardHeight;
 
-    ArrayList<Vector3f> holeCentres;
-    Vector3f end;
+    protected ArrayList<Vector3f> holeCentres;
+    protected Vector3f endPosition;
 
-    DFS dfs;
+    private DFS dfs;
 
     public Pref() {
         level = 1;
     public void init() {
         generateWalls();
         boardWidth = dfs.getHeight() / dfs.getWidth();
-        end = new Vector3f(dfs.randomX(), 0, dfs.randomY());
+        endPosition = new Vector3f(dfs.randomX(), 0, dfs.randomY());
         randomiseBallLocation();
 
+
         holeCentres = new ArrayList<Vector3f>();
         for (int i = 0; i < (5 - level); i++) {
             Vector3f vector3f;
             do {
                 vector3f = new Vector3f(dfs.randomX(), 0, dfs.randomY());
             } while (
-//                    vector3f.equals(end)
-//                            || vector3f.equals(new Vector3f(ballX, 0, ballZ))
-                    ((Math.abs(ballX - vector3f.getX()) < epsilon)
-                            && (Math.abs(ballZ - vector3f.getZ()) < epsilon))
-                            || ((Math.abs(vector3f.getX() - end.getX()) < epsilon)
-                            || (Math.abs(vector3f.getZ() - end.getZ()) < epsilon))
-                            || holeCentres.contains(vector3f)
-                    );
+                    equalVector(ballPosition, vector3f)
+                            || equalVector(endPosition, vector3f)
+                            || holeCentres.contains(vector3f));
 
             holeCentres.add(vector3f);
         }
      * Randomly sets the location of the ball.
      */
     public void randomiseBallLocation() {
+        ballPosition = new Vector3f();
         do {
-            ballX = dfs.randomX();
-            ballZ = dfs.randomY();
+            ballPosition.setX(dfs.randomX());
+            ballPosition.setZ(dfs.randomY());
         }
-        while ((Math.abs(ballX - end.getX()) < epsilon) && (Math.abs(ballZ - end.getZ()) < epsilon));
-        ballY = ballRadius * 1.1f;
+        while ((Math.abs(ballPosition.getX() - endPosition.getX()) < epsilon) && (Math.abs(ballPosition.getZ() - endPosition.getZ()) < epsilon));
+        ballPosition.setY(ballRadius * 1.1f);
     }
 
     /**
         this.level += 1;
         init();
     }
+
+    public boolean equalVector(Vector3f a, Vector3f b) {
+        return Math.abs(a.getX() - b.getX()) < epsilon
+                && Math.abs(b.getZ() - b.getZ()) < epsilon;
+    }
+
+    public float getUnit(){
+        return dfs.getUnit();
+    }
 }

File src/Game/RigidBody.java

  */
 package Game;
 
+import com.jme3.math.Vector3f;
+
 /**
  * RigidBody - This class represents every possible rigid body in the game.
  */
 public abstract class RigidBody {
-    protected Maze maze;
+    Handler handler;
+    Vector3f location;
 
-    protected RigidBody(Maze maze) {
-        this.maze = maze;
+    public RigidBody(Handler handler) {
+        this.handler = handler;
     }
 
     /**
      * A rigid body should be able to attach to the board.
      */
     public abstract void attach();
+
+    public void setLocation(Vector3f location) {
+        this.location = location;
+    }
 }

File src/Game/Score.java

 
 public class Score {
     private int score;
-    private Maze maze;
+    private Handler handler;
 
-    public Score(Maze maze) {
+    public Score(Handler handler) {
         score = 10;
-        this.maze = maze;
+        this.handler = handler;
     }
 
     /**
      */
     public void reduceScore() {
         if ((--score) < 0) {
-            maze.lose();
+            handler.loseGame();
         }
     }
 
     public void reduceScore(int n) {
         if ((score -= n) < 0) {
-            maze.lose();
+            handler.loseGame();
         }
     }
 

File src/Game/Starter.java

 
         Maze app = new Maze();
         app.setSettings(settings);
-        System.out.println(0.1 + 0.2 == 0.3);
         app.start();
     }
 }

File src/Game/Wall.java

 package Game;
 
 import com.jme3.bullet.collision.shapes.BoxCollisionShape;
-import com.jme3.math.Vector3f;
 
 public class Wall extends RigidBody {
     BoxCollisionShape wall;
-    Vector3f location;
 
-    public Wall(BoxCollisionShape wall, Vector3f location, Maze maze) {
-        super(maze);
+    public Wall(Handler handler) {
+        super(handler);
+    }
+
+    public void setWall(BoxCollisionShape wall) {
         this.wall = wall;
-        this.location = location;
-        attach();
     }
 
     @Override
     public void attach() {
-        maze.base.addChildShape(wall, location);
+        handler.addShapeToBase(wall, location);
     }
 }