Skip to content

Commit

Permalink
Drop window nearly works!
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jun 12, 2024
1 parent d382e7d commit fc34983
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 188 deletions.
3 changes: 2 additions & 1 deletion roguelike-shared/src/main/scala/roguelike/GameEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ enum HostileEvent:

enum InventoryEvent:
case UseConsumables(consumables: Consumables)
case DropItem(item: Item, mapPosition: Point)
case DropItem(inventoryPosition: Int)
case DroppedItem(item: Item, mapPosition: Point)
case UseRanged(inventoryPosition: Int, ranged: Ranged)
case TargetUsingRanged(inventoryPosition: Int, ranged: Ranged)
case PickedUp(item: Item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ final case class Inventory(backpack: Backpack, equipment: Equipment):
Outcome(
next,
Batch(
GameEvent.Inventory(InventoryEvent.DropItem(item, mapPosition)),
GameEvent.Inventory(InventoryEvent.DroppedItem(item, mapPosition)),
GameEvent.Log(
Message(
s"You dropped the ${item.name}.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class InventoryTests extends munit.FunSuite {
assert(actual.item(1) == Some(Ranged.FireballScroll))
assert(
events.head == GameEvent.Inventory(
InventoryEvent.DropItem(Ranged.LightningScroll, Point.zero)
InventoryEvent.DroppedItem(Ranged.LightningScroll, Point.zero)
)
)
assert(events.reverse.head == GameEvent.PlayerTurnEnd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ object PlayerComponent extends GameComponent[Size, Model, GameViewModel]:
context: SceneContext[Size],
model: PlayerM
): Cmds => Outcome[PlayerM] =
case Cmds.RemoveFromInvetory(at) =>
case Cmds.DropItem(at) =>
model.player.inventory
.drop(at, model.player.position)
.map { inventory =>
inventoryLens.modify(model.player, _ => inventory)
}
.map { p =>
model.next(p)
}

case Cmds.RemoveFromInventory(at) =>
Outcome(model.next(inventoryLens.modify(model.player, _.remove(at))))

case Cmds.GiveToPlayer(i) =>
Expand Down Expand Up @@ -235,7 +245,8 @@ object PlayerComponent extends GameComponent[Size, Model, GameViewModel]:

enum Cmds:
case Update
case RemoveFromInvetory(at: Int)
case RemoveFromInventory(at: Int)
case GiveToPlayer(item: Item)
case UseConsumable(c: Consumables)
case HostileInteraction(e: HostileEvent)
case DropItem(at: Int)
136 changes: 0 additions & 136 deletions roguelike/src/main/scala/roguelike/components/windows/DropMenu.scala

This file was deleted.

3 changes: 2 additions & 1 deletion roguelike/src/main/scala/roguelike/model/GameWindows.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package roguelike.model

import indigo.*
import roguelike.assets.GameAssets
import roguelike.model.entity.Collectable
import roguelike.model.entity.Player
import roguelikestarterkit.*

Expand All @@ -15,4 +16,4 @@ object GameWindows:
RoguelikeTiles.Size10x10.Fonts.fontKey
)

final case class GameWindowContext(currentFloor: Int, player: Player, messageLog: MessageLog)
final case class GameWindowContext(currentFloor: Int, player: Player, standingOn: Option[Collectable], messageLog: MessageLog)
18 changes: 15 additions & 3 deletions roguelike/src/main/scala/roguelike/model/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ final case class Model(
mouseOverWindows: Batch[WindowId]
):
val gameWindowContext: GameWindowContext =
GameWindowContext(currentFloor, player, messageLog)
GameWindowContext(
currentFloor,
player,
collectables.find(_.position == player.position),
messageLog
)

lazy val gameClickAllowed: Boolean =
mouseOverWindows.isEmpty
Expand Down Expand Up @@ -71,11 +76,18 @@ final case class Model(
def handleInventoryEvent(
context: SceneContext[Size]
): InventoryEvent => Outcome[Model] = {
case InventoryEvent.DropItem(at) =>
PlayerComponent.updateModel(
context,
this,
PlayerComponent.Cmds.DropItem(at)
)

case InventoryEvent.RemoveFromInventory(at) =>
PlayerComponent.updateModel(
context,
this,
PlayerComponent.Cmds.RemoveFromInvetory(at)
PlayerComponent.Cmds.RemoveFromInventory(at)
)

case InventoryEvent.UseConsumables(c) =>
Expand All @@ -86,7 +98,7 @@ final case class Model(
PlayerComponent.Cmds.UseConsumable(c)
)

case InventoryEvent.DropItem(item, mapPosition) =>
case InventoryEvent.DroppedItem(item, mapPosition) =>
Outcome(
this.copy(
collectables = Collectable(mapPosition, item) :: collectables
Expand Down
3 changes: 1 addition & 2 deletions roguelike/src/main/scala/roguelike/scenes/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ object GameScene extends Scene[Size, Model, ViewModel]:
)
.open(
InfoPanel.windowId,
MenuWindow.windowId,
DropWindow.windowId
MenuWindow.windowId
)
)

Expand Down
5 changes: 4 additions & 1 deletion roguelike/src/main/scala/roguelike/viewmodel/ViewModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ final case class GameViewModel(
)
)

case GameEvent.Inventory(InventoryEvent.DropItem(item, _)) =>
case GameEvent.Inventory(InventoryEvent.DropItem(_)) =>
Outcome(this)

case GameEvent.Inventory(InventoryEvent.DroppedItem(item, _)) =>
Outcome(this)
.addGlobalEvents(
FloatingMessage.spawnEvent(
Expand Down
90 changes: 55 additions & 35 deletions roguelike/src/main/scala/roguelike/windows/DropWindow.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package roguelike.windows

import indigo.*
import roguelike.ColorScheme
import roguelike.GameEvent
import roguelike.InventoryEvent
import roguelike.model.GameWindowContext
import roguelike.model.Message
import roguelike.scenes.UIElements
import roguelikestarterkit.*
import roguelikestarterkit.syntax.*
Expand All @@ -25,33 +29,43 @@ object DropWindow:
.moveTo(((screenSize - windowSize) / 2).toCoords)
.resizeTo(windowSize)

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

def initial(charSheet: CharSheet): DropMenu =
DropMenu(
ComponentGroup()
.withLayout(ComponentLayout.Vertical())
.add(
TextArea(
(ctx: GameWindowContext) =>
if ctx.player.inventory.backpack.count > 0 then
val collectables: String =
ctx.player.inventory.backpack.items
.map(_.name)
.zip(UIElements.letters)
.foldLeft(("", 0)) { case ((str, r), (collectableName, letter)) =>
(str + s"[$letter] $collectableName\n", r + 1)
}
._1

"Select a collectable to drop\n\n" + collectables
else s"""Select an collectable to drop
|
|(Empty)
|""".stripMargin,
TextArea.Theme(charSheet)
)
ComponentList(Dimensions(35, 22)) { (_: GameWindowContext) =>
Batch(
Label[GameWindowContext]("Select a collectable to drop", Label.Theme(charSheet))
)
}
.withLayout(ComponentLayout.Vertical(Padding.zero.withBottom(1)))
.add(ctx =>
if ctx.player.inventory.backpack.count > 0 then Batch.empty
else
Batch(
Label(
"(Your inventory is empty.)",
Label.Theme(charSheet)
)
)
)
.add((ctx: GameWindowContext) =>
if ctx.player.inventory.backpack.count > 0 then
val collectables: Batch[String] =
ctx.player.inventory.backpack.items
.map(_.name)
.zip(UIElements.letters)
.map { case (name, letter) =>
s"[$letter] $name"
}

collectables.map { label =>
Button[GameWindowContext](label, Button.Theme(charSheet))
// TODO: .onClick(GameEvent.Inventory(InventoryEvent.DropItem))
// Also there is a bug where opening the drop window with the keyboard drops entry 'd' from the inventory
}
else Batch.empty
)
)

Expand All @@ -61,18 +75,24 @@ object DropMenu:
context: UIContext[GameWindowContext],
model: DropMenu
): GlobalEvent => Outcome[DropMenu] =

// // Save
// case KeyboardEvent.KeyUp(Key.KEY_1) =>
// Outcome(model, Batch(GameEvent.SaveGame))

// // Save and Quit
// case KeyboardEvent.KeyUp(Key.KEY_2) =>
// Outcome(model, Batch(GameEvent.SaveGame, SceneEvent.JumpTo(MainMenuScene.name)))

// // Quit
// case KeyboardEvent.KeyUp(Key.KEY_3) =>
// Outcome(model, Batch(SceneEvent.JumpTo(MainMenuScene.name)))
case KeyboardEvent.KeyUp(key) =>
UIElements.letterPositions.get(key.key) match
case None =>
Outcome(model)

case Some(_) if context.reference.standingOn.isDefined =>
Outcome(model)
.addGlobalEvents(
GameEvent.Log(Message("Cannot drop here.", ColorScheme.invalid)),
GameEvent.WindowEvent(WindowManagerCommand.CloseAll)
)

case Some(keyIndex) =>
Outcome(model)
.addGlobalEvents(
GameEvent.Inventory(InventoryEvent.DropItem(keyIndex)),
GameEvent.WindowEvent(WindowManagerCommand.CloseAll)
)

case e =>
model.components.update(context)(e).map { c =>
Expand Down
Loading

0 comments on commit fc34983

Please sign in to comment.