Skip to content

Commit

Permalink
Updates implementation of Dice in the game loop
Browse files Browse the repository at this point in the history
* Adds a new implementation of the game loop dice, which takes the start time and running time as a seed
* Fixes an erroneous rollFromZero implementation added in a previous commit
* Updates unit tests to not use a power of 2 for the number of sides
  • Loading branch information
hobnob committed Oct 8, 2024
1 parent fa79bb9 commit 68a04b7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ final class GameEngine[StartUpData, GameModel, ViewModel](

audioPlayer.addAudioAssets(accumulatedAssetCollection.sounds)

val time = if (firstRun) 0 else gameLoopInstance.runningTimeReference
val time = (if (firstRun) 0 else gameLoopInstance.runningTimeReference) + gameLoopInstance.initialSeed

if (firstRun)
platform = new Platform(parentElement, gameConfig, globalEventStream, dynamicText)
Expand Down
7 changes: 5 additions & 2 deletions indigo/indigo/src/main/scala/indigo/gameengine/GameLoop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import indigo.shared.time.Millis
import indigo.shared.time.Seconds

import scala.collection.mutable
import scala.scalajs.js.Date
import scala.scalajs.js.JSConverters._

val initialDate = new Date()

final class GameLoop[StartUpData, GameModel, ViewModel](
rebuildGameLoop: AssetCollection => Unit,
boundaryLocator: BoundaryLocator,
Expand All @@ -32,7 +35,7 @@ final class GameLoop[StartUpData, GameModel, ViewModel](
frameProcessor: FrameProcessor[StartUpData, GameModel, ViewModel],
startFrameLocked: Boolean
):

val initialSeed = initialDate.valueOf()
@SuppressWarnings(Array("scalafix:DisableSyntax.var"))
private var _gameModelState: GameModel = initialModel
@SuppressWarnings(Array("scalafix:DisableSyntax.var"))
Expand Down Expand Up @@ -128,7 +131,7 @@ final class GameLoop[StartUpData, GameModel, ViewModel](
gameTime,
events,
_inputState,
Dice.fromSeconds(gameTime.running),
Dice.fromSeconds(gameTime.running + initialSeed),
boundaryLocator
)

Expand Down
2 changes: 1 addition & 1 deletion indigo/indigo/src/main/scala/indigo/shared/dice/Dice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ object Dice:
* @return
*/
def rollFromZero: Int =
roll(sides)
rollFromZero(sides)

/** Roll an Int from 0 to the specified number of sides (exclusive)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DiceTests extends munit.FunSuite {
}

test("all dice rolls have an approximately uniform distribution") {
val diceSides = 64
val diceSides = 63
val numRuns = 200_000_000
val dice = Dice.diceSidesN(diceSides, 0)
val expectedDistribution = 1.0 / diceSides
Expand Down Expand Up @@ -90,8 +90,8 @@ class DiceTests extends munit.FunSuite {
}

test("all dice rolls in rollRange have an approximately uniform distribution") {
val diceSides = 64
val halfSides = diceSides / 2
val diceSides = 63
val halfSides = Math.floor(diceSides / 2.0).toInt
val numRuns = 200_000_000
val dice = Dice.diceSidesN(diceSides, 0)
val expectedDistribution = 1.0 / halfSides
Expand All @@ -104,7 +104,7 @@ class DiceTests extends munit.FunSuite {
}

// Ensure that we have the right numbers generated (only numbers from just before half way through the number of sides should have een created)
assertEquals(generatedNums.size, halfSides + 1)
assertEquals(generatedNums.size, (diceSides - halfSides) + 1)
assertEquals(generatedNums.head._1, halfSides)
assertEquals(generatedNums.last._1, diceSides)

Expand Down

0 comments on commit 68a04b7

Please sign in to comment.