99import com .github .codestorm .bounceverse .typing .enums .EntityType ;
1010
1111import javafx .geometry .Rectangle2D ;
12- import javafx .scene .shape .Circle ;
1312
1413import java .io .*;
1514import java .util .*;
1615
1716/** Utilities. */
1817public 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+ }
0 commit comments