electrodynamics/src/main/scala/edx/electrical/generator/TileMotor.scala

135 lines
3.9 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.electrical.generator
2014-08-06 13:57:45 +02:00
2014-11-22 16:11:56 +01:00
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.core.Reference
import edx.mechanical.mech.grid.NodeMechanical
2014-08-06 13:57:45 +02:00
import net.minecraft.block.material.Material
import net.minecraft.entity.EntityLivingBase
2014-11-12 13:26:51 +01:00
import net.minecraft.entity.player.EntityPlayer
2014-11-22 16:11:56 +01:00
import net.minecraft.item.ItemStack
2014-08-06 13:57:45 +02:00
import net.minecraft.nbt.NBTTagCompound
2014-11-22 16:11:56 +01:00
import net.minecraft.util.{ChatComponentText, ResourceLocation}
import net.minecraftforge.client.model.AdvancedModelLoader
2015-01-07 03:59:50 +01:00
import net.minecraftforge.common.util.ForgeDirection
2014-11-22 16:11:56 +01:00
import org.lwjgl.opengl.GL11
2015-01-17 05:30:25 +01:00
import resonant.lib.grid.core.TSpatialNodeProvider
2015-01-09 05:10:44 +01:00
import resonant.lib.prefab.tile.spatial.SpatialTile
import resonant.lib.prefab.tile.traits.{TElectric, TRotatable}
2014-11-22 16:11:56 +01:00
import resonant.lib.render.RenderUtility
2014-11-12 13:26:51 +01:00
import resonant.lib.transform.vector.Vector3
2014-08-06 13:57:45 +02:00
import scala.collection.convert.wrapAll._
2015-01-17 05:26:14 +01:00
2014-08-06 13:57:45 +02:00
/**
* A kinetic energy to electrical energy converter.
*
* @author Calclavia
*/
2014-11-22 16:11:56 +01:00
object TileMotor
{
@SideOnly(Side.CLIENT)
val model = AdvancedModelLoader.loadModel(new ResourceLocation(Reference.domain, Reference.modelPath + "motor.tcn"))
@SideOnly(Side.CLIENT)
val texture = new ResourceLocation(Reference.domain, Reference.modelPath + "motor.png")
}
2015-01-09 05:10:44 +01:00
class TileMotor extends SpatialTile(Material.iron) with TElectric with TSpatialNodeProvider with TRotatable
2014-11-02 13:51:56 +01:00
{
2014-11-13 03:22:46 +01:00
var mechNode = new NodeMechanical(this)
2015-01-07 03:59:50 +01:00
{
override def canConnect(from: ForgeDirection): Boolean =
{
connectionMask = 1 << getDirection.getOpposite.ordinal
return super.canConnect(from)
}
}
2014-11-06 16:11:08 +01:00
private var gearRatio = 0
2014-08-06 13:57:45 +02:00
2014-11-23 02:16:05 +01:00
textureName = "material_wood_surface"
2014-11-06 16:11:08 +01:00
normalRender = false
isOpaqueCube = false
nodes.add(electricNode)
2014-11-12 13:26:51 +01:00
nodes.add(mechNode)
electricNode.resistance = 100
electricNode.positiveTerminals.addAll(Seq(ForgeDirection.UP, ForgeDirection.SOUTH, ForgeDirection.EAST))
electricNode.negativeTerminals.addAll(Seq(ForgeDirection.DOWN, ForgeDirection.NORTH, ForgeDirection.WEST))
2014-11-12 13:26:51 +01:00
def toggleGearRatio() = (gearRatio + 1) % 3
2014-08-06 13:57:45 +02:00
/**
* Called when the block is placed by a living entity
* @param entityLiving - entity who placed the block
* @param itemStack - ItemStack the entity used to place the block
*/
override def onPlaced(entityLiving: EntityLivingBase, itemStack: ItemStack)
{
super.onPlaced(entityLiving, itemStack)
2015-01-07 03:59:50 +01:00
if (!world.isRemote)
mechNode.reconstruct()
}
2014-11-12 13:26:51 +01:00
override def update()
2014-11-02 13:51:56 +01:00
{
super.update()
/*
//TODO: Debug with free energy
if (mechNode.power > dcNode.power)
2014-11-02 13:51:56 +01:00
{
2014-11-12 13:26:51 +01:00
//Produce electricity
dcNode.setVoltage(mechNode.power)
2014-11-12 13:26:51 +01:00
//TODO: Resist mech energy
2014-08-06 13:57:45 +02:00
}
2014-11-12 13:26:51 +01:00
// else if (dcNode.power > mechNode.power)
else*/
2014-11-02 13:51:56 +01:00
{
2014-11-12 13:26:51 +01:00
//Produce mechanical energy
2015-01-17 05:26:14 +01:00
val mechRatio = Math.pow(10, gearRatio)
val power = electricNode.power
val negate = if (electricNode.voltage > 0) 1 else -1
2015-01-17 05:26:14 +01:00
mechNode.rotate(negate * power * mechRatio, negate * power / mechRatio)
//TODO: Resist DC energy
2014-08-06 13:57:45 +02:00
}
}
2014-11-22 16:11:56 +01:00
@SideOnly(Side.CLIENT)
override def renderDynamic(pos: Vector3, frame: Float, pass: Int): Unit =
{
GL11.glPushMatrix()
GL11.glTranslatef(pos.x.toFloat + 0.5f, pos.y.toFloat + 0.5f, pos.z.toFloat + 0.5f)
GL11.glRotatef(90, 0, 1, 0)
RenderUtility.rotateBlockBasedOnDirection(getDirection)
RenderUtility.bind(TileMotor.texture)
TileMotor.model.renderAll()
GL11.glPopMatrix()
}
2014-11-02 13:51:56 +01:00
override def readFromNBT(nbt: NBTTagCompound)
{
2014-08-06 13:57:45 +02:00
super.readFromNBT(nbt)
gearRatio = nbt.getByte("gear")
}
2014-11-02 13:51:56 +01:00
override def writeToNBT(nbt: NBTTagCompound)
{
2014-08-06 13:57:45 +02:00
super.writeToNBT(nbt)
2014-11-06 16:11:08 +01:00
nbt.setByte("gear", gearRatio.toByte)
2014-08-06 13:57:45 +02:00
}
2014-11-12 13:26:51 +01:00
override def toString: String = "[TileMotor]" + x + "x " + y + "y " + z + "z "
2015-01-07 03:59:50 +01:00
2015-01-09 05:10:44 +01:00
override protected def configure(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{
if (!world.isRemote)
{
gearRatio = (gearRatio + 1) % 3
player.addChatComponentMessage(new ChatComponentText("Toggled gear ratio: " + gearRatio))
}
return true
}
2014-08-06 13:57:45 +02:00
}