MechanicalGrid now triggers onTorqueChanged() and onVelocityChanged()

This commit is contained in:
Calclavia 2014-11-09 14:07:39 +08:00
parent f039d282e1
commit b9e53b59e8
5 changed files with 36 additions and 134 deletions

View file

@ -1,17 +1,10 @@
package resonantinduction.mechanical.mech package resonantinduction.mechanical.mech
import java.util.{ArrayList, List}
import codechicken.lib.data.{MCDataInput, MCDataOutput} import codechicken.lib.data.{MCDataInput, MCDataOutput}
import codechicken.multipart._ import codechicken.multipart._
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.MovingObjectPosition
import net.minecraftforge.common.util.ForgeDirection import net.minecraftforge.common.util.ForgeDirection
import resonant.engine.ResonantEngine import resonant.lib.transform.vector.{Vector3, VectorWorld}
import resonant.lib.transform.vector.VectorWorld
import resonantinduction.core.prefab.part.connector.{PartAbstract, TPartNodeProvider} import resonantinduction.core.prefab.part.connector.{PartAbstract, TPartNodeProvider}
import resonantinduction.mechanical.mech.grid.MechanicalNode import resonantinduction.mechanical.mech.grid.MechanicalNode
@ -28,17 +21,15 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
def mechanicalNode_=(mech: MechanicalNode) def mechanicalNode_=(mech: MechanicalNode)
{ {
_mechanicalNode = mech _mechanicalNode = mech
mechanicalNode.onVelocityChanged = () => sendVelocityPacket()
nodes.add(mechanicalNode) nodes.add(mechanicalNode)
} }
protected var prevAngularVelocity: Double = .0
/** Packets */
private[mech] var markPacketUpdate: Boolean = false
/** Simple debug external GUI */
private[mech] var frame: DebugFrameMechanical = null
/** Side of the block this is placed on */ /** Side of the block this is placed on */
var placementSide: ForgeDirection = ForgeDirection.UNKNOWN var placementSide: ForgeDirection = ForgeDirection.UNKNOWN
var tier: Int = 0
/** The tier of this mechanical part */
var tier = 0
def preparePlacement(side: Int, itemDamage: Int) def preparePlacement(side: Int, itemDamage: Int)
{ {
@ -46,59 +37,8 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
this.tier = itemDamage this.tier = itemDamage
} }
override def update()
{
if (!world.isRemote)
{
checkClientUpdate()
}
if (frame != null)
{
frame.update
}
super.update()
}
override def activate(player: EntityPlayer, hit: MovingObjectPosition, itemStack: ItemStack): Boolean =
{
if (ResonantEngine.runningAsDev)
{
if (itemStack != null && !world.isRemote)
{
if (itemStack.getItem eq Items.stick)
{
if (ControlKeyModifer.isControlDown(player))
{
if (frame == null)
{
frame = new DebugFrameMechanical(this)
frame.showDebugFrame
}
else
{
frame.closeDebugFrame
frame = null
}
}
}
}
}
return super.activate(player, hit, itemStack)
}
def checkClientUpdate()
{
if (Math.abs(prevAngularVelocity - mechanicalNode.angularVelocity) >= 0.1)
{
prevAngularVelocity = mechanicalNode.angularVelocity
sendRotationPacket
}
}
/** Packet Code. */ /** Packet Code. */
def sendRotationPacket() def sendVelocityPacket()
{ {
if (world != null && !world.isRemote) if (world != null && !world.isRemote)
{ {
@ -148,27 +88,7 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
nbt.setByte("tier", tier.asInstanceOf[Byte]) nbt.setByte("tier", tier.asInstanceOf[Byte])
} }
protected def getItem: ItemStack def getPosition: VectorWorld = new VectorWorld(world, x, y, z)
override def getDrops: java.lang.Iterable[ItemStack] = override def toString: String = "[" + getClass.getSimpleName + "]" + x + "x " + y + "y " + z + "z " + getSlotMask + "s "
{
val drops: List[ItemStack] = new ArrayList[ItemStack]
drops.add(getItem)
return drops
}
override def pickItem(hit: MovingObjectPosition): ItemStack =
{
return getItem
}
def getPosition: VectorWorld =
{
return new VectorWorld(world, x, y, z)
}
override def toString: String =
{
return "[" + getClass.getSimpleName + "]" + x + "x " + y + "y " + z + "z " + getSlotMask + "s "
}
} }

View file

