Skip to content

Commit

Permalink
Prevent game clicks when mouse over windows
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed May 11, 2024
1 parent 689ecf4 commit 9c44c5d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
13 changes: 10 additions & 3 deletions roguelike/src/main/scala/roguelike/model/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import roguelike.model.gamedata.Ranged
import roguelike.windows.ActiveWindow
import roguelike.windows.WindowManagerCommand
import roguelike.windows.WindowStateManager
import roguelikestarterkit.WindowId

import scala.scalajs.js

Expand All @@ -34,11 +35,15 @@ final case class Model(
autoMovePath: Batch[Point],
activeWindow: ActiveWindow,
collectables: Batch[Collectable],
hostiles: HostilesPool
hostiles: HostilesPool,
mouseOverWindows: Batch[WindowId]
):
val gameWindowContext: GameWindowContext =
GameWindowContext(currentFloor, player, messageLog)

lazy val gameClickAllowed: Boolean =
mouseOverWindows.isEmpty

def entitiesList: js.Array[Entity] =
(collectables.toJSArray ++
hostiles.toJSArray.sortBy(_.isAlive))
Expand Down Expand Up @@ -499,7 +504,8 @@ object Model:
Batch.empty,
WindowStateManager.initialModel,
Batch.empty,
HostilesPool(Batch.empty)
HostilesPool(Batch.empty),
Batch.empty
)

def fromSaveData(model: Model, saveData: ModelSaveData): Model =
Expand Down Expand Up @@ -555,7 +561,8 @@ object Model:
Batch.empty,
WindowStateManager.initialModel,
Batch.fromList(dungeon.collectables),
HostilesPool(Batch.fromList(dungeon.hostiles))
HostilesPool(Batch.fromList(dungeon.hostiles)),
Batch.empty
)
}
}
13 changes: 12 additions & 1 deletion roguelike/src/main/scala/roguelike/scenes/GameSceneUpdate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ object GameSceneUpdate:
context: SceneContext[Size],
model: Model
): GlobalEvent => Outcome[Model] =
case WindowEvent.MouseOver(id) =>
Outcome(model.copy(mouseOverWindows = id :: model.mouseOverWindows))

case WindowEvent.MouseOut(id) =>
Outcome(model.copy(mouseOverWindows = model.mouseOverWindows.filterNot(_ == id)))

// Window close keys
case KeyboardEvent.KeyUp(KeyMapping.CloseWindow)
if !model.gameState.isWaitForInput ||
Expand All @@ -42,10 +48,15 @@ object GameSceneUpdate:
.map(_.closeAllWindows)

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

case WindowEvent.Closed(id) =>
Outcome(model.copy(mouseOverWindows = model.mouseOverWindows.filterNot(_ == id)))

// Quit window toggle
case KeyboardEvent.KeyUp(KeyMapping.Quit1) | KeyboardEvent.KeyUp(KeyMapping.Quit2)
if model.gameState.isWaitForInput =>
Expand Down
6 changes: 4 additions & 2 deletions roguelike/src/main/scala/roguelike/viewmodel/ViewModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ final case class GameViewModel(
case _: MouseEvent.Click
if !WindowStateManager.showingWindow(model) &&
model.player.position == hoverSquare &&
model.gameState.isWaitForInput =>
model.gameState.isWaitForInput &&
model.gameClickAllowed =>
Outcome(this)
.addGlobalEvents(GameEvent.PlayerTryPickUp)

case _: MouseEvent.Click
if !WindowStateManager.showingWindow(model) &&
model.gameState.isWaitForInput =>
model.gameState.isWaitForInput &&
model.gameClickAllowed =>
Outcome(this)
.addGlobalEvents(GameEvent.PlayerMoveTowards(hoverSquare))

Expand Down

0 comments on commit 9c44c5d

Please sign in to comment.