public override GameObject Build(
        PartConfig config,
        GameObject root,
        string label
        )
    {
        GameObject partsGo    = null;
        GameObject bodyGo     = null;
        GameObject steeringGo = null;
        GameObject hubBodyGo  = null;

        // build out part model first
        partsGo = base.Build(config, root, label);
        bodyGo  = partsGo.transform.Find(partsGo.name + ".body").gameObject;

        // now build out wheel parts hierarchy
        // frame is instantiated under parts.body
        if (frame != null)
        {
            frame.Build(config, bodyGo, "frame");
        }

        // steering goes next (if specified) under parts
        if (steering != null)
        {
            steeringGo = steering.Build(config, partsGo, "steering");
        }

        // hub/wheel goes next under parts
        if (hub != null)
        {
            var hubGo = hub.Build(config, (steeringGo != null) ? steeringGo : partsGo, "hub");
            // if hub part isn't specified, build dummy hub rigidbody for wheel
            if (hubGo == null)
            {
                hubBodyGo = PartUtil.BuildGo(config, partsGo, "hub.body", typeof(Rigidbody), typeof(KeepInBounds));
            }
            else
            {
                hubBodyGo = PartUtil.GetBodyGo(hubGo);
            }
            if (wheel != null)
            {
                wheel.Build(config, hubBodyGo, "wheel");
            }
        }

        return(partsGo);
    }
    public override GameObject Build(
        PartConfig config,
        GameObject root,
        string label
        )
    {
        GameObject partsGo  = null;
        GameObject bodyGo   = null;
        Vector3    rotation = Vector3.zero;
        Vector3    offset   = Vector3.zero;

        if (label == null || label == "")
        {
            label = name;
        }

        // compute merged config for root
        var mergedConfig = PartConfig.Merge(this.config, config);

        // for a bot, we want to attach rigidbody for frame directly to root
        // only do this in-game, not for in-editor preview
        if (root != null && Application.isPlaying)
        {
            if (root.GetComponent <Rigidbody>() == null)
            {
                root.AddComponent <Rigidbody>();
            }
            if (root.GetComponent <KeepInBounds>() == null)
            {
                root.AddComponent <KeepInBounds>();
            }
            bodyGo = root;
            // apply part properties
            if (mass != null)
            {
                mass.Apply(mergedConfig, root);
            }
            if (health != null)
            {
                health.Apply(mergedConfig, root);
            }
            if (damage != null)
            {
                damage.Apply(mergedConfig, root);
            }

            // apply applicators to parts container
            if (applicators != null)
            {
                for (var i = 0; i < applicators.Length; i++)
                {
                    if (applicators[i] != null)
                    {
                        applicators[i].Apply(mergedConfig, root);
                    }
                }
            }

            //PartUtil.ApplyRigidBodyProperties(bodyGo, mass, drag, angularDrag);
            // empty parts object to parent the rest of the bot
            partsGo = PartUtil.BuildGo(mergedConfig, null, label + ".parts");
            partsGo.transform.position = root.transform.position;
            partsGo.transform.rotation = root.transform.rotation;
            // keep track of parent/child links
            var childLink = root.AddComponent <ChildLink>();
            childLink.childGo = partsGo;
            var parentLink = partsGo.AddComponent <ParentLink>();
            parentLink.parentGo = root;

            // otherwise, instantiate rigidbody/parts as a normal part
        }
        else
        {
            // build out frame model first
            partsGo = base.Build(mergedConfig, root, label);
            if (partsGo != null)
            {
                // set local position to match that of root
                bodyGo = partsGo.transform.Find(partsGo.name + ".body").gameObject;
            }
        }

        // frame is instantiated under parts.body
        if (frame != null)
        {
            frame.Build(mergedConfig, bodyGo, "frame");
        }

        // now build out modules
        if (modules != null)
        {
            for (var i = 0; i < modules.Length; i++)
            {
                if (modules[i] != null && modules[i].part != null)
                {
                    // build the module under the top level parts
                    var moduleGo = modules[i].part.Build(PartConfig.Merge(modules[i].config, config), partsGo, modules[i].label);
                    if (moduleGo != null)
                    {
                        var moduleBodyGo = PartUtil.GetBodyGo(moduleGo);
                        if (moduleBodyGo != null)
                        {
                            // connect module to the
                            var joiner = moduleBodyGo.GetComponent <Joiner>();
                            if (joiner != null)
                            {
                                joiner.Join(bodyGo.GetComponent <Rigidbody>());
                            }
                        }
                    }
                }
            }
        }

        return(partsGo);
    }
Beispiel #3
0
    public virtual GameObject Build(
        PartConfig config,
        GameObject root,
        string label
        )
    {
        if (label == null || label == "")
        {
            label = name;
        }

        // creation check -- ensure part we are about to build is not a direct parent already in tree
        if (PartUtil.PartInParent(root, this))
        {
            Debug.Log("detected part loop/invalid part chain, part: " + name + " already instantiated in parent");
            return(null);
        }

        // create empty parts container
        var partsGo = PartUtil.BuildGo(config, root, label);
        var partId  = partsGo.AddComponent <PartId>();

        partId.partId = this.GetInstanceID();

        // create new rigid body for this part, set parts container as parent
        var rigidbodyGo = PartUtil.BuildGo(config, partsGo, label + ".body", typeof(Rigidbody), typeof(KeepInBounds));

        //PartUtil.ApplyRigidBodyProperties(rigidbodyGo, mass, drag, angularDrag);

        // apply part properties
        if (mass != null)
        {
            mass.Apply(config, partsGo);
        }
        if (health != null)
        {
            health.Apply(config, partsGo);
        }
        if (damage != null)
        {
            damage.Apply(config, partsGo);
        }

        // apply applicators to parts container
        if (applicators != null)
        {
            for (var i = 0; i < applicators.Length; i++)
            {
                if (applicators[i] != null)
                {
                    applicators[i].Apply(config, partsGo);
                }
            }
        }

        // instantiate model under rigid body
        if (models != null)
        {
            for (var i = 0; i < models.Length; i++)
            {
                models[i].Build(config, rigidbodyGo, label + ".model");
            }
        }

        // instantiate connected parts
        if (connectedParts != null && connectedParts.Length > 0)
        {
            for (var i = 0; i < connectedParts.Length; i++)
            {
                // check for self-references
                var childGo = connectedParts[i].Build(config, partsGo, label + ".part");
                if (childGo != null)
                {
                    // join child part to current rigidbody
                    var joiner = childGo.GetComponent <Joiner>();
                    if (joiner != null)
                    {
                        joiner.Join(rigidbodyGo.GetComponent <Rigidbody>());
                    }
                }
            }
        }
        return(partsGo);
    }