Skip to content

Commit

Permalink
Refactor: Improved Channel code
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Apr 22, 2024
1 parent 706978a commit 6f9ba6c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
8 changes: 8 additions & 0 deletions common/src/main/scala/hexacraft/util/events.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hexacraft.util

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag

trait Tracker[E] {
def notify(event: E): Unit
Expand Down Expand Up @@ -85,4 +86,11 @@ object Channel {
tx.rx = rx
(tx, rx)
}

inline def wrap[E] = [T] =>
(wrapFn: Sender[E] => T) => {
val (tx, rx) = Channel[E]()
val value = wrapFn(tx)
(value, rx)
}
}
16 changes: 9 additions & 7 deletions game/src/main/scala/hexacraft/game/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object GameScene {
blockLoader: BlockTextureLoader,
initialWindowSize: WindowSize,
audioSystem: AudioSystem
)(eventHandler: Channel.Sender[GameScene.Event]): GameScene = {
): (GameScene, Channel.Receiver[GameScene.Event]) = {

val blockSpecs = makeBlockSpecs()
val blockTextureMapping = loadBlockTextures(blockSpecs, blockLoader)
Expand Down Expand Up @@ -74,10 +74,12 @@ object GameScene {
val walkSoundBuffer1 = audioSystem.loadSoundBuffer("sounds/walk1.ogg")
val walkSoundBuffer2 = audioSystem.loadSoundBuffer("sounds/walk2.ogg")

val (tx, rx) = Channel[GameScene.Event]()

val s = new GameScene(
net,
audioSystem,
eventHandler,
tx,
blockTextureIndices,
crosshairShader,
crosshairVAO,
Expand Down Expand Up @@ -109,7 +111,7 @@ object GameScene {
}
net.runClient()

s
(s, rx)
}

private def makeBlockInHandRenderer(
Expand Down Expand Up @@ -279,14 +281,14 @@ class GameScene private (
val (tx, rx) = Channel[PauseMenu.Event]()
val pauseMenu = PauseMenu(tx)

rx.onEvent({
rx.onEvent {
case Unpause =>
overlays -= pauseMenu
pauseMenu.unload()
setPaused(false)
case QuitGame =>
eventHandler.send(GameScene.Event.GameQuit)
})
}

overlays += pauseMenu
setPaused(true)
Expand All @@ -308,7 +310,7 @@ class GameScene private (
val (tx, rx) = Channel[InventoryBox.Event]()
val inventoryScene = InventoryBox(player.inventory, blockTextureIndices)(tx)

rx.onEvent({
rx.onEvent {
case BoxClosed =>
overlays -= inventoryScene
inventoryScene.unload()
Expand All @@ -317,7 +319,7 @@ class GameScene private (
case InventoryUpdated(inv) =>
player.inventory = inv
toolbar.onInventoryUpdated(inv)
})
}

overlays += inventoryScene
isInPopup = true
Expand Down
4 changes: 4 additions & 0 deletions game/src/main/scala/hexacraft/game/Menus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ object Menus {
case Host(worldInfo: WorldInfo)
case GoBack
}

def create(saveFolder: File, fs: FileSystem): (HostWorldChooserMenu, Channel.Receiver[Event]) = {
Channel.wrap[Event](tx => new HostWorldChooserMenu(saveFolder, fs)(tx))
}
}

class HostWorldChooserMenu(saveFolder: File, fs: FileSystem)(onEvent: Channel.Sender[HostWorldChooserMenu.Event])
Expand Down
23 changes: 9 additions & 14 deletions game/src/main/scala/hexacraft/main/MainRouter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hexacraft.main

import hexacraft.game.*
import hexacraft.game.Menus.JoinWorldChooserMenu
import hexacraft.gui.Scene
import hexacraft.infra.audio.AudioSystem
import hexacraft.infra.fs.FileSystem
Expand Down Expand Up @@ -34,8 +35,7 @@ class MainRouter(
case SceneRoute.Main =>
import Menus.MainMenu.Event

val (tx, rx) = Channel[Event]()
val scene = Menus.MainMenu(multiplayerEnabled)(tx)
val (scene, rx) = Channel.wrap[Event](tx => Menus.MainMenu(multiplayerEnabled)(tx))

rx.onEvent {
case Event.Play => route(SceneRoute.WorldChooser)
Expand All @@ -49,8 +49,7 @@ class MainRouter(
case SceneRoute.WorldChooser =>
import Menus.WorldChooserMenu.Event

val (tx, rx) = Channel[Event]()
val scene = Menus.WorldChooserMenu(saveFolder, fs)(tx)
val (scene, rx) = Channel.wrap[Event](tx => Menus.WorldChooserMenu(saveFolder, fs)(tx))

rx.onEvent {
case Event.StartGame(saveDir, settings) =>
Expand All @@ -64,8 +63,7 @@ class MainRouter(
case SceneRoute.NewWorld =>
import Menus.NewWorldMenu.Event

val (tx, rx) = Channel[Event]()
val scene = Menus.NewWorldMenu(saveFolder)(tx)
val (scene, rx) = Channel.wrap[Event](tx => Menus.NewWorldMenu(saveFolder)(tx))

rx.onEvent {
case Event.StartGame(saveDir, settings) =>
Expand All @@ -78,8 +76,7 @@ class MainRouter(
case SceneRoute.Multiplayer =>
import Menus.MultiplayerMenu.Event

val (tx, rx) = Channel[Event]()
val scene = Menus.MultiplayerMenu(tx)
val (scene, rx) = Channel.wrap[Event](tx => Menus.MultiplayerMenu(tx))

rx.onEvent {
case Event.Join => route(SceneRoute.JoinWorld)
Expand All @@ -92,8 +89,7 @@ class MainRouter(
case SceneRoute.JoinWorld =>
import Menus.JoinWorldChooserMenu.Event

val (tx, rx) = Channel[Event]()
val scene = Menus.JoinWorldChooserMenu(tx)
val (scene, rx) = Channel.wrap[Event](tx => new Menus.JoinWorldChooserMenu(tx))

rx.onEvent {
case Event.Join(address, port) =>
Expand All @@ -107,8 +103,7 @@ class MainRouter(
case SceneRoute.HostWorld =>
import Menus.HostWorldChooserMenu.Event

val (tx, rx) = Channel[Event]()
val scene = Menus.HostWorldChooserMenu(saveFolder, fs)(tx)
val (scene, rx) = Menus.HostWorldChooserMenu.create(saveFolder, fs)

rx.onEvent {
case Event.Host(f) =>
Expand All @@ -133,8 +128,8 @@ class MainRouter(
}

val networkHandler = NetworkHandler(isHosting, isOnline, worldProvider, client)
val (tx, rx) = Channel[GameScene.Event]()
val scene = GameScene.create(networkHandler, kb, BlockTextureLoader.instance, window.windowSize, audioSystem)(tx)
val (scene, rx) =
GameScene.create(networkHandler, kb, BlockTextureLoader.instance, window.windowSize, audioSystem)

rx.onEvent {
case GameScene.Event.GameQuit =>
Expand Down
16 changes: 6 additions & 10 deletions game/src/test/scala/hexacraft/game/GameSceneTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import hexacraft.gui.Event.{KeyEvent, MouseClickEvent}
import hexacraft.infra.audio.AudioSystem
import hexacraft.infra.gpu.OpenGL
import hexacraft.infra.window.*
import hexacraft.util.{Channel, Tracker}
import hexacraft.util.Tracker
import hexacraft.world.{CylinderSize, FakeWorldProvider}

import munit.FunSuite
Expand All @@ -27,16 +27,15 @@ class GameSceneTest extends FunSuite {
val keyboard: GameKeyboard = _ => false
val audioSystem = AudioSystem.createNull()

val (tx, _) = Channel[GameScene.Event]()
val gameScene1 = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)(tx)
val (gameScene1, _) = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)
gameScene1.unload()

// Start listening to OpenGL events
val tracker = Tracker.withStorage[OpenGL.Event]
OpenGL.trackEvents(tracker)

// Load and unload the game again
val gameScene = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)(tx)
val (gameScene, _) = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)
gameScene.unload()

val shadersAdded = tracker.events.collect:
Expand All @@ -60,9 +59,8 @@ class GameSceneTest extends FunSuite {
val textureLoader = new FakeBlockTextureLoader
val audioSystem = AudioSystem.createNull()

val (tx, rx) = Channel[GameScene.Event]()
val (gameScene, rx) = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)
val gameSceneTracker = Tracker.fromRx(rx)
val gameScene = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)(tx)

gameScene.handleEvent(Event.KeyEvent(KeyboardKey.Escape, 0, KeyAction.Press, KeyMods.none))

Expand All @@ -88,9 +86,8 @@ class GameSceneTest extends FunSuite {
val textureLoader = new FakeBlockTextureLoader
val audioSystem = AudioSystem.createNull()

val (tx, rx) = Channel[GameScene.Event]()
val (gameScene, rx) = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)
val gameSceneTracker = Tracker.fromRx(rx)
val gameScene = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)(tx)

gameScene.player.flying = false

Expand Down Expand Up @@ -131,9 +128,8 @@ class GameSceneTest extends FunSuite {
val textureLoader = new FakeBlockTextureLoader
val audioSystem = AudioSystem.createNull()

val (tx, rx) = Channel[GameScene.Event]()
val (gameScene, rx) = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)
val gameSceneTracker = Tracker.fromRx(rx)
val gameScene = GameScene.create(networkHandler, keyboard, textureLoader, windowSize, audioSystem)(tx)

gameScene.player.flying = false

Expand Down

0 comments on commit 6f9ba6c

Please sign in to comment.