Skip to content

Commit

Permalink
WindowStateManager is no longer a GameComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed May 5, 2024
1 parent 97ee2d4 commit 93c17c7
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 65 deletions.
2 changes: 1 addition & 1 deletion roguelike-shared/src/main/scala/roguelike/GameEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package roguelike

import indigo.*
import indigo.shared.materials.Material.Bitmap
import roguelike.components.windows.WindowManagerCommand
import roguelike.model.Message
import roguelike.model.gamedata.Consumables
import roguelike.model.gamedata.Ranged
import roguelike.model.items.Item
import roguelike.windows.WindowManagerCommand

enum GameEvent extends GlobalEvent:
// Player events
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package roguelike.components.windows
package roguelike.windows

enum WindowManagerCommand:
case ShowQuit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import roguelike.ColorScheme
import roguelike.GameEvent
import roguelike.HostileEvent
import roguelike.components.GameComponent
import roguelike.components.windows.WindowManagerCommand
import roguelike.model.Message
import roguelike.model.entity.Hostile
import roguelike.model.entity.Orc
import roguelike.model.entity.Troll
import roguelike.viewmodel.ActorPosition
import roguelike.windows.WindowManagerCommand

object HostileComponent extends GameComponent[Size, Hostile, HostilesManager.HostilesVM]:
type Command = Cmds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import roguelike.ColorScheme
import roguelike.GameEvent
import roguelike.HostileEvent
import roguelike.components.GameComponent
import roguelike.components.windows.WindowManagerCommand
import roguelike.model.GameState
import roguelike.model.Inventory
import roguelike.model.Message
Expand All @@ -19,6 +18,7 @@ import roguelike.viewmodel.ActorDirection
import roguelike.viewmodel.ActorMoveState
import roguelike.viewmodel.ActorPosition
import roguelike.viewmodel.GameViewModel
import roguelike.windows.WindowManagerCommand

object PlayerComponent extends GameComponent[Size, Model, GameViewModel]:
type ComponentModel = PlayerM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import roguelike.model.Model
import roguelike.model.entity.Collectable
import roguelike.scenes.UIElements
import roguelike.viewmodel.GameViewModel
import roguelike.windows.WindowManagerCommand
import roguelikestarterkit.*

object DropMenu extends GameComponent[Size, Model, GameViewModel]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import roguelike.model.Inventory
import roguelike.model.Model
import roguelike.scenes.UIElements
import roguelike.viewmodel.GameViewModel
import roguelike.windows.WindowManagerCommand
import roguelikestarterkit.*

object EquipMenu extends GameComponent[Size, Model, GameViewModel]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import roguelike.model.Message
import roguelike.model.Model
import roguelike.model.entity.Player
import roguelike.viewmodel.GameViewModel
import roguelike.windows.WindowManagerCommand
import roguelikestarterkit.*

object LevelUp extends GameComponent[Size, Model, GameViewModel]:
Expand Down
8 changes: 4 additions & 4 deletions roguelike/src/main/scala/roguelike/model/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import roguelike.InventoryEvent
import roguelike.RogueLikeGame
import roguelike.components.entities.HostilesManager
import roguelike.components.entities.PlayerComponent
import roguelike.components.windows.ActiveWindow
import roguelike.components.windows.WindowManagerCommand
import roguelike.components.windows.WindowStateManager
import roguelike.model.dungeon.Dungeon
import roguelike.model.entity.*
import roguelike.model.gamedata.Consumables
import roguelike.model.gamedata.Ranged
import roguelike.windows.ActiveWindow
import roguelike.windows.WindowManagerCommand
import roguelike.windows.WindowStateManager

import scala.scalajs.js

