/// <summary> /// 开始一个拼图游戏。 /// </summary> /// <remarks>开始游戏前,需要保证之前的游戏已结束。</remarks> /// <param name="jigsawPieces">拼图碎片集合。</param> /// <param name="info">游戏信息。</param> /// <param name="ct">取消任务的通知。</param> /// <returns>开始拼图游戏的任务。</returns> public Task StartGame(JigsawPieceCollection jigsawPieces, JigsawInfo info, CancellationToken ct) { Debug.Assert(!this.hasGame); this.pieces = jigsawPieces; this.gameInfo = info; return(new Task(() => this.StartGame(ct), ct)); }
/// <summary> /// 准备渲染拼图碎片。 /// </summary> /// <param name="imageData">拼图使用的图片数据。</param> /// <param name="pieces">所有拼图碎片的集合。</param> /// <param name="rotatable">拼图碎片是否可以旋转。</param> /// <param name="ct">取消任务的通知。</param> public virtual void PrepareRender(byte[] imageData, JigsawPieceCollection pieces, bool rotatable, CancellationToken ct) { if (imageData == null) { throw CommonExceptions.ArgumentNull("imageData"); } ClearResources(); this.Image = this.deviceManager.LoadBitmapFromBytes(imageData); }
/// <summary> /// 开始一个拼图游戏。 /// </summary> /// <param name="pieces">拼图碎片集合。</param> /// <param name="info">游戏信息。</param> public void StartGame(JigsawPieceCollection pieces, JigsawInfo info) { this.StopGame(true); this.gamePath = null; this.ShowLoadingForm(); Task task = gameManager.StartGame(pieces, info, this.tokenCancelSource.Token); task.ContinueWith(t => BeginGame(), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith(t => HideLoadingForm()); task.Start(); }
/// <summary> /// 准备渲染拼图碎片。 /// </summary> /// <param name="imageData">拼图使用的图片数据。</param> /// <param name="pieces">所有拼图碎片的集合。</param> /// <param name="rotatable">拼图碎片是否可以旋转。</param> /// <param name="ct">取消任务的通知。</param> public override void PrepareRender(byte[] imageData, JigsawPieceCollection pieces, bool rotatable, CancellationToken ct) { base.PrepareRender(imageData, pieces, rotatable, ct); if (this.brush == null) { this.brush = new BitmapBrush(this.RenderTarget, this.Image); } else { this.brush.Bitmap = this.Image; } }
/// <summary> /// 打开拼图游戏。 /// </summary> /// <param name="fileName">要打开的拼图游戏存档路径。</param> /// <param name="ct">取消任务的通知。</param> /// <returns>打开拼图游戏的任务。</returns> public Task OpenGame(string fileName, CancellationToken ct) { using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { JigsawSerializeContext context = new JigsawSerializeContext(this.devices); BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.File, context)); this.gameInfo = (JigsawInfo)formatter.Deserialize(stream); this.pieces = (JigsawPieceCollection)formatter.Deserialize(stream); } this.gameChanged = false; return(this.StartGame(this.pieces, this.gameInfo, ct)); }
/// <summary> /// 准备渲染拼图碎片。 /// </summary> /// <param name="imageData">拼图使用的图片数据。</param> /// <param name="pieces">所有拼图碎片的集合。</param> /// <param name="rotatable">拼图碎片是否可以旋转。</param> /// <param name="ct">取消任务的通知。</param> public override void PrepareRender(byte[] imageData, JigsawPieceCollection pieces, bool rotatable, CancellationToken ct) { base.PrepareRender(imageData, pieces, rotatable, ct); ct.ThrowIfCancellationRequested(); imageSize = new Size2((int)this.Image.Size.Width, (int)this.Image.Size.Height); using (Brush imageBrush = new BitmapBrush(this.DeviceContext, this.Image)) { using (Bitmap1 source = DeviceManager.CreateBitmap(imageSize)) { ct.ThrowIfCancellationRequested(); // 设置特效的输入。 bevelBaseEffect.SetInput(0, source, true); bevelEffect.SetInput(0, source, true); // 分别按黑色与白色创建拼图碎片特效。 List <Geometry> blacks = new List <Geometry>(); List <Geometry> whites = new List <Geometry>(); foreach (JigsawPiece piece in pieces) { Geometry[] geoms = piece.OriginalPath.GetSourceGeometry(); // 划分黑色与白色,他们分别是不相邻的。 bool[] colors = piece.GetColors(); for (int i = 0; i < colors.Length; i++) { if (colors[i]) { blacks.Add(geoms[i]); } else { whites.Add(geoms[i]); } } } ct.ThrowIfCancellationRequested(); pointSpecularEffect.LightPosition = new Vector3(0, 0, 0); this.images[0] = this.CreateImage(imageBrush, source, blacks, whites, ct); if (rotatable) { // 生成不同旋转角度的图像,使用不同的光照情况。 pointSpecularEffect.LightPosition = new Vector3(0, imageSize.Height, 0); this.images[1] = this.CreateImage(imageBrush, source, blacks, whites, ct); pointSpecularEffect.LightPosition = new Vector3(imageSize.Width, imageSize.Height, 0); this.images[2] = this.CreateImage(imageBrush, source, blacks, whites, ct); pointSpecularEffect.LightPosition = new Vector3(imageSize.Width, 0, 0); this.images[3] = this.CreateImage(imageBrush, source, blacks, whites, ct); } } } }
/// <summary> /// 结束拼图游戏。 /// </summary> /// <returns>拼图游戏是否被结束。</returns> public void StopGame() { this.hasGame = false; renderPanel.SetJigsawScale(1f, new Point()); renderPanel.ImageSize = new Size2F(); // 移除界面的事件侦听器。 this.renderPanel.JigsawRegionChanged -= this.renderPanel_JigsawRegionChanged; this.renderPanel.JigsawScaleChanging -= this.renderPanel_JigsawScaleChanging; this.renderPanel.MouseDown -= this.renderPanel_MouseDown; this.renderPanel.MouseMove -= this.renderPanel_MouseMove; this.renderPanel.MouseUp -= this.renderPanel_MouseUp; this.renderer.ClearResources(); this.pieces.Dispose(); this.pieces = null; }
/// <summary> /// 新建游戏的事件。 /// </summary> private void newGameTSBtn_Click(object sender, EventArgs e) { if (this.SaveGame(true)) { this.PauseGame(); using (NewGameForm form = new NewGameForm()) { if (form.ShowDialog() == DialogResult.OK) { // 生成拼图形状。 JigsawPieceCollection pieces = new JigsawPieceCollection(this.deviceManager.D2DFactory, form.JigsawShape); this.StartGame(pieces, form.JigsawInfo); } else { this.ResumeGame(); } } } }
/// <summary> /// 准备渲染拼图碎片。 /// </summary> /// <param name="imageData">拼图使用的图片数据。</param> /// <param name="pieces">所有拼图碎片的集合。</param> /// <param name="rotatable">拼图碎片是否可以旋转。</param> /// <param name="ct">取消任务的通知。</param> public override void PrepareRender(byte[] imageData, JigsawPieceCollection pieces, bool rotatable, CancellationToken ct) { base.PrepareRender(imageData, pieces, rotatable, ct); ct.ThrowIfCancellationRequested(); imageSize = new Size2((int)this.Image.Size.Width, (int)this.Image.Size.Height); using (Brush imageBrush = new BitmapBrush(this.DeviceContext, this.Image)) { using (Bitmap1 source = DeviceManager.CreateBitmap(imageSize)) { ct.ThrowIfCancellationRequested(); // 设置特效的输入。 bevelBaseEffect.SetInput(0, source, true); bevelEffect.SetInput(0, source, true); // 分别按黑色与白色创建拼图碎片特效。 List<Geometry> blacks = new List<Geometry>(); List<Geometry> whites = new List<Geometry>(); foreach (JigsawPiece piece in pieces) { Geometry[] geoms = piece.OriginalPath.GetSourceGeometry(); // 划分黑色与白色,他们分别是不相邻的。 bool[] colors = piece.GetColors(); for (int i = 0; i < colors.Length; i++) { if (colors[i]) { blacks.Add(geoms[i]); } else { whites.Add(geoms[i]); } } } ct.ThrowIfCancellationRequested(); pointSpecularEffect.LightPosition = new Vector3(0, 0, 0); this.images[0] = this.CreateImage(imageBrush, source, blacks, whites, ct); if (rotatable) { // 生成不同旋转角度的图像,使用不同的光照情况。 pointSpecularEffect.LightPosition = new Vector3(0, imageSize.Height, 0); this.images[1] = this.CreateImage(imageBrush, source, blacks, whites, ct); pointSpecularEffect.LightPosition = new Vector3(imageSize.Width, imageSize.Height, 0); this.images[2] = this.CreateImage(imageBrush, source, blacks, whites, ct); pointSpecularEffect.LightPosition = new Vector3(imageSize.Width, 0, 0); this.images[3] = this.CreateImage(imageBrush, source, blacks, whites, ct); } } } }
/// <summary> /// 打开拼图游戏。 /// </summary> /// <param name="fileName">要打开的拼图游戏存档路径。</param> /// <param name="ct">取消任务的通知。</param> /// <returns>打开拼图游戏的任务。</returns> public Task OpenGame(string fileName, CancellationToken ct) { using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { JigsawSerializeContext context = new JigsawSerializeContext(this.devices); BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.File, context)); this.gameInfo = (JigsawInfo)formatter.Deserialize(stream); this.pieces = (JigsawPieceCollection)formatter.Deserialize(stream); } this.gameChanged = false; return this.StartGame(this.pieces, this.gameInfo, ct); }
/// <summary> /// 开始一个拼图游戏。 /// </summary> /// <remarks>开始游戏前,需要保证之前的游戏已结束。</remarks> /// <param name="jigsawPieces">拼图碎片集合。</param> /// <param name="info">游戏信息。</param> /// <param name="ct">取消任务的通知。</param> /// <returns>开始拼图游戏的任务。</returns> public Task StartGame(JigsawPieceCollection jigsawPieces, JigsawInfo info, CancellationToken ct) { Debug.Assert(!this.hasGame); this.pieces = jigsawPieces; this.gameInfo = info; return new Task(() => this.StartGame(ct), ct); }