electrodynamics/src/main/scala/edx/mechanical/mech/grid/NodeMechanical.scala

113 lines
3.3 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.mechanical.mech.grid
2014-11-06 07:05:42 +01:00
2015-01-14 12:06:03 +01:00
import edx.core.interfaces.TNodeMechanical
import edx.core.prefab.node.TMultipartNode
2015-01-26 13:17:04 +01:00
import resonantengine.api.graph.INodeProvider
import resonantengine.api.tile.IDebugInfo
import resonantengine.api.transform.vector.IVectorWorld
2015-01-26 12:40:32 +01:00
import resonantengine.lib.grid.core.{GridNode, NodeGrid, TTileConnector}
2014-11-06 07:05:42 +01:00
2014-11-13 06:16:25 +01:00
import scala.beans.BeanProperty
2014-11-22 13:21:51 +01:00
import scala.collection.convert.wrapAll._
2014-11-13 06:16:25 +01:00
2014-11-06 07:05:42 +01:00
/**
* Prefab node for the mechanical system used by almost ever mechanical object in Resonant Induction. Handles connections to other tiles, and shares power with them
*
* @author Calclavia, Darkguardsman
*/
2015-01-17 05:58:45 +01:00
class NodeMechanical(parent: INodeProvider) extends NodeGrid[NodeMechanical](parent) with TTileConnector[NodeMechanical] with TMultipartNode[NodeMechanical] with TNodeMechanical with IVectorWorld with IDebugInfo
2014-11-06 07:05:42 +01:00
{
2014-11-09 06:59:37 +01:00
/**
* Angle calculations
*/
var prevTime = System.currentTimeMillis()
2014-11-10 14:20:42 +01:00
var prevAngle = 0D
2014-11-09 05:06:09 +01:00
/**
* Events
*/
2014-11-13 06:16:25 +01:00
@BeanProperty
var onTorqueChanged: () => Unit = () => ()
2014-11-13 06:16:25 +01:00
@BeanProperty
var onVelocityChanged: () => Unit = () => ()
2015-01-08 14:24:04 +01:00
protected[grid] var prevTorque = 0D
protected[grid] var prevAngularVelocity = 0D
2014-11-22 11:25:59 +01:00
/**
2015-01-08 14:24:04 +01:00
* Buffer values used by the grid to transfer mechanical energy.
2014-11-22 11:25:59 +01:00
*/
2015-01-08 14:24:04 +01:00
protected[grid] var bufferTorque = 0D
private var _torque = 0D
private var _angularVelocity = 0D
2014-11-22 11:25:59 +01:00
/**
* An arbitrary angle value computed based on velocity
* @return The angle in radians
*/
def angle: Double =
2014-11-06 07:05:42 +01:00
{
val deltaTime = (System.currentTimeMillis() - prevTime) / 1000D
prevTime = System.currentTimeMillis()
prevAngle = (prevAngle + deltaTime * angularVelocity) % (2 * Math.PI)
2014-11-10 14:20:42 +01:00
return prevAngle
2014-11-06 07:05:42 +01:00
}
2014-11-22 13:21:51 +01:00
/**
* Sets the mechanical node's angle based on its connections
*/
def resetAngle()
{
connections.foreach(
n =>
{
2014-11-22 14:52:45 +01:00
n.prevAngle = (prevAngle + angleDisplacement) % (Math.PI * 2)
2014-11-22 13:21:51 +01:00
}
)
prevTime = System.currentTimeMillis()
}
2015-01-08 14:24:04 +01:00
/**
* The amount of angle in radians displaced. This is used to align the gear teeth.
*/
def angleDisplacement = 0D
override def accelerate(torque: Double)
2014-11-06 07:05:42 +01:00
{
2014-11-09 05:06:09 +01:00
bufferTorque += torque
2014-11-06 07:05:42 +01:00
}
2014-11-13 06:16:25 +01:00
def power: Double = torque * angularVelocity
2014-11-06 07:05:42 +01:00
override def radius(other: TNodeMechanical) = 0.5
2015-01-08 14:24:04 +01:00
2015-01-18 02:58:31 +01:00
def getMechanicalGrid: GridMechanical = super.grid.asInstanceOf[GridMechanical]
override def newGrid: GridNode[NodeMechanical] = new GridMechanical
override def isValidConnection(other: AnyRef): Boolean = other.isInstanceOf[NodeMechanical]
override def getDebugInfo = List(toString)
override def toString = "NodeMechanical [" + connections.size() + " Torque: " + BigDecimal(torque).setScale(2, BigDecimal.RoundingMode.HALF_UP) + " Velocity: " + BigDecimal(angularVelocity).setScale(2, BigDecimal.RoundingMode.HALF_UP) + "]"
/**
* Gets the angular velocity of the mechanical device from a specific side
*
* @return Angular velocity in meters per second
*/
override def angularVelocity = _angularVelocity
def angularVelocity_=(newVel: Double) = _angularVelocity = newVel
/**
* Gets the torque of the mechanical device from a specific side
*
* @return force
*/
override def torque = _torque
def torque_=(newTorque: Double) = _torque = newTorque
2014-11-22 14:52:45 +01:00
/**
* The class used to compare when making connections
*/
override protected def getCompareClass = classOf[NodeMechanical]
2014-11-06 07:05:42 +01:00
}