Ejemplo n.º 1
0
    public static JigsawState GetCurrentState(JigsawGame game)
    {
        var wrapped = new JigsawState();

        wrapped.puzzleSize           = game.puzzleSize;
        wrapped.puzzlePieceCount     = game.puzzlePieceCount;
        wrapped.pieceBoundaryPercent = game.pieceBoundaryPercent;
        wrapped.seed      = game.seed;
        wrapped.isLoading = game.isLoading;
        wrapped.isLoaded  = game.isLoaded;

        var clusters = game.clusters;

        if (clusters != null)
        {
            foreach (var cluster in clusters)
            {
                wrapped.clusters.Add(new ClusterWrapper()
                {
                    position = cluster.Key.position,
                    rotation = cluster.Key.rotation,
                    indices  = cluster.Value
                });
            }
        }

        return(wrapped);
    }
Ejemplo n.º 2
0
    public static void ApplyToGame(JigsawGame jigsawGame, JigsawState currentState)
    {
        if (currentState != null)
        {
            if (currentState.containsPuzzleSize)
            {
                jigsawGame.puzzleSize = currentState.puzzleSize;
            }
            if (currentState.containsPieceCount)
            {
                jigsawGame.puzzlePieceCount = currentState.puzzlePieceCount;
            }
            if (currentState.containsBoundaryPercent)
            {
                jigsawGame.pieceBoundaryPercent = currentState.pieceBoundaryPercent;
            }
            if (currentState.containsSeed)
            {
                jigsawGame.seed = currentState.seed;
            }
            if (!jigsawGame.isLoaded && !jigsawGame.isLoading && ((currentState.containsIsLoaded && currentState.isLoaded) || (currentState.containsIsLoading && currentState.isLoading)))
            {
                jigsawGame.LoadJigsawPuzzle();
            }
            else if ((jigsawGame.isLoaded || jigsawGame.isLoading) && (currentState.containsIsLoaded && !currentState.isLoaded) && (currentState.containsIsLoading && !currentState.isLoading))
            {
                jigsawGame.DestroyJigsawPuzzle();
            }

            if (jigsawGame.pieces != null && currentState.clusters.Count > 0)
            {
                int   columns     = jigsawGame.puzzlePieceCount.x;
                int   rows        = jigsawGame.puzzlePieceCount.y;
                float pieceWidth  = jigsawGame.puzzleSize.x / columns;
                float pieceHeight = jigsawGame.puzzleSize.z / rows;

                foreach (var cluster in currentState.clusters)
                {
                    if (cluster.indices.Count > 0)
                    {
                        var firstIndex   = cluster.indices[0];
                        int mainColIndex = firstIndex % columns;
                        int mainRowIndex = firstIndex / columns;

                        var clusterParent = jigsawGame.GetPieceKey(firstIndex);
                        if (clusterParent != null)
                        {
                            if (cluster.containsTransform)
                            {
                                clusterParent.position = cluster.position;
                                clusterParent.rotation = cluster.rotation;
                            }
                            if (cluster.containsIndices)
                            {
                                foreach (var pieceIndex in cluster.indices)
                                {
                                    var pieceParent = jigsawGame.GetPieceKey(pieceIndex);
                                    if (pieceParent != clusterParent)
                                    {
                                        int currentColIndex = pieceIndex % columns;
                                        int currentRowIndex = pieceIndex / columns;
                                        int horOffset       = currentColIndex - mainColIndex;
                                        int verOffset       = currentRowIndex - mainRowIndex;

                                        var pieceObject = jigsawGame.pieces[pieceIndex].transform;
                                        pieceObject.SetParent(clusterParent);
                                        pieceObject.localPosition = new Vector3(horOffset * pieceWidth, 0, verOffset * pieceHeight);

                                        jigsawGame.clusters[pieceParent].Remove(pieceIndex);
                                        jigsawGame.clusters[clusterParent].Add(pieceIndex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }