electrodynamics/src/main/scala/edx/mechanical/mech/process/mixer/TileMixer.scala

216 lines
7 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.mechanical.mech.process.mixer
2014-09-27 17:10:47 +02:00
import java.util.{LinkedHashSet, List, Set}
2014-10-25 13:32:36 +02:00
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.core.Reference
import edx.mechanical.mech.TileMechanical
2014-09-27 17:10:47 +02:00
import net.minecraft.block.Block
import net.minecraft.block.material.Material
import net.minecraft.entity.Entity
import net.minecraft.entity.item.EntityItem
import net.minecraft.item.ItemStack
2014-10-25 13:32:36 +02:00
import net.minecraft.util.{AxisAlignedBB, ResourceLocation}
import net.minecraftforge.client.model.{AdvancedModelLoader, IModelCustom}
2014-09-27 17:10:47 +02:00
import net.minecraftforge.fluids.IFluidBlock
2014-10-25 13:32:36 +02:00
import org.lwjgl.opengl.GL11
import org.lwjgl.opengl.GL11._
2015-01-26 13:17:04 +01:00
import resonantengine.api.edx.recipe.{MachineRecipes, RecipeType}
2015-01-26 12:40:32 +01:00
import resonantengine.lib.render.RenderUtility
import resonantengine.lib.transform.rotation.Quaternion
import resonantengine.lib.transform.vector.Vector3
import resonantengine.prefab.misc.Timer
2014-10-25 13:32:36 +02:00
2014-09-27 17:10:47 +02:00
import scala.collection.JavaConversions._
/**
* @author Calclavia
*/
object TileMixer
{
2014-10-25 13:32:36 +02:00
final val PROCESS_TIME: Int = 12 * 20
final val MIXER_ITEM_TIMER: Timer[EntityItem] = new Timer[EntityItem]
@SideOnly(Side.CLIENT) val MODEL: IModelCustom = AdvancedModelLoader.loadModel(new ResourceLocation(Reference.domain, Reference.modelPath + "mixer.tcn"))
@SideOnly(Side.CLIENT) var TEXTURE: ResourceLocation = new ResourceLocation(Reference.domain, Reference.modelPath + "mixer.png")
2014-09-27 17:10:47 +02:00
}
class TileMixer extends TileMechanical(Material.iron)
{
2014-10-25 13:32:36 +02:00
private var areaBlockedFromMoving: Boolean = false
//Constructor
mechanicalNode = new MixerNode(this)
2015-01-17 06:13:26 +01:00
isOpaqueCube = false
normalRender = false
customItemRender = true
textureName = "material_metal_top"
2014-10-25 13:32:36 +02:00
override def update
{
super.update
if (!world.isRemote && ticks % 3 == 0)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
this.areaBlockedFromMoving = checkIsBlocked
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
if (canWork)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
doWork
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
}
2014-09-27 17:10:47 +02:00
2014-10-25 13:32:36 +02:00
/** Checks to see if the area around the mixer is blocked (3x3 - excluding center)
* @return true if there is a non-fluid block inside the bounds
*/
def checkIsBlocked: Boolean =
{
for (x <- -1 to 1)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
for (z <- -1 to 1)
{
if (x != 0 && z != 0)
2014-09-27 17:10:47 +02:00
{
2021-04-05 14:41:30 +02:00
val block: Block = toVectorWorld.add(x, 0, z).getBlock(world)
2014-10-25 13:32:36 +02:00
if (block != null && !(block.isInstanceOf[IFluidBlock]))
{
return true
}
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
}
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
return false
}
2014-09-27 17:10:47 +02:00
2014-10-25 13:32:36 +02:00
/**
* Can this machine work this tick?
*
* @return
*/
def canWork: Boolean =
{
return mechanicalNode.angularVelocity != 0 && !areaBlockedFromMoving
2014-10-25 13:32:36 +02:00
}
2014-09-27 17:10:47 +02:00
2014-10-25 13:32:36 +02:00
def doWork
{
var didWork: Boolean = false
val aabb: AxisAlignedBB = AxisAlignedBB.getBoundingBox(this.xCoord - 1, this.yCoord, this.zCoord - 1, this.xCoord + 2, this.yCoord + 1, this.zCoord + 2)
val entities: List[_] = this.worldObj.getEntitiesWithinAABB(classOf[Entity], aabb)
val processItems: Set[EntityItem] = new LinkedHashSet[EntityItem]
for (obj <- entities)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
val entity: Entity = obj.asInstanceOf[Entity]
val originalPosition: Vector3 = new Vector3(entity)
2021-04-05 14:41:30 +02:00
val relativePosition: Vector3 = originalPosition - toVectorWorld.add(0.5)
relativePosition.transform(new Quaternion(-mechanicalNode.angularVelocity, new Vector3(1, 0, 0)))
2021-04-05 14:41:30 +02:00
val newPosition: Vector3 = toVectorWorld + 0.5 + relativePosition
2014-12-01 12:59:35 +01:00
val difference: Vector3 = (newPosition - originalPosition) * 0.5
2014-10-25 13:32:36 +02:00
entity.addVelocity(difference.x, difference.y, difference.z)
entity.onGround = false
if (entity.isInstanceOf[EntityItem])
{
2015-01-03 13:04:58 +01:00
if (MachineRecipes.instance.getOutput(RecipeType.MIXER.name, (entity.asInstanceOf[EntityItem]).getEntityItem).length > 0)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
processItems.add(entity.asInstanceOf[EntityItem])
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
}
}
2014-09-27 17:10:47 +02:00
2014-10-25 13:32:36 +02:00
for (processingItem <- processItems)
{
if (!TileMixer.MIXER_ITEM_TIMER.containsKey(processingItem))
{
TileMixer.MIXER_ITEM_TIMER.put(processingItem, TileMixer.PROCESS_TIME)
}
2021-04-05 14:41:30 +02:00
if (!processingItem.isDead && (toVectorWorld + 0.5).distance(new Vector3(processingItem)) < 2)
2014-10-25 13:32:36 +02:00
{
val timeLeft: Int = TileMixer.MIXER_ITEM_TIMER.decrease(processingItem)
if (timeLeft <= 0)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
if (doneWork(processingItem))
{
processingItem.getEntityItem.stackSize -= 1
if (processingItem.getEntityItem.stackSize <= 0)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
processingItem.setDead
TileMixer.MIXER_ITEM_TIMER.remove(processingItem)
2014-09-27 17:10:47 +02:00
}
else
{
2014-10-25 13:32:36 +02:00
processingItem.setEntityItemStack(processingItem.getEntityItem)
TileMixer.MIXER_ITEM_TIMER.put(processingItem, TileMixer.PROCESS_TIME)
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
}
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
else
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
processingItem.delayBeforeCanPickup = 20
this.worldObj.spawnParticle("bubble", processingItem.posX, processingItem.posY, processingItem.posZ, (Math.random - 0.5f) * 3, (Math.random - 0.5f) * 3, (Math.random - 0.5f) * 3)
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
didWork = true
}
else
{
TileMixer.MIXER_ITEM_TIMER.remove(processingItem)
}
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
if (didWork)
{
if (this.ticks % 20 == 0)
{
this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, Reference.prefix + "mixer", 0.5f, 1)
}
}
}
2014-09-27 17:10:47 +02:00
2014-10-25 13:32:36 +02:00
private def doneWork(entity: EntityItem): Boolean =
{
val mixPosition: Vector3 = new Vector3(entity.posX, yCoord, entity.posZ)
if (mixPosition.getBlock(world) ne getBlockType)
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
val block: Block = mixPosition.getBlock(worldObj)
val blockFluidFinite: Block = null //RIResourceFactory.getMixture(ResonantEngine.resourceFactory.getName(entity.getEntityItem))
2014-10-25 13:32:36 +02:00
if (blockFluidFinite != null)
{
/*if (block.isInstanceOf[BlockFluidMixture])
2014-09-27 17:10:47 +02:00
{
2014-10-25 13:32:36 +02:00
val itemStack: ItemStack = entity.getEntityItem.copy
2014-11-23 02:16:05 +01:00
if (block.asInstanceOf[BlockFluidMixture].mix(worldObj, mixPosition.xi, mixPosition.yi, mixPosition.zi, itemStack))
2014-10-25 13:32:36 +02:00
{
worldObj.notifyBlocksOfNeighborChange(mixPosition.xi, mixPosition.yi, mixPosition.zi, mixPosition.getBlock(worldObj))
return true
}
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
else if (block != null && (block == Blocks.water || block == Blocks.flowing_water))
{
mixPosition.setBlock(worldObj, blockFluidFinite)
}*/
2014-10-25 13:32:36 +02:00
}
2014-09-27 17:10:47 +02:00
}
2014-10-25 13:32:36 +02:00
return false
}
override def renderDynamic(position: Vector3, frame: Float, pass: Int): Unit =
{
GL11.glPushMatrix()
GL11.glTranslated(position.x + 0.5, position.y + 0.5, position.z + 0.5)
RenderUtility.bind(RenderMixer.TEXTURE)
RenderMixer.MODEL.renderOnly("centerTop", "centerBase")
glPushMatrix()
glRotatef(Math.toDegrees(mechanicalNode.angle.asInstanceOf[Float]).asInstanceOf[Float], 0, 1, 0)
2014-10-25 13:32:36 +02:00
RenderMixer.MODEL.renderAllExcept("centerTop", "centerBase")
glPopMatrix()
GL11.glPopMatrix()
}
2014-09-27 17:10:47 +02:00
2014-10-25 13:32:36 +02:00
override def renderInventory(itemStack: ItemStack)
{
glPushMatrix()
GL11.glScalef(0.7f, 0.7f, 0.7f)
glTranslatef(0.5F, 0.5f, 0.5f)
RenderUtility.bind(RenderMixer.TEXTURE)
RenderMixer.MODEL.renderAll()
glPopMatrix()
}
2021-04-05 14:41:30 +02:00
}