Example #1
0
 // Use this for initialization
 void Start()
 {
     // Get my rotator and radius
     MyRotator = GetComponent <RotatableController>();
     Debug.Assert(MyRotator != null);
     // Initialize neighbors
     Neighbours = new List <GameObject>();
 }
 public void ResetJammingConditions()
 {
     JammedByObstacle                = false;
     JammedByTooCloseGear            = false;
     JammedByRotationSpeedDifference = false;
     JammedByPropagation             = false;
     JammedByGear = null;
 }
Example #3
0
 public void RemoveGear(GameObject gameObj)
 {
     if (gameObj.CompareTag(GearController.TAG_GEAR))
     {
         RotatableController gearRot = gameObj.GetComponent <RotatableController>();
         Gears.Remove(gearRot);
         Destroy(gameObj);
     }
 }
 private float GetRotationSpeedTransmittedFrom(RotatableController rot)
 {
     if (rot == null)
     {
         return(0);
     }
     else if (rot.CompareTag("Respawn") || CompareTag("Finish"))
     {
         // Force emitter and sink have direct speed transmission
         return(rot.RotationSpeed);
     }
     else
     {
         // Gears transmit in reverse
         return(-rot.RotationSpeed * rot.Radius / Radius);
     }
 }
 public bool IsJammedBySomethingOtherThanMe(RotatableController me)
 {
     return(JammedByObstacle || JammedByTooCloseGear || JammedByRotationSpeedDifference || (JammedByPropagation && JammedByGear != me));
 }
 public bool IsJammedByMe(RotatableController me)
 {
     return(JammedByPropagation && JammedByGear == me);
 }
 public void PropagateJamFrom(RotatableController rot)
 {
     JammedByPropagation = true;
     JammedByGear        = rot;
 }
 public void SetTorquer(RotatableController t)
 {
     Debug.AssertFormat(t == null || t != TorqueFrom, gameObject.name);
     TorqueFrom    = t;
     RotationSpeed = GetRotationSpeedTransmittedFrom(t);
 }
Example #9
0
    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);
            }
        }
    }