electrodynamics/src/main/scala/edx/mechanical/mech/turbine/TileTurbine.scala

200 lines
5.1 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.mechanical.mech.turbine
import java.util.{HashSet, Set}
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.mechanical.mech.TileMechanical
2014-11-18 05:39:21 +01:00
import io.netty.buffer.ByteBuf
import net.minecraft.block.Block
import net.minecraft.block.material.Material
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.AxisAlignedBB
import net.minecraft.world.World
import net.minecraftforge.common.util.ForgeDirection
2015-01-26 13:28:38 +01:00
import resonantengine.core.network.discriminator.PacketType
2015-01-26 12:40:32 +01:00
import resonantengine.lib.transform.vector.Vector3
import resonantengine.lib.wrapper.ByteBufWrapper._
2015-01-26 13:29:32 +01:00
import resonantengine.prefab.block.multiblock.IMultiBlockStructure
2014-11-18 05:39:21 +01:00
import scala.collection.JavaConversions._
/** Reduced version of the main turbine class */
2014-11-14 17:22:38 +01:00
class TileTurbine extends TileMechanical(Material.wood) with IMultiBlockStructure[TileTurbine]
{
2015-01-14 12:06:03 +01:00
/** MutliBlock methods. */
private val multiBlock = new TurbineMBlockHandler(this)
2014-11-14 17:22:38 +01:00
/** Tier of the turbine */
var tier = 0
/** Radius of large turbine? */
var multiBlockRadius = 1
//Constructor
2014-11-23 02:16:05 +01:00
mechanicalNode = new NodeTurbine(this)
2014-11-14 17:22:38 +01:00
normalRender = false
isOpaqueCube = false
2014-11-23 02:16:05 +01:00
textureName = "material_wood_surface"
2014-11-14 17:22:38 +01:00
rotationMask = 63
override def onRemove(block: Block, par1: Int)
{
getMultiBlock.deconstruct()
2014-11-18 05:39:21 +01:00
super.onRemove(block, par1)
2014-11-14 17:22:38 +01:00
}
override def update()
{
super.update()
2014-11-18 05:39:21 +01:00
2014-11-14 17:22:38 +01:00
getMultiBlock.update()
if (getMultiBlock.isPrimary)
{
2014-11-14 17:22:38 +01:00
if (mechanicalNode.angularVelocity != 0)
{
2014-11-18 05:39:21 +01:00
playSound()
2014-11-14 17:22:38 +01:00
}
}
2014-11-14 17:22:38 +01:00
}
/** Called to play sound effects */
2014-11-18 05:39:21 +01:00
def playSound()
2014-11-14 17:22:38 +01:00
{
}
2015-01-14 12:06:03 +01:00
def getArea: Int = (((multiBlockRadius + 0.5) * 2) * ((multiBlockRadius + 0.5) * 2)).toInt
2014-11-14 17:22:38 +01:00
@SideOnly(Side.CLIENT)
override def getRenderBoundingBox: AxisAlignedBB =
{
return AxisAlignedBB.getBoundingBox(this.xCoord - multiBlockRadius, this.yCoord - multiBlockRadius, this.zCoord - multiBlockRadius, this.xCoord + 1 + multiBlockRadius, this.yCoord + 1 + multiBlockRadius, this.zCoord + 1 + multiBlockRadius)
}
def getMultiBlockVectors: java.lang.Iterable[Vector3] =
{
val vectors: Set[Vector3] = new HashSet[Vector3]
val dir: ForgeDirection = getDirection
val xMulti: Int = if (dir.offsetX != 0) 0 else 1
val yMulti: Int = if (dir.offsetY != 0) 0 else 1
val zMulti: Int = if (dir.offsetZ != 0) 0 else 1
for (x: Int <- -multiBlockRadius to multiBlockRadius)
{
2014-11-14 17:22:38 +01:00
for (y: Int <- -multiBlockRadius to multiBlockRadius)
{
2014-11-14 17:22:38 +01:00
for (z: Int <- -multiBlockRadius to multiBlockRadius)
{
2014-11-14 17:22:38 +01:00
vectors.add(new Vector3(x * xMulti, y * yMulti, z * zMulti))
}
2014-11-14 17:22:38 +01:00
}
}
2014-11-14 17:22:38 +01:00
return vectors
}
2021-04-05 14:41:30 +02:00
override def getPosition: Vector3 = toVectorWorld
2014-11-14 17:22:38 +01:00
2014-11-22 14:52:45 +01:00
def onMultiBlockChanged()
2014-11-14 17:22:38 +01:00
{
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, if (getBlockType != null) getBlockType else null)
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord)
}
2014-11-23 04:03:11 +01:00
/** Reads a tile entity from NBT. */
override def readFromNBT(nbt: NBTTagCompound)
2014-11-18 05:39:21 +01:00
{
2014-11-23 04:03:11 +01:00
super.readFromNBT(nbt)
multiBlockRadius = nbt.getInteger("multiBlockRadius")
tier = nbt.getInteger("tier")
getMultiBlock.load(nbt)
}
2014-11-18 05:39:21 +01:00
2014-11-23 04:03:11 +01:00
/** Writes a tile entity to NBT. */
override def writeToNBT(nbt: NBTTagCompound)
{
super.writeToNBT(nbt)
nbt.setInteger("multiBlockRadius", multiBlockRadius)
nbt.setInteger("tier", tier)
getMultiBlock.save(nbt)
2014-11-18 05:39:21 +01:00
}
override def read(buf: ByteBuf, id: Int, packetType: PacketType)
{
super.read(buf, id, packetType)
if (id == 0)
2014-11-23 04:03:11 +01:00
{
multiBlockRadius = buf.readInt()
tier = buf.readInt()
buf >>>> getMultiBlock.load
}
}
2015-01-27 09:15:44 +01:00
def getMultiBlock: TurbineMBlockHandler =
{
return multiBlock
}
2014-11-23 04:03:11 +01:00
override def write(buf: ByteBuf, id: Int)
{
super.write(buf, id)
if (id == 0)
{
buf <<< multiBlockRadius
buf <<< tier
buf <<<< getMultiBlock.save
}
2014-11-18 05:39:21 +01:00
}
2014-11-14 17:22:38 +01:00
def getWorld: World =
{
return worldObj
}
override def configure(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
if (!player.isSneaking)
{
2014-11-14 17:22:38 +01:00
if (getMultiBlock.isConstructed)
{
2014-11-18 05:39:21 +01:00
getMultiBlock.deconstruct()
2014-11-14 17:22:38 +01:00
multiBlockRadius += 1
2014-11-18 05:39:21 +01:00
if (!getMultiBlock.construct())
{
2014-11-14 17:22:38 +01:00
multiBlockRadius = 1
}
2014-11-14 17:22:38 +01:00
return true
}
else
{
2014-11-18 05:39:21 +01:00
if (!getMultiBlock.construct())
{
2014-11-14 17:22:38 +01:00
multiBlockRadius = 1
2014-11-18 05:39:21 +01:00
getMultiBlock.construct()
}
2014-11-14 17:22:38 +01:00
}
}
2014-11-14 17:22:38 +01:00
else
{
2014-11-14 17:22:38 +01:00
val toFlip: Set[TileTurbine] = new HashSet[TileTurbine]
if (!getMultiBlock.isConstructed)
{
toFlip.add(this)
}
else
{
val str: Set[TileTurbine] = getMultiBlock.getPrimary.getMultiBlock.getStructure
if (str != null) toFlip.addAll(str)
}
for (turbine <- toFlip)
{
if (side == turbine.getDirection.ordinal) world.setBlockMetadataWithNotify(turbine.xCoord, turbine.yCoord, turbine.zCoord, side ^ 1, 3)
else world.setBlockMetadataWithNotify(turbine.xCoord, turbine.yCoord, turbine.zCoord, side, 3)
}
}
2014-11-14 17:22:38 +01:00
return true
}
2021-04-05 14:41:30 +02:00
}