public LeModule.axis RandomAxis()
    {
        // Get a random axis
        List <string> axis = new List <string>();

        axis.Add(Module.axis.x.ToString());
        axis.Add(Module.axis.y.ToString());
        axis.Add(Module.axis.z.ToString());
        var choice = ExtRandom <string> .WeightedChoice(axis, new int[] { 33, 33, 33 });

        LeModule.axis newAxis = LeModule.axis.x;
        switch (choice)
        {
        case "x":
            newAxis = LeModule.axis.x;
            break;

        case "y":
            newAxis = LeModule.axis.y;
            break;

        case "z":
            newAxis = LeModule.axis.z;
            break;
        }
        return(newAxis);
    }
    public void RandomDivision()
    {
        LeModule module = childModule;

        if (ExtRandom <bool> .Chance(1, 10))
        {
            LeModule.axis newAxis = RandomAxis();

            module.Subdivide(newAxis);

            foreach (var child in module.children)
            {
                Renderer renderer = child.go.GetComponent <Renderer>();
                RandomColor(renderer);
                child.gameObject.AddComponent <ModuleSwitcher>();
                child.gameObject.AddComponent <ModuleCollider>();
                child.gameObject.AddComponent <ModuleMove>();
            }
        }
        else
        {
            module.UnDivide();
            Debug.Log("module.UnDivide()");
        }
    }
    public void onPointer(RaycastHit hit)
    {
        if (move)
        {
            ModuleMove moveModule = hit.collider.GetComponent <ModuleMove>();
            if (moveModule != null)
            {
                moveModule.active = true;
            }
        }
        else
        {
            ModuleSwitcher switcher = hit.collider.GetComponent <ModuleSwitcher>();
            if (switcher)
            {
                if (subdivide)
                {
                    LeModule.axis newAxis = switcher.RandomAxis();

                    switcher.childModule.Subdivide(newAxis);
                    bool active = switcher.GetComponent <ModuleMove>().active;
                    foreach (var child in switcher.childModule.children)
                    {
                        Renderer renderer = child.go.GetComponent <Renderer>();

                        var childSwitch = child.gameObject.AddComponent <ModuleSwitcher>();
                        childSwitch.RandomColorFromList(renderer, LeModular.colours);
                        child.gameObject.AddComponent <ModuleCollider>();
                        child.gameObject.AddComponent <ModuleCheck>();
                        ModuleMove moduleMove = child.gameObject.AddComponent <ModuleMove>();
                        moduleMove.active = active;
                    }
                }
                else
                {
                    switcher.childModule.UnDivide();
                }
            }
        }
    }
Example #4
0
    public void Subdivide(LeModule.axis axis)
    {
        subdivisionAxis = axis;
        if (children == null)
        {
            children = new List <LeModule>();
        }
        // Create modules if this is the first time we've been divided
        while (children.Count < 2)
        {
            var go = new GameObject();
            go.transform.SetParent(this.transform);
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
            var node = go.AddComponent <LeModule>();
            children.Add(node);
            node.parent = this;
            var prefab = Resources.Load <GameObject>("Prefabs/Cube");
            node.go = GameObject.Instantiate(prefab);
            node.go.transform.SetParent(node.transform);
            node.go.transform.localPosition = Vector3.zero;
            node.go.transform.localRotation = Quaternion.identity;
            Debug.Log(uid + " Added child");
        }
        if (children != null)
        {
            if (children.Count < 2)
            {
                Debug.LogError(uid + " childNodes.Count < 2");
                return;
            }
        }
        else
        {
            Debug.LogError(uid + " childNodes list == null");
            return;
        }
        // Get a list containing two modulor values
        List <float> ms = new List <float>();

        foreach (LeModule child in children)
        {
            switch (subdivisionAxis)
            {
            case axis.x:
                ms = Modulor.GetList(size.x, 2);
                break;

            case axis.y:
                ms = Modulor.GetList(size.y, 2);
                break;

            case axis.z:
                ms = Modulor.GetList(size.z, 2);
                break;
            }
        }
        // Check sizes to see if they are out of bounds
        foreach (var value in ms)
        {
        }
        // Revese the list randomly
        if (ExtRandom <bool> .Chance(1, 2))
        {
            ms.Reverse();
        }

        for (int i = 0; i < children.Count; i++)
        {
            switch (subdivisionAxis)
            {
            case axis.x:
                children[i].size.x = ms[i];
                break;

            case axis.y:
                children[i].size.y = ms[i];
                break;

            case axis.z:
                children[i].size.z = ms[i];
                break;
            }
        }
        foreach (LeModule child in children)
        {
            var scale = child.go.transform.localScale;
            switch (subdivisionAxis)
            {
            case axis.x:
                scale.x = child.size.x;
                scale.y = size.y;
                scale.z = size.z;
                break;

            case axis.y:
                scale.x = size.x;
                scale.y = child.size.y;
                scale.z = size.z;
                break;

            case axis.z:
                scale.x = size.x;
                scale.y = size.y;
                scale.z = child.size.z;
                break;
            }
            child.go.transform.localScale = scale;
        }

        // Effect position
        // index 0 pos = index 1 scale / 2
        // index 1 should be moved the scale of index 0 / 2
        foreach (LeModule child in children)
        {
            var pos        = child.transform.localPosition;
            int childIndex = children.IndexOf(child);
            if (childIndex == 0)
            {
                switch (subdivisionAxis)
                {
                case axis.x:
                    pos.x = children[1].size.x / 2 * -1;
                    pos.y = 0;
                    pos.z = 0;
                    break;

                case axis.y:
                    pos.x = 0;
                    pos.y = children[1].size.y / 2 * -1;
                    pos.z = 0;
                    break;

                case axis.z:
                    pos.x = 0;
                    pos.y = 0;
                    pos.z = children[1].size.z / 2 * -1;
                    break;
                }
            }
            else if (childIndex == 1)
            {
                switch (subdivisionAxis)
                {
                case axis.x:
                    pos.x = children[0].size.x / 2;
                    pos.y = 0;
                    pos.z = 0;
                    break;

                case axis.y:
                    pos.x = 0;
                    pos.y = children[0].size.y / 2;
                    pos.z = 0;
                    break;

                case axis.z:
                    pos.x = 0;
                    pos.y = 0;
                    pos.z = children[0].size.z / 2;
                    break;
                }
            }
            else
            {
                Debug.LogError(uid + "Index should not be greater than 1");
            }
            child.transform.localPosition = pos;
        }
        // Set the size of the un effected axises to the values in the parent
        foreach (LeModule child in children)
        {
            switch (subdivisionAxis)
            {
            case axis.x:
                child.size.y = size.y;
                child.size.z = size.z;
                break;

            case axis.y:
                child.size.x = size.x;
                child.size.z = size.z;
                break;

            case axis.z:
                child.size.x = size.x;
                child.size.y = size.y;
                break;
            }
        }
        // Hide self
        foreach (LeModule item in children)
        {
            item.go.SetActive(true);
        }
        go.SetActive(false);
    }