From 68a04b788cef280f0252d0e87214925962a822fe Mon Sep 17 00:00:00 2001 From: David North Date: Tue, 8 Oct 2024 17:03:26 +0100 Subject: [PATCH] Updates implementation of Dice in the game loop * 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 --- .../src/main/scala/indigo/gameengine/GameEngine.scala | 2 +- .../src/main/scala/indigo/gameengine/GameLoop.scala | 7 +++++-- .../indigo/src/main/scala/indigo/shared/dice/Dice.scala | 2 +- .../src/test/scala/indigo/shared/dice/DiceTests.scala | 8 ++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/indigo/indigo/src/main/scala/indigo/gameengine/GameEngine.scala b/indigo/indigo/src/main/scala/indigo/gameengine/GameEngine.scala index b3993dcb7..eec0e832c 100644 --- a/indigo/indigo/src/main/scala/indigo/gameengine/GameEngine.scala +++ b/indigo/indigo/src/main/scala/indigo/gameengine/GameEngine.scala @@ -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) diff --git a/indigo/indigo/src/main/scala/indigo/gameengine/GameLoop.scala b/indigo/indigo/src/main/scala/indigo/gameengine/GameLoop.scala index d1cd7da78..2e3526128 100644 --- a/indigo/indigo/src/main/scala/indigo/gameengine/GameLoop.scala +++ b/indigo/indigo/src/main/scala/indigo/gameengine/GameLoop.scala @@ -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, @@ -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")) @@ -128,7 +131,7 @@ final class GameLoop[StartUpData, GameModel, ViewModel]( gameTime, events, _inputState, - Dice.fromSeconds(gameTime.running), + Dice.fromSeconds(gameTime.running + initialSeed), boundaryLocator ) diff --git a/indigo/indigo/src/main/scala/indigo/shared/dice/Dice.scala b/indigo/indigo/src/main/scala/indigo/shared/dice/Dice.scala index af15dd609..62bea348c 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/dice/Dice.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/dice/Dice.scala @@ -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) * diff --git a/indigo/indigo/src/test/scala/indigo/shared/dice/DiceTests.scala b/indigo/indigo/src/test/scala/indigo/shared/dice/DiceTests.scala index 70704bec6..af134a9ac 100644 --- a/indigo/indigo/src/test/scala/indigo/shared/dice/DiceTests.scala +++ b/indigo/indigo/src/test/scala/indigo/shared/dice/DiceTests.scala @@ -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 @@ -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 @@ -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)