Repository navigation
Feature 62: Automatic game end #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
bbae970
508839e
20f371b
c7fdbab
5a888e2
8fab4ec
84b8920
c31c2a9
c4f94f4
f4ae70e
d496e6c
f840516
d147c1a
2ec566e
157ffda
b1a3886
bf248fd
7477972
a75ba69
b13811e
96ec235
1d53ae2
77b8aef
1cb68ca
5bd3fa9
d602ea6
31dc848
ce7e2c7
bddb9eb
20ec68b
7bf0009
3e1e95a
0cc0e75
e013190
15383e3
0aa888f
901a00b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,11 @@ class GameManager : Runnable { | |
| private var gameSpeedFactor: Double | ||
| var gameOverSoon: Boolean | ||
|
|
||
| private var gameEndDurationInMs: Long = 0 | ||
| private var gameEndCalcDurationInMs: Long = 0 | ||
| private var gameEndTimeout: GameTimeout? = null | ||
| private var gameEndCalcTimeout: GameTimeout? = null | ||
|
Comment on lines
+55
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make non nullable and directly initialize it please.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calculation is always at the end of the game, thus this names are quite confusing. Furthermore its not a timeout of the game end but a game timeout right ;) |
||
|
|
||
| init { | ||
| LOGGER.info("Create $this.") | ||
| this.gameThread = Thread(this, "GameThread") | ||
|
|
@@ -116,7 +121,15 @@ class GameManager : Runnable { | |
| LOGGER.info("Set $level as new level.") | ||
| } | ||
|
|
||
| fun startGame() { | ||
| fun setGameEndDuration(durationInMs: Long) { | ||
| gameEndDurationInMs = durationInMs | ||
| } | ||
|
|
||
| fun setGameEndCalcDuration(durationInMs: Long) { | ||
| gameEndCalcDurationInMs = durationInMs | ||
| } | ||
|
Comment on lines
+124
to
+130
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats java not kotlin, just make the var public accessable. |
||
|
|
||
| fun startGame(gameEndFunction: () -> Unit) { | ||
| val gameStartThread: Thread = object : Thread("Gamestart Thread") { | ||
| override fun run() { | ||
| LOGGER.info("Init game start...") | ||
|
|
@@ -146,11 +159,24 @@ class GameManager : Runnable { | |
| } | ||
| } | ||
| level?.reset() | ||
|
|
||
| gameEndTimeout = null | ||
| gameEndCalcTimeout = null | ||
|
|
||
| if (gameEndDurationInMs > 0) { | ||
| gameEndTimeout = GameTimeout(level!!, gameEndDurationInMs) { setGameOverSoon() } | ||
| if (gameEndCalcDurationInMs > 0) { | ||
| gameEndCalcTimeout = GameTimeout(level!!, gameEndDurationInMs + gameEndCalcDurationInMs) { gameEndFunction() } | ||
| } | ||
| } | ||
|
|
||
| setGameState(GameState.Running) | ||
| Thread(level, "Levelrunner").start() | ||
| lock.withLock { | ||
| condition.signalAll() | ||
| } | ||
| gameEndTimeout?.startTimer() | ||
| gameEndCalcTimeout?.startTimer() | ||
| LOGGER.info("Game is Running.") | ||
| } | ||
| } | ||
|
|
@@ -171,11 +197,23 @@ class GameManager : Runnable { | |
| private fun setGameState(state: GameState) { | ||
| GUIController.setEvent(PropertyChangeEvent(this, GUIController.GAME_STATE_CHANGE, gameState, state)) | ||
| this.gameState = state | ||
| isPause = state == GameState.Break | ||
|
|
||
| if (state == GameState.Break) { | ||
| isPause = true | ||
| gameEndCalcTimeout?.stopTimer() | ||
| gameEndTimeout?.stopTimer() | ||
| } else if (isPause && gameState == GameState.Running) { | ||
| isPause = false | ||
| gameEndCalcTimeout?.startTimer() | ||
| gameEndTimeout?.startTimer() | ||
| } | ||
|
|
||
|
Comment on lines
+201
to
+210
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not letting the timeout consume the GameState? by that you could save lots of boilerplate code. |
||
| if (state == GameState.Running) { | ||
| isGameOver = false | ||
| } else if (state == GameState.Configuration) { | ||
| isGameOver = true | ||
| gameEndCalcTimeout?.stopTimer() | ||
| gameEndTimeout?.stopTimer() | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package org.openbase.planetsudo.game | ||
|
|
||
| import org.openbase.planetsudo.level.AbstractLevel | ||
| import java.awt.event.ActionEvent | ||
| import java.awt.event.ActionListener | ||
| import javax.swing.Timer | ||
|
|
||
| // This still de-syncs slightly from the timer in LevelMenuPanel | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this an open todo / issue? Or what is the intent of this comment? :) |
||
| class GameTimeout(level: AbstractLevel, durationInMs: Long, val function: () -> Unit) : ActionListener { | ||
| private val timer: Timer = Timer(calcTimerDelay(level.getGameSpeedFactor()), this) | ||
| private var secondsRemaining = durationInMs / 1000 | ||
| private var fired: Boolean = false | ||
|
|
||
| init { // This is a blatant copy of LevelMenuPanel, thus knowingly violating single source of truth | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| level.addPropertyChangeListener { | ||
| if (it.propertyName == AbstractLevel.GAME_SPEED_FACTOR_CHANGED) { | ||
| timer.isRunning.let { running -> | ||
| updateDelay(it.newValue as Double) | ||
| if (running) { | ||
| timer.restart() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun updateDelay(gameSpeed: Double) { | ||
| val delay = calcTimerDelay(gameSpeed) | ||
| timer.delay = delay | ||
| timer.initialDelay = delay | ||
| } | ||
|
|
||
| private fun calcTimerDelay(gameSpeed: Double): Int { | ||
| return (1000.0 / gameSpeed).toInt() | ||
| } | ||
|
|
||
| fun startTimer() { | ||
| if (!timer.isRunning && !fired) { | ||
| timer.start() | ||
| } | ||
| } | ||
|
|
||
| fun stopTimer() { | ||
| if (timer.isRunning) { | ||
| timer.stop() | ||
| } | ||
| } | ||
|
|
||
| override fun actionPerformed(ex: ActionEvent) { | ||
| secondsRemaining-- | ||
| if (secondsRemaining <= 0 && !fired) { | ||
| function() | ||
| fired = true | ||
| stopTimer() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,7 +260,7 @@ class MainGUI : JFrame, PropertyChangeListener { | |
|
|
||
| private var finished = false | ||
|
|
||
| private fun finalizeGame() { | ||
| fun finalizeGame() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why public? |
||
| finished = true | ||
|
|
||
| gameManager.switchGameState(GameState.Break) | ||
|
|
@@ -460,7 +460,7 @@ class MainGUI : JFrame, PropertyChangeListener { | |
|
|
||
| private fun startPauseMenuItemActionPerformed(evt: ActionEvent) { // GEN-FIRST:event_startPauseMenuItemActionPerformed | ||
| when (gameManager.gameState) { | ||
| GameState.Configuration -> gameManager.startGame() | ||
| GameState.Configuration -> gameManager.startGame { finalizeGame() } | ||
| GameState.Running -> gameManager.switchGameState(GameState.Break) | ||
| GameState.Break -> gameManager.switchGameState(GameState.Running) | ||
| else -> {} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not using Duration for that?