-
-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Ensure MiniGame ends correctly and returns result object us…
…ing winRatio property
- Loading branch information
Showing
1 changed file
with
60 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<TriggerSequenceMiniGame>(miniGame) { | ||
class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMiniGame(winRatio = 1.0)) : MiniGameView<TriggerSequenceMiniGame>(miniGame) { | ||
|
||
private val animationGood: Animation<*> | ||
private val animationBad: Animation<*> | ||
|
@@ -38,9 +39,11 @@ class TriggerSequenceView(miniGame: TriggerSequenceMiniGame = TriggerSequenceMin | |
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(100.0, 0.0, 100.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"))) | ||
|
@@ -82,36 +85,62 @@ 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) { | ||
input.addTriggerListener(object : TriggerListener() { | ||
override fun onActionBegin(trigger: Trigger) { | ||
if (trigger is KeyTrigger) { | ||
val key = trigger.key | ||
val currentTrigger = triggerViews.children[0] | ||
|
||
circle.opacity = 1.0 | ||
if (triggerViews.children.isEmpty()) | ||
return | ||
|
||
if (triggerViews.children.isNotEmpty()) | ||
triggerViews.children.removeAt(0) | ||
val currentTrigger = triggerViews.children.removeAt(0) | ||
|
||
// Has the correct key been pressed, and is the current view between the two lines | ||
if (miniGame.press(key) && | ||
if (miniGame.isCorrect(key, currentTriggerIndex) && | ||
currentTrigger.translateX >= line1.startX && | ||
currentTrigger.translateX <= line2.startX) | ||
{ | ||
animationGood.start() | ||
bg.fill = Color.GREEN | ||
|
||
circle.children[1] = good | ||
|
||
startAnimationGood() | ||
numCorrectTriggers++ | ||
} else { | ||
animationBad.start() | ||
bg.fill = Color.RED | ||
|
||
circle.children[1] = bad | ||
startAnimationBad() | ||
} | ||
// After checking, increment trigger index | ||
currentTriggerIndex++ | ||
} | ||
} | ||
}) | ||
|
@@ -122,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>() | ||
|
||
|
@@ -157,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 { | ||
|
||
} |