electrodynamics/src/main/scala/edx/mechanical/mech/gear/PartGear.scala

197 lines
5 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.mechanical.mech.gear
2014-09-27 20:37:05 +02:00
import java.util
import codechicken.lib.vec.{Cuboid6, Vector3}
2014-09-27 20:37:05 +02:00
import codechicken.microblock.FaceMicroClass
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.core.Reference
import edx.core.prefab.part.CuboidShapes
import edx.mechanical.MechanicalContent
import edx.mechanical.mech.PartMechanical
2014-09-27 20:37:05 +02:00
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.MovingObjectPosition
import net.minecraft.world.World
import net.minecraftforge.common.util.ForgeDirection
2014-12-09 23:46:07 +01:00
import resonant.api.tile.node.INode
2014-12-10 19:47:51 +01:00
import resonant.lib.prefab.tile.multiblock.reference.IMultiBlockStructure
2014-09-27 20:37:05 +02:00
import resonant.lib.utility.WrenchUtility
2014-11-02 13:51:56 +01:00
/**
* We assume all the force acting on the gear is 90 degrees.
*
* @author Calclavia
*/
2014-09-27 20:37:05 +02:00
class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
{
2015-01-14 12:06:03 +01:00
val multiBlock = new GearMultiBlockHandler(this)
2014-11-22 13:21:51 +01:00
var isClockwiseCrank = true
2014-11-13 14:11:00 +01:00
var manualCrankTime = 0
2014-11-22 13:21:51 +01:00
var multiBlockRadius = 1
2014-09-27 20:37:05 +02:00
2014-11-02 13:51:56 +01:00
//Constructor
2014-11-13 06:16:25 +01:00
mechanicalNode = new NodeGear(this)
2014-11-22 13:21:51 +01:00
2014-11-09 07:49:56 +01:00
mechanicalNode.onVelocityChanged = () =>
{
if (getMultiBlock.isPrimary)
if (world != null) sendPacket(1)
2014-11-13 14:34:16 +01:00
2014-11-22 11:25:59 +01:00
if (mechanicalNode.angularVelocity == 0)
if (world != null) sendPacket(2)
2014-11-09 07:49:56 +01:00
}
2014-11-02 13:51:56 +01:00
2014-11-23 04:59:44 +01:00
mechanicalNode.onGridReconstruct = () =>
{
if (world != null)
{
sendPacket(1)
sendPacket(2)
}
}
2014-11-10 14:20:42 +01:00
2014-11-09 06:28:58 +01:00
//TODO: Can we not have update ticks here?
override def update()
2014-11-02 13:51:56 +01:00
{
2014-11-09 06:28:58 +01:00
super.update()
2014-11-23 04:59:44 +01:00
if (!world.isRemote)
2014-11-02 13:51:56 +01:00
{
if (manualCrankTime > 0)
{
2014-11-21 05:17:28 +01:00
//A punch has around 5000 Newtons
2015-01-23 02:18:36 +01:00
mechanicalNode.accelerate((if (isClockwiseCrank) 2 else -2) * manualCrankTime)
2014-11-13 14:11:00 +01:00
manualCrankTime -= 1
2014-11-02 13:51:56 +01:00
}
}
2014-11-09 06:28:58 +01:00
getMultiBlock.update()
2014-11-02 13:51:56 +01:00
}
override def activate(player: EntityPlayer, hit: MovingObjectPosition, itemStack: ItemStack): Boolean =
{
2014-11-13 07:17:05 +01:00
if (!world.isRemote)
2014-11-23 04:59:44 +01:00
println(mechanicalNode + " in " + mechanicalNode.grid)
2014-11-13 14:34:16 +01:00
2014-11-02 13:51:56 +01:00
if (itemStack != null && itemStack.getItem.isInstanceOf[ItemHandCrank])
{
isClockwiseCrank = player.isSneaking
2014-11-13 14:11:00 +01:00
getMultiBlock.get.manualCrankTime = 40
2014-11-11 15:28:25 +01:00
2014-11-02 13:51:56 +01:00
world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, Reference.prefix + "gearCrank", 0.5f, 0.9f + world.rand.nextFloat * 0.2f)
player.addExhaustion(0.01f)
return true
}
2014-11-13 07:17:05 +01:00
2014-11-02 13:51:56 +01:00
if (WrenchUtility.isWrench(itemStack))
{
2014-11-13 07:17:05 +01:00
getMultiBlock.toggleConstruct()
2014-11-02 13:51:56 +01:00
return true
}
2014-11-13 07:17:05 +01:00
2014-11-02 13:51:56 +01:00
return super.activate(player, hit, itemStack)
}
2014-11-18 15:35:54 +01:00
override def preRemove()
2014-11-02 13:51:56 +01:00
{
2014-11-18 15:35:54 +01:00
super.preRemove()
2014-11-13 14:34:16 +01:00
getMultiBlock.deconstruct()
2014-11-02 13:51:56 +01:00
}
2015-01-14 12:06:03 +01:00
override def getMultiBlock: GearMultiBlockHandler = multiBlock
2014-11-13 16:04:21 +01:00
/**
* Is this gear block the one in the center-edge of the multiblock that can interact with other gears?
* @return Returning true implies that this gear is able to connect to other ones side-by-side.
*/
2014-11-02 13:51:56 +01:00
def isCenterMultiBlock: Boolean =
{
if (!getMultiBlock.isConstructed)
{
return true
}
2014-11-13 16:04:21 +01:00
val primaryPos = getMultiBlock.getPrimary.getPosition
return (primaryPos.xi == x && placementSide.offsetX == 0) || (primaryPos.yi == y && placementSide.offsetY == 0) || (primaryPos.zi == z && placementSide.offsetZ == 0)
2014-11-02 13:51:56 +01:00
}
2014-11-09 07:49:56 +01:00
@SideOnly(Side.CLIENT)
override def renderDynamic(pos: Vector3, frame: Float, pass: Int)
2014-11-02 13:51:56 +01:00
{
if (pass == 0)
RenderGear.renderDynamic(this, pos.x, pos.y, pos.z, tier)
2014-11-02 13:51:56 +01:00
}
override def load(nbt: NBTTagCompound)
{
super.load(nbt)
getMultiBlock.load(nbt)
}
override def save(nbt: NBTTagCompound)
{
super.save(nbt)
getMultiBlock.save(nbt)
}
2014-11-13 07:17:05 +01:00
override def getMultiBlockVectors: java.util.List[resonant.lib.transform.vector.Vector3] = new resonant.lib.transform.vector.Vector3().getAround(this.world, placementSide, 1)
2014-11-02 13:51:56 +01:00
def getWorld: World =
{
return world
}
2014-09-27 20:37:05 +02:00
2014-11-13 14:34:16 +01:00
def onMultiBlockChanged()
2014-11-02 13:51:56 +01:00
{
if (world != null)
2014-09-27 20:37:05 +02:00
{
2014-11-02 13:51:56 +01:00
tile.notifyPartChange(this)
2014-11-13 14:34:16 +01:00
2014-11-02 13:51:56 +01:00
if (!world.isRemote)
{
2014-11-13 14:34:16 +01:00
mechanicalNode.reconstruct()
sendDescUpdate()
2014-11-02 13:51:56 +01:00
}
2014-09-27 20:37:05 +02:00
}
2014-11-02 13:51:56 +01:00
}
2014-09-27 20:37:05 +02:00
2014-11-02 13:51:56 +01:00
override def getNode[N <: INode](nodeType: Class[_ <: N], from: ForgeDirection): N =
{
2014-11-26 16:13:24 +01:00
if (nodeType.isAssignableFrom(mechanicalNode.getClass) && from == placementSide)
2014-11-02 13:51:56 +01:00
return getMultiBlock.get.mechanicalNode.asInstanceOf[N]
2014-09-27 20:37:05 +02:00
2014-11-02 13:51:56 +01:00
return null.asInstanceOf[N]
}
2014-09-27 20:37:05 +02:00
2014-11-02 13:51:56 +01:00
/** Multipart Bounds */
def getOcclusionBoxes: java.lang.Iterable[Cuboid6] =
{
val list: java.util.List[Cuboid6] = new util.ArrayList[Cuboid6];
2014-11-11 13:51:23 +01:00
for (v <- CuboidShapes.panel(this.placementSide.ordinal))
2014-09-27 20:37:05 +02:00
{
2014-11-02 13:51:56 +01:00
list.add(v)
2014-09-27 20:37:05 +02:00
}
2014-11-02 13:51:56 +01:00
return list
}
2014-09-27 20:37:05 +02:00
2014-11-02 13:51:56 +01:00
def getBounds: Cuboid6 =
{
return FaceMicroClass.aBounds(0x10 | this.placementSide.ordinal)
}
2014-09-27 20:37:05 +02:00
2014-11-10 12:28:36 +01:00
@SideOnly(Side.CLIENT)
override def getRenderBounds: Cuboid6 = Cuboid6.full.copy.expand(multiBlockRadius)
2014-09-27 20:37:05 +02:00
2014-11-02 13:51:56 +01:00
override def toString = "[PartGear]" + x + "x " + y + "y " + z + "z " + getSlotMask + "s "
2015-01-14 12:06:03 +01:00
def getSlotMask: Int =
{
return 1 << this.placementSide.ordinal
}
protected def getItem: ItemStack =
{
return new ItemStack(MechanicalContent.itemGear, 1, tier)
}
2014-09-27 20:37:05 +02:00
}