Skip to content

Commit

Permalink
Replaced most implicit parameters with explicit parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Dec 9, 2023
1 parent 3a0952f commit 4ad8806
Show file tree
Hide file tree
Showing 28 changed files with 119 additions and 134 deletions.
33 changes: 19 additions & 14 deletions game/src/main/scala/hexacraft/game/GameScene.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hexacraft.game

import com.martomate.nbt.Nbt
import hexacraft.game.inventory.{GuiBlockRenderer, InventoryBox, Toolbar}
import hexacraft.gui.*
import hexacraft.gui.comp.{Component, GUITransformation}
Expand All @@ -9,7 +8,7 @@ import hexacraft.infra.gpu.OpenGL
import hexacraft.infra.window.*
import hexacraft.renderer.*
import hexacraft.util.{ResourceWrapper, TickableTimer, Tracker}
import hexacraft.world.{World, WorldProvider}
import hexacraft.world.{CylinderSize, World, WorldProvider}
import hexacraft.world.block.{Block, BlockSpecRegistry, BlockState, HexBox}
import hexacraft.world.camera.{Camera, CameraProjection}
import hexacraft.world.coord.CoordUtils
Expand All @@ -22,6 +21,7 @@ import hexacraft.world.ray.{Ray, RayTracer}
import hexacraft.world.render.WorldRenderer
import hexacraft.world.settings.WorldInfo

import com.martomate.nbt.Nbt
import org.joml.{Matrix4f, Vector2f, Vector3f}

import scala.collection.mutable
Expand All @@ -34,27 +34,30 @@ object GameScene {
case CursorReleased
}

class GameScene(worldProvider: WorldProvider, initialWindowSize: WindowSize)(
eventHandler: Tracker[GameScene.Event]
)(using keyboard: GameKeyboard, blockLoader: BlockTextureLoader)
class GameScene(
worldProvider: WorldProvider,
keyboard: GameKeyboard,
blockLoader: BlockTextureLoader,
initialWindowSize: WindowSize
)(eventHandler: Tracker[GameScene.Event])
extends Scene:

TextureArray.registerTextureArray("blocks", 32, new ResourceWrapper(() => blockLoader.reload().images))

given BlockSpecRegistry = BlockSpecRegistry.load(blockLoader.textureMapping)
private val blockSpecRegistry = BlockSpecRegistry.load(blockLoader.textureMapping)

private val crosshairShader = new CrosshairShader()
private val crosshairVAO: VAO = makeCrosshairVAO
private val crosshairRenderer: Renderer =
new Renderer(OpenGL.PrimitiveMode.Lines, GpuState.of(OpenGL.State.DepthTest -> false))

given entityModelLoader: EntityModelLoader = new EntityModelLoader()
private val entityModelLoader: EntityModelLoader = new EntityModelLoader()
private val playerModel: EntityModel = entityModelLoader.load("player")
private val sheepModel: EntityModel = entityModelLoader.load("sheep")

private val worldInfo = worldProvider.getWorldInfo
private val world = World(worldProvider, worldInfo)
import world.size.impl
private val world = World(worldProvider, worldInfo, entityModelLoader)
given CylinderSize = world.size

val player: Player = makePlayer(worldInfo.player)

