Example #1
0
    void OnGUI()
    {
        GUI.Box(new Rect(0, Screen.height - 30, 250, 30), "Press F1 to save, F2 to load");
        GUI.Box(new Rect(0, Screen.height - 60, 250, 30), "Press i to open or close inventory");
        GUI.Box(new Rect(0, Screen.height - 90, 250, 30), "Press 1-4 to select blocks");

        //just act as a crosshair**************************************************************************************************************************
        GUI.Label(new Rect(Screen.width / 2 - 10, Screen.height / 2 - 10, 20, 20), "+");



        //choose different blocks by pressing 1,2,3 or 4 key***********************************************************************************************
        //changing the alpha level based on which block is chosen, so other blocks will not be as visable as the chosen block******************************
        Color colorHolder = GUI.color;

        GUI.Box(new Rect(Screen.width / 4, Screen.height / 1.2f, Screen.width / 2, Screen.height / 8), "");

        GUI.color = new Color(1, 1, 1, alphaLevel1);
        GUI.DrawTexture(new Rect(Screen.width / 3.9f, Screen.height / 1.2f, Screen.width / 10, Screen.height / 8), selectionTextures [0]);
        GUI.color = new Color(1, 1, 1, alphaLevel2);
        GUI.DrawTexture(new Rect(Screen.width / 2.65f, Screen.height / 1.2f, Screen.width / 10, Screen.height / 8), selectionTextures [1]);
        GUI.color = new Color(1, 1, 1, alphaLevel3);
        GUI.DrawTexture(new Rect(Screen.width / 1.98f, Screen.height / 1.2f, Screen.width / 10, Screen.height / 8), selectionTextures [2]);
        GUI.color = new Color(1, 1, 1, alphaLevel4);
        GUI.DrawTexture(new Rect(Screen.width / 1.58f, Screen.height / 1.2f, Screen.width / 10, Screen.height / 8), selectionTextures [3]);

        GUI.color = colorHolder;

        if (Input.GetKeyDown("1"))
        {
            selectBlockType = 1;
            alphaLevel1     = 1f;
            alphaLevel2     = 0.4f;
            alphaLevel3     = 0.4f;
            alphaLevel4     = 0.4f;
        }
        else if (Input.GetKeyDown("2"))
        {
            selectBlockType = 2;
            alphaLevel1     = 0.4f;
            alphaLevel2     = 1f;
            alphaLevel3     = 0.4f;
            alphaLevel4     = 0.4f;
        }
        else if (Input.GetKeyDown("3"))
        {
            selectBlockType = 3;
            alphaLevel1     = 0.4f;
            alphaLevel2     = 0.4f;
            alphaLevel3     = 1f;
            alphaLevel4     = 0.4f;
        }
        else if (Input.GetKeyDown("4"))
        {
            selectBlockType = 4;
            alphaLevel1     = 0.4f;
            alphaLevel2     = 0.4f;
            alphaLevel3     = 0.4f;
            alphaLevel4     = 1f;
        }



        //On pressing the F1 key, a prompt will show up and ask user to type in a file name*************************************************************
        //and SAVE the terrain, player position and angle in to that xml file***************************************************************************
        //camera and controller rotation are disable during this process********************************************************************************
        if (Input.GetKeyDown(KeyCode.F1))
        {
            F1Trigger = true;
        }
        if (F1Trigger == true)
        {
            foreach (MouseLook n in lockRotation)
            {
                n.enabled = false;
            }

            GUI.Label(new Rect(Screen.width / 3, Screen.height / 2.5f, 300, 40), "Please type in a file name to save");

            inputName = GUI.TextField(new Rect(Screen.width / 3, Screen.height / 2, 300, 40), inputName, 30);
            if (GUI.Button(new Rect(Screen.width / 1.5f, Screen.height / 1.5f, 100, 50), "Confirm"))
            {
                foreach (MouseLook n in lockRotation)
                {
                    n.enabled = true;
                }
                float PosX = this.gameObject.transform.position.x;
                float PosY = this.gameObject.transform.position.y;
                float PosZ = this.gameObject.transform.position.z;
                float RotX = this.gameObject.transform.eulerAngles.x;
                float RotY = this.gameObject.transform.eulerAngles.y;
                float RotZ = this.gameObject.transform.eulerAngles.z;
                XMLVoxelFileWriter.SaveChunkToXMLFile(voxelChunk.terrainArray, PosX, PosY, PosZ, RotX, RotY, RotZ, inputName);
//				playerPosTrigger=true;
                F1Trigger = false;
            }
        }


        //On pressing the F2 key, a prompt will show up and ask user to type in a file name*************************************************************
        //and LOAD the terrain, player position and angle from that xml file****************************************************************************
        //camera and controller rotation are disable during this process********************************************************************************
        //Start and end position for pathfinding will also read from here if it exist(removed as its not necessary)*************************************
        if (Input.GetKeyDown(KeyCode.F2))
        {
            F2Trigger = true;
        }
        if (F2Trigger == true)
        {
            foreach (MouseLook n in lockRotation)
            {
                //lockRotationX.enabled=false;
                n.enabled = false;
            }

            GUI.Label(new Rect(Screen.width / 3, Screen.height / 2.5f, 300, 40), "Please type in a file name to load");

            inputName = GUI.TextField(new Rect(Screen.width / 3, Screen.height / 2, 300, 40), inputName, 30);
            if (GUI.Button(new Rect(Screen.width / 1.5f, Screen.height / 1.5f, 100, 50), "Confirm"))
            {
                //lockRotationX.enabled=true;
                //lockRotationY.enabled=true;
                foreach (MouseLook n in lockRotation)
                {
                    n.enabled = true;
                }


                if (System.IO.File.Exists(inputName + ".xml"))
                {
                    this.gameObject.transform.position = XMLVoxelFileWriter.LoadPosFromXMLFile(inputName);
                    this.gameObject.transform.rotation = XMLVoxelFileWriter.LoadRotFromXMLFile(inputName);

//					XMLVoxelFileWriter.ReadStartAndEndPosition(out pathfinder.startPosition, out pathfinder.endPosition,inputName);
//					{
//						//Debug.Log (s);
//						pathfinder.startPosition=s;
//						pathfinder.endPosition=e;
//					}
                    //get terrainArray from xml
                    voxelChunk.terrainArray = XMLVoxelFileWriter.LoadChunkFromXMLFile(16, inputName);
                    //draw the correct faces
                    voxelChunk.CreateTerrain();
                    //Update mesh info
                    voxelGenerator.UpdateMesh();
                    F2Trigger = false;
                }
            }
        }
    }