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

Commit 2e4d181

Browse files
committed
feat(powerup): merge and restructure power-up system
- move ExpandPaddlePowerUp, ShrinkPaddlePowerUp into new folders - add PowerUpManager and FallingComponent - update PhysicSystem and PaddleFactory integration - refactor Explosion, HealthDeath, Special behaviors
1 parent f3239a4 commit 2e4d181

15 files changed

Lines changed: 287 additions & 108 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public final class Explosion extends Attack {
2828

2929
@Override
3030
public void execute(List<Object> data) {
31-
final var attributes = entity.getComponent(Attributes.class);
3231
final double cx = getEntity().getCenter().getX();
3332
final double cy = getEntity().getCenter().getY();
3433

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.github.codestorm.bounceverse.components.behaviors;
22

3+
import java.util.List;
4+
35
import com.almasb.fxgl.dsl.components.HealthIntComponent;
46
import com.almasb.fxgl.entity.component.Required;
57
import com.github.codestorm.bounceverse.core.BackgroundColorManager;
68
import com.github.codestorm.bounceverse.typing.enums.EntityType;
79

8-
import java.util.List;
9-
1010
/**
1111
*
1212
*

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class Special extends Component {
1313

1414
@Override
1515
public void onRemoved() {
16-
if (Math.random() < 0.3) { // 30% tỉ lệ rơi PowerUp
17-
FXGL.spawn("powerUp", getEntity().getCenter());
18-
}
16+
FXGL.spawn("powerUp", getEntity().getCenter());
1917
}
2018
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.codestorm.bounceverse.components.properties.paddle;
2+
3+
import com.almasb.fxgl.entity.component.Component;
4+
5+
import javafx.scene.Node;
6+
7+
/**
8+
* Quản lý kích thước gốc của Paddle và cho phép reset.
9+
*/
10+
public class PaddleSizeManager extends Component {
11+
12+
private double originalWidth;
13+
private double originalScaleX;
14+
15+
@Override
16+
public void onAdded() {
17+
Node view = getEntity().getViewComponent().getChildren().get(0);
18+
originalWidth = view.getBoundsInLocal().getWidth();
19+
originalScaleX = view.getScaleX();
20+
}
21+
22+
/** Phóng to paddle theo hệ số. */
23+
public void expand(double factor) {
24+
Node view = getEntity().getViewComponent().getChildren().get(0);
25+
view.setScaleX(factor);
26+
}
27+
28+
/** Thu nhỏ paddle theo hệ số. */
29+
public void shrink(double factor) {
30+
Node view = getEntity().getViewComponent().getChildren().get(0);
31+
view.setScaleX(factor);
32+
}
33+
34+
/** Reset paddle về kích thước gốc. */
35+
public void resetSize() {
36+
Node view = getEntity().getViewComponent().getChildren().get(0);
37+
view.setScaleX(originalScaleX);
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.codestorm.bounceverse.components.properties.powerup;
2+
3+
import com.almasb.fxgl.entity.component.Component;
4+
5+
/**
6+
* Giúp PowerUp rơi xuống mà không cần PhysicsComponent.
7+
* Dùng update thủ công để tránh lỗi "Physics not initialized yet".
8+
*/
9+
public class FallingComponent extends Component {
10+
11+
private static final double FALL_SPEED = 150; // pixel/s
12+
13+
@Override
14+
public void onUpdate(double tpf) {
15+
// Dịch chuyển entity xuống dưới theo thời gian
16+
getEntity().translateY(FALL_SPEED * tpf);
17+
}
18+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.codestorm.bounceverse.components.properties.powerup;
2+
3+
import com.almasb.fxgl.dsl.FXGL;
4+
import com.almasb.fxgl.time.TimerAction;
5+
import javafx.util.Duration;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* Quản lý các PowerUp đang hoạt động: thời gian, gia hạn, và hết hiệu lực.
12+
* Dùng singleton.
13+
*/
14+
public final class PowerUpManager {
15+
16+
/** Mỗi loại power-up gắn với 1 timer riêng. */
17+
private final Map<String, TimerAction> activeTimers = new HashMap<>();
18+
19+
private static final PowerUpManager INSTANCE = new PowerUpManager();
20+
21+
private PowerUpManager() {}
22+
23+
public static PowerUpManager getInstance() {
24+
return INSTANCE;
25+
}
26+
27+
/**
28+
* Kích hoạt hoặc gia hạn PowerUp.
29+
* @param name Tên power-up (ví dụ: "ExpandPaddle")
30+
* @param duration Thời gian hiệu lực
31+
* @param onActivate Logic khi kích hoạt
32+
* @param onExpire Logic khi hết hiệu lực
33+
*/
34+
public void activate(String name, Duration duration, Runnable onActivate, Runnable onExpire) {
35+
// Nếu đang chạy timer cũ → huỷ để gia hạn
36+
if (activeTimers.containsKey(name)) {
37+
var oldTimer = activeTimers.get(name);
38+
if (!oldTimer.isExpired()) {
39+
oldTimer.expire();
40+
}
41+
}
42+
43+
onActivate.run();
44+
45+
var timer = FXGL.getGameTimer().runOnceAfter(() -> {
46+
onExpire.run();
47+
activeTimers.remove(name);
48+
}, duration);
49+
50+
activeTimers.put(name, timer);
51+
}
52+
53+
public void clearAll() {
54+
activeTimers.values().forEach(TimerAction::expire);
55+
activeTimers.clear();
56+
}
57+
}

src/main/java/com/github/codestorm/bounceverse/components/properties/powerup/types/ExpandPaddlePowerUp.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/com/github/codestorm/bounceverse/components/properties/powerup/types/ShrinkPaddlePowerUp.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.codestorm.bounceverse.components.properties.powerup.types.ball;
2+
3+
import com.almasb.fxgl.dsl.FXGL;
4+
import com.almasb.fxgl.entity.Entity;
5+
import com.github.codestorm.bounceverse.components.properties.powerup.PowerUpManager;
6+
import com.github.codestorm.bounceverse.typing.enums.EntityType;
7+
import javafx.util.Duration;
8+
import javafx.geometry.Point2D;
9+
10+
/**
11+
* PowerUp nhân đôi bóng hiện có.
12+
* Nếu có nhiều bóng, mỗi bóng sẽ sinh thêm 1 bóng phụ cùng hướng.
13+
*/
14+
public class DuplicateBallPowerUp extends com.almasb.fxgl.entity.component.Component {
15+
16+
private static final Duration DURATION = Duration.seconds(0.1);
17+
18+
@Override
19+
public void onAdded() {
20+
PowerUpManager.getInstance().activate(
21+
"DuplicateBall",
22+
DURATION,
23+
this::duplicateBalls,
24+
() -> {}
25+
);
26+
}
27+
28+
private void duplicateBalls() {
29+
var world = FXGL.getGameWorld();
30+
var balls = world.getEntitiesByType(EntityType.BALL);
31+
32+
for (Entity ball : balls) {
33+
// Lấy vị trí và vận tốc hiện tại của bóng
34+
var physics = ball.getComponentOptional(com.almasb.fxgl.physics.PhysicsComponent.class);
35+
if (physics.isEmpty()) continue;
36+
37+
Point2D velocity = physics.get().getLinearVelocity();
38+
Point2D pos = ball.getCenter();
39+
40+
// Spawn bóng mới
41+
Entity newBall = FXGL.spawn("ball", pos);
42+
newBall.getComponent(com.almasb.fxgl.physics.PhysicsComponent.class)
43+
.setLinearVelocity(velocity.multiply(-1));
44+
}
45+
}
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.codestorm.bounceverse.components.properties.powerup.types.ball;
2+
3+
import com.almasb.fxgl.dsl.FXGL;
4+
import com.almasb.fxgl.entity.Entity;
5+
import com.github.codestorm.bounceverse.components.properties.powerup.PowerUpManager;
6+
import com.github.codestorm.bounceverse.typing.enums.EntityType;
7+
import javafx.util.Duration;
8+
9+
/**
10+
* PowerUp làm to bóng trong một thời gian ngắn.
11+
*/
12+
public class ExpandBallPowerUp extends com.almasb.fxgl.entity.component.Component {
13+
14+
private static final Duration DURATION = Duration.seconds(8);
15+
16+
@Override
17+
public void onAdded() {
18+
PowerUpManager.getInstance().activate(
19+
"ExpandBall",
20+
DURATION,
21+
this::expandBalls,
22+
this::resetBalls
23+
);
24+
}
25+
26+
private void expandBalls() {
27+
for (Entity ball : FXGL.getGameWorld().getEntitiesByType(EntityType.BALL)) {
28+
var view = ball.getViewComponent().getChildren().get(0);
29+
view.setScaleX(1.5);
30+
view.setScaleY(1.5);
31+
}
32+
}
33+
34+
private void resetBalls() {
35+
for (Entity ball : FXGL.getGameWorld().getEntitiesByType(EntityType.BALL)) {
36+
var view = ball.getViewComponent().getChildren().get(0);
37+
view.setScaleX(1);
38+
view.setScaleY(1);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)