Skip to content

Commit

Permalink
add scary type-safe getter
Browse files Browse the repository at this point in the history
  • Loading branch information
epic-64 committed Oct 20, 2024
1 parent c8457f9 commit 95a7002
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/main/scala/ScalaScape/ScalaScape.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ class ScalaScape(forceTerminal: Boolean):
while (running) {
val startTime = System.nanoTime()

update(state)
draw(graphics)
try {
update(state)
draw(graphics)
} catch {
case e: Exception =>
running = false
e.printStackTrace()
}
screen.refresh()

val endTime = System.nanoTime()
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/ScalaScape/components/GameState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GameState:
end GameState

class SkillList:
val woodcutting: Woodcutting = Woodcutting()
val woodCuttingOak: WoodCuttingOak = WoodCuttingOak()
val woodCuttingTeak: WoodCuttingTeak = WoodCuttingTeak()
val woodcutting: Woodcutting = Woodcutting()
// val woodCuttingOak: WoodCuttingOak = WoodCuttingOak()
// val woodCuttingTeak: WoodCuttingTeak = WoodCuttingTeak()
end SkillList
16 changes: 9 additions & 7 deletions src/main/scala/ScalaScape/components/Scenes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ class WoodCuttingOakScene extends SubSkillScene:
override val name = "Oak"
override val description = "Cut down some oak trees."

override def getSkill(state: GameState): WoodCuttingOak = state.skills.woodCuttingOak
override def asciiArt(pos: Pos): TerminalParagraph = WoodCuttingArtwork(pos)
override def previousScene: Scene = WoodCuttingMenuScene()
override def getSkill(state: GameState): WoodCuttingOak =
state.skills.woodcutting.subSkill[WoodCuttingOak]
override def asciiArt(pos: Pos): TerminalParagraph = WoodCuttingArtwork(pos)
override def previousScene: Scene = WoodCuttingMenuScene()
end WoodCuttingOakScene

class WoodCuttingTeakScene extends SubSkillScene:
override val name = "Teak"
override val description = "Cut down some teak trees."

override def getSkill(state: GameState): WoodCuttingTeak = state.skills.woodCuttingTeak
override def asciiArt(pos: Pos): TerminalParagraph = WoodCuttingArtwork(pos)
override def previousScene: Scene = WoodCuttingMenuScene()

override def getSkill(state: GameState): WoodCuttingTeak =
state.skills.woodcutting.subSkill[WoodCuttingTeak]
override def asciiArt(pos: Pos): TerminalParagraph = WoodCuttingArtwork(pos)
override def previousScene: Scene = WoodCuttingMenuScene()
end WoodCuttingTeakScene

class MiningMenuScene extends MenuScene:
Expand Down
17 changes: 13 additions & 4 deletions src/main/scala/ScalaScape/components/Skills.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ScalaScape.components

import com.googlecode.lanterna.TextColor.ANSI.*
import scala.reflect.ClassTag

trait Skill:
val name: String
Expand Down Expand Up @@ -102,6 +103,18 @@ trait SubSkill extends Skill:
end SubSkill

case class Woodcutting() extends Skill:
private val subSkills: List[SubSkill] = List(
WoodCuttingOak(),
WoodCuttingTeak(),
)

def subSkill[T <: SubSkill : ClassTag]: T =
val skill = subSkills.collectFirst { case skill: T => skill }
skill match {
case Some(s) => s
case None => throw new Exception(s"SubSkill ${implicitly[ClassTag[T]].runtimeClass.getSimpleName} not found.")
}

override val name: String = "Woodcutting"

override def onComplete(state: GameState, gainedXp: WidthInColumns): Unit =
Expand All @@ -114,8 +127,6 @@ class WoodCuttingOak() extends SubSkill:

override def parent(state: GameState): Woodcutting = state.skills.woodcutting

def getInventorySlot(state: GameState): InventoryItem = state.inventory.items("Oak")

override def onComplete(state: GameState, gainedXp: Int): Unit =
val key = "Oak"
val item: InventoryItem = state.inventory.items(key)
Expand All @@ -134,8 +145,6 @@ class WoodCuttingTeak() extends SubSkill:

override def parent(state: GameState): Woodcutting = state.skills.woodcutting

def getInventorySlot(state: GameState): InventoryItem = state.inventory.items("Teak")

override def onComplete(state: GameState, gainedXp: Int): Unit =
val key = "Teak"
val item: InventoryItem = state.inventory.items(key)
Expand Down

0 comments on commit 95a7002

Please sign in to comment.