コード例 #1
0
    public void Build(LevelLayout inputLayout)
    {
        int levelWidth  = inputLayout.GetWidth();
        int levelHeight = inputLayout.GetHeight();

        for (int y = 0; y < levelHeight; ++y)
        {
            for (int x = 0; x < levelWidth; ++x)
            {
                Vector2Int tilePos = new Vector2Int(x, y);

                if (inputLayout.IsWalkable(tilePos))
                {
                    Vector3    worldPos         = LevelLayout.TilePosToWorldVec3(tilePos);
                    GameObject floorMeshClone   = (GameObject)Instantiate(m_FloorMesh, worldPos, Quaternion.identity);
                    GameObject ceilingMeshClone = (GameObject)Instantiate(m_CeilingMesh, worldPos, Quaternion.identity);

                    // check neighbours

                    // west
                    if (!inputLayout.IsWalkable(new Vector2Int(tilePos.x - 1, tilePos.y)))
                    {
                        float      angle           = 90.0f;
                        GameObject wallMeshClone   = (GameObject)Instantiate(m_WallMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                        GameObject columnMeshClone = (GameObject)Instantiate(m_ColumnMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                    }

                    if (!inputLayout.IsWalkable(new Vector2Int(tilePos.x + 1, tilePos.y)))
                    {
                        // east
                        float      angle           = 270.0f;
                        GameObject wallMeshClone   = (GameObject)Instantiate(m_WallMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                        GameObject columnMeshClone = (GameObject)Instantiate(m_ColumnMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                    }

                    // north
                    if (!inputLayout.IsWalkable(new Vector2Int(tilePos.x, tilePos.y + 1)))
                    {
                        float      angle           = 180.0f;
                        GameObject wallMeshClone   = (GameObject)Instantiate(m_WallMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                        GameObject columnMeshClone = (GameObject)Instantiate(m_ColumnMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                    }

                    // south
                    if (!inputLayout.IsWalkable(new Vector2Int(tilePos.x, tilePos.y - 1)))
                    {
                        float      angle           = 0.0f;
                        GameObject wallMeshClone   = (GameObject)Instantiate(m_WallMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                        GameObject columnMeshClone = (GameObject)Instantiate(m_ColumnMesh, worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                    }

                    // place prop
                    {
                        float randomValue = Random.value;
                        randomValue *= (m_NumberOfPropVariants - 1);
                        int propIndex = Mathf.RoundToInt(randomValue);

                        randomValue  = Random.value;
                        randomValue *= 3;
                        int angle = Mathf.RoundToInt(randomValue);
                        angle *= 90;
                        GameObject propMeshClone = (GameObject)Instantiate(m_PropMeshes[propIndex], worldPos, Quaternion.AngleAxis(angle, Vector3.up));
                    }
                }
            }
        }
    }