Skip to content

Commit

Permalink
Game events :3
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatGravyBoat committed Sep 18, 2024
1 parent 56dbec7 commit 550830d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ gradle-app.setting
.vscode/settings.json
bin/
eclipse-formatter.xml
/.vscode/
/.vscode/

.kotlin
4 changes: 4 additions & 0 deletions src/main/kotlin/gay/j10a1n15/sillygames/games/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ import gg.essential.elementa.UIComponent

abstract class Game {
abstract fun getDisplay(): UIComponent

open fun onTick() {}

open fun onKeyClick(key: Int) {}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/gay/j10a1n15/sillygames/games/GameEventManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gay.j10a1n15.sillygames.games

import gay.j10a1n15.sillygames.events.Events

object GameEventManager {

private var game: Game? = null

init {
Events.TICK.register { game?.onTick() }
Events.KEYBOARD.register { game?.onKeyClick(it) }
}

fun setGame(game: Game?) {
this.game = game
}
}
10 changes: 2 additions & 8 deletions src/main/kotlin/gay/j10a1n15/sillygames/games/Snake.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gay.j10a1n15.sillygames.games

import gay.j10a1n15.sillygames.events.Events
import gay.j10a1n15.sillygames.utils.Vector2d
import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UIBlock
Expand All @@ -17,11 +16,6 @@ import java.awt.Color

class Snake : Game() {

init {
Events.TICK.register { onTick() }
Events.KEYBOARD.register { onKeyClick(it) }
}

private val gridSize = 20
private val gridWidth = 30
private val gridHeight = 20
Expand All @@ -35,7 +29,7 @@ class Snake : Game() {
private val gameSpeed = 200L
private var lastUpdateTime = System.currentTimeMillis()

private fun onTick() {
override fun onTick() {
if (gameOver.get()) return
if (snake.isEmpty()) return

Expand All @@ -59,7 +53,7 @@ class Snake : Game() {
}.toMutableList()
}

private fun onKeyClick(key: Int) {
override fun onKeyClick(key: Int) {
if (gameOver.get()) return

val newDirection = when (key) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/gay/j10a1n15/sillygames/screens/FullScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gay.j10a1n15.sillygames.screens

import gay.j10a1n15.sillygames.games.Game
import gay.j10a1n15.sillygames.games.GameEventManager
import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.WindowScreen
import gg.essential.elementa.components.UIBlock
Expand All @@ -13,6 +14,11 @@ import gg.essential.elementa.utils.invisible
import java.awt.Color

class FullScreen(private val element: Game) : WindowScreen(ElementaVersion.V5) {

init {
GameEventManager.setGame(element)
}

private val container = UIBlock().constrain {
x = CenterConstraint()
y = CenterConstraint()
Expand All @@ -37,5 +43,10 @@ class FullScreen(private val element: Game) : WindowScreen(ElementaVersion.V5) {
height = 100.percent
} childOf container
}

override fun onScreenClose() {
super.onScreenClose()
GameEventManager.setGame(null)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gay.j10a1n15.sillygames.screens

import gay.j10a1n15.sillygames.events.Events
import gay.j10a1n15.sillygames.games.Game
import gay.j10a1n15.sillygames.games.GameEventManager
import gay.j10a1n15.sillygames.games.Snake
import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.WindowScreen
Expand All @@ -21,7 +22,15 @@ object PictureInPicture : WindowScreen(ElementaVersion.V5) {
}

var game: Game? = Snake()
set(value) {
field = value
updateEventListeners()
}
var visible = false
set(value) {
field = value
updateEventListeners()
}

private fun onRender(matrix: UMatrixStack) {
if (!visible) return
Expand All @@ -40,4 +49,8 @@ object PictureInPicture : WindowScreen(ElementaVersion.V5) {

window.draw(matrix)
}

private fun updateEventListeners() {
GameEventManager.setGame(if (visible) game else null)
}
}

0 comments on commit 550830d

Please sign in to comment.