Skip to content

Commit

Permalink
Level Up Window
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed May 11, 2024
1 parent 1a6e806 commit 689ecf4
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 109 deletions.
5 changes: 5 additions & 0 deletions roguelike-shared/src/main/scala/roguelike/GameEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ enum GameEvent extends GlobalEvent:
// ViewModel events
case CameraSnapToPlayer

// Stats
case ModifyPower(amount: Int)
case ModifyMaxHp(amount: Int)
case ModifyDefense(amount: Int)

enum HostileEvent:
case HostileMeleeAttack(attackerName: String, power: Int)
case HostileGiveXP(amount: Int)
Expand Down
106 changes: 0 additions & 106 deletions roguelike/src/main/scala/roguelike/components/windows/LevelUp.scala

This file was deleted.

9 changes: 9 additions & 0 deletions roguelike/src/main/scala/roguelike/model/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ final case class Model(
}

def update(context: SceneContext[Size]): GameEvent => Outcome[Model] =
case GameEvent.ModifyDefense(amount) =>
player.increaseDefense(amount).map(p => this.copy(player = p))

case GameEvent.ModifyMaxHp(amount) =>
player.increaseMaxHp(amount).map(p => this.copy(player = p))

case GameEvent.ModifyPower(amount) =>
player.increasePower(amount).map(p => this.copy(player = p))

case GameEvent.GenerateLevel =>
Outcome(this)

