Skip to content

Commit

Permalink
refactor: make wire and gate renderers more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Nov 21, 2022
1 parent c44c302 commit 6595d8b
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import net.minecraftforge.api.distmarker.{Dist, OnlyIn}

import java.util.{Collections, Collection => JCollection}

abstract class GatePart(gateType:GateType) extends TMultiPart with TNormalOcclusionPart with TFaceConnectable with TSwitchPacket with ITickablePart with TIconHitEffectsPart
abstract class GatePart(gateType:GateType) extends TMultiPart with TNormalOcclusionPart with TFaceConnectable with TSwitchPacket with ITickablePart with TIconHitEffectsPart with IGateRenderKey
{
private var gateShape:Byte = 0

Expand Down Expand Up @@ -262,7 +262,7 @@ abstract class GatePart(gateType:GateType) extends TMultiPart with TNormalOcclus
override def renderStatic(layer:RenderType, ccrs:CCRenderState) = {
if (layer == null || (layer == RenderType.cutout() && Configurator.staticGates)) {
ccrs.setBrightness(world, this.pos)
RenderGate.instance().renderStatic(this, Vector3.ZERO, ccrs)
RenderGate.instance().renderStatic(this, ccrs)
true
} else
false
Expand All @@ -276,8 +276,8 @@ abstract class GatePart(gateType:GateType) extends TMultiPart with TNormalOcclus
ccrs.brightness = packedLight
ccrs.overlay = packedOverlay
ccrs.bind(new TransformingVertexBuilder(buffers.getBuffer(RenderType.cutout()), mStack), DefaultVertexFormats.BLOCK)
RenderGate.instance().renderDynamic(this, Vector3.ZERO, partialTicks, ccrs)
RenderGate.instance().renderCustomDynamic(this, Vector3.ZERO, mStack, buffers, packedLight, packedOverlay, partialTicks)
RenderGate.instance().renderDynamic(this, partialTicks, ccrs)
RenderGate.instance().renderCustomDynamic(this, mStack, buffers, packedLight, packedOverlay, partialTicks, ccrs)
}

override def getBounds:Cuboid6 = new Cuboid6(getShape(ISelectionContext.empty()).bounds())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ abstract class ArrayGatePartCrossing(gateType:GateType) extends ArrayGatePart(ga

def sendSignalUpdate():Unit = { sendUpdate(11, _.writeByte(signal1).writeByte(signal2)) }

override def bottomSignal: Byte = signal1
override def topSignal: Byte = signal2

override def topSignalConnMask:Int = {
IGateWireRenderConnect.getConnsAtHeight(this, 10.0D)
}

override def gateLogicOnChange():Unit = {
val oldSignal = (state&1) != 0
val newSignal = signal1 != 0
Expand Down Expand Up @@ -378,6 +385,10 @@ abstract class ArrayGatePartTopOnly(gateType:GateType) extends ArrayGatePart(gat
super.onSignalUpdate()
sendSignalUpdate()
}


override def topSignal: Byte = signal
override def topSignalConnMask: Int = IGateWireRenderConnect.getConnsAtHeight(this, 10.0D)
}

class ANDCell extends ArrayGatePartTopOnly(GateType.AND_CELL) with TSimpleRSGatePart with IGateWireRenderConnect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ abstract class BundledGatePart(gateType:GateType) extends RedstoneGatePart(gateT
class BusTransceiver extends BundledGatePart(GateType.BUS_TRANSCEIVER)
{
var input0, output0, input2, output2:Array[Byte] = null
var packedOutput:Int = 0

override def bundledOutputMask(shape:Int) = 5
override def bundledInputMask(shape:Int) = 5
Expand Down Expand Up @@ -137,10 +138,14 @@ class BusTransceiver extends BundledGatePart(GateType.BUS_TRANSCEIVER)
def packClientData:Int = packDigital(output0)|packDigital(output2)<<16

def unpackClientData(packed:Int):Unit = {
packedOutput = packed
output0 = unpackDigital(output0, packed&0xFFFF)
output2 = unpackDigital(output2, packed>>>16)
}

override def bOutput0: Short = (packedOutput&0xFFFF).toShort
override def bOutput2: Short = (packedOutput>>>16).toShort

override def getBundledOutput(r:Int):Array[Byte] = if (r == 0) output0 else output2

def calcBundledInput(r:Int):Array[Byte] = raiseSignal(copySignal(getBundledInput(r)), getBundledOutput(r)) //OR'd w/ output
Expand Down Expand Up @@ -200,6 +205,8 @@ class BusTransceiver extends BundledGatePart(GateType.BUS_TRANSCEIVER)
}

override def getLightValue:Int = 0


}

class BusRandomizer extends BundledGatePart(GateType.BUS_RANDOMIZER)
Expand Down Expand Up @@ -253,6 +260,9 @@ class BusRandomizer extends BundledGatePart(GateType.BUS_RANDOMIZER)
sendUpdate(12, _.writeShort(mask))
}

override def bOutput0: Short = output.toShort
override def bInput2: Short = mask.toShort

override def gateLogicOnChange():Unit = {
var inputChanged = false
val oldInput = state&0xF
Expand Down Expand Up @@ -315,6 +325,8 @@ class BusConverter extends BundledGatePart(GateType.BUS_CONVERTER)
var rsIn, rsOut = 0
var bOutUnpacked:Array[Byte] = null

override def rsIO: Int = rsIn | rsOut

override def bundledOutputMask(shape:Int):Int = if (shape == 0) 1 else 0
override def bundledInputMask(shape:Int):Int = if (shape == 0) 0 else 1
override def outputMask(shape:Int):Int = if (shape == 0) 10 else 14
Expand Down Expand Up @@ -474,6 +486,9 @@ class BusInputPanel extends BundledGatePart(GateType.BUS_INPUT_PANEL)
override def outputMask(shape:Int) = 0
override def inputMask(shape:Int) = 1

override def bOutput2: Short = bOut.toShort
override def bInput0: Short = pressMask.toShort

def setBOut(newBOut:Int):Unit = {
if (bOut != newBOut) {
bOut = newBOut
Expand Down Expand Up @@ -573,7 +588,7 @@ class BusInputPanel extends BundledGatePart(GateType.BUS_INPUT_PANEL)

class SegmentDisplay extends BundledGatePart(GateType.SEGMENT_DISPLAY)
{
var bInH = 0
var bInH = 0 //TODO move colour to state, and bInL to new short 'bInput0'
var colour:Byte = EnumColour.RED.ordinal.toByte

override def bundledInputMask(shape:Int) = 1
Expand Down Expand Up @@ -618,6 +633,9 @@ class SegmentDisplay extends BundledGatePart(GateType.SEGMENT_DISPLAY)
sendUpdate(12, _.writeByte(colour))
}

override def bInHigh: Int = bInH
override def segmentColour: Byte = colour

override def gateLogicCycleShape():Boolean = {
setShape(shape^1)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
package mrtjp.projectred.integration

import codechicken.lib.data.{MCDataInput, MCDataOutput}
import codechicken.lib.math.MathHelper
import codechicken.lib.vec.Rotation
import codechicken.multipart.api.part.INeighborTileChangePart
import codechicken.multipart.util.PartRayTraceResult
import com.google.common.base.Predicate
import mrtjp.projectred.api.IScrewdriver
import mrtjp.projectred.core.Configurator
import mrtjp.projectred.core.TFaceOrient._
import net.minecraft.block.material.Material
import net.minecraft.entity.Entity
import net.minecraft.entity.item.ItemFrameEntity
import net.minecraft.entity.player.PlayerEntity
Expand All @@ -29,7 +29,7 @@ trait TExtraStateGatePart extends RedstoneGatePart
{
private var lState2:Byte = 0

def state2 = lState2&0xFF
override def state2 = lState2&0xFF
def setState2(state:Int){ lState2 = state.toByte }

def clientState2 = false
Expand Down Expand Up @@ -260,7 +260,7 @@ trait TTimerGatePart extends RedstoneGatePart with ITimerGuiLogic
if (pointer_start >= 0) pointer_start = world.getGameTime-pointer_start
}

def pointerValue:Int =
override def pointerValue:Int =
if (pointer_start < 0) 0
else (world.getGameTime-pointer_start).toInt

Expand Down Expand Up @@ -303,7 +303,8 @@ trait TTimerGatePart extends RedstoneGatePart with ITimerGuiLogic
}
}

def interpPointer(f:Float):Float = if (pointer_start < 0) 0f else (pointerValue+f)/pointer_max
override def isPointerStarted:Boolean = pointer_start >= 0
override def pointerMax:Int = pointer_max

override def gateLogicActivate(player:PlayerEntity, held:ItemStack, hit:PartRayTraceResult):Boolean = {
if (held.isEmpty || !held.getItem.isInstanceOf[IScrewdriver]) {
Expand Down Expand Up @@ -397,6 +398,12 @@ class Sequencer extends RedstoneGatePart(GateType.SEQUENCER) with ITimerGuiLogic

def sendPointerMaxUpdate():Unit = { sendUpdate(12, _.writeInt(pointer_max)) }

override def isPointerStarted: Boolean = true

override def pointerValue: Int = (world.getDayTime%(pointer_max*4)).toInt

override def pointerMax: Int = pointer_max

override def gateLogicOnTick():Unit = {
if (!world.isClientSide) {
val oldOut = state>>4
Expand Down Expand Up @@ -551,6 +558,10 @@ class Counter extends RedstoneGatePart(GateType.COUNTER) with ICounterGuiLogic
if (newOutput != oldOutput) onOutputChange(5)
}

override def isPointerStarted:Boolean = true
override def pointerValue:Int = value
override def pointerMax:Int = max

override def gateLogicActivate(player:PlayerEntity, held:ItemStack, hit:PartRayTraceResult):Boolean = {
if (held.isEmpty || !held.getItem.isInstanceOf[IScrewdriver]) {
if (!world.isClientSide) GuiCounter.open(player, this)
Expand Down Expand Up @@ -670,7 +681,7 @@ class Comparator extends RedstoneGatePart(GateType.COMPARATOR) with INeighborTil
{
var lState2:Short = 0

def state2:Int = lState2&0xFFFF
override def state2:Int = lState2&0xFFFF
def setState2(i:Int){ lState2 = i.toShort }

override def outputMask(shape:Int) = 1
Expand Down
Loading

0 comments on commit 6595d8b

Please sign in to comment.