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

Commit 2218e3e

Browse files
committed
feat: add Special.java and update BrickFactory for strong & shield bricks
1 parent 157661f commit 2218e3e

3 files changed

Lines changed: 63 additions & 59 deletions

File tree

mailmap.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import com.almasb.fxgl.dsl.FXGL;
44
import com.almasb.fxgl.entity.component.Component;
5+
import com.github.codestorm.bounceverse.typing.annotations.ForEntity;
6+
import com.github.codestorm.bounceverse.typing.enums.EntityType;
57

6-
public class Special extends Component{
8+
/**
9+
* Khi brick bị phá thì spawn PowerUp.
10+
*/
11+
@ForEntity({ EntityType.BRICK })
12+
public class Special extends Component {
713

814
@Override
915
public void onRemoved() {
10-
// Khi bị phá → spawn PowerUp ngẫu nhiên
1116
FXGL.spawn("powerUp", getEntity().getCenter());
1217
}
1318
}
Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.github.codestorm.bounceverse.factory.entities;
22

3+
import java.util.Arrays;
34
import java.util.List;
45
import java.util.Random;
56

7+
import javax.management.Attribute;
8+
69
import org.jetbrains.annotations.NotNull;
710

811
import com.almasb.fxgl.dsl.FXGL;
@@ -19,13 +22,16 @@
1922
import com.github.codestorm.bounceverse.components.behaviors.Explosion;
2023
import com.github.codestorm.bounceverse.components.behaviors.HealthDeath;
2124
import com.github.codestorm.bounceverse.components.behaviors.Special;
25+
import com.github.codestorm.bounceverse.components.properties.Attributes;
2226
import com.github.codestorm.bounceverse.components.properties.Shield;
2327
import com.github.codestorm.bounceverse.typing.enums.EntityType;
2428

2529
import javafx.geometry.Point2D;
2630
import javafx.geometry.Side;
2731
import javafx.scene.Node;
2832
import javafx.scene.image.ImageView;
33+
import javafx.scene.paint.Color;
34+
import javafx.scene.shape.Rectangle;
2935

3036
/**
3137
*
@@ -43,23 +49,47 @@ public final class BrickFactory implements EntityFactory {
4349
private static final int DEFAULT_HEIGHT = 30;
4450
private static final int DEFAULT_HP = 1;
4551

46-
private static final List<String> BRICK_TEXTURES = List.of(
52+
private static final Random RANDOM = new Random();
53+
54+
private static final List<String> NORMAL_TEXTURES = List.of(
4755
"bricks/normalBrick/06_test11.png",
4856
"bricks/normalBrick/07_test11.png",
4957
"bricks/normalBrick/08_test11.png",
5058
"bricks/normalBrick/09_test11.png",
5159
"bricks/normalBrick/10_test11.png",
52-
"bricks/normalBrick/11_test11.png"
53-
);
60+
"bricks/normalBrick/11_test11.png");
5461

55-
private static final Random RANDOM = new Random();
62+
private static final List<String> STRONG_TEXTURES = List.of(
63+
"bricks/strongBrick/12_test11.png");
64+
65+
private static final List<String> SHIELD_TEXTURES = List.of(
66+
"bricks/shieldBrick/00_test11.png");
67+
68+
private static final List<String> EXPLODING_TEXTURES = List.of(
69+
"bricks/shieldBrick/00_test11.png");
70+
71+
private static final List<String> SPECIAL_TEXTURES = List.of(
72+
"bricks/specialBrick/special_01.png");
73+
74+
private static Node makeView(String texturePath) {
75+
ImageView view = new ImageView(FXGL.image(texturePath));
76+
view.setFitWidth(DEFAULT_WIDTH);
77+
view.setFitHeight(DEFAULT_HEIGHT);
78+
view.setSmooth(false);
79+
view.setPreserveRatio(false);
80+
return view;
81+
}
82+
83+
private static Node makeColorView(Color color) {
84+
return new Rectangle(DEFAULT_WIDTH, DEFAULT_HEIGHT, color);
85+
}
5686

5787
/**
5888
* Tạo mới một entity brick.
5989
*
60-
* @param pos Vị trí
61-
* @param hp HP
62-
* @param view Khung nhìn
90+
* @param pos Vị trí
91+
* @param hp HP
92+
* @param view Khung nhìn
6393
* @param components Các components thêm vào
6494
* @return Entity Brick mới tạo
6595
*/
@@ -71,41 +101,20 @@ private static Entity newBrick(Point2D pos, int hp, Node view, Component... comp
71101
var fixture = new FixtureDef();
72102
fixture.setFriction(0f);
73103
fixture.setRestitution(1f);
74-
75104
physics.setFixtureDef(fixture);
76105
physics.setBodyType(BodyType.STATIC);
77106

78-
return FXGL.entityBuilder()
107+
var builder = FXGL.entityBuilder()
79108
.type(EntityType.BRICK)
80109
.at(pos)
81110
.viewWithBBox(view)
82111
.collidable()
83-
.with(physics, new HealthIntComponent(hp), new HealthDeath())
84-
.with(components)
85-
.build();
86-
}
112+
.with(physics, new Attributes(), new HealthIntComponent(hp), new HealthDeath());
87113

88-
/**
89-
* Tạo mới một entity brick với khung nhìn mặc định.
90-
*
91-
* @param pos Vị trí
92-
* @param hp HP
93-
* @param components Các components thêm vào
94-
* @return Entity Brick mới tạo
95-
*/
96-
@NotNull
97-
private static Entity newBrick(Point2D pos, int hp, Component... components) {
98-
String randomTexture = BRICK_TEXTURES.get(RANDOM.nextInt(BRICK_TEXTURES.size()));
114+
if (components != null && components.length > 0)
115+
builder.with(components);
99116

100-
ImageView view = new ImageView(FXGL.image(randomTexture));
101-
view.setFitWidth(DEFAULT_WIDTH);
102-
view.setFitHeight(DEFAULT_HEIGHT);
103-
view.setSmooth(false);
104-
view.setPreserveRatio(false);
105-
view.setOpacity(1.0);
106-
view.setBlendMode(null);
107-
108-
return newBrick(pos, hp, view, components);
117+
return builder.build();
109118
}
110119

111120
/**
@@ -114,11 +123,10 @@ private static Entity newBrick(Point2D pos, int hp, Component... components) {
114123
* @param pos Vị trí
115124
* @return Entity Brick mới tạo
116125
*/
117-
@NotNull
118126
@Spawns("normalBrick")
119-
public static Entity newNormalBrick(SpawnData pos) {
120-
return newBrick(
121-
new Point2D(pos.getX(), pos.getY()), DEFAULT_HP);
127+
public static Entity newNormalBrick(SpawnData data) {
128+
String tex = NORMAL_TEXTURES.get(RANDOM.nextInt(NORMAL_TEXTURES.size()));
129+
return newBrick(new Point2D(data.getX(), data.getY()), DEFAULT_HP, makeView(tex));
122130
}
123131

124132
/**
@@ -129,9 +137,8 @@ public static Entity newNormalBrick(SpawnData pos) {
129137
*/
130138
@Spawns("strongBrick")
131139
public static Entity newStrongBrick(SpawnData data) {
132-
return newBrick(
133-
new Point2D(data.getX(), data.getY()),
134-
DEFAULT_HP + 2);
140+
String tex = STRONG_TEXTURES.get(RANDOM.nextInt(STRONG_TEXTURES.size()));
141+
return newBrick(new Point2D(data.getX(), data.getY()), DEFAULT_HP + 2, makeView(tex));
135142
}
136143

137144
/**
@@ -142,14 +149,12 @@ public static Entity newStrongBrick(SpawnData data) {
142149
*/
143150
@NotNull
144151
@Spawns("shieldBrick")
145-
public static Entity newShieldBrick(SpawnData pos) {
146-
152+
public static Entity newShieldBrick(SpawnData data) {
153+
String tex = SHIELD_TEXTURES.get(RANDOM.nextInt(SHIELD_TEXTURES.size()));
147154
var shield = new Shield();
148155
shield.addSide(Side.LEFT, Side.RIGHT, Side.BOTTOM);
149-
150-
var brick = newBrick(new Point2D(pos.getX(), pos.getY()), DEFAULT_HP + 1);
156+
var brick = newBrick(new Point2D(data.getX(), data.getY()), DEFAULT_HP + 1, makeView(tex));
151157
brick.addComponent(shield);
152-
153158
return brick;
154159
}
155160

@@ -160,21 +165,16 @@ public static Entity newShieldBrick(SpawnData pos) {
160165
*/
161166
@Spawns("explodingBrick")
162167
public static Entity newExplodingBrick(SpawnData data) {
168+
String tex = EXPLODING_TEXTURES.get(RANDOM.nextInt(EXPLODING_TEXTURES.size()));
163169
var explosion = new Explosion(120);
164-
return newBrick(
165-
new Point2D(data.getX(), data.getY()),
166-
DEFAULT_HP,
167-
explosion
168-
);
170+
return newBrick(new Point2D(data.getX(), data.getY()), DEFAULT_HP, makeView(tex), explosion);
169171
}
170172

171173
@Spawns("specialBrick")
172174
public static Entity newSpecialBrick(SpawnData data) {
173-
174-
return newBrick(
175-
new Point2D(data.getX(), data.getY()),
176-
DEFAULT_HP,
177-
new Special()
178-
);
175+
String tex = SPECIAL_TEXTURES.get(RANDOM.nextInt(SPECIAL_TEXTURES.size()));
176+
return newBrick(new Point2D(data.getX(), data.getY()), DEFAULT_HP, makeView(tex), new Special());
179177
}
178+
179+
180180
}

0 commit comments

Comments
 (0)