+135
-0
lines changedFilter options
+135
-0
lines changed Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
1
+
/*
2
+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3
+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4
+
* See LICENSE for details.
5
+
*/
6
+
7
+
package sandbox;
8
+
9
+
import com.almasb.fxgl.app.GameApplication;
10
+
import com.almasb.fxgl.app.GameSettings;
11
+
import javafx.scene.input.KeyCode;
12
+
import javafx.util.Duration;
13
+
14
+
import java.util.Map;
15
+
16
+
import static com.almasb.fxgl.dsl.FXGL.*;
17
+
18
+
/**
19
+
* @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
20
+
*/
21
+
public class VarChangeSample extends GameApplication {
22
+
23
+
@Override
24
+
protected void initSettings(GameSettings settings) { }
25
+
26
+
@Override
27
+
protected void initInput() {
28
+
onKeyDown(KeyCode.F, "update", () -> {
29
+
inc("time", +1.0);
30
+
31
+
var name = gets("name");
32
+
set("name", name + "H");
33
+
});
34
+
}
35
+
36
+
@Override
37
+
protected void initGameVars(Map<String, Object> vars) {
38
+
vars.put("hp", 0);
39
+
vars.put("time", 0.0);
40
+
vars.put("name", "Hello");
41
+
}
42
+
43
+
@Override
44
+
protected void initGame() {
45
+
// the event builder way
46
+
eventBuilder()
47
+
.when(() -> geti("hp") == 7)
48
+
.limit(4)
49
+
.thenRun(() -> System.out.println("hello"))
50
+
.buildAndStart();
51
+
52
+
// the DSL way
53
+
onDoubleChange("time", value -> {
54
+
System.out.println(value);
55
+
});
56
+
57
+
onStringChange("name", value -> {
58
+
System.out.println(value);
59
+
});
60
+
61
+
onStringChangeTo("name", "HelloHH", () -> {
62
+
System.out.println("bye");
63
+
});
64
+
65
+
onIntChangeTo("hp", 5, () -> System.out.println("Hello"));
66
+
67
+
run(() -> inc("hp", +1), Duration.seconds(1));
68
+
}
69
+
70
+
public static void main(String[] args) {
71
+
launch(args);
72
+
}
73
+
}
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import com.almasb.fxgl.app.services.SystemBundleService
16
16
import com.almasb.fxgl.audio.AudioPlayer
17
17
import com.almasb.fxgl.audio.Music
18
18
import com.almasb.fxgl.core.EngineService
19
+
import com.almasb.fxgl.core.collection.PropertyChangeListener
19
20
import com.almasb.fxgl.core.concurrent.Async
20
21
import com.almasb.fxgl.core.concurrent.Executor
21
22
import com.almasb.fxgl.core.math.FXGLMath
@@ -326,6 +327,67 @@ class FXGL private constructor() { companion object {
326
327
327
328
@JvmStatic fun inc(varName: String, value: Double) = getWorldProperties().increment(varName, value)
328
329
330
+
@JvmStatic fun onIntChangeTo(varName: String, value: Int, action: Runnable): PropertyChangeListener<Int> {
331
+
return onIntChange(varName) {
332
+
if (it == value)
333
+
action.run()
334
+
}
335
+
}
336
+
337
+
@JvmStatic fun onIntChange(varName: String, action: Consumer<Int>): PropertyChangeListener<Int> {
338
+
val listener = PropertyChangeListener<Int> { _, newValue -> action.accept(newValue) }
339
+
340
+
getWorldProperties().addListener(varName, listener)
341
+
342
+
return listener
343
+
}
344
+
345
+
@JvmStatic fun onDoubleChange(varName: String, action: Consumer<Double>): PropertyChangeListener<Double> {
346
+
val listener = PropertyChangeListener<Double> { _, newValue -> action.accept(newValue) }
347
+
348
+
getWorldProperties().addListener(varName, listener)
349
+
350
+
return listener
351
+
}
352
+
353
+
@JvmStatic fun onBooleanChangeTo(varName: String, value: Boolean, action: Runnable): PropertyChangeListener<Boolean> {
354
+
return onBooleanChange(varName) {
355
+
if (it == value)
356
+
action.run()
357
+
}
358
+
}
359
+
360
+
@JvmStatic fun onBooleanChange(varName: String, action: Consumer<Boolean>): PropertyChangeListener<Boolean> {
361
+
val listener = PropertyChangeListener<Boolean> { _, newValue -> action.accept(newValue) }
362
+
363
+
getWorldProperties().addListener(varName, listener)
364
+
365
+
return listener
366
+
}
367
+
368
+
@JvmStatic fun onStringChangeTo(varName: String, value: String, action: Runnable): PropertyChangeListener<String> {
369
+
return onStringChange(varName) {
370
+
if (it == value)
371
+
action.run()
372
+
}
373
+
}
374
+
375
+
@JvmStatic fun onStringChange(varName: String, action: Consumer<String>): PropertyChangeListener<String> {
376
+
val listener = PropertyChangeListener<String> { _, newValue -> action.accept(newValue) }
377
+
378
+
getWorldProperties().addListener(varName, listener)
379
+
380
+
return listener
381
+
}
382
+
383
+
@JvmStatic fun <T> onObjectChange(varName: String, action: Consumer<T>): PropertyChangeListener<T> {
384
+
val listener = PropertyChangeListener<T> { _, newValue -> action.accept(newValue) }
385
+
386
+
getWorldProperties().addListener(varName, listener)
387
+
388
+
return listener
389
+
}
390
+
329
391
/* ASSET LOADING */
330
392
331
393
@JvmStatic fun image(assetName: String): Image = getAssetLoader().loadImage(assetName)
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4