Skip to content

Commit

Permalink
Removed the need for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Sep 20, 2023
1 parent f3ee4f5 commit ee3c90e
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 218 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def lwjglDependencies = {

def otherDependencies = Seq(
"org.joml" % "joml" % "1.10.5",
"com.eclipsesource.minimal-json" % "minimal-json" % "0.9.5",
"com.flowpowered" % "flow-nbt" % "1.0.0"
)

Expand Down
61 changes: 0 additions & 61 deletions src/main/resources/spec/entities/player.json

This file was deleted.

53 changes: 0 additions & 53 deletions src/main/resources/spec/entities/sheep.json

This file was deleted.

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

import org.joml.{Vector3d, Vector3f}

class Camera(val proj: CameraProjection)(implicit val worldSize: CylinderSize) {
class Camera(val proj: CameraProjection)(using worldSize: CylinderSize) {
val view = new CameraView
def position: Vector3d = view.position
def rotation: Vector3f = view.rotation
Expand Down
27 changes: 5 additions & 22 deletions src/main/scala/hexacraft/world/entity/EntityModelLoader.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
package hexacraft.world.entity

import hexacraft.infra.fs.FileUtils
import hexacraft.world.block.HexBox
import hexacraft.world.coord.fp.CylCoords
import hexacraft.world.entity.base.BasicEntityModel
import hexacraft.world.entity.player.PlayerEntityModel
import hexacraft.world.entity.sheep.SheepEntityModel

import com.eclipsesource.json.{Json, JsonObject}
class EntityModelLoader {

class EntityModelLoader(basePath: String = "spec/entities") {
private def makeEntity(name: String, setup: JsonObject): EntityModel = name match {
case "player" => PlayerEntityModel.fromJson(setup)
case "sheep" => SheepEntityModel.fromJson(setup)
case _ => BasicEntityModel.create(CylCoords.Offset(0, 0, 0), new HexBox(0, 0, 0))
}

def load(name: String): EntityModel = {
FileUtils.getResourceFile(s"$basePath/$name.json") match {
case Some(file) =>
val reader = FileUtils.getBufferedReader(file)
if (reader != null) {
val base = Json.parse(reader).asObject()
val name = base.get("texture").asString()
val entity = makeEntity(name, base)
entity
} else makeEntity("", null)
case None =>
makeEntity("", null)
}
def load(name: String): EntityModel = name match {
case "player" => PlayerEntityModel.create("player")
case "sheep" => SheepEntityModel.create("sheep")
case _ => BasicEntityModel.create(CylCoords.Offset(0, 0, 0), HexBox(0, 0, 0))
}
}
29 changes: 10 additions & 19 deletions src/main/scala/hexacraft/world/entity/base/BasicEntityPart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,27 @@ import hexacraft.world.block.HexBox
import hexacraft.world.coord.fp.CylCoords
import hexacraft.world.entity.EntityPart

import com.eclipsesource.json.JsonObject
import org.joml.{Matrix4f, Vector3f}

class BasicEntityPart(
override val box: HexBox,
pos: CylCoords.Offset,
val rotation: Vector3f,
parentPart: EntityPart = null,
setup: JsonObject = new JsonObject
textureBaseOffset: (Int, Int) = (0, 0),
parentPart: EntityPart = null
) extends EntityPart {
private val boxRadius = (box.radius * 32 / 0.5f).round
private val boxHeight = ((box.top - box.bottom) * 32 / 0.5f).round

private val textureBaseOffset = {
val tex = setup.get("textures").asObject()
val defTag = tex.get("default")
if (defTag != null) {
val texInfo = defTag.asObject()
(texInfo.get("xoff").asInt(), texInfo.get("yoff").asInt())
} else (0, 0)
}
override def baseTransform: Matrix4f =
val m = if parentPart != null then Matrix4f(parentPart.baseTransform) else Matrix4f()

override def baseTransform: Matrix4f = (if (parentPart != null)
new Matrix4f(parentPart.baseTransform)
else new Matrix4f())
.translate(pos.toVector3f)
.rotateZ(rotation.z)
.rotateX(rotation.x)
.rotateY(rotation.y)
.translate(0, box.bottom, 0)
m
.translate(pos.toVector3f)
.rotateZ(rotation.z)
.rotateX(rotation.x)
.rotateY(rotation.y)
.translate(0, box.bottom, 0)

override def transform: Matrix4f = baseTransform
.scale(new Vector3f(box.radius, box.top - box.bottom, box.radius))
Expand Down
34 changes: 12 additions & 22 deletions src/main/scala/hexacraft/world/entity/horse/HorseEntityModel.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package hexacraft.world.entity.horse

import hexacraft.math.MathUtils
import hexacraft.renderer.TextureSingle
import hexacraft.world.CylinderSize
import hexacraft.world.block.HexBox
import hexacraft.world.coord.fp.BlockCoords
import hexacraft.world.entity.{EntityModel, EntityPart}
import hexacraft.world.entity.base.BasicEntityPart

import com.eclipsesource.json.JsonObject
import hexacraft.math.MathUtils
import org.joml.Vector3f

class HorseEntityModel(
Expand Down Expand Up @@ -53,9 +52,7 @@ class HorseAnimation(model: HorseEntityModel):
model.head.rotation.x = MathUtils.remap(-1, 1, pi / 4, pi / 2, neckSin)

object HorseEntityModel:
def fromJson(setup: JsonObject): HorseEntityModel =
val partsNBT = setup.get("parts").asObject()

def fromJson(textureName: String): HorseEntityModel =
def makeHexBox(r: Int, b: Int, h: Int): HexBox =
new HexBox(r / 32f * 0.5f, b / 32f * 0.5f, (h + b) / 32f * 0.5f)

Expand Down Expand Up @@ -83,25 +80,22 @@ object HorseEntityModel:
-(bodyLength - neckRadius * 2 + 2) * pixSizeZ
)
.toCylCoordsOffset,
new Vector3f(
Vector3f(
0,
math.Pi.toFloat / 2,
-math.Pi.toFloat / 2 + math.Pi.toFloat / 4
),
setup = partsNBT.get("body").asObject()
)
)
val head = new BasicEntityPart(
makeHexBox(headRadius, -neckRadius, headDepth),
BlockCoords.Offset(0, neckDepth / 32d, 0).toCylCoordsOffset,
new Vector3f(math.Pi.toFloat / 4, 0, 0),
parentPart = neck,
setup = partsNBT.get("head").asObject()
Vector3f(math.Pi.toFloat / 4, 0, 0),
parentPart = neck
)
val body = new BasicEntityPart(
makeHexBox(bodyRadius, 0, bodyLength),
BlockCoords.Offset(0, (legLength + legYOffset) / 32d, 0).toCylCoordsOffset,
new Vector3f(0, math.Pi.toFloat / 2, -math.Pi.toFloat / 2),
setup = partsNBT.get("body").asObject()
Vector3f(0, math.Pi.toFloat / 2, -math.Pi.toFloat / 2)
)
val frontRightLeg = new BasicEntityPart(
makeHexBox(legRadius, 0, legLength),
Expand All @@ -112,8 +106,7 @@ object HorseEntityModel:
legOffset / 32d - (bodyLength - legRadius) * pixSizeZ
)
.toCylCoordsOffset,
new Vector3f(math.Pi.toFloat, 0, 0),
setup = partsNBT.get("front_right_leg").asObject()
Vector3f(math.Pi.toFloat, 0, 0)
)
val frontLeftLeg = new BasicEntityPart(
makeHexBox(legRadius, 0, legLength),
Expand All @@ -124,8 +117,7 @@ object HorseEntityModel:
-legOffset / 32d - (bodyLength - legRadius) * pixSizeZ
)
.toCylCoordsOffset,
new Vector3f(math.Pi.toFloat, 0, 0),
setup = partsNBT.get("front_left_leg").asObject()
Vector3f(math.Pi.toFloat, 0, 0)
)
val backRightLeg = new BasicEntityPart(
makeHexBox(legRadius, 0, legLength),
Expand All @@ -136,8 +128,7 @@ object HorseEntityModel:
legOffset / 32d - legRadius * pixSizeZ
)
.toCylCoordsOffset,
new Vector3f(math.Pi.toFloat, 0, 0),
setup = partsNBT.get("back_right_leg").asObject()
Vector3f(math.Pi.toFloat, 0, 0)
)
val backLeftLeg = new BasicEntityPart(
makeHexBox(legRadius, 0, legLength),
Expand All @@ -148,8 +139,7 @@ object HorseEntityModel:
-legOffset / 32d - legRadius * pixSizeZ
)
.toCylCoordsOffset,
new Vector3f(math.Pi.toFloat, 0, 0),
setup = partsNBT.get("back_left_leg").asObject()
Vector3f(math.Pi.toFloat, 0, 0)
)

new HorseEntityModel(
Expand All @@ -160,5 +150,5 @@ object HorseEntityModel:
frontLeftLeg,
backRightLeg,
backLeftLeg,
setup.getString("texture", "")
textureName
)
Loading

0 comments on commit ee3c90e

Please sign in to comment.