コード例 #1
0
    // ピースの位置をシャッフルする.
    private void    shuffle_pieces()
    {
        #if true
        // ピースをグリッドに順番に並べる.

        int[] piece_index = new int[this.shuffle_grid_num * this.shuffle_grid_num];

        for (int i = 0; i < piece_index.Length; i++)
        {
            if (i < this.all_pieces.Length)
            {
                piece_index[i] = i;
            }
            else
            {
                piece_index[i] = -1;
            }
        }

        // ピース二つをランダムに選んで、位置を交換する.

        for (int i = 0; i < piece_index.Length - 1; i++)
        {
            int j = Random.Range(i + 1, piece_index.Length);

            int temp = piece_index[j];

            piece_index[j] = piece_index[i];

            piece_index[i] = temp;
        }

        // 場所のインデックスから実際の座標に変換して配置する.

        Vector3 pitch;

        pitch = this.shuffle_zone.size / (float)this.shuffle_grid_num;

        for (int i = 0; i < piece_index.Length; i++)
        {
            if (piece_index[i] < 0)
            {
                continue;
            }

            PieceControl piece = this.all_pieces[piece_index[i]];

            Vector3 position = piece.finished_position;

            int ix = i % this.shuffle_grid_num;
            int iz = i / this.shuffle_grid_num;

            position.x = ix * pitch.x;
            position.z = iz * pitch.z;

            position.x += this.shuffle_zone.center.x - pitch.x * (this.shuffle_grid_num / 2.0f - 0.5f);
            position.z += this.shuffle_zone.center.z - pitch.z * (this.shuffle_grid_num / 2.0f - 0.5f);

            position.y = piece.finished_position.y;

            piece.start_position = position;
        }

        // 少しづつ(グリッドのマス目内で)ランダムに位置をずらす.

        Vector3 offset_cycle = pitch / 2.0f;
        Vector3 offset_add   = pitch / 5.0f;

        Vector3 offset = Vector3.zero;

        for (int i = 0; i < piece_index.Length; i++)
        {
            if (piece_index[i] < 0)
            {
                continue;
            }

            PieceControl piece = this.all_pieces[piece_index[i]];

            Vector3 position = piece.start_position;

            position.x += offset.x;
            position.z += offset.z;

            piece.start_position = position;

            //


            offset.x += offset_add.x;

            if (offset.x > offset_cycle.x / 2.0f)
            {
                offset.x -= offset_cycle.x;
            }

            offset.z += offset_add.z;

            if (offset.z > offset_cycle.z / 2.0f)
            {
                offset.z -= offset_cycle.z;
            }
        }

        // 全体を回転させる.

        foreach (PieceControl piece in this.all_pieces)
        {
            Vector3 position = piece.start_position;

            position -= this.shuffle_zone.center;

            position = Quaternion.AngleAxis(this.pazzle_rotation, Vector3.up) * position;

            position += this.shuffle_zone.center;

            piece.start_position = position;
        }

        this.pazzle_rotation += 90;
        #else
        // 単純に乱数で座標を決める場合.
        foreach (PieceControl piece in this.all_pieces)
        {
            Vector3 position;

            Bounds piece_bounds = piece.GetBounds(Vector3.zero);

            position.x = Random.Range(this.shuffle_zone.min.x - piece_bounds.min.x, this.shuffle_zone.max.x - piece_bounds.max.x);
            position.z = Random.Range(this.shuffle_zone.min.z - piece_bounds.min.z, this.shuffle_zone.max.z - piece_bounds.max.z);

            position.y = piece.finished_position.y;

            piece.start_position = position;
        }
        #endif
    }
コード例 #2
0
    // 对碎片位置进行随机洗牌
    private void    shuffle_pieces()
    {
        #if true
        // 将碎片按照网格顺序排列

        int[] piece_index = new int[this.shuffle_grid_num * this.shuffle_grid_num];

        for (int i = 0; i < piece_index.Length; i++)
        {
            if (i < this.all_pieces.Length)
            {
                piece_index[i] = i;
            }
            else
            {
                piece_index[i] = -1;
            }
        }

        // 随机选取两个碎片,交换位置

        for (int i = 0; i < piece_index.Length - 1; i++)
        {
            int j = Random.Range(i + 1, piece_index.Length);

            int temp = piece_index[j];

            piece_index[j] = piece_index[i];

            piece_index[i] = temp;
        }

        // 通过位置的索引变换为实际的坐标来进行配置

        Vector3 pitch;

        pitch = this.shuffle_zone.size / (float)this.shuffle_grid_num;

        for (int i = 0; i < piece_index.Length; i++)
        {
            if (piece_index[i] < 0)
            {
                continue;
            }

            PieceControl piece = this.all_pieces[piece_index[i]];

            Vector3 position = piece.finished_position;

            int ix = i % this.shuffle_grid_num;
            int iz = i / this.shuffle_grid_num;

            position.x = ix * pitch.x;
            position.z = iz * pitch.z;

            position.x += this.shuffle_zone.center.x - pitch.x * (this.shuffle_grid_num / 2.0f - 0.5f);
            position.z += this.shuffle_zone.center.z - pitch.z * (this.shuffle_grid_num / 2.0f - 0.5f);

            position.y = piece.finished_position.y;

            piece.start_position = position;
        }

        // 逐步(网格的格子内)随机移动位置

        Vector3 offset_cycle = pitch / 2.0f;
        Vector3 offset_add   = pitch / 5.0f;

        Vector3 offset = Vector3.zero;

        for (int i = 0; i < piece_index.Length; i++)
        {
            if (piece_index[i] < 0)
            {
                continue;
            }

            PieceControl piece = this.all_pieces[piece_index[i]];

            Vector3 position = piece.start_position;

            position.x += offset.x;
            position.z += offset.z;

            piece.start_position = position;

            //


            offset.x += offset_add.x;

            if (offset.x > offset_cycle.x / 2.0f)
            {
                offset.x -= offset_cycle.x;
            }

            offset.z += offset_add.z;

            if (offset.z > offset_cycle.z / 2.0f)
            {
                offset.z -= offset_cycle.z;
            }
        }

        // 使全体旋转

        foreach (PieceControl piece in this.all_pieces)
        {
            Vector3 position = piece.start_position;

            position -= this.shuffle_zone.center;

            position = Quaternion.AngleAxis(this.pazzle_rotation, Vector3.up) * position;

            position += this.shuffle_zone.center;

            piece.start_position = position;
        }

        this.pazzle_rotation += 90;
        #else
        // 简单地使用随机数来决定坐标时的情况
        foreach (PieceControl piece in this.all_pieces)
        {
            Vector3 position;

            Bounds piece_bounds = piece.GetBounds(Vector3.zero);

            position.x = Random.Range(this.shuffle_zone.min.x - piece_bounds.min.x, this.shuffle_zone.max.x - piece_bounds.max.x);
            position.z = Random.Range(this.shuffle_zone.min.z - piece_bounds.min.z, this.shuffle_zone.max.z - piece_bounds.max.z);

            position.y = piece.finished_position.y;

            piece.start_position = position;
        }
        #endif
    }
