private void OnEnable()
        {
            mazeEngine = (QMazeEngine)target;

            mazeIcon  = QInspectorUtils.getAsset <Texture2D>("MazeIcon t:texture2D");
            pieceIcon = QInspectorUtils.getAsset <Texture2D>("PieceIcon t:texture2D");

            generateStart     = mazeEngine.generateStartCount > 0 || mazeEngine.startPointList.Count > 0 ? true : false;
            generateFinish    = mazeEngine.generateFinishCount > 0 || mazeEngine.startPointList.Count > 0 ? true : false;
            generateExit      = mazeEngine.exitPointList.Count > 0;
            generateObstacles = mazeEngine.obstaclePointList.Count > 0;

            mazeEngineSO      = new SerializedObject(target);
            startPointList    = mazeEngineSO.FindProperty("startPointList");
            finishPointList   = mazeEngineSO.FindProperty("finishPointList");
            exitPointList     = mazeEngineSO.FindProperty("exitPointList");
            obstaclePointList = mazeEngineSO.FindProperty("obstaclePointList");
        }
        private void OnEnable()
        {
            inited                    = serializedObject.FindProperty("inited");
            mazeGeneratedEvent        = serializedObject.FindProperty("mazeGeneratedEvent");
            mazePieceGeneratedEvent   = serializedObject.FindProperty("mazePieceGeneratedEvent");
            mazeGenerateProgressEvent = serializedObject.FindProperty("mazeGenerateProgressEvent");

            mazeEngine = (QMazeEngine)target;

            QInspectorUtils.SetIcon(mazeEngine, QInspectorUtils.getAsset <Texture2D>("MazeEngineIcon t:texture2D"));

            mazeIcon  = QInspectorUtils.getAsset <Texture2D>("MazeIcon t:texture2D");
            pieceIcon = QInspectorUtils.getAsset <Texture2D>("PieceIcon t:texture2D");

            startFoldout    = mazeEngine.getStartPositionList().Count > 0 || mazeEngine.getStartRandomPositionCount() > 0;
            finishFoldout   = mazeEngine.getFinishPositionList().Count > 0 || mazeEngine.getFinishRandomPositionCount() > 0;
            exitFoldout     = mazeEngine.getExitPositionList().Count > 0;
            obstacleFoldout = mazeEngine.getObstaclePositionList().Count > 0;
            eventFoldout    = mazeEngine.mazeGeneratedEvent.GetPersistentEventCount() +
                              mazeEngine.mazePieceGeneratedEvent.GetPersistentEventCount() +
                              mazeEngine.mazeGenerateProgressEvent.GetPersistentEventCount() > 0;
        }
        public IEnumerator generateGeometry(QMazeEngine mazeEngine, List<QMazeOutput>[][] mazeArray, float maxTime = 0.1f)
        {
            Vector3 transformPosition = transform.position;
            Quaternion transformRotation = transform.rotation;
            transform.rotation = Quaternion.identity;

            float time = Time.realtimeSinceStartup;
            int mazeWidth = mazeEngine.mazeWidth;
            int mazeHeight = mazeEngine.mazeHeight;
            float mazeSize = mazeWidth * mazeHeight;
            float count = 0;
            instantiatingProgress = 0;
            bool wasError = false;

            QMazePiecePack piecePack = mazeEngine.piecePack;
            List<QMazePiece> pieces = piecePack.toMazePieceList();
            for (int i = 0; i < pieces.Count; i++)
            {
                if ((!pieces[i].use && !pieces[i].require) ||
                    pieces[i].type == QMazePieceType.Start ||
                    pieces[i].type == QMazePieceType.Finish)
                {
                    pieces.RemoveAt(i);
                    i--;
                }
            }

            for (int ix = 0; ix < mazeWidth; ix++)
            {
                for (int iy = 0; iy < mazeHeight; iy++)
                {
                    List<QMazeOutput> mazeOutputData = mazeArray[ix][iy];

                    QMazePiece targetPiece = null;

                    if (QListUtil.has(mazeEngine.startPointList, ix, iy) && mazeOutputData != null && piecePack.getPiece(QMazePieceType.Start).checkFit(mazeOutputData))
                    {
                        targetPiece = piecePack.getPiece(QMazePieceType.Start);
                    }
                    else if (QListUtil.has(mazeEngine.finishPointList, ix, iy) && mazeOutputData != null && piecePack.getPiece(QMazePieceType.Finish).checkFit(mazeOutputData))
                    {
                        targetPiece = piecePack.getPiece(QMazePieceType.Finish);
                    }
                    else
                    {
                        QListUtil.Shuffle<QMazePiece>(pieces);
                        for (int i = 0; i < pieces.Count; i++)
                        {
                            if (pieces[i].checkFit(mazeOutputData))
                            {
                                targetPiece = pieces[i];
                                break;
                            }
                        }
                    }

                    if (targetPiece == null)
                    {
                        if (mazeEngine.pointInMaze(new QVector2Int(ix, iy)) || mazeEngine.obstacleIsEmpty)
                        {
                            targetPiece = piecePack.getPiece(QMazePieceType.Empty);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (targetPiece.geometryList.Count == 0)
                    {
                        if (mazeEngine.pointInMaze(new QVector2Int(ix, iy)))
                        {
                            if (!wasError)
                            {
                                wasError = true;
                                Debug.LogWarning("QMaze: Geometry for " + targetPiece.type + " piece is not found. Please check that geometry is specified for it in the piece pack.");
                            }
                        }
                        continue;
                    }

                    GameObject prefab = targetPiece.geometryList[QMath.getRandom(0, targetPiece.geometryList.Count)];
                    GameObject go;
                    #if UNITY_EDITOR
                    if (Application.isPlaying)
                    {
                        go = (GameObject)GameObject.Instantiate(prefab, new Vector3(), Quaternion.AngleAxis(randomPiecesRotation ? ((int)(UnityEngine.Random.value * 360 / 90)) * 90 : -targetPiece.getRotation(), Vector3.up));
                    }
                    else
                    {

                        go = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                        go.transform.rotation = Quaternion.AngleAxis(randomPiecesRotation ? ((int)(UnityEngine.Random.value * 360 / 90)) * 90 : -targetPiece.getRotation(), Vector3.up);
                    }
                    #else
                        go = (GameObject)GameObject.Instantiate(prefab, new Vector3(), Quaternion.AngleAxis(randomPiecesRotation ? ((int)(UnityEngine.Random.value * 360 / 90)) * 90 : -targetPiece.getRotation(), Vector3.up));
                    #endif
                    go.transform.position = transformPosition + new Vector3(ix * mazeEngine.mazePieceWidth, 0, -iy * mazeEngine.mazePieceHeight);
                    Vector3 scale = go.transform.localScale;
                    go.transform.parent = transform;
                    go.transform.localScale = scale;

                    count++;
                    instantiatingProgress = count / mazeSize;

                    if (Time.realtimeSinceStartup - time > maxTime)
                    {
                        time = Time.realtimeSinceStartup;
                        yield return null;
                    }
                }
            }

            transform.rotation = transformRotation;
        }
        private void OnEnable()
        {
            mazeEngine = (QMazeEngine)target;

            mazeIcon   = QInspectorUtils.getAsset<Texture2D>("MazeIcon t:texture2D");
            pieceIcon  = QInspectorUtils.getAsset<Texture2D>("PieceIcon t:texture2D");

            generateStart = mazeEngine.generateStartCount > 0 || mazeEngine.startPointList.Count > 0 ? true : false;
            generateFinish = mazeEngine.generateFinishCount > 0 || mazeEngine.startPointList.Count > 0 ? true : false;
            generateExit = mazeEngine.exitPointList.Count > 0;
            generateObstacles = mazeEngine.obstaclePointList.Count > 0;

            mazeEngineSO = new SerializedObject(target);
            startPointList = mazeEngineSO.FindProperty("startPointList");
            finishPointList = mazeEngineSO.FindProperty("finishPointList");
            exitPointList = mazeEngineSO.FindProperty("exitPointList");
            obstaclePointList = mazeEngineSO.FindProperty("obstaclePointList");
        }
示例#5
0
        public IEnumerator generateGeometry(QMazeEngine mazeEngine, List <QMazeOutput>[][] mazeArray, float maxTime = 0.1f)
        {
            Vector3    transformPosition = transform.position;
            Quaternion transformRotation = transform.rotation;

            transform.rotation = Quaternion.identity;

            float time       = Time.realtimeSinceStartup;
            int   mazeWidth  = mazeEngine.mazeWidth;
            int   mazeHeight = mazeEngine.mazeHeight;
            float mazeSize   = mazeWidth * mazeHeight;
            float count      = 0;

            instantiatingProgress = 0;
            bool wasError = false;

            QMazePiecePack    piecePack = mazeEngine.piecePack;
            List <QMazePiece> pieces    = piecePack.toMazePieceList();

            for (int i = 0; i < pieces.Count; i++)
            {
                if ((!pieces[i].use && !pieces[i].require) ||
                    pieces[i].type == QMazePieceType.Start ||
                    pieces[i].type == QMazePieceType.Finish)
                {
                    pieces.RemoveAt(i);
                    i--;
                }
            }

            for (int ix = 0; ix < mazeWidth; ix++)
            {
                for (int iy = 0; iy < mazeHeight; iy++)
                {
                    List <QMazeOutput> mazeOutputData = mazeArray[ix][iy];

                    QMazePiece targetPiece = null;

                    if (QListUtil.has(mazeEngine.startPointList, ix, iy) && mazeOutputData != null && piecePack.getPiece(QMazePieceType.Start).checkFit(mazeOutputData))
                    {
                        targetPiece = piecePack.getPiece(QMazePieceType.Start);
                    }
                    else if (QListUtil.has(mazeEngine.finishPointList, ix, iy) && mazeOutputData != null && piecePack.getPiece(QMazePieceType.Finish).checkFit(mazeOutputData))
                    {
                        targetPiece = piecePack.getPiece(QMazePieceType.Finish);
                    }
                    else
                    {
                        QListUtil.Shuffle <QMazePiece>(pieces);
                        for (int i = 0; i < pieces.Count; i++)
                        {
                            if (pieces[i].checkFit(mazeOutputData))
                            {
                                targetPiece = pieces[i];
                                break;
                            }
                        }
                    }

                    if (targetPiece == null)
                    {
                        if (mazeEngine.pointInMaze(new QVector2Int(ix, iy)) || mazeEngine.obstacleIsEmpty)
                        {
                            targetPiece = piecePack.getPiece(QMazePieceType.Empty);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (targetPiece.geometryList.Count == 0)
                    {
                        if (mazeEngine.pointInMaze(new QVector2Int(ix, iy)))
                        {
                            if (!wasError)
                            {
                                wasError = true;
                                Debug.LogWarning("QMaze: Geometry for " + targetPiece.type + " piece is not found. Please check that geometry is specified for it in the piece pack.");
                            }
                        }
                        continue;
                    }

                    GameObject prefab = targetPiece.geometryList[QMath.getRandom(0, targetPiece.geometryList.Count)];
                    GameObject go;
                                        #if UNITY_EDITOR
                    if (Application.isPlaying)
                    {
                        go = (GameObject)GameObject.Instantiate(prefab, new Vector3(), Quaternion.AngleAxis(randomPiecesRotation ? ((int)(UnityEngine.Random.value * 360 / 90)) * 90 : -targetPiece.getRotation(), Vector3.up));
                    }
                    else
                    {
                        go = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                        go.transform.rotation = Quaternion.AngleAxis(randomPiecesRotation ? ((int)(UnityEngine.Random.value * 360 / 90)) * 90 : -targetPiece.getRotation(), Vector3.up);
                    }
                                        #else
                    go = (GameObject)GameObject.Instantiate(prefab, new Vector3(), Quaternion.AngleAxis(randomPiecesRotation ? ((int)(UnityEngine.Random.value * 360 / 90)) * 90 : -targetPiece.getRotation(), Vector3.up));
                                        #endif
                    go.transform.position = transformPosition + new Vector3(ix * mazeEngine.mazePieceWidth, 0, -iy * mazeEngine.mazePieceHeight);
                    Vector3 scale = go.transform.localScale;
                    go.transform.parent     = transform;
                    go.transform.localScale = scale;

                    count++;
                    instantiatingProgress = count / mazeSize;

                    if (Time.realtimeSinceStartup - time > maxTime)
                    {
                        time = Time.realtimeSinceStartup;
                        yield return(null);
                    }
                }
            }

            transform.rotation = transformRotation;
        }