示例#1
0
    // Update is called once per frame
    void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit) && hit.collider.tag == "gridElement")
        {
            this.transform.position = hit.collider.transform.position;
            lastHit = hit.collider.gameObject.GetComponent <gridElement>();
            this.rectTransform.sizeDelta = new Vector2(1.0f, lastHit.GetElementHeight());
            if (Input.GetMouseButtonDown(1))
            {
                SetCurserButton(0);
            }
        }



        // reset when press 5
        //read a file, instantiate a constructor by width height. then read an array to enable and disable gridElement.
        if (Input.GetKeyDown("5"))
        {
            constructor.instance.freeMemory();
            constructor.instance.initialize(10, 2);
        }
        //save file
        if (Input.GetKeyDown("1"))
        {
            XMLOp.Serialize(constructor.instance.info, "constructorInfo.xml");
        }
        if (Input.GetKeyDown("2"))
        {
            constructorInfo outInfo = new constructorInfo();
            outInfo = XMLOp.Deserialize <constructorInfo>("constructorInfo.xml");
            constructor.instance.info.width  = outInfo.width;
            constructor.instance.info.height = outInfo.height;

            constructor.instance.freeMemory();
            constructor.instance.initialize(outInfo.width, outInfo.height);
            for (int i = 0; i < outInfo.width * outInfo.width * outInfo.height; i++)
            {
                if (outInfo.gridElementStatus[i] == 0)
                {
                    Debug.Log(i);
                    constructor.instance.info.gridElementStatus[i] = 0;
                    constructor.instance.gridElements[i].SetDisable();
                }
                else
                {
                    constructor.instance.info.gridElementStatus[i] = 1;
                    constructor.instance.gridElements[i].SetEnable();
                }
            }
        }
    }
示例#2
0
              select(
 constructorInfo,
 parameters
 )).ToArray();
示例#3
0
    public void initialize(int width, int height)
    {
        info           = new constructorInfo(width, height);
        this.height    = height;
        this.width     = width;
        basementHeight = 1.5f - floorHeight / 2;
        float elementHeight = 1f;

        gridElementPrefabs   = new GameObject[width * width * height];
        cornerElementPrefabs = new GameObject[(width + 1) * (width + 1) * (height + 1)];
        gridElements         = new gridElement[width * width * height];
        cornerElements       = new cornerElement[(width + 1) * (width + 1) * (height + 1)];


        //Instantiate and register each CE
        for (int y = 0; y < height + 1; y++)
        {
            for (int x = 0; x < width + 1; x++)
            {
                for (int z = 0; z < width + 1; z++)
                {
                    //intantiate (place) prefab there
                    GameObject    cornerElementPrefabInstance = Instantiate(cornerElementPrefab, Vector3.zero, Quaternion.identity, this.transform);
                    cornerElement cornerElementInstance       = cornerElementPrefabInstance.GetComponent <cornerElement>();
                    //set instance
                    cornerElementInstance.Initialize(x, y, z);
                    //record cornerElement.
                    cornerElements[x + (width + 1) * (z + (width + 1) * y)]       = cornerElementInstance;
                    cornerElementPrefabs[x + (width + 1) * (z + (width + 1) * y)] = cornerElementPrefabInstance;
                }
            }
        }
        //Instantiate and register each GE
        for (int y = 0; y < height; y++)
        {
            float yPos = y;
            if (y == 0) //floor
            {
                elementHeight = floorHeight;
            }

            else if (y == 1)//basement
            {
                elementHeight = basementHeight;
                yPos          = floorHeight / 2 + basementHeight / 2;
            }
            else
            {
                elementHeight = 1;
            }

            for (int x = 0; x < width; x++)
            {
                for (int z = 0; z < width; z++)
                {
                    //intantiate (place) prefab there
                    GameObject  gridElementPrefabInstance = Instantiate(gridElementPrefab, new Vector3(x, yPos, z), Quaternion.identity, this.transform);
                    gridElement gridElementInstance       = gridElementPrefabInstance.GetComponent <gridElement>();
                    //set instance, record surrounding cornerElement and place them.
                    gridElementInstance.Initialize(x, y, z, elementHeight);
                    //record gridElement.
                    gridElements[x + width * (z + width * y)]       = gridElementInstance;
                    gridElementPrefabs[x + width * (z + width * y)] = gridElementPrefabInstance;
                }
            }
        }


        //register surrounding GE nearby foreach CE
        foreach (cornerElement corner in cornerElements)
        {
            corner.SetNearGridElements();
        }

        //set every GE enabled.
        foreach (gridElement gridElement in gridElements)
        {
            gridElement.SetEnable();
        }
    }