コード例 #3
0
    private void shuffle_pieces()
    {
#if true
        int[] piece_index = new int[this.shuffle_grid_num * this.shuffle_grid_num];

        // 10个碎片会有 4*4 格子,空的格子标记 -1
        for (int i = 0; i < piece_index.Length; i++)
        {
            if (i < this.all_pieces.Length)
            {
                piece_index[i] = i;
            }
            else
            {
                piece_index[i] = -1;
            }
        }

        // 随机交换两个格子位置
        for (int i = 0; i < piece_index.Length - 1; i++)
        {
            int j = Random.Range(i + 1, piece_index.Length);

            int temp = piece_index[j];
            piece_index[j] = piece_index[i];
            piece_index[i] = temp;
        }

        // 标记数字的格子填充该数字对应的碎片
        Vector3 pitch;
        pitch = this.shuffle_zone.size / (float)this.shuffle_grid_num;
        for (int i = 0; i < piece_index.Length; i++)
        {
            if (piece_index[i] < 0)
            {
                continue;
            }

            PieceControl piece = this.all_pieces[piece_index[i]];

            // 根据格子在总格子的索引,结合中心点位置计算出格子坐标,把碎片坐标设置为那个格子坐标
            Vector3 position = piece.finished_position;

            int ix = i % this.shuffle_grid_num;
            int iz = i / this.shuffle_grid_num;

            position.x = ix * pitch.x;
            position.z = iz * pitch.z;

            // 相对左上角第一个格子的【中心位置】计算,所以多了个 0.5
            position.x += this.shuffle_zone.center.x - pitch.x * (this.shuffle_grid_num / 2.0f - 0.5f);
            position.z += this.shuffle_zone.center.z - pitch.z * (this.shuffle_grid_num / 2.0f - 0.5f);

            position.y = piece.finished_position.y;

            piece.start_position = position;
        }

        // 碎片在自己的小格内做小范围偏移
        Vector3 offset_cycle = pitch / 2.0f;
        Vector3 offset_add   = pitch / 5.0f;
        Vector3 offset       = Vector3.zero;
        for (int i = 0; i < piece_index.Length; i++)
        {
            if (piece_index[i] < 0)
            {
                continue;
            }
            PieceControl piece    = this.all_pieces[piece_index[i]];
            Vector3      position = piece.start_position;
            position.x          += offset.x;
            position.z          += offset.z;
            piece.start_position = position;

            offset.x += offset_add.x;
            if (offset.x > offset_cycle.x / 2.0f)
            {
                offset.x -= offset_cycle.x;
            }
            offset.z += offset_add.z;
            if (offset.z > offset_cycle.z / 2.0f)
            {
                offset.z -= offset_cycle.z;
            }
        }

        // shuffle_zone 只是一个 bound 所以没办法旋转整个 shuffle_zone 来旋转所有碎片,而是每个碎片单独相对 shuffle_zone 的中心轴作旋转
        foreach (PieceControl piece in this.all_pieces)
        {
            Vector3 position = piece.start_position;
            position            -= this.shuffle_zone.center;
            position             = Quaternion.AngleAxis(this.pazzle_rotation, Vector3.up) * position;
            position            += this.shuffle_zone.center;
            piece.start_position = position;
        }
        // 这里作用是按重置按钮的时候,整体旋转角再变化
        this.pazzle_rotation += 90;
#else
        // 简单的使用随机数来决定坐标的情况
        foreach (PieceControl piece in this.all_pieces)
        {
            Vector3 position;
            Bounds  piece_bounds = piece.GetBounds(Vector3.zero);

            position.x = Random.Range(this.shuffle_zone.min.x - piece_bounds.min.x, this.shuffle_zone.max.x - piece_bounds.max.x);
            position.z = Random.Range(this.shuffle_zone.min.x - piece_bounds.min.z, this.shuffle_zone.max.z - piece_bounds.max.z);

            position.y           = piece.finished_position.y;
            piece.start_position = position;
        }
#endif
    }