-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from novoda/extract_game_loop
[GoL] Extract game loop
- Loading branch information
Showing
23 changed files
with
181 additions
and
124 deletions.
There are no files selected for viewing
32 changes: 0 additions & 32 deletions
32
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/BoardModelImpl.kt
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/GameLoop.kt
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.novoda.gol | ||
|
||
|
||
expect class GameLoop() { | ||
|
||
var onTick: () -> Unit | ||
|
||
fun startWith(intervalMs: Int) | ||
|
||
fun stop() | ||
|
||
fun isLooping(): Boolean | ||
} |
3 changes: 0 additions & 3 deletions
3
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/PositionEntity.kt
This file was deleted.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
.../main/kotlin/com/novoda/gol/CellEntity.kt → .../kotlin/com/novoda/gol/data/CellEntity.kt
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.novoda.gol | ||
package com.novoda.gol.data | ||
|
||
data class CellEntity(val isAlive: Boolean) { | ||
|
||
|
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
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
3 changes: 3 additions & 0 deletions
3
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/data/PositionEntity.kt
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.novoda.gol.data | ||
|
||
data class PositionEntity(val x: Int, val y: Int) |
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
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
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
6 changes: 3 additions & 3 deletions
6
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/patterns/Glider.kt
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
4 changes: 2 additions & 2 deletions
4
game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/patterns/PatternEntity.kt
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.novoda.gol.patterns | ||
|
||
import com.novoda.gol.CellMatrix | ||
import com.novoda.gol.data.CellMatrix | ||
|
||
interface PatternEntity : CellMatrix { | ||
fun getName(): String | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...f-life-multiplatform/common/src/main/kotlin/com/novoda/gol/presentation/BoardModelImpl.kt
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.GameLoop | ||
import com.novoda.gol.data.BoardEntity | ||
import com.novoda.gol.data.ListBasedMatrix | ||
import com.novoda.gol.data.PositionEntity | ||
import com.novoda.gol.data.SimulationBoardEntity | ||
import com.novoda.gol.patterns.PatternEntity | ||
import kotlin.properties.Delegates.observable | ||
|
||
|
||
class BoardModelImpl private constructor(initialBoard: BoardEntity, private val gameLoop: GameLoop) : BoardModel { | ||
|
||
private var board: BoardEntity by observable(initialBoard) { _, _, newValue -> | ||
onBoardChanged.invoke(newValue) | ||
} | ||
private var pattern: PatternEntity? = null | ||
|
||
override var onBoardChanged by observable<(BoardEntity) -> Unit>({}, { _, _, newValue -> | ||
newValue.invoke(board) | ||
}) | ||
|
||
init { | ||
gameLoop.onTick = { | ||
board = board.nextIteration() | ||
} | ||
} | ||
|
||
override fun toggleCellAt(positionEntity: PositionEntity) { | ||
if (gameLoop.isLooping()) { | ||
return | ||
} | ||
board = board.toggleCell(positionEntity.x, positionEntity.y) | ||
} | ||
|
||
override fun selectPattern(pattern: PatternEntity) { | ||
if (gameLoop.isLooping() || this.pattern == pattern) { | ||
return | ||
} | ||
this.pattern = pattern | ||
board = board.applyPattern(pattern) | ||
} | ||
|
||
override fun startSimulation() { | ||
if (gameLoop.isLooping()) { | ||
return | ||
} | ||
gameLoop.startWith(300) | ||
} | ||
|
||
override fun stopSimulation() { | ||
if (gameLoop.isLooping().not()) { | ||
return | ||
} | ||
gameLoop.stop() | ||
} | ||
|
||
companion object { | ||
|
||
fun create(width: Int, height: Int): BoardModel = BoardModelImpl(SimulationBoardEntity(ListBasedMatrix(width, height)), GameLoop()) | ||
|
||
} | ||
} |
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
7 changes: 5 additions & 2 deletions
7
...c/main/kotlin/com/novoda/gol/BoardView.kt → .../com/novoda/gol/presentation/BoardView.kt
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
26 changes: 26 additions & 0 deletions
26
game-of-life-multiplatform/game-of-life-js/src/main/kotlin/com/novoda/gol/GameLoop.kt
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.novoda.gol | ||
|
||
import kotlin.browser.window | ||
|
||
|
||
actual class GameLoop { | ||
|
||
private var loop: Int? = null | ||
|
||
actual var onTick: () -> Unit = {} | ||
|
||
actual fun startWith(intervalMs: Int) { | ||
loop = window.setInterval({ | ||
onTick.invoke() | ||
}, intervalMs) | ||
} | ||
|
||
actual fun stop() { | ||
if (loop != null) { | ||
window.clearInterval(loop!!) | ||
loop = null | ||
} | ||
} | ||
|
||
actual fun isLooping() = loop != null | ||
} |
Oops, something went wrong.