Gear teeth now align properly

This commit is contained in:
Calclavia 2014-11-10 21:20:42 +08:00
parent 50bd4d9af0
commit a5c8641710
7 changed files with 85 additions and 43 deletions

View file

@ -1,6 +1,6 @@
package resonantinduction.core.prefab.part.connector
import codechicken.lib.data.MCDataInput
import codechicken.lib.data.{MCDataInput, MCDataOutput}
import codechicken.multipart.{IRedstonePart, TMultiPart}
import net.minecraft.item.ItemStack
import net.minecraft.util.MovingObjectPosition
@ -59,12 +59,44 @@ abstract class PartAbstract extends TMultiPart with TraitTicker
return false
}
/**
* Packet methods
*
* Zero is always the description packet.
*/
def sendPacket(id: Int)
{
assert(!world.isRemote, "[PartAbstract] Attempt to send packet from client side!")
write(getWriteStream, id)
}
def write(packet: MCDataOutput, id: Int)
{
packet.writeByte(id)
}
/**
* Should NOT override!
*/
override def writeDesc(packet: MCDataOutput)
{
write(packet, 0)
}
/**
* Should NOT override!
*/
override def readDesc(packet: MCDataInput)
{
read(packet)
}
override def read(packet: MCDataInput)
{
read(packet, packet.readUByte)
}
def read(packet: MCDataInput, packetID: Int)
def read(packet: MCDataInput, id: Int)
{
}

View file

@ -47,7 +47,8 @@ trait TPartNodeProvider extends PartAbstract with INodeProvider
override def onWorldSeparate()
{
nodes.foreach(_.deconstruct())
if (!world.isRemote)
nodes.foreach(_.deconstruct())
}
override def save(nbt: NBTTagCompound)

View file

@ -42,7 +42,7 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
if (markVelocityUpdate)
{
sendVelocityPacket()
sendPacket(1)
markVelocityUpdate = false
}
}
@ -57,45 +57,40 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
{
if (!world.isRemote)
{
println("Vel: " + mechanicalNode.angularVelocity)
println("Angle: " + mechanicalNode.prevAngle)
sendPacket(2)
}
super.activate(player, hit, item)
}
/** Packet Code. */
def sendVelocityPacket()
override def write(packet: MCDataOutput, id: Int)
{
if (world != null && !world.isRemote)
super.write(packet, id)
id match
{
getWriteStream.writeByte(1).writeDouble(mechanicalNode.angularVelocity)
case 0 =>
{
val nbt = new NBTTagCompound
save(nbt)
packet.writeNBTTagCompound(nbt)
}
case 1 => packet.writeFloat(mechanicalNode.angularVelocity.toFloat)
case 2 => packet.writeFloat(mechanicalNode.prevAngle.toFloat)
}
}
override def read(packet: MCDataInput, packetID: Int)
override def read(packet: MCDataInput, id: Int)
{
if (packetID == 0)
{
load(packet.readNBTTagCompound)
}
else if (packetID == 1)
{
mechanicalNode.angularVelocity = packet.readDouble
}
}
super.read(packet, id)
override def readDesc(packet: MCDataInput)
{
packet.readByte
load(packet.readNBTTagCompound)
}
override def writeDesc(packet: MCDataOutput)
{
packet.writeByte(0)
val nbt: NBTTagCompound = new NBTTagCompound
save(nbt)
packet.writeNBTTagCompound(nbt)
id match
{
case 0 => load(packet.readNBTTagCompound)
case 1 => mechanicalNode.angularVelocity = packet.readFloat()
case 2 => mechanicalNode.prevAngle = packet.readFloat()
}
}
override def redstoneConductionMap: Int = 0

View file

