コード例 #1
0
ファイル: GFGear.cs プロジェクト: zhiqich/EECS494
    /// <summary>
    /// Clone a gear and place the duplicate version aligned.
    /// </summary>
    /// <param name="direction">Direction at which to align it to</param>
    /// <returns>The cloned gear as gameobject</returns>
    public GameObject Clone(Vector3 direction)
    {
        GFGear    gear    = this;
        GFGearGen gearGen = (GFGearGen)this.gameObject.GetComponent(typeof(GFGearGen));
        float     d;

        if (gearGen != null)
        {
            d = gearGen.radius + (gearGen.radius - gearGen.tipLength);
        }
        else
        {
            d = (gear.GetRadius() * 2.1f) - gear.toothSize;
        }

        GameObject newGearGO = (GameObject)Instantiate(gear.gameObject);

        newGearGO.transform.parent = gear.gameObject.transform.parent;
        GFGear newGear = newGearGO.GetComponent(typeof(GFGear)) as GFGear;

        newGear.DrivenBy        = gear;
        newGear.AutoSetDrivenBy = false;

        GFGearGen newGearGen = newGear.GetComponent <GFGearGen>();

        if (newGearGen != null)
        {
            newGearGen.alignTeethWithParent  = true;
            newGearGen.alignRadiusWithParent = true;
        }

        newGearGO.transform.position = gear.transform.position + (direction.normalized * d);

        // Get number that is not followed by any other number
        string re   = @"(\d+)(?!.*\d)";
        Match  m    = Regex.Match(gear.gameObject.name, re);
        int    nmbr = 1;

        // Add one to the last number (if exists)
        if (m != null && m.Value != null && m.Value != "" && gear.DrivenBy != null)
        {
            nmbr = int.Parse(m.Value) + 1;
        }
        // Rename object to sequentially numbered object
        newGearGO.name = Regex.Replace(gear.gameObject.name, re, "").Trim() + " " + nmbr.ToString();

        newGearGen.Align(gearGen);

        return(newGearGO);
    }
コード例 #2
0
    public void RecalculateGears(GFGear g1)
    {
        GFGear prevDrivenBy = g1.DrivenBy;

        if (g1.AutoSetDrivenBy)
        {
            g1.DrivenBy = null;
        }
        foreach (GFGear otherGear in g1.otherGears)
        {
            if (!otherGear.gameObject.Equals(g1.gameObject) && ((g1.AutoSetDrivenBy && g1.Intersects(otherGear)) || !g1.AutoSetDrivenBy))
            {
                // Set initial rotation
                if (g1.machine == null || (g1.machine != null && g1.machine.PoweredGear != g1))
                {
                    if (g1.AutoSetDrivenBy && g1.DrivenBy != otherGear)
                    {
                        // If redundant / circular reference is occuring: restore previous link and abort
                        if (otherGear.DrivenBy == g1)
                        {
                            g1.DrivenBy = prevDrivenBy;
                        }
                        else
                        {
                            g1.DrivenBy = otherGear;
                        }
                    }

                    if (g1.DrivenBy != null)
                    {
                        // If gear is auto generated, we can align the teeth
                        GFGearGen gg1 = g1.GetComponent(typeof(GFGearGen)) as GFGearGen;
                        GFGearGen gg2 = g1.DrivenBy.GetComponent(typeof(GFGearGen)) as GFGearGen;
                        if (gg1 != null && gg2 != null)
                        {
                            if (gg1.alignTeethWithParent)
                            {
                                gg1.Align(gg2);
                            }
                            else
                            {
                                if (gg1.alignRadiusWithParent)
                                {
                                    gg1.AlignRadius(gg2);
                                }
                            }
                        }
                        else
                        {
                            // If gear is not auto generated, we can align the positions if autoAlign is true.
                            if (gearAutoAlign.boolValue)
                            {
                                g1.Align(g1.DrivenBy);
                            }
                        }
                    }
                }

                // Tell the unity editor we changed something by code, so it gets saved.
                UnityEditor.EditorUtility.SetDirty(g1);
                // All aligned and set: get the hell out of this loop!
                break;
            }
        }
    }