Expand All @@ -63,7 +66,7 @@ class GameScene(worldProvider: WorldProvider, initialWindowSize: WindowSize)(
private val otherPlayer: ControlledPlayerEntity =
new ControlledPlayerEntity(playerModel, new EntityBaseData(CylCoords(player.position)))

private val worldRenderer: WorldRenderer = new WorldRenderer(world, initialWindowSize.physicalSize)
private val worldRenderer: WorldRenderer = new WorldRenderer(world, blockSpecRegistry, initialWindowSize.physicalSize)
world.trackEvents(worldRenderer.onWorldEvent _)

// worldRenderer.addPlayer(otherPlayer)
Expand Down Expand Up @@ -139,13 +142,15 @@ class GameScene(worldProvider: WorldProvider, initialWindowSize: WindowSize)(
then
setUseMouse(false)
isInPopup = true
inventoryScene = InventoryBox(player.inventory)(() => {

val onCloseInventory = () => {
overlays -= inventoryScene
inventoryScene.unload()
inventoryScene = null
isInPopup = false
setUseMouse(true)
})
}
inventoryScene = InventoryBox(player.inventory, onCloseInventory, blockSpecRegistry)

overlays += inventoryScene
case KeyboardKey.Letter('M') =>
Expand Down Expand Up @@ -406,7 +411,7 @@ class GameScene(worldProvider: WorldProvider, initialWindowSize: WindowSize)(
private def viewDistance: Double = world.renderDistance

private def makeBlockInHandRenderer(world: World, camera: Camera): GuiBlockRenderer =
val renderer = GuiBlockRenderer(1, 1)
val renderer = GuiBlockRenderer(1, 1)(blockSpecRegistry)
renderer.setViewMatrix(makeBlockInHandViewMatrix)
renderer.setWindowAspectRatio(initialWindowSize.logicalAspectRatio)
renderer
Expand All @@ -422,7 +427,7 @@ class GameScene(worldProvider: WorldProvider, initialWindowSize: WindowSize)(
private def makeToolbar(player: Player, windowSize: WindowSize): Toolbar =
val location = LocationInfo(-4.5f * 0.2f, -0.83f - 0.095f, 2 * 0.9f, 2 * 0.095f)

val toolbar = new Toolbar(location, player.inventory)
val toolbar = new Toolbar(location, player.inventory)(blockSpecRegistry)
toolbar.setSelectedIndex(player.selectedItemSlot)
toolbar.setWindowAspectRatio(windowSize.logicalAspectRatio)
toolbar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object GuiBlockRenderer:
private val guiBlockShader = new GuiBlockShader(isSide = false)
private val guiBlockSideShader = new GuiBlockShader(isSide = true)

class GuiBlockRenderer(w: Int, h: Int, separation: Float = 0.2f)(using blockSpecs: BlockSpecRegistry):
class GuiBlockRenderer(w: Int, h: Int, separation: Float = 0.2f)(blockSpecs: BlockSpecRegistry):
private val guiBlockRenderers =
for s <- 0 until 8 yield BlockRenderer(GuiBlockVao.forSide(s), GpuState.of(OpenGL.State.DepthTest -> false))

Expand Down
66 changes: 40 additions & 26 deletions game/src/main/scala/hexacraft/game/inventory/InventoryBox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,42 @@ import hexacraft.world.player.Inventory

import org.joml.{Matrix4f, Vector4f}

class InventoryBox(inventory: Inventory)(closeScene: () => Unit)(using BlockSpecRegistry) extends Component {
object InventoryBox {
def apply(inventory: Inventory, closeScene: () => Unit, specs: BlockSpecRegistry): InventoryBox =
val box = new InventoryBox(
inventory,
closeScene,
makeGuiBlockRenderer(specs),
makeFloatingBlockRenderer(specs)
)
box.updateRendererContent()
box

private def makeGuiBlockRenderer(specs: BlockSpecRegistry) =
val renderer = new GuiBlockRenderer(9, 4)(specs)
renderer.setViewMatrix(makeTiltedBlockViewMatrix)
renderer

private def makeFloatingBlockRenderer(specs: BlockSpecRegistry) =
val renderer = GuiBlockRenderer(1, 1)(specs)
renderer.setViewMatrix(makeTiltedBlockViewMatrix)
renderer

private def makeTiltedBlockViewMatrix =
new Matrix4f()
.translate(0, 0, -14f)
.rotateX(3.1415f / 6)
.rotateY(3.1415f / 24)
.translate(0, -0.25f, 0)
}

class InventoryBox private (
inventory: Inventory,
closeScene: () => Unit,
gridRenderer: GuiBlockRenderer,
floatingBlockRenderer: GuiBlockRenderer
) extends Component {

private val location: LocationInfo = LocationInfo(-4.5f * 0.2f, -2.5f * 0.2f, 9 * 0.2f, 4 * 0.2f)
private val backgroundColor = new Vector4f(0.4f, 0.4f, 0.4f, 0.75f)
private val selectedColor = new Vector4f(0.2f, 0.2f, 0.2f, 0.25f)
Expand All @@ -20,14 +55,10 @@ class InventoryBox(inventory: Inventory)(closeScene: () => Unit)(using BlockSpec
/** The block currently being moved */
private var floatingBlock: Option[Block] = None

private val guiBlockRenderer = makeGuiBlockRenderer()
private val floatingBlockRenderer = makeFloatingBlockRenderer()
updateRendererContent()

private val revokeInventoryTracker = inventory.trackChanges(_ => updateRendererContent())

private def updateRendererContent(): Unit =
guiBlockRenderer.updateContent(-4 * 0.2f, -2 * 0.2f, (0 until 9 * 4).map(i => inventory(i)))
gridRenderer.updateContent(-4 * 0.2f, -2 * 0.2f, (0 until 9 * 4).map(i => inventory(i)))
if floatingBlock.isEmpty then floatingBlockRenderer.updateContent(0, 0, Seq(Block.Air))

private def calculateHoverIndex(mx: Float, my: Float) =
Expand Down Expand Up @@ -67,7 +98,7 @@ class InventoryBox(inventory: Inventory)(closeScene: () => Unit)(using BlockSpec
case None =>

override def render(transformation: GUITransformation)(using context: RenderContext): Unit =
guiBlockRenderer.setWindowAspectRatio(context.windowAspectRatio)
gridRenderer.setWindowAspectRatio(context.windowAspectRatio)
floatingBlockRenderer.setWindowAspectRatio(context.windowAspectRatio)

val mousePos = context.heightNormalizedMousePos
Expand All @@ -84,31 +115,14 @@ class InventoryBox(inventory: Inventory)(closeScene: () => Unit)(using BlockSpec
val yOffset = transformation.y + (hoverIndex.get / 9) * 0.2f
Component.drawRect(selectedBox, xOffset, yOffset, selectedColor, context.windowAspectRatio)

guiBlockRenderer.render(transformation)
gridRenderer.render(transformation)

if floatingBlock.isDefined
then floatingBlockRenderer.render(transformation)

private def makeGuiBlockRenderer() =
val renderer = new GuiBlockRenderer(9, 4)
renderer.setViewMatrix(makeTiltedBlockViewMatrix)
renderer

private def makeFloatingBlockRenderer() =
val renderer = GuiBlockRenderer(1, 1)
renderer.setViewMatrix(makeTiltedBlockViewMatrix)
renderer

private def makeTiltedBlockViewMatrix =
new Matrix4f()
.translate(0, 0, -14f)
.rotateX(3.1415f / 6)
.rotateY(3.1415f / 24)
.translate(0, -0.25f, 0)

override def unload(): Unit =
revokeInventoryTracker()
guiBlockRenderer.unload()
gridRenderer.unload()
floatingBlockRenderer.unload()
super.unload()
}
4 changes: 2 additions & 2 deletions game/src/main/scala/hexacraft/game/inventory/Toolbar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import hexacraft.world.player.Inventory

import org.joml.{Matrix4f, Vector4f}

class Toolbar(location: LocationInfo, inventory: Inventory)(using BlockSpecRegistry)
class Toolbar(location: LocationInfo, inventory: Inventory)(specs: BlockSpecRegistry)
extends Component
with SubComponents {
private val backgroundColor = new Vector4f(0.4f, 0.4f, 0.4f, 0.75f)
Expand All @@ -22,7 +22,7 @@ class Toolbar(location: LocationInfo, inventory: Inventory)(using BlockSpecRegis
def setSelectedIndex(idx: Int): Unit = selectedIndex = idx

private val guiBlockRenderer =
val renderer = new GuiBlockRenderer(9, 1)
val renderer = new GuiBlockRenderer(9, 1)(specs)
renderer.setViewMatrix(
new Matrix4f()
.translate(0, 0, -14f)
Expand Down
14 changes: 9 additions & 5 deletions game/src/main/scala/hexacraft/main/MainRouter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ object MainRouter {
case QuitRequested
}

class MainRouter(saveFolder: File, multiplayerEnabled: Boolean, fs: FileSystem, window: GameWindow)(
eventListener: Tracker[MainRouter.Event]
)(using kb: GameKeyboard) {
class MainRouter(
saveFolder: File,
multiplayerEnabled: Boolean,
fs: FileSystem,
window: GameWindow,
kb: GameKeyboard
)(eventListener: Tracker[MainRouter.Event]) {

def route(sceneRoute: SceneRoute): Unit = eventListener.notify(MainRouter.Event.SceneChanged(createScene(sceneRoute)))

Expand Down Expand Up @@ -76,9 +80,9 @@ class MainRouter(saveFolder: File, multiplayerEnabled: Boolean, fs: FileSystem,
new SettingsMenu(() => route(SceneRoute.Main))

case SceneRoute.Game(saveDir, settings) =>
given blockLoader: BlockTextureLoader = BlockTextureLoader.instance // this loads it to memory
val worldProvider = new WorldProviderFromFile(saveDir, settings, fs)

GameScene(new WorldProviderFromFile(saveDir, settings, fs), window.windowSize):
GameScene(worldProvider, kb, BlockTextureLoader.instance, window.windowSize):
case GameScene.Event.GameQuit =>
route(SceneRoute.Main)
System.gc()
Expand Down
4 changes: 1 addition & 3 deletions game/src/main/scala/hexacraft/main/MainWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ class MainWindow(isDebug: Boolean, saveFolder: File) extends GameWindow:
scene = newScene

private def makeSceneRouter(): MainRouter =
given GameKeyboard = keyboard

MainRouter(saveFolder, multiplayerEnabled, fs, this):
MainRouter(saveFolder, multiplayerEnabled, fs, this, keyboard):
case MainRouter.Event.SceneChanged(newScene) => setScene(newScene)
case MainRouter.Event.QuitRequested => tryQuit()

Expand Down
2 changes: 0 additions & 2 deletions game/src/main/scala/hexacraft/world/CylinderSize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ case class CylinderSize(worldSize: Int) extends AnyVal:
*/
def circumference: Double = totalSize * CylinderSize.y60

implicit def impl: CylinderSize = this

object CylinderSize:
val y60: Double = Math.sqrt(3) / 2

Expand Down
2 changes: 1 addition & 1 deletion game/src/main/scala/hexacraft/world/LightPropagator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import hexacraft.world.coord.integer.{BlockRelChunk, BlockRelWorld, NeighborOffs

import scala.collection.mutable

class LightPropagator(world: BlocksInWorld)(implicit cylSize: CylinderSize) {
class LightPropagator(world: BlocksInWorld)(using CylinderSize) {
private val chunkCache: ChunkCache = new ChunkCache(world)

def initBrightnesses(chunk: Chunk): Unit = {
Expand Down
9 changes: 4 additions & 5 deletions game/src/main/scala/hexacraft/world/World.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ object World:

var shouldChillChunkLoader = false

def apply(provider: WorldProvider, worldInfo: WorldInfo)(using EntityModelLoader): World =
new World(provider, worldInfo, makeEntityRegistry)
def apply(provider: WorldProvider, worldInfo: WorldInfo, modelLoader: EntityModelLoader): World =
new World(provider, worldInfo, makeEntityRegistry(modelLoader))

private def makeEntityRegistry(using modelLoader: EntityModelLoader): EntityRegistry =
private def makeEntityRegistry(modelLoader: EntityModelLoader): EntityRegistry =
val entityTypes = Map(
"player" -> new PlayerFactory(() => modelLoader.load("player")),
"sheep" -> new SheepFactory(() => modelLoader.load("sheep"))
Expand All @@ -39,8 +39,7 @@ object World:
class World(worldProvider: WorldProvider, worldInfo: WorldInfo, val entityRegistry: EntityRegistry)
extends BlockRepository
with BlocksInWorld:
val size: CylinderSize = worldInfo.worldSize
import size.impl
given size: CylinderSize = worldInfo.worldSize

private val worldGenerator = new WorldGenerator(worldInfo.gen)
private val worldPlanner: WorldPlanner = WorldPlanner(this, entityRegistry, worldInfo.gen.seed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import hexacraft.math.MathUtils
import hexacraft.world.CylinderSize
import hexacraft.world.coord.fp

class SkewCylCoords private (_x: Double, _y: Double, _z: Double)(implicit
val cylSize: CylinderSize
) extends AbstractCoords[SkewCylCoords](_x, _y, _z) {
class SkewCylCoords private (_x: Double, _y: Double, _z: Double)(using CylinderSize)
extends AbstractCoords[SkewCylCoords](_x, _y, _z) {

def toNormalCoords(reference: CylCoords): NormalCoords = toCylCoords.toNormalCoords(reference)
def toCylCoords: CylCoords = CylCoords(x * CylinderSize.y60, y, z + x * 0.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object ChunkLoadingEdge {
case ChunkLoadable(chunk: ChunkRelWorld, loadable: Boolean)
}

class ChunkLoadingEdge(implicit cylSize: CylinderSize) {
class ChunkLoadingEdge(using CylinderSize) {
private val chunksLoaded: mutable.Set[ChunkRelWorld] = mutable.HashSet.empty
private val chunksEdge: mutable.Set[ChunkRelWorld] = mutable.HashSet.empty
private val chunksLoadable: mutable.Set[ChunkRelWorld] = mutable.HashSet.empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ object ChunkRenderDataFactory:
chunkCoords: ChunkRelWorld,
blocks: Array[LocalBlockState],
world: BlocksInWorld,
transmissiveBlocks: Boolean
)(using CylinderSize, BlockSpecRegistry): ChunkRenderData =
transmissiveBlocks: Boolean,
blockSpecs: BlockSpecRegistry
)(using CylinderSize): ChunkRenderData =
val chunkCache = new ChunkCache(world)

val sidesToRender = Array.tabulate[util.BitSet](8)(_ => new util.BitSet(16 * 16 * 16))
Expand Down Expand Up @@ -87,7 +88,7 @@ object ChunkRenderDataFactory:
case None => 0
}

populateBuffer(chunkCoords, blocks, side, shouldRender, brightnessFn, buf)
populateBuffer(chunkCoords, blocks, side, shouldRender, brightnessFn, buf, blockSpecs)
buf.flip()
buf

Expand All @@ -99,8 +100,9 @@ object ChunkRenderDataFactory:
side: Int,
shouldRender: java.util.BitSet,
brightness: BlockRelWorld => Float,
buf: ByteBuffer
)(using blockSpecs: BlockSpecRegistry, cylSize: CylinderSize): Unit =
buf: ByteBuffer,
blockSpecs: BlockSpecRegistry
)(using CylinderSize): Unit =
val verticesPerInstance = if (side < 2) 7 else 4

var i1 = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import org.joml.{Vector2ic, Vector3f}
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

class WorldRenderer(world: BlocksInWorld, initialFramebufferSize: Vector2ic)(using CylinderSize, BlockSpecRegistry):
class WorldRenderer(world: BlocksInWorld, blockSpecs: BlockSpecRegistry, initialFramebufferSize: Vector2ic)(using
CylinderSize
):
private val skyShader = new SkyShader()
private val entityShader = new EntityShader(isSide = false)
private val entitySideShader = new EntityShader(isSide = true)
Expand Down Expand Up @@ -68,8 +70,8 @@ class WorldRenderer(world: BlocksInWorld, initialFramebufferSize: Vector2ic)(usi
then (ChunkRenderData.empty, ChunkRenderData.empty)
else
(
ChunkRenderDataFactory.makeChunkRenderData(ch.coords, ch.blocks, world, false),
ChunkRenderDataFactory.makeChunkRenderData(ch.coords, ch.blocks, world, true)
ChunkRenderDataFactory.makeChunkRenderData(ch.coords, ch.blocks, world, false, blockSpecs),
ChunkRenderDataFactory.makeChunkRenderData(ch.coords, ch.blocks, world, true, blockSpecs)
)

chunkHandler.setChunkRenderData(ch.coords, opaqueBlocks, transmissiveBlocks)
Expand Down
Loading

0 comments on commit 4ad8806

Please sign in to comment.