Skip to content
This repository was archived by the owner on Dec 24, 2025. It is now read-only.

Commit ae47152

Browse files
committed
fix(core): update gameplay components and system logic for power-up integration
1 parent c65bf77 commit ae47152

12 files changed

Lines changed: 300 additions & 302 deletions

File tree

src/main/java/com/github/codestorm/bounceverse/Utilities.java

Lines changed: 36 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@
99
import com.github.codestorm.bounceverse.typing.enums.EntityType;
1010

1111
import javafx.geometry.Rectangle2D;
12-
import javafx.scene.shape.Circle;
1312

1413
import java.io.*;
1514
import java.util.*;
1615

1716
/** Utilities. */
1817
public final class Utilities {
19-
private Utilities() {}
18+
private Utilities() {
19+
}
2020

2121
/** Input/Output utilities. */
2222
public static final class IO {
23-
private IO() {}
23+
// ... (Nội dung của lớp IO giữ nguyên, không cần thay đổi)
24+
private IO() {
25+
}
2426

25-
/**
26-
* Load .properties file.
27-
*
28-
* @param path Relative path
29-
* @return Parsed properties
30-
* @throws IOException if an error occurred when reading from the input stream.
31-
*/
3227
public static Properties loadProperties(String path) throws IOException {
3328
var fileStream = IO.class.getResourceAsStream(path);
3429
if (fileStream == null) {
@@ -41,24 +36,10 @@ public static Properties loadProperties(String path) throws IOException {
4136
return prop;
4237
}
4338

44-
/**
45-
* Convert an array of key=value pairs into a hashmap. The string "key=" maps key onto "",
46-
* while just "key" maps key onto null. The value may contain '=' characters, only the first
47-
* "=" is a delimiter. <br>
48-
* Source code from <a href="https://stackoverflow.com/a/52940215/16410937">here</a>.
49-
*
50-
* @param args command-line arguments in the key=value format (or just key= or key)
51-
* @param defaults a map of default values, may be null. Mappings to null are not copied to
52-
* the resulting map.
53-
* @param whiteList if not null, the keys not present in this map cause an exception (and
54-
* keys mapped to null are ok)
55-
* @return a map that maps these keys onto the corresponding values.
56-
*/
5739
public static HashMap<String, String> parseArgs(
5840
String[] args,
5941
HashMap<String, String> defaults,
6042
HashMap<String, String> whiteList) {
61-
// HashMap allows null values
6243
var res = new HashMap<String, String>();
6344
if (defaults != null) {
6445
for (var e : defaults.entrySet()) {
@@ -77,13 +58,6 @@ public static HashMap<String, String> parseArgs(
7758
return res;
7859
}
7960

80-
/**
81-
* Read text file (txt) and put all lines into {@link List}.
82-
*
83-
* @param path File path
84-
* @return All lines in text file
85-
* @throws FileNotFoundException if an I/O error occurs.
86-
*/
8761
public static List<String> readTextFile(String path) throws IOException {
8862
final var res = new ArrayList<String>();
8963
final var stream = IO.class.getResourceAsStream(path);
@@ -101,27 +75,14 @@ public static List<String> readTextFile(String path) throws IOException {
10175
}
10276

10377
public static final class Geometric {
104-
private Geometric() {}
105-
106-
/**
107-
* Lọc các Entity trong phạm vi Hình tròn.
108-
*
109-
* @param circle Hình tròn
110-
* @return Các entity
111-
*/
112-
public static List<Entity> getEntityInCircle(Circle circle) {
113-
final var cx = circle.getCenterX();
114-
final var cy = circle.getCenterY();
115-
final var radius = circle.getRadius();
116-
117-
return getEntityInCircle(cx, cy, radius);
78+
private Geometric() {
11879
}
11980

12081
/**
12182
* Lọc các Entity trong phạm vi Hình tròn.
12283
*
123-
* @param cx Tâm X
124-
* @param cy Tâm Y
84+
* @param cx Tâm X
85+
* @param cy Tâm Y
12586
* @param radius Bán kính
12687
* @return Các entity
12788
*/
@@ -130,53 +91,60 @@ public static List<Entity> getEntityInCircle(double cx, double cy, double radius
13091
return FXGL.getGameWorld().getEntitiesInRange(outRect).stream()
13192
.filter(
13293
e -> {
133-
var nearestX =
134-
Math.max(e.getX(), Math.min(cx, e.getX() + e.getWidth()));
135-
var nearestY =
136-
Math.max(e.getY(), Math.min(cy, e.getY() + e.getHeight()));
94+
var nearestX = Math.max(e.getX(), Math.min(cx, e.getX() + e.getWidth()));
95+
var nearestY = Math.max(e.getY(), Math.min(cy, e.getY() + e.getHeight()));
13796
var dx = cx - nearestX;
13897
var dy = cy - nearestY;
13998
return (dx * dx + dy * dy) <= radius * radius;
14099
})
141100
.toList();
142101
}
102+
103+
/**
104+
* Lọc các Entity trong phạm vi Hình chữ nhật.
105+
*
106+
* @param centerX Tâm X của hình chữ nhật
107+
* @param centerY Tâm Y của hình chữ nhật
108+
* @param width Chiều rộng của hình chữ nhật
109+
* @param height Chiều cao của hình chữ nhật
110+
* @return Danh sách các entity nằm trong khu vực đó
111+
*/
112+
public static List<Entity> getEntitiesInRectangle(double centerX, double centerY, double width, double height) {
113+
double topLeftX = centerX - width / 2;
114+
double topLeftY = centerY - height / 2;
115+
Rectangle2D explosionArea = new Rectangle2D(topLeftX, topLeftY, width, height);
116+
return FXGL.getGameWorld().getEntitiesInRange(explosionArea);
117+
}
143118
}
144119

120+
// ... (Các lớp Collision, Compatibility, Typing giữ nguyên, không cần thay đổi)
145121
public static final class Collision {
146-
private Collision() {}
122+
private Collision() {
123+
}
147124

148125
public static DirectionUnit getCollisionDirection(Entity source, Entity target) {
149126
var fromBox = source.getBoundingBoxComponent();
150127
var toBox = target.getBoundingBoxComponent();
151-
152128
var fCenter = fromBox.getCenterWorld();
153129
var tCenter = toBox.getCenterWorld();
154-
155130
var direction = tCenter.subtract(fCenter);
156-
157131
return Math.abs(direction.getX()) > Math.abs(direction.getY())
158132
? direction.getX() > 0 ? DirectionUnit.RIGHT : DirectionUnit.LEFT
159133
: direction.getY() > 0 ? DirectionUnit.DOWN : DirectionUnit.UP;
160134
}
161135
}
162136

163137
public static final class Compatibility {
164-
private Compatibility() {}
138+
private Compatibility() {
139+
}
165140

166-
/**
167-
* Throw {@link IllegalArgumentException} nếu như có component trong {@code params} không
168-
* phù hợp với {@code entityType}.
169-
*
170-
* @param entityType {@link EntityType} muốn kiểm tra tương thích
171-
* @param components Các component cần kiểm tra
172-
*/
173-
public static void throwIfNotCompatible(EntityType entityType, com.almasb.fxgl.entity.component.Component... components) {
141+
public static void throwIfNotCompatible(EntityType entityType,
142+
com.almasb.fxgl.entity.component.Component... components) {
174143
for (var param : components) {
175144
final var annotation = param.getClass().getAnnotation(OnlyForEntity.class);
176145
if (annotation == null) {
177146
continue;
178147
}
179-
180148
final var paramEntityTypeSet = EnumSet.copyOf(Arrays.asList(annotation.value()));
181149
if (!paramEntityTypeSet.contains(entityType)) {
182150
throw new IllegalArgumentException(
@@ -188,13 +156,6 @@ public static void throwIfNotCompatible(EntityType entityType, com.almasb.fxgl.e
188156
}
189157
}
190158

191-
/**
192-
* {@link #throwIfNotCompatible(EntityType, Component...)} nhưng không throw exception.
193-
*
194-
* @param entityType {@link EntityType} muốn kiểm tra tương thích
195-
* @param params Các component cần kiểm tra
196-
* @return {@code true} nếu tất cả tương thích, ngược lại {@code false}.
197-
*/
198159
public static boolean isCompatible(EntityType entityType, Component... params) {
199160
try {
200161
throwIfNotCompatible(entityType, params);
@@ -206,34 +167,19 @@ public static boolean isCompatible(EntityType entityType, Component... params) {
206167
}
207168

208169
public static final class Typing {
209-
private Typing() {}
170+
private Typing() {
171+
}
210172

211-
/**
212-
* Xử lý nhanh lấy data từ {@link SpawnData}.
213-
*
214-
* @param data Dữ liệu cần lấy
215-
* @param key Khóa cần lấy
216-
* @param ifNot Nếu không có thì trả về cái này
217-
* @return Giá trị
218-
* @param <T> Kiểu của Giá trị
219-
*/
220173
public static <T> T getOr(SpawnData data, String key, T ifNot) {
221174
if (data.hasKey(key)) {
222175
return data.get(key);
223176
}
224177
return ifNot;
225178
}
226179

227-
/**
228-
* Hợp nhanh varargs sang Array.
229-
*
230-
* @param varargs Varargs
231-
* @return Array tương ứng
232-
* @param <T> Kiểu của Giá trị
233-
*/
234180
@SafeVarargs
235181
public static <T> T[] toArray(T... varargs) {
236182
return varargs;
237183
}
238184
}
239-
}
185+
}

src/main/java/com/github/codestorm/bounceverse/components/behaviors/Attachment.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,18 @@ public void releaseBall() {
6868
physics.overwritePosition(entity.getPosition());
6969
physics.getBody().setAwake(true);
7070

71-
double ballCenterX = entity.getCenter().getX();
72-
double paddleCenterX = paddle.getCenter().getX();
73-
double dir = (ballCenterX >= paddleCenterX) ? 1 : -1;
74-
75-
double angle = Math.toRadians(45);
76-
double speed = 350;
77-
78-
double vx = speed * Math.sin(angle) * dir;
79-
double vy = -speed * Math.cos(angle);
71+
double speed = 350; // Giữ nguyên tốc độ
72+
73+
// Vận tốc ngang (vx) bằng 0 để bóng bay thẳng
74+
double vx = 0;
75+
76+
// Vận tốc dọc (vy) là một giá trị âm để bóng bay lên (vì trục Y hướng xuống)
77+
double vy = -speed;
8078

8179
physics.setLinearVelocity(new Point2D(vx, vy));
80+
81+
// ---------- KẾT THÚC SỬA ĐỔI ----------
82+
8283
move = false;
8384
}
8485

@@ -93,4 +94,4 @@ public static boolean isMove() {
9394
public static void setMove(boolean value) {
9495
move = value;
9596
}
96-
}
97+
}

src/main/java/com/github/codestorm/bounceverse/components/behaviors/Explosion.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,56 @@
1212
* Hành vi nổ của Brick – gây damage cho các đối tượng xung quanh.
1313
*/
1414
@Required(Attributes.class)
15-
@OnlyForEntity({EntityType.BRICK})
15+
@OnlyForEntity({ EntityType.BRICK })
1616
public final class Explosion extends Attack {
17-
public static final int DEFAULT_RADIUS = 1;
18-
private int radius = DEFAULT_RADIUS;
17+
18+
// Thay thế 'radius' bằng 'explosionWidth' và 'explosionHeight'
19+
private double explosionWidth;
20+
private double explosionHeight;
1921

2022
@Override
2123
public void execute(List<Object> data) {
2224
double cx = getEntity().getCenter().getX();
2325
double cy = getEntity().getCenter().getY();
24-
var nearEntities = Utilities.Geometric.getEntityInCircle(cx, cy, radius);
25-
super.execute(nearEntities.stream().map(e -> (Object) e).toList());
26+
27+
// Sử dụng hàm mới để lấy các entity trong hình chữ nhật
28+
var nearEntities = Utilities.Geometric.getEntitiesInRectangle(cx, cy, explosionWidth, explosionHeight);
29+
30+
// Lọc ra để vụ nổ không tự phá hủy chính nó (nếu logic game thay đổi)
31+
var filteredEntities = nearEntities.stream()
32+
.filter(e -> !e.equals(getEntity()))
33+
.map(e -> (Object) e)
34+
.toList();
35+
36+
super.execute(filteredEntities);
2637
}
2738

2839
@Override
2940
public void onRemoved() {
30-
execute(null);
41+
// execute(null) sẽ không hoạt động đúng nếu data là null, truyền vào một danh
42+
// sách trống
43+
execute(List.of());
44+
}
45+
46+
// Constructor mới nhận chiều rộng và cao
47+
public Explosion(double width, double height) {
48+
this.explosionWidth = width;
49+
this.explosionHeight = height;
50+
}
51+
52+
public double getExplosionWidth() {
53+
return explosionWidth;
3154
}
3255

33-
public int getRadius() {
34-
return radius;
56+
public void setExplosionWidth(double explosionWidth) {
57+
this.explosionWidth = explosionWidth;
3558
}
3659

37-
public void setRadius(int radius) {
38-
this.radius = Math.abs(radius);
60+
public double getExplosionHeight() {
61+
return explosionHeight;
3962
}
4063

41-
public Explosion() {}
42-
public Explosion(int radius) {
43-
setRadius(radius);
64+
public void setExplosionHeight(double explosionHeight) {
65+
this.explosionHeight = explosionHeight;
4466
}
45-
}
67+
}

src/main/java/com/github/codestorm/bounceverse/components/behaviors/FallingComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public final class FallingComponent extends Component {
99

10-
private static final double SPEED = 150;
10+
private static final double SPEED = 150;
1111

1212
@Override
1313
public void onUpdate(double tpf) {

0 commit comments

Comments
 (0)