// The force emitter has torque, as do all rotators directly connected to it public bool HasTorque() { return(CompareTag("Respawn") || (TorqueFrom != null && TorqueFrom.HasTorque())); }
void FixedUpdate() { MyRotator.ResetJammingConditions(); MySink = null; // Go through current neighbours and see if any of them have torque RotatableController myNewTorquer = null; foreach (GameObject neighbor in Neighbours) { RotatableController neighborRot = neighbor.GetComponent <RotatableController>(); if (neighborRot != null) { // First check if we are at the force emitter if (neighbor.CompareTag("Respawn")) { if (Vector2.Distance(neighbor.transform.position, transform.position) < EMITTER_TOLERANCE) { myNewTorquer = neighborRot; } } else if (neighbor.CompareTag("Finish")) { // If it's a force sink, rotate it if close if (Vector2.Distance(neighborRot.transform.position, transform.position) < SINK_TOLERANCE) { MySink = neighborRot; } } else if (neighbor.CompareTag(TAG_GEAR)) { // Check if this gear is jammed by someone else if (neighborRot.IsJammedBySomethingOtherThanMe(MyRotator)) { MyRotator.PropagateJamFrom(neighborRot); } // If it's a gear, jam if the neighbor is too close float maxDistanceBeforeJam = MyRotator.Radius + neighborRot.Radius - NEIGHBOUR_GEAR_TOLERANCE; if (Vector2.Distance(neighbor.transform.position, transform.position) < maxDistanceBeforeJam) { MyRotator.JamByCollidingGear(); } else if (neighborRot.HasTorque() && neighborRot.TorqueFrom != MyRotator) { if (myNewTorquer == null) { myNewTorquer = neighborRot; } else if (Mathf.Abs(myNewTorquer.RotationSpeed - neighborRot.RotationSpeed) > SPEED_DIFF_TOLERANCE) { // JAM if two nearby torquers have a large speed difference! MyRotator.JamByRotationalDifference(); } } } } else if (!neighbor.CompareTag(ElectrodeController.TAG_ELECTRODE)) { // Electrodes don't interfere with gears... MyRotator.JamByObstacle(); } } // If a suitable torquer has been found, adopt its rotation speed if (myNewTorquer != MyRotator.TorqueFrom) { MyRotator.SetTorquer(myNewTorquer != null ? myNewTorquer : null); } // If I am in contact with a sink, propagate either my speed or my jam to it if (MySink != null) { // Check for jams if (MyRotator.IsJammed) { MySink.PropagateJamFrom(MyRotator); } else if (MySink.IsJammedByMe(MyRotator)) { MySink.ResetJammingConditions(); } // Check whether my speed can be propagated if (!MyRotator.IsJammed && Mathf.Abs(MyRotator.RotationSpeed) > 0 && MySink.TorqueFrom != MyRotator) { MySink.SetTorquer(MyRotator); } } }