From 84c21cc85d59afd0ca44c4ec9473cab701ed1e62 Mon Sep 17 00:00:00 2001 From: Almas Baim Date: Sat, 28 Dec 2024 08:39:24 +0000 Subject: [PATCH] clean up VarChangeSample and move out of sandbox --- .../VarChangeSample.java | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) rename fxgl-samples/src/main/java/{sandbox => intermediate}/VarChangeSample.java (50%) diff --git a/fxgl-samples/src/main/java/sandbox/VarChangeSample.java b/fxgl-samples/src/main/java/intermediate/VarChangeSample.java similarity index 50% rename from fxgl-samples/src/main/java/sandbox/VarChangeSample.java rename to fxgl-samples/src/main/java/intermediate/VarChangeSample.java index 9698f0d5f..ecdd825f2 100644 --- a/fxgl-samples/src/main/java/sandbox/VarChangeSample.java +++ b/fxgl-samples/src/main/java/intermediate/VarChangeSample.java @@ -4,11 +4,10 @@ * See LICENSE for details. */ -package sandbox; +package intermediate; import com.almasb.fxgl.app.GameApplication; import com.almasb.fxgl.app.GameSettings; -import javafx.scene.input.KeyCode; import javafx.util.Duration; import java.util.Map; @@ -16,23 +15,15 @@ import static com.almasb.fxgl.dsl.FXGL.*; /** - * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com) + * Shows how to listen for changes in variables and react. + * + * @author Almas Baim (https://github.com/AlmasB) */ public class VarChangeSample extends GameApplication { @Override protected void initSettings(GameSettings settings) { } - @Override - protected void initInput() { - onKeyDown(KeyCode.F, "update", () -> { - inc("time", +1.0); - - var name = gets("name"); - set("name", name + "H"); - }); - } - @Override protected void initGameVars(Map vars) { vars.put("hp", 0); @@ -42,29 +33,44 @@ protected void initGameVars(Map vars) { @Override protected void initGame() { - // the event builder way - eventBuilder() - .when(() -> geti("hp") == 7) - .limit(4) - .thenRun(() -> System.out.println("hello")) - .buildAndStart(); - - // the DSL way + // the DSL way, if you need something set up quickly onDoubleChange("time", value -> { - System.out.println(value); + System.out.println("The var