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

160 lines
4.5 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
2014-11-12 13:26:51 +01:00
import net.minecraft.entity.player.EntityPlayer
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
import resonant.lib.content.prefab.TIO
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
/**
* 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-22 10:42:53 +01:00
val fieldStrength = 1
val coils = 10
val area = 1
val motorConstant = fieldStrength * area * coils
2014-11-22 16:11:56 +01:00
}
class TileMotor extends SpatialTile(Material.iron) with TIO 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
ioMap = 0
2015-01-21 13:50:01 +01:00
saveIOMap = true
nodes.add(electricNode)
2014-11-12 13:26:51 +01:00
nodes.add(mechNode)
2015-01-22 10:42:53 +01:00
electricNode.resistance = 10
2014-11-12 13:26:51 +01:00
def toggleGearRatio() = (gearRatio + 1) % 3
2014-08-06 13:57:45 +02:00
override def start()
{
super.start()
updateConnectionMask()
}
2015-01-07 03:59:50 +01:00
2014-11-12 13:26:51 +01:00
override def update()
2014-11-02 13:51:56 +01:00
{
super.update()
2015-01-22 10:42:53 +01:00
/**
* Produce torque based on current.
*/
val power = electricNode.power
val torque = TileMotor.motorConstant * electricNode.current
//TODO: Check if angular velocity should be generated based on torque
if (torque != 0)
mechNode.rotate(torque, power / torque)
/**
* Motors produce emf or counter-emf by Lenz's law based on angular velocity
* emf = change of flux/time
* = (NBCos(x))/time
*/
val inducedEmf = TileMotor.motorConstant * mechNode.angularVelocity // * Math.sin(mechNode.angularVelocity * System.currentTimeMillis() / 1000d)
electricNode.generateVoltage(inducedEmf * -1)
2014-08-06 13:57:45 +02:00
}
override def setIO(dir: ForgeDirection, ioType: Int)
{
if (dir != getDirection || dir != getDirection.getOpposite)
{
super.setIO(dir, ioType)
//Auto-set opposite side for unreachable sides
if (ioType != 0)
super.setIO(dir.getOpposite, (ioType % 2) + 1)
updateConnectionMask()
}
}
2015-01-22 10:42:53 +01:00
def updateConnectionMask()
{
electricNode.connectionMask = ForgeDirection.VALID_DIRECTIONS.filter(getIO(_) > 0).map(d => 1 << d.ordinal()).foldLeft(0)(_ | _)
electricNode.positiveTerminals.clear()
electricNode.negativeTerminals.clear()
electricNode.positiveTerminals.addAll(getInputDirections())
electricNode.negativeTerminals.addAll(getOutputDirections())
electricNode.reconstruct()
notifyChange()
markUpdate()
}
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)
{
2015-01-21 13:50:01 +01:00
super[TIO].readFromNBT(nbt)
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)
{
2015-01-21 13:50:01 +01:00
super[TIO].writeToNBT(nbt)
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 (player.isSneaking)
2015-01-09 05:10:44 +01:00
{
if (!world.isRemote)
{
gearRatio = (gearRatio + 1) % 3
player.addChatComponentMessage(new ChatComponentText("Toggled gear ratio: " + gearRatio))
}
return true
2015-01-09 05:10:44 +01:00
}
return super.configure(player, side, hit)
2015-01-09 05:10:44 +01:00
}
2014-08-06 13:57:45 +02:00
}