diff --git a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/MiniGameService.kt b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/MiniGameService.kt index 134a0c3cfc..955d266a1c 100644 --- a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/MiniGameService.kt +++ b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/MiniGameService.kt @@ -51,8 +51,8 @@ class MiniGameService : EngineService() { startMiniGame(RandomOccurrenceView(miniGame)) { callback.accept(it) } } - fun startTriggerSequence(keys: List, callback: Consumer) { - val miniGame = TriggerSequenceMiniGame() + fun startTriggerSequence(keys: List, winRatio: Double, callback: Consumer) { + val miniGame = TriggerSequenceMiniGame(winRatio) miniGame.triggers += keys.map { KeyTrigger(it) } startMiniGame(TriggerSequenceView(miniGame)) { callback.accept(it) } diff --git a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/triggersequence/TriggerSequenceMiniGame.kt b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/triggersequence/TriggerSequenceMiniGame.kt index 541d06119e..4836d136a8 100644 --- a/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/triggersequence/TriggerSequenceMiniGame.kt +++ b/fxgl-gameplay/src/main/kotlin/com/almasb/fxgl/minigames/triggersequence/TriggerSequenceMiniGame.kt @@ -20,6 +20,7 @@ import com.almasb.fxgl.minigames.MiniGameResult import com.almasb.fxgl.minigames.MiniGameView import javafx.geometry.Point2D import javafx.scene.Group +import javafx.scene.Node import javafx.scene.image.Image import javafx.scene.image.ImageView import javafx.scene.input.KeyCode @@ -29,7 +30,7 @@ import javafx.scene.shape.Circle import javafx.scene.shape.Line import javafx.util.Duration -class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMiniGame()) : MiniGameView(miniGame) { +class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMiniGame(winRatio = 1.0)) : MiniGameView(miniGame) { private val animationGood: Animation<*> private val animationBad: Animation<*> @@ -37,14 +38,18 @@ class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMin private val circle = StackPane() private val bg = Circle(40.0, 40.0, 40.0, Color.GREEN) + private val line1 = Line(0.0, 0.0, 0.0, 300.0) + private val line2 = Line(150.0, 0.0, 150.0, 300.0) + private val triggerViews = Group() + private var numCorrectTriggers = 0 + private var currentTriggerIndex = 0 private val good = ImageView(Image(javaClass.getResourceAsStream("checkmark.png"))) private val bad = ImageView(Image(javaClass.getResourceAsStream("cross.png"))) init { - val line1 = Line(0.0, 0.0, 0.0, 300.0) - val line2 = Line(100.0, 0.0, 100.0, 300.0) + line1.strokeWidth = 2.0 line2.strokeWidth = 2.0 @@ -80,6 +85,37 @@ class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMin animationGood.onUpdate(tpf) animationBad.onUpdate(tpf) + + if (triggerViews.children.isNotEmpty()) { + checkTriggerPosition(triggerViews.children[0]) + } else { + miniGame.endGame(numCorrectTriggers) + } + } + + private fun checkTriggerPosition(currentTriggerView: Node) { + if (currentTriggerView.translateX < line1.startX) { + triggerViews.children.removeAt(0) // TODO Check index + currentTriggerIndex++ + startAnimationBad() + } + + } + + private fun startAnimationGood() { + circle.opacity = 1.0 + animationGood.start() + bg.fill = Color.GREEN + + circle.children[1] = good + } + + private fun startAnimationBad() { + circle.opacity = 1.0 + animationBad.start() + bg.fill = Color.RED + + circle.children[1] = bad } override fun onInitInput(input: Input) { @@ -88,23 +124,23 @@ class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMin if (trigger is KeyTrigger) { val key = trigger.key - circle.opacity = 1.0 - - if (triggerViews.children.isNotEmpty()) - triggerViews.children.removeAt(0) + if (triggerViews.children.isEmpty()) + return - if (miniGame.press(key)) { - animationGood.start() - bg.fill = Color.GREEN - - circle.children[1] = good + val currentTrigger = triggerViews.children.removeAt(0) + // Has the correct key been pressed, and is the current view between the two lines + if (miniGame.isCorrect(key, currentTriggerIndex) && + currentTrigger.translateX >= line1.startX && + currentTrigger.translateX <= line2.startX) + { + startAnimationGood() + numCorrectTriggers++ } else { - animationBad.start() - bg.fill = Color.RED - - circle.children[1] = bad + startAnimationBad() } + // After checking, increment trigger index + currentTriggerIndex++ } } }) @@ -115,17 +151,15 @@ class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMin * * @author Almas Baimagambetov (almaslvl@gmail.com) */ -class TriggerSequenceMiniGame : MiniGame() { +class TriggerSequenceMiniGame(val winRatio: Double) : MiniGame() { private val log = Logger.get(javaClass) var numTriggersForSuccess = 0 - var numTriggers = 4 - var moveSpeed = 350 - - private var numCorrectTriggers = 0 + val numTriggers: Int + get() = triggers.size - private var currentIndex = 0 + var moveSpeed = 350 val triggers = arrayListOf() @@ -150,27 +184,18 @@ class TriggerSequenceMiniGame : MiniGame() { views.forEach { it.translateX -= moveSpeed * tpf } } - fun press(key: KeyCode): Boolean { - if (currentIndex >= triggers.size) { - log.warning("Current index is greater or equal to number of triggers") - isDone = true - result = TriggerSequenceResult(true) - return true - } - - val ok = triggers[currentIndex++].key == key - - if (ok) { - numCorrectTriggers++ - } - - if (currentIndex == triggers.size) { - isDone = true - result = TriggerSequenceResult(numCorrectTriggers >= numTriggersForSuccess) - } + fun endGame(numCorrectTriggers: Int) { + // Trigger the end of the MiniGame + isDone = true + val ratio = numCorrectTriggers / numTriggers.toDouble() + result = TriggerSequenceResult(ratio >= winRatio, ratio) + } - return ok + fun isCorrect(key: KeyCode, currentIndex: Int): Boolean { + return triggers[currentIndex].key == key } } -class TriggerSequenceResult(override val isSuccess: Boolean) : MiniGameResult +class TriggerSequenceResult(override val isSuccess: Boolean, val ratio: Double ) : MiniGameResult { + +} diff --git a/fxgl-samples/src/main/java/sandbox/minigames/MiniGameSample.java b/fxgl-samples/src/main/java/sandbox/minigames/MiniGameSample.java index f5b5917faf..7adce29bc5 100644 --- a/fxgl-samples/src/main/java/sandbox/minigames/MiniGameSample.java +++ b/fxgl-samples/src/main/java/sandbox/minigames/MiniGameSample.java @@ -44,7 +44,7 @@ protected void initGame() { var btn1 = new Button("Trigger Sequence"); btn1.setOnAction(e -> { - getMiniGameService().startTriggerSequence(List.of(S, F, A), result -> { + getMiniGameService().startTriggerSequence(List.of(S, F, A), 1.0, result -> { debugText.setText(result.isSuccess() ? "Success" : "Fail"); }); });