Skip to content

Commit

Permalink
Context to replace FrameContext
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 9, 2024
1 parent 8762461 commit fc4db79
Show file tree
Hide file tree
Showing 47 changed files with 466 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import indigo.shared.datatypes.BindingKey
import indigo.shared.events.GlobalEvent
import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId

/** The JobMarket is a subsystem that manages a global pool of available jobs.
Expand Down Expand Up @@ -36,7 +36,7 @@ final case class JobMarket[Model](id: SubSystemId, availableJobs: List[Job]) ext
private given CanEqual[Option[Job], Option[Job]] = CanEqual.derived

def update(
frameContext: SubSystemFrameContext[ReferenceData],
frameContext: SubSystemContext[ReferenceData],
jobs: List[Job]
): JobMarketEvent => Outcome[List[Job]] = {
case JobMarketEvent.Post(job) =>
Expand All @@ -57,7 +57,7 @@ final case class JobMarket[Model](id: SubSystemId, availableJobs: List[Job]) ext
Outcome(jobs)
}

def present(frameContext: SubSystemFrameContext[ReferenceData], jobs: List[Job]): Outcome[SceneUpdateFragment] =
def present(frameContext: SubSystemContext[ReferenceData], jobs: List[Job]): Outcome[SceneUpdateFragment] =
Outcome(SceneUpdateFragment.empty)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import indigo.shared.events.GlobalEvent
import indigo.shared.events.SubSystemEvent
import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId

// Provides "at least once" message delivery for updates on a bundle's loading status.
Expand All @@ -37,7 +37,7 @@ final class AssetBundleLoader[Model] extends SubSystem[Model]:
private given CanEqual[Option[Set[AssetType]], Option[Set[AssetType]]] = CanEqual.derived

def update(
frameContext: SubSystemFrameContext[ReferenceData],
frameContext: SubSystemContext[ReferenceData],
tracker: AssetBundleTracker
): GlobalEvent => Outcome[AssetBundleTracker] =
// Asset Bundle Loader Commands
Expand Down Expand Up @@ -76,7 +76,7 @@ final class AssetBundleLoader[Model] extends SubSystem[Model]:
Outcome(tracker)

def present(
frameContext: SubSystemFrameContext[ReferenceData],
frameContext: SubSystemContext[ReferenceData],
model: AssetBundleTracker
): Outcome[SceneUpdateFragment] =
Outcome(SceneUpdateFragment.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import indigo.shared.events.SubSystemEvent
import indigo.shared.scenegraph.SceneNode
import indigo.shared.scenegraph._
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId
import indigo.shared.temporal.Signal
import indigo.shared.temporal.SignalReader
Expand Down Expand Up @@ -57,7 +57,7 @@ final case class Automata[Model](
private given CanEqual[Option[Int], Option[Int]] = CanEqual.derived

def update(
frameContext: SubSystemFrameContext[ReferenceData],
frameContext: SubSystemContext[ReferenceData],
state: AutomataState
): AutomataEvent => Outcome[AutomataState] =
case Spawn(key, position, lifeSpan, payload) if key == poolKey =>
Expand All @@ -68,7 +68,7 @@ final case class Automata[Model](
automaton.onCull,
new AutomatonSeedValues(
position,
frameContext.gameTime.running,
frameContext.time.running,
lifeSpan.getOrElse(automaton.lifespan),
frameContext.dice.roll,
payload
Expand Down Expand Up @@ -108,21 +108,21 @@ final case class Automata[Model](

case Update(key) if key == poolKey =>
val cullEvents = state.pool
.filterNot(_.isAlive(frameContext.gameTime.running))
.filterNot(_.isAlive(frameContext.time.running))
.flatMap(sa => sa.onCull(sa.seedValues))

Outcome(
state.copy(
pool = state.pool.filter(_.isAlive(frameContext.gameTime.running))
pool = state.pool.filter(_.isAlive(frameContext.time.running))
),
Batch(cullEvents)
)

case _ =>
Outcome(state)

def present(frameContext: SubSystemFrameContext[ReferenceData], state: AutomataState): Outcome[SceneUpdateFragment] =
val updated = Automata.renderNoLayer(state.pool, frameContext.gameTime)
def present(frameContext: SubSystemContext[ReferenceData], state: AutomataState): Outcome[SceneUpdateFragment] =
val updated = Automata.renderNoLayer(state.pool, frameContext.time)

Outcome(
SceneUpdateFragment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.scenegraph.Shape
import indigo.shared.scenegraph.TextBox
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId
import indigo.shared.time.FPS
import indigo.shared.time.Seconds
Expand Down Expand Up @@ -56,15 +56,15 @@ final case class FPSCounter[Model](
Outcome(FPSCounterState.initial(startPosition))

def update(
context: SubSystemFrameContext[ReferenceData],
context: SubSystemContext[ReferenceData],
model: FPSCounterState
): GlobalEvent => Outcome[FPSCounterState] = {
case FrameTick =>
if (context.gameTime.running >= (model.lastInterval + Seconds(1)))
if (context.time.running >= (model.lastInterval + Seconds(1)))
Outcome(
model.copy(
fps = decideNextFps(model.frameCountSinceInterval),
lastInterval = context.gameTime.running,
lastInterval = context.time.running,
frameCountSinceInterval = 0
)
)
Expand All @@ -82,16 +82,15 @@ final case class FPSCounter[Model](
.withFontFamily(fontFamily)
.withFontSize(fontSize)

def present(context: SubSystemFrameContext[ReferenceData], model: FPSCounterState): Outcome[SceneUpdateFragment] =
def present(context: SubSystemContext[ReferenceData], model: FPSCounterState): Outcome[SceneUpdateFragment] =
val text: TextBox =
textBox
.withText(s"""FPS ${model.fps.toString}""")
.withColor(pickTint(idealFps, model.fps))
.moveTo(model.position + 2)

val size: Rectangle =
context.boundaryLocator
.measureText(text)
context.bounds.measureText(text)

val boxSize =
({ (s: Size) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package indigoextras.ui

import indigo.shared.BoundaryLocator
import indigo.shared.FrameContext
import indigo.shared.Context
import indigo.shared.Outcome
import indigo.shared.collections.Batch
import indigo.shared.constants.Key
Expand Down Expand Up @@ -35,8 +35,8 @@ final case class InputField(
onLoseFocus: () => Batch[GlobalEvent]
) derives CanEqual:

def bounds(boundaryLocator: BoundaryLocator): Option[Rectangle] =
boundaryLocator.findBounds(assets.text.withText(text).moveTo(position))
def bounds(_bounds: Context.Services.Bounds): Option[Rectangle] =
_bounds.find(assets.text.withText(text).moveTo(position))

def withText(newText: String): InputField =
this.copy(
Expand Down Expand Up @@ -166,7 +166,7 @@ final case class InputField(
def withLoseFocusActions(actions: => Batch[GlobalEvent]): InputField =
this.copy(onLoseFocus = () => actions)

def update(frameContext: FrameContext[?]): Outcome[InputField] = {
def update(context: Context[?]): Outcome[InputField] = {
@tailrec
def rec(
keysReleased: List[Key],
Expand All @@ -177,7 +177,7 @@ final case class InputField(
keysReleased match {
case Nil =>
if (touched)
Outcome(acc.copy(lastCursorMove = frameContext.gameTime.running), Batch.fromOption(changeEvent))
Outcome(acc.copy(lastCursorMove = context.frame.time.running), Batch.fromOption(changeEvent))
else
Outcome(acc, Batch.fromOption(changeEvent))

Expand Down Expand Up @@ -215,13 +215,13 @@ final case class InputField(

val updated: Outcome[InputField] =
if (hasFocus)
rec(frameContext.inputState.keyboard.keysReleased.toList, this, false, None)
rec(context.frame.input.keyboard.keysReleased.toList, this, false, None)
else Outcome(this)

if (frameContext.inputState.pointers.isReleased)
bounds(frameContext.boundaryLocator) match
if (context.frame.input.pointers.isReleased)
bounds(context.services.bounds) match
case Some(bounds) =>
if frameContext.inputState.pointers.wasUpWithin(bounds, MouseButton.LeftMouseButton) then
if context.frame.input.pointers.wasUpWithin(bounds, MouseButton.LeftMouseButton) then
updated.flatMap(_.giveFocus)
else updated.flatMap(_.loseFocus)
case _ =>
Expand All @@ -231,7 +231,7 @@ final case class InputField(

def draw(
gameTime: GameTime,
boundaryLocator: BoundaryLocator
boundaryLocator: Context.Services.Bounds
): Batch[SceneNode] = {
val field =
assets.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import indigo.shared.events.FrameTick
import indigo.shared.events.InputState
import indigo.shared.scenegraph.SceneAudio
import indigo.shared.subsystems
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId
import indigo.shared.time.GameTime

class JobMarketTests extends munit.FunSuite {

val context =
SubSystemFrameContext[Unit](
SubSystemContext[Unit](
GameTime.zero,
Dice.loaded(6),
InputState.default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ import indigo.shared.BoundaryLocator
import indigo.shared.FontRegister
import indigo.shared.dice.Dice
import indigo.shared.events.InputState
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.time.GameTime
import indigo.shared.time.Seconds

object FakeSubSystemFrameContext:

def context(sides: Int): SubSystemFrameContext[Unit] =
SubSystemFrameContext(
def context(sides: Int): SubSystemContext[Unit] =
SubSystemContext(
GameTime.zero,
Dice.loaded(sides),
InputState.default,
new BoundaryLocator(new AnimationsRegister, new FontRegister, new DynamicText),
()
)

def context(sides: Int, time: Seconds): SubSystemFrameContext[Unit] =
SubSystemFrameContext(
def context(sides: Int, time: Seconds): SubSystemContext[Unit] =
SubSystemContext(
GameTime.is(time),
Dice.loaded(sides),
InputState.default,
new BoundaryLocator(new AnimationsRegister, new FontRegister, new DynamicText),
()
)

def context(sides: Int, time: Seconds, delta: Seconds): SubSystemFrameContext[Unit] =
SubSystemFrameContext(
def context(sides: Int, time: Seconds, delta: Seconds): SubSystemContext[Unit] =
SubSystemContext(
GameTime.withDelta(time, delta),
Dice.loaded(sides),
InputState.default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import indigo.platform.renderer.Renderer
import indigo.shared.AnimationsRegister
import indigo.shared.BoundaryLocator
import indigo.shared.FontRegister
import indigo.shared.FrameContext
import indigo.shared.Context
import indigo.shared.assets.AssetName
import indigo.shared.collections.Batch
import indigo.shared.constants.Key
Expand Down Expand Up @@ -276,8 +276,8 @@ class InputFieldTests extends munit.FunSuite {
KeyboardEvent.KeyUp(Key.KEY_C)
)

def context: FrameContext[Unit] =
new FrameContext[Unit](
def context: Context[Unit] =
new Context[Unit](
GameTime.zero,
Dice.loaded(1),
new InputState(Mouse.default, new Keyboard(keysUp, Batch.empty, None), Gamepad.default, Pointers.default),
Expand Down
6 changes: 3 additions & 3 deletions indigo/indigo/src/main/scala/indigo/IndigoDemo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ trait IndigoDemo[BootData, StartUpData, Model, ViewModel] extends GameLauncher[S
* A function that maps GlobalEvent's to the next version of your model, and encapsuates failures or resulting
* events within the Outcome wrapper.
*/
def updateModel(context: FrameContext[StartUpData], model: Model): GlobalEvent => Outcome[Model]
def updateModel(context: Context[StartUpData], model: Model): GlobalEvent => Outcome[Model]

