diff --git a/src/main/java/doodle_jump/Game.java b/src/main/java/doodle_jump/Game.java index 5c6807a..59be947 100644 --- a/src/main/java/doodle_jump/Game.java +++ b/src/main/java/doodle_jump/Game.java @@ -84,7 +84,7 @@ protected void preAction() { */ protected void postAction() { if (this.flag) { - moveStageUp(); + this.platforms.moveStageUp(); System.out.println("Flag is true"); this.flag = false; } @@ -122,18 +122,6 @@ public void play() { this.showResultScreen(); } - private void moveStageUp() { - int offset = UP_LIMIT; - for (Platform p : this.platforms.list) { - p.setCoordinates(p.getCoordinateX(), p.getCoordinateY() + offset); - if (p.getCoordinateX() > this.getHeight()) { - p.setCoordinates( - Math.random() * (getWidth() - p.getWidth()), - (Math.random() * 50) - 60); - } - } - } - private class DoodleGameKeyListener extends KeyAdapter { @Override diff --git a/src/main/java/doodle_jump/PlatformCollection.java b/src/main/java/doodle_jump/PlatformCollection.java index c7f4ae3..6b5b7cc 100644 --- a/src/main/java/doodle_jump/PlatformCollection.java +++ b/src/main/java/doodle_jump/PlatformCollection.java @@ -35,9 +35,21 @@ public void genNewPlatforms() { int x = (int) (Math.random() * (getWidth() - p.getWidth())); int y = i * ((int) (Math.random() * 50) + 70); p.setCoordinates(x, y); - this.list.add(p); - this.add(p); + this.addObj(p); } } + + public void moveStageUp() { + int offset = Game.UP_LIMIT; + + for (Platform p : this.getCollection()) { + p.setCoordinates(p.getCoordinateX(), p.getCoordinateY() + offset); + if (p.getCoordinateY() > (double) this.getHeight()) { + p.setCoordinates( + Math.random() * (getWidth() - p.getWidth()), + (Math.random() * 50) - 60); + } + } + } } diff --git a/src/main/java/game_engine/GameObjectCollection.java b/src/main/java/game_engine/GameObjectCollection.java index a52b6e6..289219d 100644 --- a/src/main/java/game_engine/GameObjectCollection.java +++ b/src/main/java/game_engine/GameObjectCollection.java @@ -1,9 +1,12 @@ package game_engine; import java.util.ArrayList; + +import javax.swing.JCheckBox; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.image.ImageObserver; +import java.awt.Component; import game_engine.GameObject; @@ -12,9 +15,7 @@ * GameObjects have the same class. */ public class GameObjectCollection extends JPanel { - public ArrayList list; - int width; - int height; + private ArrayList list; /** * Constructor. @@ -26,8 +27,6 @@ public GameObjectCollection(int width, int height) { this.setOpaque(false); this.setLocation(0, 0); this.setSize(width, height); - this.width = width; - this.height = height; this.list = new ArrayList(); } @@ -49,12 +48,16 @@ public void addObj(T obj) { */ public boolean removeObj(T obj) { boolean isRemoved = false; + Component[] componentList = this.getComponents(); - if (this.list.remove(obj)) { - this.remove(obj); - isRemoved = true; + for (Component component: componentList) { + if (component == obj) { + this.list.remove(obj); + this.remove(component); - this.update(); + isRemoved = true; + this.update(); + } } return isRemoved; @@ -94,9 +97,9 @@ public int getCollectionSize() { } /** - * Remove all object from list. + * Remove all objects from list. */ - public void clearObj() { + public void clearCollection() { this.removeAll(); this.list.clear(); this.update(); @@ -110,11 +113,11 @@ private void update() { this.repaint(); } - public int getWidth() { - return this.width; - } - - public int getHeight() { - return this.height; + /** + * Get object collection. + * @return object collection. + */ + public ArrayList getCollection() { + return this.list; } } diff --git a/src/main/java/game_engine/GamePanel.java b/src/main/java/game_engine/GamePanel.java index 1ff75c9..b103eed 100644 --- a/src/main/java/game_engine/GamePanel.java +++ b/src/main/java/game_engine/GamePanel.java @@ -22,10 +22,15 @@ public GamePanel(int x, int y, Image image) { this.image = image; this.setSize(this.image.getWidth(null), this.image.getHeight(null)); + this.point = new Point(); this.setCoordinates(x, y); } + /** + * Constructor to create with coordinates (0, 0). + * @param image image. + */ public GamePanel(Image image) { this(0, 0, image); } @@ -46,28 +51,56 @@ public Image getImage() { return this.image; } + /** + * Set coordinates in floating-point format (double). + * JPanel.setLocation gets coordinates in integer format after casting. + * @param x x-coordinate of top-left corner. + * @param y y-coordinate of top-left corner. + */ public void setCoordinates(double x, double y) { this.setLocation((int) x, (int) y); this.point.setCoordinates(x, y); } + /** + * Set coordinates in integer format (int). + * JPanel.setLocation gets coordinates in integer format without casting. + * @param x x-coordinate of top-left corner. + * @param y y-coordinate of top-left corner. + */ public void setCoordinates(int x, int y) { this.setLocation(x, y); this.point.setCoordinates((double) x, (double) y); } + /** + * Set x-coordinate. + * @param x x-coordinate of top-left corner. + */ public void setCoordinateX(double x) { this.setCoordinates(x, this.point.getX()); } + /** + * Set y-coordinates. + * @param y y-coordinate of top-left corner. + */ public void setCoordinateY(double y) { this.setCoordinates(this.point.getY(), y); } + /** + * Get x-coordinate. + * @return x-coordinate. + */ public double getCoordinateX() { return this.point.getX(); } + /** + * Get y-coordinate. + * @return y-coordinate. + */ public double getCoordinateY() { return this.point.getY(); }