Skip to content

Commit

Permalink
Change PlatformCollection class and fix GameObjectCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
alex8399 committed Oct 19, 2023
1 parent 1a5cee8 commit ab2551b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
14 changes: 1 addition & 13 deletions src/main/java/doodle_jump/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/doodle_jump/PlatformCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
37 changes: 20 additions & 17 deletions src/main/java/game_engine/GameObjectCollection.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,9 +15,7 @@
* GameObjects have the same class.
*/
public class GameObjectCollection<T extends GameObject> extends JPanel {
public ArrayList<T> list;
int width;
int height;
private ArrayList<T> list;

/**
* Constructor.
Expand All @@ -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<T>();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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<T> getCollection() {
return this.list;
}
}
33 changes: 33 additions & 0 deletions src/main/java/game_engine/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double>();
this.setCoordinates(x, y);
}

/**
* Constructor to create with coordinates (0, 0).
* @param image image.
*/
public GamePanel(Image image) {
this(0, 0, image);
}
Expand All @@ -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();
}
Expand Down

0 comments on commit ab2551b

Please sign in to comment.