예제 #1
0
    void Update()
    {
        if (m_state != m_previousState) //if we changed state
        {
            if (m_state == State.inventory)
            {
                GetComponent <FPSInputController>().enabled = false;
                GetComponent <CharacterMotor>().enabled     = false;
                foreach (MouseLook ml in GetComponentsInChildren <MouseLook>())
                {
                    ml.enabled = false;
                }

                Screen.showCursor = true;
                Screen.lockCursor = false;
                m_inventoryCanvas.SetActive(true);
                GetComponent <InventoryManager>().RefreshInventory();
            }

            if (m_state == State.playing)
            {
                GetComponent <FPSInputController>().enabled = true;
                GetComponent <CharacterMotor>().enabled     = true;
                foreach (MouseLook ml in GetComponentsInChildren <MouseLook>())
                {
                    ml.enabled = true;
                }

                Screen.showCursor = false;
                Screen.lockCursor = true;
                m_inventoryCanvas.SetActive(false);
            }

            m_previousState = m_state;
        }

        if (m_state == State.playing)
        {
            //select block with 1 - 4 keys
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                m_selectedBlock = 1;
            }
            if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                m_selectedBlock = 2;
            }
            if (Input.GetKeyDown(KeyCode.Alpha3))
            {
                m_selectedBlock = 3;
            }
            if (Input.GetKeyDown(KeyCode.Alpha4))
            {
                m_selectedBlock = 4;
            }

            //remove and place blocks
            if (Input.GetButtonDown("Fire1"))
            {
                Vector3 v;
                if (PickThisBlock(out v, 4))
                {
                    int iType = m_activeChunk.m_terrainArray[(int)v.x, (int)v.y, (int)v.z];
                    //print(iType);

                    m_activeChunk.SetBlock(v, m_voxelType.empty);

                    m_voxelType vType = m_voxelType.grass;
                    switch (iType)
                    {
                    case 1:
                        vType = m_voxelType.grass;
                        break;

                    case 2:
                        vType = m_voxelType.dirt;
                        break;

                    case 3:
                        vType = m_voxelType.stone;
                        break;

                    case 4:
                        vType = m_voxelType.sand;
                        break;
                    }
                    //spawn block there
                    m_activeChunk.SpawnSmallVoxel(v, vType);
                }
            }
            else if (Input.GetButtonDown("Fire2"))
            {
                Vector3 v;
                if (PickEmptyBlock(out v, 4))
                {
                    m_voxelType vType = m_voxelType.grass;
                    switch (m_selectedBlock)
                    {
                    case 1:
                        vType = m_voxelType.grass;
                        break;

                    case 2:
                        vType = m_voxelType.stone;
                        break;

                    case 3:
                        vType = m_voxelType.dirt;
                        break;

                    case 4:
                        vType = m_voxelType.sand;
                        break;
                    }
                    //Debug.Log(v);
                    m_activeChunk.SetBlock(v, vType);
                }
            }
            else if (Input.GetButtonUp("Inventory"))
            {
                m_state = State.inventory;
            }
        }
        else if (Input.GetButtonUp("Inventory")) //if we are in the inventory screen already, we start playing
        {
            m_state = State.playing;
        }
    }