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

Commit 235b80a

Browse files
committed
fix: update logic and UI behavior after merge (Attachment, BrickFactory, UISystem)
1 parent 224cec4 commit 235b80a

6 files changed

Lines changed: 329 additions & 206 deletions

File tree

settings.ADMIN.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ fullscreen = false
55
[audio]
66
music = 1.0
77
sound = 1.0
8+
[controls]
9+
moveLeft = "LEFT"
10+
moveRight = "RIGHT"
11+
launchBall = "SPACE"

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Attachment extends Component {
1818
private boolean attached = true;
1919
private PhysicsComponent physics;
2020

21-
private static boolean move = false;
21+
private boolean move = false;
2222
private double moveSpeed = 50;
2323
private double direction = 1;
2424
private double maxOffset = 50;
@@ -79,12 +79,4 @@ public void releaseBall() {
7979
public boolean isAttached() {
8080
return attached;
8181
}
82-
83-
public static boolean isMove() {
84-
return move;
85-
}
86-
87-
public static void setMove(boolean value) {
88-
move = value;
89-
}
9082
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
// C:\Users\Admin\Documents\bounceverse\src\main\java\com\github\codestorm\bounceverse\components\behaviors\HealthDeath.java
12
package com.github.codestorm.bounceverse.components.behaviors;
23

34
import java.util.List;
45

6+
import com.almasb.fxgl.dsl.FXGL;
57
import com.almasb.fxgl.dsl.components.HealthIntComponent;
68
import com.almasb.fxgl.entity.component.Required;
79
import com.github.codestorm.bounceverse.components.Behavior;
810
import com.github.codestorm.bounceverse.components.properties.brick.BrickTextureManager;
11+
import com.github.codestorm.bounceverse.systems.init.GameSystem;
912
import com.github.codestorm.bounceverse.systems.init.UISystem;
1013
import com.github.codestorm.bounceverse.typing.enums.EntityType;
14+
1115
import javafx.scene.paint.Color;
16+
import javafx.util.Duration;
1217

1318
@Required(HealthIntComponent.class)
1419
public class HealthDeath extends Behavior {
@@ -22,14 +27,43 @@ public void execute(List<Object> data) {
2227
Color brickColor = textureManager.getColor();
2328

2429
UISystem.getInstance().addColorToWave(brickColor);
30+
31+
int scoreToAdd = 0;
32+
switch (textureManager.brickType) {
33+
case NORMAL:
34+
scoreToAdd = 10;
35+
break;
36+
case SHIELD:
37+
scoreToAdd = 20;
38+
break;
39+
case STRONG:
40+
scoreToAdd = 30;
41+
break;
42+
case KEY:
43+
scoreToAdd = 10;
44+
break;
45+
case EXPLODING:
46+
scoreToAdd = 0;
47+
break;
48+
}
49+
50+
if (scoreToAdd > 0) {
51+
FXGL.inc("score", scoreToAdd);
52+
}
2553
});
2654
}
2755
entity.removeFromWorld();
56+
57+
FXGL.runOnce(() -> {
58+
if (FXGL.getGameWorld().getEntitiesByType(EntityType.BRICK).isEmpty()) {
59+
GameSystem.nextLevel();
60+
}
61+
}, Duration.seconds(0.1));
2862
}
2963
}
3064

3165
@Override
3266
public void onUpdate(double tpf) {
3367
execute(null);
3468
}
35-
}
69+
}
Lines changed: 90 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.codestorm.bounceverse.factory.entities;
22

3+
import java.util.List;
4+
import java.util.Random;
5+
36
import com.almasb.fxgl.dsl.EntityBuilder;
47
import com.almasb.fxgl.dsl.FXGL;
58
import com.almasb.fxgl.dsl.components.HealthIntComponent;
@@ -22,36 +25,86 @@
2225
import com.github.codestorm.bounceverse.typing.enums.BrickType;
2326
import com.github.codestorm.bounceverse.typing.enums.CollisionGroup;
2427
import com.github.codestorm.bounceverse.typing.enums.EntityType;
28+
2529
import javafx.geometry.Point2D;
2630
import javafx.geometry.Side;
2731
import javafx.scene.paint.Color;
2832

