Skip to content

Commit

Permalink
Merge pull request #346 from novoda/extract_game_loop
Browse files Browse the repository at this point in the history
[GoL] Extract game loop
  • Loading branch information
tasomaniac authored Jan 10, 2018
2 parents ecd8895 + 8de1875 commit eac0b1a
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 124 deletions.

This file was deleted.

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
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.novoda.gol
package com.novoda.gol.data

import com.novoda.gol.patterns.PatternEntity

Expand All @@ -8,5 +8,5 @@ interface BoardEntity : CellMatrix {

fun toggleCell(x: Int, y: Int): BoardEntity

fun applyPattern(patternEntity: PatternEntity):BoardEntity
}
fun applyPattern(patternEntity: PatternEntity): BoardEntity
}
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) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.novoda.gol
package com.novoda.gol.data

interface CellMatrix {

Expand All @@ -9,4 +9,4 @@ interface CellMatrix {
fun getHeight(): Int

fun getSeeds(): List<PositionEntity>
}
}
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 ListBasedMatrix(private val width: Int, private val height: Int, private val seeds: List<PositionEntity> = listOf()) : CellMatrix {

Expand All @@ -24,4 +24,4 @@ data class ListBasedMatrix(private val width: Int, private val height: Int, priv

override fun getSeeds() = seeds

}
}
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)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.novoda.gol
package com.novoda.gol.data

import com.novoda.gol.patterns.PatternEntity

Expand Down Expand Up @@ -98,4 +98,4 @@ data class SimulationBoardEntity(private val cellMatrix: CellMatrix) : BoardEnti
val nextIterationMatrix = ListBasedMatrix(getWidth(), getHeight(), seeds)
return SimulationBoardEntity(nextIterationMatrix)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.novoda.gol.patterns

import com.novoda.gol.CellMatrix
import com.novoda.gol.data.CellMatrix

abstract class AbstractPattern(private val cellMatrix: CellMatrix) : PatternEntity {

Expand Down Expand Up @@ -28,4 +28,4 @@ abstract class AbstractPattern(private val cellMatrix: CellMatrix) : PatternEnti
}


}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.novoda.gol.patterns

import com.novoda.gol.CellMatrix
import com.novoda.gol.ListBasedMatrix
import com.novoda.gol.PositionEntity
import com.novoda.gol.data.CellMatrix
import com.novoda.gol.data.ListBasedMatrix
import com.novoda.gol.data.PositionEntity

class Blinker private constructor(cellMatrix: CellMatrix) : AbstractPattern(cellMatrix) {

Expand All @@ -17,4 +17,4 @@ class Blinker private constructor(cellMatrix: CellMatrix) : AbstractPattern(cell
return Blinker(ListBasedMatrix(width = 3, height = 3, seeds = seeds))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.novoda.gol.patterns

import com.novoda.gol.CellMatrix
import com.novoda.gol.ListBasedMatrix
import com.novoda.gol.PositionEntity
import com.novoda.gol.data.CellMatrix
import com.novoda.gol.data.ListBasedMatrix
import com.novoda.gol.data.PositionEntity

class Glider private constructor(cellMatrix: CellMatrix) : AbstractPattern(cellMatrix) {
override fun getName() = "Glider"
Expand Down
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
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.novoda.gol
package com.novoda.gol.presentation

import com.novoda.gol.data.BoardEntity
import com.novoda.gol.data.PositionEntity
import com.novoda.gol.patterns.PatternEntity

interface BoardModel {
Expand All @@ -10,5 +12,7 @@ interface BoardModel {

fun selectPattern(pattern: PatternEntity)

fun nextIteration()
fun startSimulation()

fun stopSimulation()
}
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())

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.novoda.gol
package com.novoda.gol.presentation


class BoardPresenter(width: Int, height: Int) {

private val boardModel = BoardModelImpl(width, height)
private val boardModel = BoardModelImpl.create(width, height)

fun bind(boardView: BoardView) {

Expand All @@ -15,20 +15,24 @@ class BoardPresenter(width: Int, height: Int) {
boardModel.toggleCellAt(position)
}

boardView.onTick = {
boardModel.nextIteration()
}

boardView.onPatternSelected = { pattern ->
boardModel.selectPattern(pattern)
}

boardView.onStartSimulationClicked = {
boardModel.startSimulation()
}

boardView.onStopSimulationClicked = {
boardModel.stopSimulation()
}
}

fun unbind(boardView: BoardView) {
boardModel.onBoardChanged = { }
boardView.onCellClicked = { }
boardView.onTick = {}
boardView.onPatternSelected = {}
boardView.onStartSimulationClicked = {}
boardView.onStopSimulationClicked = {}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.novoda.gol
package com.novoda.gol.presentation

import com.novoda.gol.data.BoardEntity
import com.novoda.gol.data.PositionEntity
import com.novoda.gol.patterns.PatternEntity

interface BoardView {

var onPatternSelected: (pattern: PatternEntity) -> Unit
var onCellClicked: (position: PositionEntity) -> Unit
var onTick: () -> Unit
var onStartSimulationClicked: () -> Unit
var onStopSimulationClicked: () -> Unit

fun renderBoard(boardEntity: BoardEntity)

Expand Down
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
}
Loading

0 comments on commit eac0b1a

Please sign in to comment.