@ -36,6 +36,7 @@ class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
//Constructor //Constructor
mechanicalNode = new GearNode(this) mechanicalNode = new GearNode(this)
mechanicalNode.onVelocityChanged = () => if (getMultiBlock.isPrimary) sendVelocityPacket()
//TODO: Can we not have update ticks here? //TODO: Can we not have update ticks here?
override def update() override def update()
@ -54,11 +55,6 @@ class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
getMultiBlock.update() getMultiBlock.update()
} }
override def checkClientUpdate()
{
if (getMultiBlock.isPrimary) super.checkClientUpdate
}
override def activate(player: EntityPlayer, hit: MovingObjectPosition, itemStack: ItemStack): Boolean = override def activate(player: EntityPlayer, hit: MovingObjectPosition, itemStack: ItemStack): Boolean =
{ {
if (!world.isRemote) if (!world.isRemote)

View file

@ -2,35 +2,27 @@ package resonantinduction.mechanical.mech.gearshaft
import net.minecraft.tileentity.TileEntity import net.minecraft.tileentity.TileEntity
import net.minecraftforge.common.util.ForgeDirection import net.minecraftforge.common.util.ForgeDirection
import resonantinduction.mechanical.mech.gear.PartGear
import resonant.api.grid.INodeProvider import resonant.api.grid.INodeProvider
import resonant.lib.transform.vector.Vector3 import resonant.lib.transform.vector.Vector3
import resonantinduction.mechanical.mech.gear.PartGear
import resonantinduction.mechanical.mech.grid.MechanicalNode import resonantinduction.mechanical.mech.grid.MechanicalNode
class GearShaftNode(parent: INodeProvider) extends MechanicalNode(parent) class GearShaftNode(parent: PartGearShaft) extends MechanicalNode(parent)
{ {
override def getTorqueLoad: Double = override def getTorqueLoad: Double =
{ {
shaft.tier match return shaft.tier match
{ {
case 1 => case 0 => 0.03
return 0.02 case 1 => 0.02
case 2 => case 2 => 0.01
return 0.01
case _ =>
return 0.03
} }
} }
override def getAngularVelocityLoad: Double = override def getAngularVelocityLoad: Double = 0
{
return 0
}
override def reconstruct() override def rebuild()
{ {
connections.clear
for (ch <- List(shaft.placementSide, shaft.placementSide.getOpposite)) for (ch <- List(shaft.placementSide, shaft.placementSide.getOpposite))
{ {
val checkDir: ForgeDirection = ch val checkDir: ForgeDirection = ch
@ -81,13 +73,10 @@ class GearShaftNode(parent: INodeProvider) extends MechanicalNode(parent)
{ {
if (shaft.placementSide.offsetY != 0 || shaft.placementSide.offsetZ != 0) if (shaft.placementSide.offsetY != 0 || shaft.placementSide.offsetZ != 0)
{ {
return dir eq shaft.placementSide.getOpposite return dir == shaft.placementSide.getOpposite
} }
return dir eq shaft.placementSide return dir == shaft.placementSide
} }
def shaft: PartGearShaft = def shaft: PartGearShaft = getParent.asInstanceOf[PartGearShaft]
{
return this.getParent.asInstanceOf[PartGearShaft]
}
} }

View file

@ -41,31 +41,21 @@ class PartGearShaft extends PartMechanical
tier = itemDamage tier = itemDamage
} }
protected def getItem: ItemStack = protected def getItem: ItemStack = new ItemStack(MechanicalContent.itemGearShaft, 1, tier)
{
return new ItemStack(MechanicalContent.itemGearShaft, 1, tier)
}
@SideOnly(Side.CLIENT) override def renderDynamic(pos: Vector3, frame: Float, pass: Int) @SideOnly(Side.CLIENT)
override def renderDynamic(pos: Vector3, frame: Float, pass: Int)
{ {
if (pass == 0) if (pass == 0)
{
RenderGearShaft.INSTANCE.renderDynamic(this, pos.x, pos.y, pos.z, frame) RenderGearShaft.INSTANCE.renderDynamic(this, pos.x, pos.y, pos.z, frame)
}
} }
/** /**
* Multipart Bounds * Multipart Bounds
*/ */
def getSlotMask: Int = def getSlotMask: Int = PartMap.CENTER.mask
{
return PartMap.CENTER.mask
}
def getOcclusionBoxes: java.lang.Iterable[Cuboid6] = def getOcclusionBoxes: java.lang.Iterable[Cuboid6] = getCollisionBoxes
{
return getCollisionBoxes
}
override def getCollisionBoxes: java.lang.Iterable[Cuboid6] = override def getCollisionBoxes: java.lang.Iterable[Cuboid6] =
{ {
@ -92,8 +82,5 @@ class PartGearShaft extends PartMechanical
return subParts return subParts
} }
def getBounds: Cuboid6 = def getBounds: Cuboid6 = new Cuboid6(0.375, 0.375, 0.375, 0.625, 0.625, 0.625)
{
return new Cuboid6(0.375, 0.375, 0.375, 0.625, 0.625, 0.625)
}
} }

View file

@ -77,9 +77,19 @@ class MechanicalGrid extends GridNode[MechanicalNode](classOf[MechanicalNode]) w
//Set torque and angular velocity of all nodes //Set torque and angular velocity of all nodes
getNodes.foreach(n => getNodes.foreach(n =>
{ {
val prevTorque = n.torque
val prevAngularVelocity = n.angularVelocity
val inversion = if (spinMap(n)) 1 else -1 val inversion = if (spinMap(n)) 1 else -1
n.torque = delta._1 * n.ratio * inversion n.torque = delta._1 * n.ratio * inversion
n.angularVelocity = delta._2 / n.ratio * inversion n.angularVelocity = delta._2 / n.ratio * inversion
if (Math.abs(prevTorque - n.torque) >= 0.1)
n.onTorqueChanged()
if (Math.abs(prevAngularVelocity - n.angularVelocity) >= 0.1)
n.onVelocityChanged()
}) })
//Clear buffers //Clear buffers