Expand Down
6 changes: 6 additions & 0 deletions roguelike/src/main/scala/roguelike/scenes/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ object GameScene extends Scene[Size, Model, ViewModel]:
GameWindows.defaultCharSheet
)
)
.register(
LevelUpWindow.window(
Dimensions(1280, 720) / 10,
GameWindows.defaultCharSheet
)
)
.open(
InfoPanel.windowId,
MenuWindow.windowId
Expand Down
9 changes: 9 additions & 0 deletions roguelike/src/main/scala/roguelike/viewmodel/ViewModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ final case class GameViewModel(
context: SceneContext[Size],
model: Model
): GameEvent => Outcome[GameViewModel] =
case GameEvent.ModifyDefense(_) =>
Outcome(this)

case GameEvent.ModifyMaxHp(_) =>
Outcome(this)

case GameEvent.ModifyPower(_) =>
Outcome(this)

case GameEvent.GenerateLevel =>
Outcome(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ object ControlsWindow:
def cascade(model: Unit, newBounds: Bounds): Unit =
model

def refresh(model: Unit): Unit =
model

private val separator = " | "
private val commandsAndValues = KeyMapping.helpText.map(p => (p._1, p._2.mkString(separator)))
private val longest = KeyMapping.longestMappings + separator.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ object HistoryWindow:

def cascade(model: TerminalClones, newBounds: Bounds): TerminalClones =
model

def refresh(model: TerminalClones): TerminalClones =
model
3 changes: 3 additions & 0 deletions roguelike/src/main/scala/roguelike/windows/InfoPanel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ object InfoPanel:
def cascade(model: Unit, newBounds: Bounds): Unit =
model

def refresh(model: Unit): Unit =
model

def renderBar(charSheet: CharSheet, player: Player, totalWidth: Int, position: Point): Group =
val height = charSheet.size.height + 4
val width = charSheet.size.width * totalWidth
Expand Down
120 changes: 120 additions & 0 deletions roguelike/src/main/scala/roguelike/windows/LevelUpWindow.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package roguelike.windows

import indigo.*
import roguelike.GameEvent
import roguelike.model.GameWindowContext
import roguelikestarterkit.*

object LevelUpWindow:

val windowId: WindowId = WindowId("Level up")

private val windowSize = Dimensions(40, 10)

def window(
screenSize: Dimensions,
charSheet: CharSheet
): WindowModel[LevelUpMenu, GameWindowContext] =
WindowModel(
windowId,
charSheet,
LevelUpMenu.initial(charSheet)
)
.withTitle("Level Up!")
.moveTo(((screenSize - windowSize) / 2).toCoords)
.resizeTo(windowSize)

final case class LevelUpMenu(components: ComponentGroup[GameWindowContext])
object LevelUpMenu:

def initial(charSheet: CharSheet): LevelUpMenu =
val buttonTheme =
Button.Theme(
charSheet,
RGBA.Silver -> RGBA.Black,
RGBA.White -> RGBA.Black,
RGBA.Black -> RGBA.White
)

LevelUpMenu(
ComponentGroup(Bounds(0, 0, 16, 1))
.withLayout(ComponentLayout.Vertical())
.add(
Label("Congratulations! You level up!", Label.Theme(charSheet)),
Label("Select an attribute to increase.", Label.Theme(charSheet)),
Label("", Label.Theme(charSheet))
)
.add(
Button(
(ctx: GameWindowContext) =>
s"[1] Constitution (+20 HP, from ${ctx.player.fighter.maxHp})",
buttonTheme
).onClick(
GameEvent.ModifyMaxHp(20),
GameEvent.WindowEvent(WindowManagerCommand.CloseAll)
),
Button(
(ctx: GameWindowContext) =>
s"[2] Strength (+1 attack, from ${ctx.player.fighter.power})",
buttonTheme
).onClick(
GameEvent.ModifyPower(1),
GameEvent.WindowEvent(WindowManagerCommand.CloseAll)
),
Button(
(ctx: GameWindowContext) =>
s"[3] Agility (+1 defense, from ${ctx.player.fighter.defense})",
buttonTheme
).onClick(
GameEvent.ModifyDefense(1),
GameEvent.WindowEvent(WindowManagerCommand.CloseAll)
)
)
)

given (using
cg: WindowContent[ComponentGroup[GameWindowContext], GameWindowContext]
): WindowContent[LevelUpMenu, GameWindowContext] with

def updateModel(
context: UiContext[GameWindowContext],
model: LevelUpMenu
): GlobalEvent => Outcome[LevelUpMenu] =

// Constitution
case KeyboardEvent.KeyUp(Key.KEY_1) =>
Outcome(
model,
Batch(GameEvent.ModifyMaxHp(20), GameEvent.WindowEvent(WindowManagerCommand.CloseAll))
)

// Strength
case KeyboardEvent.KeyUp(Key.KEY_2) =>
Outcome(
model,
Batch(GameEvent.ModifyPower(1), GameEvent.WindowEvent(WindowManagerCommand.CloseAll))
)

// Agility
case KeyboardEvent.KeyUp(Key.KEY_3) =>
Outcome(
model,
Batch(GameEvent.ModifyDefense(1), GameEvent.WindowEvent(WindowManagerCommand.CloseAll))
)

case e =>
cg.updateModel(context, model.components)(e).map { c =>
model.copy(components = c)
}

def present(
context: UiContext[GameWindowContext],
model: LevelUpMenu
): Outcome[Layer] =
cg.present(context, model.components)

def cascade(model: LevelUpMenu, newBounds: Bounds): LevelUpMenu =
model.copy(components = cg.cascade(model.components, newBounds))

def refresh(model: LevelUpMenu): LevelUpMenu =
model.copy(components = cg.refresh(model.components))
5 changes: 4 additions & 1 deletion roguelike/src/main/scala/roguelike/windows/MenuWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object MenuWindow:
)
.isStatic

final case class MainMenu(components: ComponentGroup)
final case class MainMenu(components: ComponentGroup[GameWindowContext])
object MainMenu:

def initial(charSheet: CharSheet): MainMenu =
Expand Down Expand Up @@ -81,3 +81,6 @@ object MainMenu:

def cascade(model: MainMenu, newBounds: Bounds): MainMenu =
model.copy(components = model.components.cascade(newBounds))

def refresh(model: MainMenu): MainMenu =
model.copy(components = model.components.reflow)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object QuitSaveWindow:
.moveTo(((screenSize - windowSize) / 2).toCoords)
.resizeTo(windowSize)

final case class QuitMenu(components: ComponentGroup)
final case class QuitMenu(components: ComponentGroup[GameWindowContext])
object QuitMenu:

def initial(charSheet: CharSheet): QuitMenu =
Expand Down Expand Up @@ -96,3 +96,6 @@ object QuitMenu:

def cascade(model: QuitMenu, newBounds: Bounds): QuitMenu =
model.copy(components = model.components.cascade(newBounds))

def refresh(model: QuitMenu): QuitMenu =
model.copy(components = model.components.reflow)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object WindowStateManager:

case WindowManagerCommand.ShowLevelUp =>
Outcome(updateActive.set(model.pauseForWindow, ActiveWindow.LevelUp))
.addGlobalEvents(WindowEvent.Open(LevelUpWindow.windowId))

case WindowManagerCommand.ShowDropMenu =>
Outcome(updateActive.set(model.pauseForWindow, ActiveWindow.DropMenu))
Expand All @@ -49,7 +50,7 @@ object WindowStateManager:
model.activeWindow match
case ActiveWindow.None => Batch.empty
case ActiveWindow.Quit => Batch(WindowEvent.Close(QuitSaveWindow.windowId))
case ActiveWindow.LevelUp => Batch()
case ActiveWindow.LevelUp => Batch(WindowEvent.Close(LevelUpWindow.windowId))
case ActiveWindow.DropMenu => Batch()
case ActiveWindow.EquipMenu => Batch()
case ActiveWindow.History => Batch(WindowEvent.Close(HistoryWindow.windowId))
Expand Down

0 comments on commit 689ecf4

Please sign in to comment.