/** A pure function for updating your game's view model in the context of the running frame and the events acting upon
* it.
Expand All @@ -101,7 +101,7 @@ trait IndigoDemo[BootData, StartUpData, Model, ViewModel] extends GameLauncher[S
* events within the Outcome wrapper.
*/
def updateViewModel(
context: FrameContext[StartUpData],
context: Context[StartUpData],
model: Model,
viewModel: ViewModel
): GlobalEvent => Outcome[ViewModel]
Expand All @@ -120,7 +120,7 @@ trait IndigoDemo[BootData, StartUpData, Model, ViewModel] extends GameLauncher[S
* A function that produces a description of what to present next, and encapsuates failures or resulting events
* within the Outcome wrapper.
*/
def present(context: FrameContext[StartUpData], model: Model, viewModel: ViewModel): Outcome[SceneUpdateFragment]
def present(context: Context[StartUpData], model: Model, viewModel: ViewModel): Outcome[SceneUpdateFragment]

private val subSystemsRegister: SubSystemsRegister[Model] =
new SubSystemsRegister()
Expand Down
6 changes: 3 additions & 3 deletions indigo/indigo/src/main/scala/indigo/IndigoGame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ trait IndigoGame[BootData, StartUpData, Model, ViewModel] extends GameLauncher[S
* A function that maps GlobalEvent's to the next version of your model, and encapsulates failures or resulting
* events within the Outcome wrapper.
*/
def updateModel(context: FrameContext[StartUpData], model: Model): GlobalEvent => Outcome[Model]
def updateModel(context: Context[StartUpData], model: Model): GlobalEvent => Outcome[Model]

/** A pure function for updating your game's view model in the context of the running frame and the events acting upon
* it.
Expand All @@ -122,7 +122,7 @@ trait IndigoGame[BootData, StartUpData, Model, ViewModel] extends GameLauncher[S
* resulting events within the Outcome wrapper.
*/
def updateViewModel(
context: FrameContext[StartUpData],
context: Context[StartUpData],
model: Model,
viewModel: ViewModel
): GlobalEvent => Outcome[ViewModel]
Expand All @@ -141,7 +141,7 @@ trait IndigoGame[BootData, StartUpData, Model, ViewModel] extends GameLauncher[S
* A function that produces a description of what to present next, and encapsulates failures or resulting events
* within the Outcome wrapper.
*/
def present(context: FrameContext[StartUpData], model: Model, viewModel: ViewModel): Outcome[SceneUpdateFragment]
def present(context: Context[StartUpData], model: Model, viewModel: ViewModel): Outcome[SceneUpdateFragment]

private val subSystemsRegister: SubSystemsRegister[Model] =
new SubSystemsRegister()
Expand Down
6 changes: 3 additions & 3 deletions indigo/indigo/src/main/scala/indigo/IndigoSandbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ trait IndigoSandbox[StartUpData, Model] extends GameLauncher[StartUpData, Model,
* A function that maps GlobalEvent's to the next version of your model, and encapsuates failures or resulting
* events within the Outcome wrapper.
*/
def updateModel(context: FrameContext[StartUpData], model: Model): GlobalEvent => Outcome[Model]
def updateModel(context: Context[StartUpData], model: Model): GlobalEvent => Outcome[Model]

/** A pure function for presenting your game. The result is a side effect free declaration of what you intend to be
* presented to the player next.
Expand All @@ -91,11 +91,11 @@ trait IndigoSandbox[StartUpData, Model] extends GameLauncher[StartUpData, Model,
* A function that produces a description of what to present next, and encapsuates failures or resulting events
* within the Outcome wrapper.
*/
def present(context: FrameContext[StartUpData], model: Model): Outcome[SceneUpdateFragment]
def present(context: Context[StartUpData], model: Model): Outcome[SceneUpdateFragment]

private def indigoGame: GameEngine[StartUpData, Model, Unit] = {

val updateViewModel: (FrameContext[StartUpData], Model, Unit) => GlobalEvent => Outcome[Unit] =
val updateViewModel: (Context[StartUpData], Model, Unit) => GlobalEvent => Outcome[Unit] =
(_, _, vm) => _ => Outcome(vm)

val eventFilters: EventFilters =
Expand Down
6 changes: 3 additions & 3 deletions indigo/indigo/src/main/scala/indigo/IndigoShader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ trait IndigoShader extends GameLauncher[IndigoShaderModel, IndigoShaderModel, Un
Outcome(startupData)

private def updateModel(
context: FrameContext[IndigoShaderModel],
context: Context[IndigoShaderModel],
model: IndigoShaderModel
): GlobalEvent => Outcome[IndigoShaderModel] = {
case ViewportResize(vp) =>
Expand All @@ -130,7 +130,7 @@ trait IndigoShader extends GameLauncher[IndigoShaderModel, IndigoShaderModel, Un
}

private def present(
context: FrameContext[IndigoShaderModel],
context: Context[IndigoShaderModel],
model: IndigoShaderModel
): Outcome[SceneUpdateFragment] =
Outcome(
Expand All @@ -155,7 +155,7 @@ trait IndigoShader extends GameLauncher[IndigoShaderModel, IndigoShaderModel, Un
boot: BootResult[IndigoShaderModel, IndigoShaderModel]
): GameEngine[IndigoShaderModel, IndigoShaderModel, Unit] = {

val updateViewModel: (FrameContext[IndigoShaderModel], IndigoShaderModel, Unit) => GlobalEvent => Outcome[Unit] =
val updateViewModel: (Context[IndigoShaderModel], IndigoShaderModel, Unit) => GlobalEvent => Outcome[Unit] =
(_, _, vm) => _ => Outcome(vm)

val eventFilters: EventFilters =
Expand Down
Loading

0 comments on commit fc4db79

Please sign in to comment.