11package com .github .codestorm .bounceverse .factory .entities ;
22
3+ import java .util .List ;
4+ import java .util .Random ;
5+
36import com .almasb .fxgl .dsl .EntityBuilder ;
47import com .almasb .fxgl .dsl .FXGL ;
58import com .almasb .fxgl .dsl .components .HealthIntComponent ;
2225import com .github .codestorm .bounceverse .typing .enums .BrickType ;
2326import com .github .codestorm .bounceverse .typing .enums .CollisionGroup ;
2427import com .github .codestorm .bounceverse .typing .enums .EntityType ;
28+
2529import javafx .geometry .Point2D ;
2630import javafx .geometry .Side ;
2731import 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- */
3533public 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