Skip to content

Commit

Permalink
fix: TriggerSequence Minigame check if trigger is between two lines, c…
Browse files Browse the repository at this point in the history
…loses #796
  • Loading branch information
BeckFW authored Mar 21, 2024
1 parent 5ad6bd8 commit 8dfef54
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class MiniGameService : EngineService() {
startMiniGame(RandomOccurrenceView(miniGame)) { callback.accept(it) }
}

fun startTriggerSequence(keys: List<KeyCode>, callback: Consumer<TriggerSequenceResult>) {
val miniGame = TriggerSequenceMiniGame()
fun startTriggerSequence(keys: List<KeyCode>, winRatio: Double, callback: Consumer<TriggerSequenceResult>) {
val miniGame = TriggerSequenceMiniGame(winRatio)
miniGame.triggers += keys.map { KeyTrigger(it) }

startMiniGame(TriggerSequenceView(miniGame)) { callback.accept(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,22 +30,26 @@ import javafx.scene.shape.Circle
import javafx.scene.shape.Line
import javafx.util.Duration

class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMiniGame()) : MiniGameView<TriggerSequenceMiniGame>(miniGame) {
class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMiniGame(winRatio = 1.0)) : MiniGameView<TriggerSequenceMiniGame>(miniGame) {

private val animationGood: Animation<*>
private val animationBad: Animation<*>

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

Expand Down Expand Up @@ -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) {
Expand All @@ -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++
}
}
})
Expand All @@ -115,17 +151,15 @@ class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMin
*
* @author Almas Baimagambetov ([email protected])
*/
class TriggerSequenceMiniGame : MiniGame<TriggerSequenceResult>() {
class TriggerSequenceMiniGame(val winRatio: Double) : MiniGame<TriggerSequenceResult>() {

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<KeyTrigger>()

Expand All @@ -150,27 +184,18 @@ class TriggerSequenceMiniGame : MiniGame<TriggerSequenceResult>() {
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 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
Expand Down

0 comments on commit 8dfef54

Please sign in to comment.