193 lines
8.0 KiB
Kotlin
193 lines
8.0 KiB
Kotlin
package net.shadowfacts.phycon.client.model
|
|
|
|
import net.minecraft.block.BlockState
|
|
import net.minecraft.client.render.model.*
|
|
import net.minecraft.client.texture.Sprite
|
|
import net.minecraft.client.util.SpriteIdentifier
|
|
import net.minecraft.util.DyeColor
|
|
import net.minecraft.util.Identifier
|
|
import net.minecraft.util.math.Direction
|
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
|
import net.shadowfacts.phycon.block.FaceDeviceBlock
|
|
import net.shadowfacts.phycon.block.FaceDeviceBlock.FaceCableConnection
|
|
import net.shadowfacts.phycon.client.util.bakeRecoloredCable
|
|
import java.util.Random
|
|
import java.util.function.Function
|
|
|
|
/**
|
|
* @author shadowfacts
|
|
*/
|
|
abstract class FaceDeviceModel: UnbakedModel, BakedModel {
|
|
|
|
private val interfaceCableStraightID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_straight")
|
|
private val interfaceCableCornerID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_corner")
|
|
private val interfaceCableCorner2ID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_corner_2")
|
|
private val interfaceCableCapID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_cap")
|
|
// private var interfaceCableStraight = Array<BakedModel?>(6) { null }
|
|
// private var interfaceCableCap = Array<BakedModel?>(6) { null }
|
|
// private var interfaceCableCorner = mutableMapOf<ModelRotation, BakedModel>()
|
|
// private var interfaceCableCorner2 = mutableMapOf<ModelRotation, BakedModel>()
|
|
private var interfaceCableStraight = mutableMapOf<DyeColor, Array<BakedModel>>()
|
|
private var interfaceCableCap = mutableMapOf<DyeColor, Array<BakedModel>>()
|
|
private var interfaceCableCorner = mutableMapOf<DyeColor, MutableMap<ModelRotation, BakedModel>>()
|
|
private var interfaceCableCorner2 = mutableMapOf<DyeColor, MutableMap<ModelRotation, BakedModel>>()
|
|
|
|
protected val defaultRotations = listOf(
|
|
ModelRotation.X0_Y0,
|
|
ModelRotation.X180_Y0,
|
|
ModelRotation.X90_Y180,
|
|
ModelRotation.X90_Y0,
|
|
ModelRotation.X90_Y90,
|
|
ModelRotation.X90_Y270,
|
|
)
|
|
|
|
abstract fun getSideModelIDs(): Collection<Identifier>
|
|
abstract fun bakeSideModels(loader: ModelLoader, textureGetter: Function<SpriteIdentifier, Sprite>)
|
|
abstract fun getSideModel(state: BlockState): BakedModel?
|
|
|
|
override fun getModelDependencies(): Collection<Identifier> {
|
|
return getSideModelIDs() + listOf(
|
|
interfaceCableStraightID,
|
|
interfaceCableCornerID,
|
|
interfaceCableCorner2ID,
|
|
interfaceCableCapID
|
|
)
|
|
}
|
|
|
|
override fun getTextureDependencies(
|
|
unbakedModelGetter: Function<Identifier, UnbakedModel>,
|
|
unresolvedTextureReferences: MutableSet<com.mojang.datafixers.util.Pair<String, String>>
|
|
): Collection<SpriteIdentifier> {
|
|
val textures = mutableListOf<SpriteIdentifier>()
|
|
for (dep in modelDependencies) {
|
|
val unbakedDep = unbakedModelGetter.apply(dep)
|
|
val depTextures = unbakedDep.getTextureDependencies(unbakedModelGetter, unresolvedTextureReferences)
|
|
for (tex in depTextures) {
|
|
if (tex.textureId.namespace == PhysicalConnectivity.MODID && tex.textureId.path.startsWith("block/cable/color/")) {
|
|
for (color in DyeColor.values()) {
|
|
val newPath = tex.textureId.path.replace("block/cable/color/", "block/cable/${color.getName()}/")
|
|
val substituted = SpriteIdentifier(tex.atlasId, Identifier(PhysicalConnectivity.MODID, newPath))
|
|
textures.add(substituted)
|
|
}
|
|
} else {
|
|
textures.add(tex)
|
|
}
|
|
}
|
|
}
|
|
return textures
|
|
}
|
|
|
|
override fun bake(loader: ModelLoader, textureGetter: Function<SpriteIdentifier, Sprite>, rotationContainer: ModelBakeSettings, modelId: Identifier): BakedModel {
|
|
bakeSideModels(loader, textureGetter)
|
|
|
|
DyeColor.values().forEach { color ->
|
|
interfaceCableStraight[color] = Array(6) { i ->
|
|
loader.bakeRecoloredCable(interfaceCableStraightID, defaultRotations[i], textureGetter, color)
|
|
}
|
|
interfaceCableCap[color] = Array(6) { i ->
|
|
loader.bakeRecoloredCable(interfaceCableCapID, defaultRotations[i], textureGetter, color)
|
|
}
|
|
|
|
mapOf(
|
|
interfaceCableCorner to interfaceCableCornerID to ModelRotation.values().toList(),
|
|
interfaceCableCorner2 to interfaceCableCorner2ID to listOf(
|
|
ModelRotation.X0_Y0,
|
|
ModelRotation.X0_Y90,
|
|
ModelRotation.X0_Y180,
|
|
ModelRotation.X0_Y270,
|
|
ModelRotation.X180_Y0,
|
|
ModelRotation.X180_Y90,
|
|
ModelRotation.X180_Y180,
|
|
ModelRotation.X180_Y270,
|
|
),
|
|
).forEach { (k, rotations) ->
|
|
val (map, id) = k
|
|
map[color] = mutableMapOf()
|
|
rotations.forEach { rot ->
|
|
map[color]!![rot] = loader.bakeRecoloredCable(id, rot, textureGetter, color)
|
|
}
|
|
}
|
|
}
|
|
|
|
return this
|
|
}
|
|
|
|
override fun getQuads(state: BlockState?, face: Direction?, random: Random): List<BakedQuad> {
|
|
if (state == null) return listOf()
|
|
val facing = state[FaceDeviceBlock.FACING]
|
|
val connection = state[FaceDeviceBlock.CABLE_CONNECTION]
|
|
val color = state[FaceDeviceBlock.COLOR]
|
|
|
|
val sideQuads = getSideModel(state)?.getQuads(state, face, random) ?: listOf()
|
|
val cableQuads = if (connection.direction == facing.opposite) {
|
|
interfaceCableStraight[color]!![facing.ordinal]?.getQuads(state, face, random) ?: listOf()
|
|
} else if (connection == FaceCableConnection.NONE) {
|
|
interfaceCableCap[color]!![facing.ordinal]?.getQuads(state, face, random) ?: listOf()
|
|
} else {
|
|
val model = when (facing) {
|
|
Direction.DOWN -> when (connection) {
|
|
FaceCableConnection.NORTH -> interfaceCableCorner[color]!![ModelRotation.X0_Y0]
|
|
FaceCableConnection.EAST -> interfaceCableCorner[color]!![ModelRotation.X0_Y90]
|
|
FaceCableConnection.SOUTH -> interfaceCableCorner[color]!![ModelRotation.X0_Y180]
|
|
FaceCableConnection.WEST -> interfaceCableCorner[color]!![ModelRotation.X0_Y270]
|
|
else -> null
|
|
}
|
|
Direction.UP -> when (connection) {
|
|
FaceCableConnection.NORTH -> interfaceCableCorner[color]!![ModelRotation.X180_Y180]
|
|
FaceCableConnection.EAST -> interfaceCableCorner[color]!![ModelRotation.X180_Y270]
|
|
FaceCableConnection.SOUTH -> interfaceCableCorner[color]!![ModelRotation.X180_Y0]
|
|
FaceCableConnection.WEST -> interfaceCableCorner[color]!![ModelRotation.X180_Y90]
|
|
else -> null
|
|
}
|
|
Direction.NORTH -> when (connection) {
|
|
FaceCableConnection.UP -> interfaceCableCorner[color]!![ModelRotation.X270_Y0]
|
|
FaceCableConnection.EAST -> interfaceCableCorner2[color]!![ModelRotation.X180_Y180]
|
|
FaceCableConnection.DOWN -> interfaceCableCorner[color]!![ModelRotation.X90_Y180]
|
|
FaceCableConnection.WEST -> interfaceCableCorner2[color]!![ModelRotation.X0_Y0]
|
|
else -> null
|
|
}
|
|
Direction.SOUTH -> when (connection) {
|
|
FaceCableConnection.UP -> interfaceCableCorner[color]!![ModelRotation.X270_Y180]
|
|
FaceCableConnection.WEST -> interfaceCableCorner2[color]!![ModelRotation.X180_Y0]
|
|
FaceCableConnection.DOWN -> interfaceCableCorner[color]!![ModelRotation.X90_Y0]
|
|
FaceCableConnection.EAST -> interfaceCableCorner2[color]!![ModelRotation.X0_Y180]
|
|
else -> null
|
|
}
|
|
Direction.WEST -> when (connection) {
|
|
FaceCableConnection.UP -> interfaceCableCorner[color]!![ModelRotation.X270_Y270]
|
|
FaceCableConnection.NORTH -> interfaceCableCorner2[color]!![ModelRotation.X180_Y90]
|
|
FaceCableConnection.DOWN -> interfaceCableCorner[color]!![ModelRotation.X90_Y90]
|
|
FaceCableConnection.SOUTH -> interfaceCableCorner2[color]!![ModelRotation.X0_Y270]
|
|
else -> null
|
|
}
|
|
Direction.EAST -> when (connection) {
|
|
FaceCableConnection.UP -> interfaceCableCorner[color]!![ModelRotation.X270_Y90]
|
|
FaceCableConnection.SOUTH -> interfaceCableCorner2[color]!![ModelRotation.X180_Y270]
|
|
FaceCableConnection.DOWN -> interfaceCableCorner[color]!![ModelRotation.X90_Y270]
|
|
FaceCableConnection.NORTH -> interfaceCableCorner2[color]!![ModelRotation.X0_Y90]
|
|
else -> null
|
|
}
|
|
else -> null
|
|
}
|
|
model?.getQuads(state, face, random) ?: listOf()
|
|
}
|
|
|
|
return sideQuads + cableQuads
|
|
}
|
|
|
|
override fun useAmbientOcclusion() = true
|
|
|
|
override fun hasDepth() = false
|
|
|
|
override fun isSideLit() = false
|
|
|
|
override fun isBuiltin() = false
|
|
|
|
abstract override fun getSprite(): Sprite
|
|
|
|
override fun getTransformation() = null
|
|
|
|
override fun getOverrides() = null
|
|
|
|
}
|