Skip to content

Commit

Permalink
added gameinformation class
Browse files Browse the repository at this point in the history
Signed-off-by: J10a1n15 <45315647+j10a1n15@users.noreply.github.com>
  • Loading branch information
j10a1n15 committed Sep 27, 2024
1 parent 45f2ec3 commit 8305062
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
9 changes: 6 additions & 3 deletions src/main/kotlin/gay/j10a1n15/sillygames/games/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import gg.essential.elementa.UIComponent
abstract class Game {
abstract fun getDisplay(): UIComponent

abstract val name: String

open fun onTick() {}

open fun onKeyHeld(key: Int) {}

open fun onKeyPressed(key: Int): Boolean {
return false
}
}

open val supportsPictureInPicture = false
abstract class GameInformation {
abstract val name: String
abstract val description: String
open val icon: String? = null
open val supportsPictureInPicture = false
abstract val factory: () -> Game
}
9 changes: 6 additions & 3 deletions src/main/kotlin/gay/j10a1n15/sillygames/games/Snake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ class Snake : Game(), RpcProvider {
if (it in snake) randomLocation() else it
}

override val supportsPictureInPicture = true
override fun getRpcInfo() = rpc
}

object SnakeInformation : GameInformation() {
override val name = "Snake"

override fun getRpcInfo() = rpc
override val description = "A classic game of Snake"
override val factory = { Snake() }
override val supportsPictureInPicture = true
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gay.j10a1n15.sillygames.games.wordle

import gay.j10a1n15.sillygames.games.Game
import gay.j10a1n15.sillygames.games.GameInformation
import gay.j10a1n15.sillygames.rpc.RpcProvider
import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UIBlock
Expand Down Expand Up @@ -273,7 +274,12 @@ class Wordle : Game(), RpcProvider {
}
}

override fun getRpcInfo() = this.state.getRpcInfo()
}

object WordleInformation : GameInformation() {
override val name = "Wordle"
override val description = "Guess the word in 6 tries"
override val icon = "wordle_logo"
override fun getRpcInfo() = this.state.getRpcInfo()
override val factory = { Wordle() }
}
29 changes: 13 additions & 16 deletions src/main/kotlin/gay/j10a1n15/sillygames/screens/GameSelector.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gay.j10a1n15.sillygames.screens

import gay.j10a1n15.sillygames.games.Game
import gay.j10a1n15.sillygames.games.Snake
import gay.j10a1n15.sillygames.games.wordle.Wordle
import gay.j10a1n15.sillygames.games.GameInformation
import gay.j10a1n15.sillygames.games.SnakeInformation
import gay.j10a1n15.sillygames.games.wordle.WordleInformation
import gay.j10a1n15.sillygames.utils.SillyUtils.display
import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
Expand All @@ -25,11 +25,9 @@ import gg.essential.elementa.utils.withAlpha
import gg.essential.universal.GuiScale
import java.awt.Color

val gameFactories = listOf<() -> Game>(
{ Wordle() },
{ Snake() },
{ Snake() },
{ Wordle() },
val games = listOf(
SnakeInformation,
WordleInformation,
)

class GameSelector : WindowScreen(
Expand All @@ -51,23 +49,22 @@ class GameSelector : WindowScreen(
} childOf container

init {
gameFactories.chunked(2).forEachIndexed { row, factoryPair ->
factoryPair.forEachIndexed { index, factory ->
createButton(factory, row, index)
games.chunked(2).forEachIndexed { row, pair ->
pair.forEachIndexed { index, info ->
createButton(info, row, index)
}
}
}

private fun createButton(factory: () -> Game, row: Int, index: Int) {
val game = factory()
private fun createButton(game: GameInformation, row: Int, index: Int) {
val button = UIBlock().constrain {
x = (5 + 50 * index).percent()
y = (20 + 30 * row).percent()
width = 40.percent()
height = 20.percent()
color = Color(0x424242).toConstraint()
}.onMouseClick {
FullScreen(game).display()
FullScreen(game.factory()).display()
} childOf container

if (game.icon != null) {
Expand All @@ -85,7 +82,7 @@ class GameSelector : WindowScreen(
} childOf button
}

private fun addTextAndPipSupport(container: UIComponent, game: Game) {
private fun addTextAndPipSupport(container: UIComponent, game: GameInformation) {
val textContainer = UIContainer().constrain {
x = SiblingConstraint()
width = FillConstraint()
Expand All @@ -105,7 +102,7 @@ class GameSelector : WindowScreen(
height = 25.percent()
color = Color(0x000000).withAlpha(0.5f).toConstraint()
}.onMouseClick {
PictureInPicture.game = game
PictureInPicture.game = game.factory()
PictureInPicture.visible = true
} childOf textContainer
}
Expand Down

0 comments on commit 8305062

Please sign in to comment.