Skip to content

Commit d2d0ed2

Browse files
authored
Merge pull request #314 from AlmasB/0.3.2
0.3.2
2 parents 035aebc + a7f7774 commit d2d0ed2

File tree

275 files changed

+7683
-4314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

275 files changed

+7683
-4314
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ JavaFX Game Development Framework
1010

1111
## Why?
1212

13-
* No native libraries, 0 setup required
13+
* No native libraries, no setup required
1414
* Simple and clean API
1515
* Brings real-world game development techniques to JavaFX
1616

@@ -61,7 +61,7 @@ public class BasicGameApp extends GameApplication {
6161
<dependency>
6262
<groupId>com.github.almasb</groupId>
6363
<artifactId>fxgl</artifactId>
64-
<version>0.3.1</version>
64+
<version>0.3.2</version>
6565
</dependency>
6666
```
6767

@@ -73,7 +73,7 @@ repositories {
7373
}
7474
7575
dependencies {
76-
compile 'com.github.almasb:fxgl:0.3.1'
76+
compile 'com.github.almasb:fxgl:0.3.2'
7777
}
7878
```
7979

fxgl-ai/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<parent>
3333
<artifactId>fxgl-framework</artifactId>
3434
<groupId>com.github.almasb</groupId>
35-
<version>0.3.1</version>
35+
<version>0.3.2</version>
3636
</parent>
3737

3838
<artifactId>fxgl-ai</artifactId>

fxgl-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<parent>
3333
<artifactId>fxgl-framework</artifactId>
3434
<groupId>com.github.almasb</groupId>
35-
<version>0.3.1</version>
35+
<version>0.3.2</version>
3636
</parent>
3737

3838
<artifactId>fxgl-core</artifactId>

fxgl-core/src/main/java/com/almasb/fxgl/core/math/FXGLMath.java

+48-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030

3131
package com.almasb.fxgl.core.math;
3232

33+
import javafx.geometry.Point2D;
34+
import javafx.scene.paint.Color;
35+
3336
import java.util.Random;
3437

3538
/**
3639
* Utility and fast math functions.
3740
* Thanks to Riven on JavaGaming.org for the basis of sin/cos/floor/ceil.
3841
*
3942
* @author Nathan Sweet
40-
**/
43+
* @author Almas Baimagambetov (AlmasB) ([email protected])
44+
*/
4145
public final class FXGLMath {
4246

4347
private FXGLMath() {}
@@ -292,6 +296,31 @@ public static float randomTriangular(float min, float max, float mode) {
292296
return max - (float) Math.sqrt((1 - u) * d * (max - mode));
293297
}
294298

299+
/**
300+
* @return new random vector of unit length as Vec2
301+
*/
302+
public static Vec2 randomVec2() {
303+
return new Vec2(random(-1f, 1f), random(-1f, 1f)).normalizeLocal();
304+
}
305+
306+
/**
307+
* @return new random vector of unit length as Point2D
308+
*/
309+
public static Point2D randomPoint2D() {
310+
double x = random(-1f, 1f);
311+
double y = random(-1f, 1f);
312+
313+
double length = Math.sqrt(x * x + y * y);
314+
if (length < EPSILON)
315+
return Point2D.ZERO;
316+
317+
return new Point2D(x / length, y / length);
318+
}
319+
320+
public static Color randomColor() {
321+
return Color.color(random(), random(), random());
322+
}
323+
295324
/* RANDOM END */
296325

297326
public static float sqrt(float x) {
@@ -353,6 +382,16 @@ public static double clamp(double value, double min, double max) {
353382
return value;
354383
}
355384

385+
/**
386+
* Map value of a given range to a target range.
387+
*
388+
* @param value the value to map
389+
* @return mapped value
390+
*/
391+
public static double map(double value, double currentRangeStart, double currentRangeStop, double targetRangeStart, double targetRangeStop) {
392+
return targetRangeStart + (targetRangeStop - targetRangeStart) * ((value - currentRangeStart) / (currentRangeStop - currentRangeStart));
393+
}
394+
356395
/**
357396
* @param fromValue start value
358397
* @param toValue end value
@@ -514,4 +553,12 @@ public static float log2(float value) {
514553
public static BezierSpline closedBezierSpline(Vec2[] points) {
515554
return ClosedBezierSplineFactory.newBezierSpline(points);
516555
}
556+
557+
/**
558+
* @param t current time * frequency (lower frequency -> smoother output)
559+
* @return perlin noise in 1D quality in [0..1)
560+
*/
561+
public static float noise1D(double t) {
562+
return PerlinNoiseGenerator.INSTANCE.noise1D((float) t) + 0.5f;
563+
}
517564
}

fxgl-core/src/main/java/com/almasb/fxgl/core/math/Vec2.java

+13
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,19 @@ public boolean distanceGreaterThanOrEqual(double otherX, double otherY, double d
329329
return distanceSquared(otherX, otherY) >= distance * distance;
330330
}
331331

332+
/**
333+
* @return new normalized vector
334+
*/
335+
public Vec2 normalizeVec() {
336+
float length = length();
337+
if (length < FXGLMath.EPSILON) {
338+
return new Vec2();
339+
}
340+
341+
float invLength = 1.0f / length;
342+
return new Vec2(x * invLength, y * invLength);
343+
}
344+
332345
/**
333346
* Normalize this vector and return the length before normalization.
334347
* Alters this vector.

fxgl-core/src/main/java/com/almasb/fxgl/core/reflect/ReflectionUtils.java

+56-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626

2727
package com.almasb.fxgl.core.reflect;
2828

29+
import com.almasb.fxgl.core.collection.Array;
30+
31+
import java.lang.reflect.*;
32+
import java.lang.reflect.Field;
2933
import java.lang.reflect.Method;
30-
import java.lang.reflect.Proxy;
3134
import java.util.HashMap;
3235
import java.util.Map;
3336
import java.util.function.Function;
@@ -99,4 +102,56 @@ public static <T> T call(Object instance, Method method, Object... args) {
99102
public static <T, R> Function<T, R> mapToFunction(Object instance, Method method) {
100103
return input -> call(instance, method, input);
101104
}
105+
106+
public static <A extends java.lang.annotation.Annotation> Array<Field>
107+
findFieldsByAnnotation(Object instance, Class<A> annotationClass) {
108+
109+
Array<Field> fields = new Array<>();
110+
111+
for (java.lang.reflect.Field field : instance.getClass().getDeclaredFields()) {
112+
if (field.getDeclaredAnnotation(annotationClass) != null) {
113+
fields.add(field);
114+
}
115+
}
116+
117+
return fields;
118+
}
119+
120+
/**
121+
* Find all fields of instance that have type / subtype of given type parameter.
122+
*
123+
* @param instance object whose fields to search
124+
* @param type super type
125+
* @return all fields that meet criteria
126+
*/
127+
public static Array<Field> findFieldsByType(Object instance, Class<?> type) {
128+
129+
Array<Field> fields = new Array<>();
130+
131+
for (java.lang.reflect.Field field : instance.getClass().getDeclaredFields()) {
132+
if (type.isAssignableFrom(field.getType())) {
133+
fields.add(field);
134+
}
135+
}
136+
137+
return fields;
138+
}
139+
140+
/**
141+
* Injects field of an instance to injectionInstance.
142+
*
143+
* @param field the field object
144+
* @param instance field's object
145+
* @param injectionInstance the target value to inject
146+
*/
147+
public static void inject(Field field, Object instance, Object injectionInstance) {
148+
try {
149+
if (!field.isAccessible()) {
150+
field.setAccessible(true);
151+
}
152+
field.set(instance, injectionInstance);
153+
} catch (Exception e) {
154+
throw new RuntimeException("Cannot inject " + injectionInstance + " into " + field.getName() + " Error: " + e);
155+
}
156+
}
102157
}

fxgl-core/src/main/kotlin/com/almasb/fxgl/core/concurrent/FXCoroutine.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ class FXCoroutine<T>(private val func: Callable<T>) : Async<T>() {
4242
private var value: T? = null
4343

4444
init {
45-
Platform.runLater {
45+
if (Platform.isFxApplicationThread()) {
4646
try {
4747
value = func.call()
4848
} finally {
4949
latch.countDown()
5050
}
51+
} else {
52+
Platform.runLater {
53+
try {
54+
value = func.call()
55+
} finally {
56+
latch.countDown()
57+
}
58+
}
5159
}
5260
}
5361

fxgl-core/src/main/kotlin/com/almasb/fxgl/core/logging/FXGLLogger.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private constructor() {
5252
@JvmStatic fun configure(fileName: String) {
5353
cleanOldLogs()
5454
Configurator.initialize("FXGL", fileName)
55-
logSystemInfo()
55+
//logSystemInfo()
5656
}
5757

5858
@JvmStatic fun get(name: String): Logger {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* FXGL - JavaFX Game Library
5+
*
6+
* Copyright (c) 2015-2017 AlmasB ([email protected])
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
package com.almasb.fxgl.core.math
28+
29+
/**
30+
* Adapted from https://github.com/CRYTEK/CRYENGINE/blob/release/Code/CryEngine/CryCommon/CryMath/PNoise3.h
31+
*
32+
* @author Almas Baimagambetov ([email protected])
33+
*/
34+
internal object PerlinNoiseGenerator {
35+
36+
// from CRYtek
37+
private val NOISE_TABLE_SIZE = 256
38+
private val NOISE_MASK = 255
39+
40+
private val gx = FloatArray(NOISE_TABLE_SIZE)
41+
private val gy = FloatArray(NOISE_TABLE_SIZE)
42+
43+
init {
44+
setSeedAndReinitialize()
45+
}
46+
47+
private fun setSeedAndReinitialize() {
48+
// Generate the gradient lookup tables
49+
for (i in 0..NOISE_TABLE_SIZE - 1) {
50+
// Ken Perlin proposes that the gradients are taken from the unit
51+
// circle/sphere for 2D/3D.
52+
// So lets generate a good pseudo-random vector and normalize it
53+
54+
val v = Vec2()
55+
// random is in the 0..1 range, so we bring to -0.5..0.5
56+
v.x = FXGLMath.random() - 0.5f
57+
v.y = FXGLMath.random() - 0.5f
58+
v.normalizeLocal()
59+
60+
gx[i] = v.x
61+
gy[i] = v.y
62+
}
63+
}
64+
65+
/**
66+
* Generates a value in [-0.5..0.5), t > 0.
67+
*/
68+
fun noise1D(t: Float): Float {
69+
// Compute what gradients to use
70+
var qx0 = Math.floor(t.toDouble()).toInt()
71+
var qx1 = qx0 + 1
72+
val tx0 = t - qx0
73+
val tx1 = tx0 - 1
74+
75+
// Make sure we don't come outside the lookup table
76+
qx0 = qx0 and NOISE_MASK
77+
qx1 = qx1 and NOISE_MASK
78+
79+
// Compute the dotproduct between the vectors and the gradients
80+
val v0 = gx[qx0] * tx0
81+
val v1 = gx[qx1] * tx1
82+
83+
// Modulate with the weight function
84+
val wx = (3 - 2 * tx0) * tx0 * tx0
85+
return v0 - wx * (v0 - v1)
86+
}
87+
}

fxgl-ecs/pom.xml

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<parent>
3333
<artifactId>fxgl-framework</artifactId>
3434
<groupId>com.github.almasb</groupId>
35-
<version>0.3.1</version>
35+
<version>0.3.2</version>
3636
</parent>
3737

3838
<artifactId>fxgl-ecs</artifactId>
@@ -41,6 +41,12 @@
4141
<description>Simple entity component system (control) framework</description>
4242

4343
<dependencies>
44+
<dependency>
45+
<groupId>org.jetbrains.kotlin</groupId>
46+
<artifactId>kotlin-stdlib</artifactId>
47+
<version>${kotlin.version}</version>
48+
</dependency>
49+
4450
<dependency>
4551
<groupId>com.github.almasb</groupId>
4652
<artifactId>fxgl-io</artifactId>
@@ -56,6 +62,17 @@
5662

5763
<build>
5864
<plugins>
65+
<!-- Compile java -->
66+
<plugin>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
</plugin>
69+
70+
<!-- Compile kotlin -->
71+
<plugin>
72+
<groupId>org.jetbrains.kotlin</groupId>
73+
<artifactId>kotlin-maven-plugin</artifactId>
74+
</plugin>
75+
5976
<!-- Create sources.jar -->
6077
<plugin>
6178
<groupId>org.apache.maven.plugins</groupId>

0 commit comments

Comments
 (0)