@ -18,7 +18,7 @@ import resonantinduction.mechanical.mech.grid.MechanicalNode
*/
class GearNode(parent: PartGear) extends MechanicalNode(parent: PartGear)
{
teethDisplacement = 0.01D
angleDisplacement = Math.PI / 12
protected def gear = getParent.asInstanceOf[PartGear]
@ -52,7 +52,7 @@ class GearNode(parent: PartGear) extends MechanicalNode(parent: PartGear)
val tileBehind: TileEntity = new Vector3(gear.tile).add(gear.placementSide).getTileEntity(world)
if (tileBehind.isInstanceOf[INodeProvider])
{
val instance: MechanicalNode = (tileBehind.asInstanceOf[INodeProvider]).getNode(classOf[MechanicalNode], gear.placementSide.getOpposite).asInstanceOf[MechanicalNode]
val instance: MechanicalNode = (tileBehind.asInstanceOf[INodeProvider]).getNode(classOf[MechanicalNode], gear.placementSide.getOpposite)
if (instance != null && instance != this && !(instance.getParent.isInstanceOf[PartGearShaft]) && instance.canConnect(this, gear.placementSide.getOpposite))
{
connect(instance, gear.placementSide)
@ -86,7 +86,7 @@ class GearNode(parent: PartGear) extends MechanicalNode(parent: PartGear)
val checkTile: TileEntity = new Vector3(gear.tile).add(checkDir).getTileEntity(world)
if (!directionMap.containsValue(checkDir) && checkTile.isInstanceOf[INodeProvider])
{
val instance: MechanicalNode = (checkTile.asInstanceOf[INodeProvider]).getNode(classOf[MechanicalNode], gear.placementSide).asInstanceOf[MechanicalNode]
val instance: MechanicalNode = (checkTile.asInstanceOf[INodeProvider]).getNode(classOf[MechanicalNode], gear.placementSide)
if (instance != null && instance != this && instance.canConnect(this, checkDir.getOpposite) && !(instance.getParent.isInstanceOf[PartGearShaft]))
{
connect(instance, checkDir)

View file

@ -42,6 +42,8 @@ class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
markVelocityUpdate = true
}
mechanicalNode.onGridReconstruct = () => if(!world.isRemote) sendPacket(2)
//TODO: Can we not have update ticks here?
override def update()
{

View file

@ -17,7 +17,7 @@ class MechanicalGrid extends GridNode[MechanicalNode](classOf[MechanicalNode]) w
* A map marking out the relative spin directions of each node.
* Updated upon recache
*/
protected[grid] val spinMap = mutable.WeakHashMap.empty[MechanicalNode, Boolean]
val spinMap = mutable.WeakHashMap.empty[MechanicalNode, Boolean]
/**
* The power of the mechanical grid
@ -41,6 +41,10 @@ class MechanicalGrid extends GridNode[MechanicalNode](classOf[MechanicalNode]) w
super.populateNode(node, prev)
val dir = if (prev != null) (if (node.inverseRotation(prev)) !spinMap(prev) else spinMap(prev)) else false
spinMap += (node -> dir)
//Set mechanical node's initial angle
if (prev != null)
node.prevAngle = (prev.prevAngle + prev.angleDisplacement) % (2 * Math.PI)
}
override def update(deltaTime: Double)

View file

@ -15,23 +15,30 @@ import resonantinduction.core.prefab.node.TMultipartNode
*/
class MechanicalNode(parent: INodeProvider) extends NodeGrid[MechanicalNode](parent) with TMultipartNode[MechanicalNode] with TMechanicalNode with IVectorWorld
{
/**
* Allows the node to share its power with other nodes
*/
var torque = 0D
var angularVelocity = 0D
/**
* Buffer values used by the grid to transfer mechanical energy.
*/
protected[grid] var bufferTorque = 0D
protected[grid] var bufferAngle = 0D
/**
* A percentage value indicating how much friction the node has.
*/
var load = 0.2
/**
* Angle calculations
*/
private var prevTime = 0L
private var prevAngle = 0D
protected var teethDisplacement = 0D
protected var prevTime = 0L
var prevAngle = 0D
/**
* The amount of angle in radians displaced. This is used to align the gear teeth.
*/
var angleDisplacement = 0D
/**
* Events
@ -48,7 +55,7 @@ class MechanicalNode(parent: INodeProvider) extends NodeGrid[MechanicalNode](par
val deltaTime = (System.currentTimeMillis() - prevTime) / 1000D
prevTime = System.currentTimeMillis()
prevAngle = (prevAngle + deltaTime * angularVelocity) % (2 * Math.PI)
return prevAngle + (if (getMechanicalGrid != null && getMechanicalGrid.spinMap(this)) teethDisplacement else 0D)
return prevAngle
}
@deprecated
@ -76,7 +83,8 @@ class MechanicalNode(parent: INodeProvider) extends NodeGrid[MechanicalNode](par
*/
def getAngularVelocityLoad: Double = load
def getPower: Double = getMechanicalGrid.power
//TODO: Create new grids automatically?
def getPower: Double = if (getMechanicalGrid != null) getMechanicalGrid.power else 0
def getMechanicalGrid: MechanicalGrid = super.getGrid.asInstanceOf[MechanicalGrid]