Expand Down Expand Up @@ -183,7 +183,7 @@ final case class Model(
pickUp

case GameEvent.WindowEvent(command) =>
WindowStateManager.updateModel(context, this, command)
WindowStateManager.updateModel(context, this)(command)

case GameEvent.Log(message) =>
Outcome(
Expand Down
32 changes: 11 additions & 21 deletions roguelike/src/main/scala/roguelike/scenes/GameSceneUpdate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import indigo.scenes.SceneContext
import roguelike.ColorScheme
import roguelike.GameEvent
import roguelike.components.entities.HostilesManager
import roguelike.components.windows.*
import roguelike.model.GameState
import roguelike.model.Message
import roguelike.model.Model
import roguelike.model.gamedata.KeyMapping
import roguelike.windows.HistoryWindow
import roguelike.windows.WindowManagerCommand
import roguelike.windows.WindowStateManager
import roguelikestarterkit.WindowEvent

object GameSceneUpdate:
Expand All @@ -37,22 +38,21 @@ object GameSceneUpdate:
if !model.gameState.isWaitForInput ||
WindowStateManager.showingCloseableWindow(model) =>
WindowStateManager
.updateModel(context, model, WindowManagerCommand.CloseAll)
.updateModel(context, model)(WindowManagerCommand.CloseAll)
.map(_.closeAllWindows)

case WindowEvent.Closed(HistoryWindow.windowId) =>
WindowStateManager
.updateModel(context, model, WindowManagerCommand.CloseAll)
.updateModel(context, model)(WindowManagerCommand.CloseAll)
.map(_.closeAllWindows)

// Quit window toggle
case KeyboardEvent.KeyUp(KeyMapping.Quit1) | KeyboardEvent.KeyUp(KeyMapping.Quit2)
if model.gameState.isWaitForInput =>
WindowStateManager.updateModel(
context,
model,
WindowManagerCommand.ShowQuit
)
model
)(WindowManagerCommand.ShowQuit)

// Looking around
case KeyboardEvent.KeyDown(KeyMapping.MoveUp) if model.gameState.lookingAround =>
Expand Down Expand Up @@ -110,44 +110,34 @@ object GameSceneUpdate:
case KeyboardEvent.KeyUp(KeyMapping.ViewHistory)
if model.gameState.isWaitForInput ||
!WindowStateManager.showingWindow(model) =>
WindowStateManager.updateModel(
context,
model,
WindowStateManager.updateModel(context, model)(
WindowManagerCommand.ShowHistory
)
case KeyboardEvent.KeyUp(KeyMapping.ViewControls)
if model.gameState.isWaitForInput ||
!WindowStateManager.showingWindow(model) =>
WindowStateManager.updateModel(
context,
model,
WindowStateManager.updateModel(context, model)(
WindowManagerCommand.ShowControls
)

case KeyboardEvent.KeyUp(KeyMapping.Inventory)
if model.gameState.isWaitForInput ||
!WindowStateManager.showingWindow(model) =>
WindowStateManager.updateModel(
context,
model,
WindowStateManager.updateModel(context, model)(
WindowManagerCommand.ShowInventoryMenu
)

case KeyboardEvent.KeyUp(KeyMapping.Equipment)
if model.gameState.isWaitForInput ||
!WindowStateManager.showingWindow(model) =>
WindowStateManager.updateModel(
context,
model,
WindowStateManager.updateModel(context, model)(
WindowManagerCommand.ShowEquipMenu
)

case KeyboardEvent.KeyUp(KeyMapping.Drop)
if model.gameState.isWaitForInput ||
!WindowStateManager.showingWindow(model) =>
WindowStateManager.updateModel(
context,
model,
WindowStateManager.updateModel(context, model)(
WindowManagerCommand.ShowDropMenu
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import roguelike.GameEvent
import roguelike.InventoryEvent
import roguelike.components.entities.HostilesManager
import roguelike.components.entities.PlayerComponent
import roguelike.components.windows.WindowStateManager
import roguelike.model.GameTile
import roguelike.model.Model
import roguelike.model.entity.Collectable
import roguelike.model.entity.Player
import roguelike.model.gamedata.KeyMapping
import roguelike.subsystems.FloatingMessage
import roguelike.viewmodel.ui.*
import roguelike.windows.WindowStateManager

import scala.scalajs.js
import scala.scalajs.js.JSConverters.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package roguelike.windows

import indigo.*
import roguelike.GameEvent
import roguelike.components.windows.WindowManagerCommand
import roguelike.model.GameWindowContext
import roguelikestarterkit.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package roguelike.components.windows
package roguelike.windows

import indigo.*
import indigo.scenes.SceneContext
import roguelike.GameEvent
import roguelike.components.GameComponent
import roguelike.model.Model
import roguelike.viewmodel.GameViewModel
import roguelike.windows.ControlsWindow
import roguelike.windows.HistoryWindow
import roguelike.windows.QuitSaveWindow
import roguelikestarterkit.WindowEvent

object WindowStateManager extends GameComponent[Size, Model, GameViewModel]:
type Command = WindowManagerCommand
type ComponentModel = Model
type ComponentViewModel = GameViewModel

def modelLens: Lens[Model, Model] =
Lens.keepLatest

def viewModelLens: Lens[GameViewModel, GameViewModel] =
Lens.keepLatest
object WindowStateManager:

val updateActive: Lens[Model, ActiveWindow] =
Lens(
_.activeWindow,
(m, next) => m.copy(activeWindow = next)
)

def nextModel(
def updateModel(
context: SceneContext[Size],
model: Model
): WindowManagerCommand => Outcome[Model] =
Expand Down Expand Up @@ -73,25 +59,11 @@ object WindowStateManager extends GameComponent[Size, Model, GameViewModel]:
Outcome(updateActive.set(model.closeAllWindows, ActiveWindow.None))
.addGlobalEvents(closeActive)

def nextViewModel(
context: SceneContext[Size],
model: Model,
viewModel: GameViewModel
): WindowManagerCommand => Outcome[GameViewModel] =
_ => Outcome(viewModel)

def view(
context: SceneContext[Size],
model: Model,
viewModel: GameViewModel
): Outcome[Batch[SceneNode]] =
Outcome(Batch.empty)

val initialModel: ActiveWindow =
ActiveWindow.None

def showingWindow(model: Model): Boolean =
modelLens.get(model).activeWindow match
model.activeWindow match
case ActiveWindow.None => false
case _ => true

Expand All @@ -102,10 +74,10 @@ object WindowStateManager extends GameComponent[Size, Model, GameViewModel]:
showingWindow(model) && isCloseable(model)

def isCloseable(model: Model): Boolean =
modelLens.get(model).activeWindow.closeable
model.activeWindow.closeable

def isUnCloseable(model: Model): Boolean =
!modelLens.get(model).activeWindow.closeable
!model.activeWindow.closeable

enum ActiveWindow(val closeable: Boolean):
case None extends ActiveWindow(true)
Expand Down

0 comments on commit 93c17c7

Please sign in to comment.