29-
import java.util.List;
30-
import java.util.Random;
31-
32-
/**
33-
* Factory sinh ra các loại gạch (Brick) trong trò chơi.
34-
*/
3533
public final class BrickFactory extends EntityFactory {
3634

3735
private static final int DEFAULT_WIDTH = 80;
3836
private static final int DEFAULT_HEIGHT = 30;
3937
private static final int DEFAULT_HP = 1;
40-
4138
private static final Random RANDOM = new Random();
4239
private static final List<String> COLORS = List.of("blue", "green", "orange", "pink", "red", "yellow");
4340

44-
// ... (Các phương thức getBuilder, newNormalBrick, newStrongBrick,
45-
// newShieldBrick giữ nguyên)
41+
private String getColorName(Color color) {
42+
if (color.equals(Color.BLUE)) {
43+
return "blue";
44+
}
45+
if (color.equals(Color.GREEN)) {
46+
return "green";
47+
}
48+
if (color.equals(Color.ORANGE)) {
49+
return "orange";
50+
}
51+
if (color.equals(Color.PINK)) {
52+
return "pink";
53+
}
54+
if (color.equals(Color.RED)) {
55+
return "red";
56+
}
57+
if (color.equals(Color.YELLOW)) {
58+
return "yellow";
59+
}
60+
return "blue";
61+
}
62+
63+
// Phương thức trợ giúp để lấy Color object từ SpawnData
64+
private Color getSpawnColor(SpawnData data) {
65+
Object colorValue = data.get("color");
66+
if (colorValue instanceof Color) {
67+
return (Color) colorValue;
68+
} else if (colorValue instanceof String) {
69+
return switch ((String) colorValue) {
70+
case "blue" ->
71+
Color.BLUE;
72+
case "green" ->
73+
Color.GREEN;
74+
case "orange" ->
75+
Color.ORANGE;
76+
case "pink" ->
77+
Color.PINK;
78+
case "red" ->
79+
Color.RED;
80+
case "yellow" ->
81+
Color.YELLOW;
82+
default ->
83+
Color.BLUE;
84+
};
85+
}
86+
return Color.BLUE; // Mặc định
87+
}
88+
4689
@Override
4790
protected EntityBuilder getBuilder(SpawnData data) {
4891
int hp = ((Number) Utilities.Typing.getOr(data, "hp", DEFAULT_HP)).intValue();
4992
int width = ((Number) Utilities.Typing.getOr(data, "width", DEFAULT_WIDTH)).intValue();
5093
int height = ((Number) Utilities.Typing.getOr(data, "height", DEFAULT_HEIGHT)).intValue();
5194

52-
Point2D pos = new Point2D(data.getX(), data.getY());
95+
Point2D pos = data.hasKey("pos") ? data.get("pos") : new Point2D(data.getX(), data.getY());
96+
97+
// CHỈ LẤY STRING KEY: Đảm bảo chỉ làm việc với String ở đây
98+
String colorKey;
99+
Object colorValue = data.get("color");
53100

54-
String colorKey = Utilities.Typing.getOr(data, "color", COLORS.get(RANDOM.nextInt(COLORS.size())));
101+
if (colorValue instanceof Color) {
102+
colorKey = getColorName((Color) colorValue); // Chuyển Color object thành String key
103+
} else if (colorValue instanceof String) {
104+
colorKey = (String) colorValue;
105+
} else {
106+
colorKey = COLORS.get(RANDOM.nextInt(COLORS.size()));
107+
}
55108

56109
var colorAsset = AssetsPath.Textures.Bricks.COLORS.get(colorKey);
57110
if (colorAsset == null) {
@@ -61,10 +114,7 @@ protected EntityBuilder getBuilder(SpawnData data) {
61114
}
62115

63116
BrickType type = Utilities.Typing.getOr(data, "type", BrickType.NORMAL);
64-
65-
double hpPercent = 1.0;
66-
String texturePath = colorAsset.getTexture(type, hpPercent);
67-
117+
String texturePath = colorAsset.getTexture(type, 1.0);
68118
var texture = FXGL.texture(texturePath);
69119
texture.setFitWidth(width);
70120
texture.setFitHeight(height);
@@ -73,58 +123,40 @@ protected EntityBuilder getBuilder(SpawnData data) {
73123
var fixture = new FixtureDef();
74124
fixture.setFriction(0f);
75125
fixture.setRestitution(1f);
76-
77-
// Lọc va chạm: Gạch thuộc nhóm BRICK và chỉ va chạm với BALL, BULLET
78126
fixture.getFilter().categoryBits = CollisionGroup.BRICK.bits;
79127
fixture.getFilter().maskBits = CollisionGroup.BALL.bits | CollisionGroup.BULLET.bits;
80-
81128
physics.setFixtureDef(fixture);
82129
physics.setBodyType(BodyType.STATIC);
83130

84-
var color = switch (colorKey) {
85-
case "green" -> Color.GREEN;
86-
case "orange" -> Color.ORANGE;
87-
case "pink" -> Color.PINK;
88-
case "red" -> Color.RED;
89-
case "yellow" -> Color.YELLOW;
90-
default -> Color.BLUE;
91-
};
92-
93131
return FXGL.entityBuilder(data)
94132
.type(EntityType.BRICK)
95133
.bbox(BoundingShape.box(width, height))
96134
.viewWithBBox(texture)
97135
.at(pos)
98136
.collidable()
99-
// SỬA ĐỔI DÒNG NÀY: Thêm BrickTextureManager vào
100-
.with(physics, new Attributes(), new HealthIntComponent(hp), new HealthDeath(),
101-
new BrickTextureManager(type, color));
137+
.with(physics, new Attributes(), new HealthIntComponent(hp), new HealthDeath());
102138
}
103139

104140
@Spawns("normalBrick")
105141
public Entity newNormalBrick(SpawnData data) {
106142
data.put("type", BrickType.NORMAL);
107143
data.put("hp", (double) DEFAULT_HP);
108-
return getBuilder(data).buildAndAttach();
144+
var color = getSpawnColor(data);
145+
data.put("color", getColorName(color));
146+
return getBuilder(data)
147+
.with(new BrickTextureManager(BrickType.NORMAL, color))
148+
.buildAndAttach();
109149
}
110150

111151
@Spawns("strongBrick")
112152
public Entity newStrongBrick(SpawnData data) {
113153
data.put("type", BrickType.STRONG);
114154
data.put("hp", DEFAULT_HP + 2);
115-
116-
String colorKey = Utilities.Typing.getOr(data, "color", COLORS.get(RANDOM.nextInt(COLORS.size())));
117-
var color = switch (colorKey) {
118-
case "green" -> Color.GREEN;
119-
case "orange" -> Color.ORANGE;
120-
case "pink" -> Color.PINK;
121-
case "red" -> Color.RED;
122-
case "yellow" -> Color.YELLOW;
123-
default -> Color.BLUE;
124-
};
125-
155+
var color = getSpawnColor(data);
156+
data.put("color", getColorName(color));
126157
return getBuilder(data)
127158
.with(new StrongBrickTextureUpdater().withColor(color))
159+
.with(new BrickTextureManager(BrickType.STRONG, color))
128160
.buildAndAttach();
129161
}
130162

@@ -133,29 +165,39 @@ public Entity newShieldBrick(SpawnData data) {
133165
data.put("type", BrickType.SHIELD);
134166
data.put("hp", (double) (DEFAULT_HP));
135167
var shield = new Shield(Side.LEFT, Side.RIGHT, Side.BOTTOM);
136-
return getBuilder(data).with(shield).buildAndAttach();
168+
var color = getSpawnColor(data);
169+
data.put("color", getColorName(color));
170+
return getBuilder(data)
171+
.with(shield)
172+
.with(new BrickTextureManager(BrickType.SHIELD, color))
173+
.buildAndAttach();
137174
}
138175

139-
/** Gạch nổ — khi bị phá sẽ kích hoạt Explosion gây sát thương lan */
140176
@Spawns("explodingBrick")
141177
public Entity newExplodingBrick(SpawnData data) {
142178
data.put("type", BrickType.EXPLODING);
143179
data.put("hp", (double) DEFAULT_HP);
144180
double explosionWidth = DEFAULT_WIDTH * 1.5;
145181
double explosionHeight = DEFAULT_HEIGHT * 2.5;
146-
147182
var explosion = new Explosion(explosionWidth, explosionHeight);
148-
149-
return getBuilder(data).with(explosion).buildAndAttach();
183+
var color = getSpawnColor(data);
184+
data.put("color", getColorName(color));
185+
return getBuilder(data)
186+
.with(explosion)
187+
.with(new BrickTextureManager(BrickType.EXPLODING, color))
188+
.buildAndAttach();
150189
}
151190

152191
@Spawns("keyBrick")
153192
public Entity newKeyBrick(SpawnData data) {
154193
data.put("type", BrickType.KEY);
155194
data.put("hp", (double) DEFAULT_HP);
156195
var special = new Special();
196+
var color = getSpawnColor(data);
197+
data.put("color", getColorName(color));
157198
return getBuilder(data)
158199
.with(special)
200+
.with(new BrickTextureManager(BrickType.KEY, color))
159201
.buildAndAttach();
160202
}
161-
}
203+
}

0 commit comments

Comments
 (0)