This repository has been archived on 2024-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
CDSAE3/CDSAE3_Lian_Lian_Kan/Forms/GameControl.cs

400 lines
16 KiB
C#
Raw Normal View History

2024-04-02 15:14:34 +00:00
using CDSAE3_Lian_Lian_Kan.Boards;
2024-04-18 00:38:59 +00:00
using CDSAE3_Lian_Lian_Kan.Forms.Interface;
2024-04-19 02:00:37 +00:00
using NAudio.Gui;
2024-03-22 09:03:01 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
2024-03-29 06:18:38 +00:00
using System.IO;
2024-03-22 09:03:01 +00:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2024-04-19 02:00:37 +00:00
using static CDSAE3_Lian_Lian_Kan.Etcs;
2024-03-22 09:03:01 +00:00
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace CDSAE3_Lian_Lian_Kan.Forms
{
2024-03-29 06:18:38 +00:00
public partial class GameControl : UserControl, IGameControl
2024-03-22 09:03:01 +00:00
{
2024-04-19 02:00:37 +00:00
IBoard board = Etcs.curAlgorithm switch
{
Algorithm.Array => new Board(),
Algorithm.Graph => new Graph_Board(),
_ => new Board()
};
2024-03-29 06:18:38 +00:00
IGameMode? iGameMode;
Queue<((int, int), Single_Block)> queue = new Queue<((int, int), Single_Block)>();//y,x
bool do_search = false;
List<Single_Block> hint_blocks = new List<Single_Block>();
2024-04-19 02:00:37 +00:00
bool doRandomGift = false;
bool doSubScore = false;
Dictionary<(int, int), (GiftArgs.GiftType, PictureBox)> giftBlock = new();
2024-03-22 09:03:01 +00:00
public GameControl()
2024-04-19 02:00:37 +00:00
{
2024-04-22 05:48:40 +00:00
2024-04-19 02:00:37 +00:00
}
2024-04-22 05:48:40 +00:00
public GameControl(Action<int> loadFinished)
2024-03-22 09:03:01 +00:00
{
InitializeComponent();
2024-04-08 13:04:47 +00:00
DoubleBuffered = true;
2024-04-18 00:38:59 +00:00
initWorker.WorkerReportsProgress = true;
initWorker.WorkerSupportsCancellation = true;
2024-04-19 02:00:37 +00:00
this.loadFinished = loadFinished;
2024-04-18 00:38:59 +00:00
initWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWorker);
initWorker.RunWorkerCompleted += InitWorker_RunWorkerCompleted;
}
2024-04-19 02:00:37 +00:00
Action<int>? loadFinished = null;
2024-04-18 00:38:59 +00:00
private void InitWorker_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
{
Etcs.loadFinished = true;
2024-04-19 02:00:37 +00:00
loadFinished?.Invoke(0);
2024-04-18 00:38:59 +00:00
}
public void GameControl_Load(object sender, EventArgs e)
{
2024-04-19 02:00:37 +00:00
if (Etcs.current_difficulty == Etcs.Difficulty.challenge)
doSubScore = doRandomGift = true;
Etcs.gameForm = this;
iGameMode = Etcs.gameModeForm;
2024-04-18 00:38:59 +00:00
Console.WriteLine("start");
Etcs.loadFinished = false;
initWorker.RunWorkerAsync();
Console.WriteLine("end");
}
protected override void OnPaint(PaintEventArgs e)
{
Console.WriteLine(e);
base.OnPaint(e);
}
private void bgWorker_DoWorker(object? sender, DoWorkEventArgs e)
{
2024-04-02 13:57:04 +00:00
playPanel_set(board.make_board());
2024-03-22 09:03:01 +00:00
}
2024-04-18 00:38:59 +00:00
2024-04-08 13:04:47 +00:00
void playPanel_set(int[,] bd)
2024-03-22 09:03:01 +00:00
{
2024-04-19 02:00:37 +00:00
SuspendLayout();
2024-03-22 09:03:01 +00:00
playPanel_size_change();
2024-04-19 02:00:37 +00:00
for (int i = 0; i < _row; i++)
for (int j = 0; j < _column; j++)
2024-03-22 09:03:01 +00:00
{
2024-04-18 00:38:59 +00:00
Console.WriteLine(i.ToString() + j.ToString());
2024-03-22 09:03:01 +00:00
if (bd[i, j] == -1)
{
2024-04-19 02:00:37 +00:00
var x = new Single_Block((j, i));
AddItem(x, i, j);
_index.Add((j, i), x);
2024-03-22 09:03:01 +00:00
}
else
{
2024-04-02 13:57:04 +00:00
var x = new Single_Block(bd[i, j], Etcs.def_Color, Etcs.sel_Color, (j, i));
2024-04-19 02:00:37 +00:00
AddItem(x, i, j);
_index.Add((j, i), x);
2024-03-22 09:03:01 +00:00
}
}
2024-04-19 02:00:37 +00:00
Invoke(() => ResumeLayout());
}
private void AddItem(Single_Block x, int i, int j)
{
2024-04-22 05:48:40 +00:00
x.Size = new Size((int)single_width, (int)single_height);
x.Location = new Point((int)(j * single_width), (int)(i * single_height));
Invoke(() => Controls.Add(x));
2024-03-22 09:03:01 +00:00
}
2024-03-29 06:18:38 +00:00
/// <summary>
/// 由form和to两个点获取方向
/// </summary>
/// <param name="from">起始点</param>
/// <param name="to">终点</param>
/// <returns></returns>
2024-04-02 13:57:04 +00:00
Etcs.Direction get_Direction((int, int) from, (int, int) to) //x,y
2024-03-29 06:18:38 +00:00
{
if (from.Item1 == to.Item1)
if (from.Item2 > to.Item2)
2024-04-02 13:57:04 +00:00
return Etcs.Direction.up;
2024-03-29 06:18:38 +00:00
else
2024-04-02 13:57:04 +00:00
return Etcs.Direction.down;
2024-03-29 06:18:38 +00:00
else
if (from.Item1 > to.Item1)
2024-04-02 13:57:04 +00:00
return Etcs.Direction.left;
2024-03-29 06:18:38 +00:00
else
2024-04-02 13:57:04 +00:00
return Etcs.Direction.right;
2024-03-29 06:18:38 +00:00
}
/// <summary>
/// 设置单个点方向
/// </summary>
/// <param name="point"></param>
/// <param name="direction"></param>
/// <param name="blocks"></param>
/// <param name="decrease"></param>
/// <returns></returns>
2024-04-08 13:04:47 +00:00
async Task set_PathAsync((int, int) point, Etcs.Direction direction, List<Single_Block> blocks, int wait_time, bool is_hint)
2024-03-29 06:18:38 +00:00
{
if (wait_time != 0)
await Task.Delay(wait_time);
2024-04-19 02:00:37 +00:00
Single_Block? control = _index.ContainsKey((point.Item1, point.Item2)) ? _index[(point.Item1, point.Item2)] : null;
if (control != null)
2024-03-29 06:18:38 +00:00
{
if (is_hint)
2024-04-19 02:00:37 +00:00
control.hint_path(direction);
2024-03-29 06:18:38 +00:00
else
2024-04-19 02:00:37 +00:00
control.to_path(direction);
blocks.Add(control);
2024-03-29 06:18:38 +00:00
}
}
2024-04-02 13:57:04 +00:00
async Task to_path((int, int) from, (int, int) to, bool include_end, Etcs.Direction extra_Direction, List<Single_Block> blocks, int wait_time, bool is_hint)
2024-03-29 06:18:38 +00:00
{
var direction = get_Direction(from, to);
switch (direction)
{
2024-04-02 13:57:04 +00:00
case Etcs.Direction.up:
2024-03-29 06:18:38 +00:00
for (int i = from.Item2 - 1; i != to.Item2; i--)
2024-04-08 13:04:47 +00:00
await set_PathAsync((from.Item1, i), Etcs.Direction.up_down, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
2024-04-02 13:57:04 +00:00
case Etcs.Direction.down:
2024-03-29 06:18:38 +00:00
for (int i = from.Item2 + 1; i != to.Item2; i++)
2024-04-08 13:04:47 +00:00
await set_PathAsync((from.Item1, i), Etcs.Direction.up_down, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
2024-04-02 13:57:04 +00:00
case Etcs.Direction.right:
2024-03-29 06:18:38 +00:00
for (int i = from.Item1 + 1; i != to.Item1; i++)
2024-04-08 13:04:47 +00:00
await set_PathAsync((i, from.Item2), Etcs.Direction.left_right, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
2024-04-02 13:57:04 +00:00
case Etcs.Direction.left:
2024-03-29 06:18:38 +00:00
for (int i = from.Item1 - 1; i != to.Item1; i--)
2024-04-08 13:04:47 +00:00
await set_PathAsync((i, from.Item2), Etcs.Direction.left_right, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
}
if (include_end)
{
2024-04-02 13:57:04 +00:00
direction = ((int)direction & 3) > 0 ? (Etcs.Direction)((int)direction << 2) : (Etcs.Direction)((int)direction >> 2);
2024-03-29 06:18:38 +00:00
direction = direction | extra_Direction;
2024-04-08 13:04:47 +00:00
await set_PathAsync(to, direction, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
}
}
async Task path_drawerAsync(List<(int, int)> path, int wait_time, List<Single_Block> blocks, bool is_hint)
{
switch (path.Count)
{
case 2:
2024-04-08 13:04:47 +00:00
await to_path(path[0], path[1], false, Etcs.Direction.none, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
case 3:
var extra_direction = get_Direction(path[1], path[2]);
2024-04-08 13:04:47 +00:00
await to_path(path[0], path[1], true, extra_direction, blocks, wait_time, is_hint);
await to_path(path[1], path[2], false, Etcs.Direction.none, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
case 4:
2024-04-02 13:57:04 +00:00
Etcs.Direction extra_directionA = get_Direction(path[1], path[2]), extra_directionB = get_Direction(path[2], path[3]);
2024-04-08 13:04:47 +00:00
await to_path(path[0], path[1], true, extra_directionA, blocks, wait_time, is_hint);
await to_path(path[1], path[2], true, extra_directionB, blocks, wait_time, is_hint);
await to_path(path[2], path[3], false, Etcs.Direction.none, blocks, wait_time, is_hint);
2024-03-29 06:18:38 +00:00
break;
}
}
internal void set_tip()
{
if (queue.Count != 1)
return;
List<List<(int, int)>> paths = board.get_tip(queue.Peek().Item1);
foreach (var path in paths)
2024-04-08 13:04:47 +00:00
_ = path_drawerAsync(path, 0, hint_blocks, true);
2024-03-29 06:18:38 +00:00
}
internal void de_set_tip()
{
2024-04-18 00:38:59 +00:00
for (int i = 0; i < hint_blocks.Count; i++)
hint_blocks[i].de_path();
2024-03-29 06:18:38 +00:00
hint_blocks.Clear();
}
2024-03-22 09:03:01 +00:00
public void Selected_Handler(Single_Block sender, SelectedEventArgs e)
{
Task.Run(() =>
{
2024-03-29 06:18:38 +00:00
iGameMode?.De_pause(this, e);
2024-03-22 09:03:01 +00:00
if (e.be_selected)
{
switch (queue.Count)
{
case 0:
queue.Enqueue((e.position, sender));
2024-03-29 06:18:38 +00:00
if (do_search)
{
de_set_tip();
2024-04-08 13:04:47 +00:00
set_tip();
2024-03-29 06:18:38 +00:00
}
2024-03-22 09:03:01 +00:00
break;
case 1:
var (posa, sendera) = queue.Dequeue();
var posb = e.position;
var (could, path) = board.test(posa, posb);
if (could)
{
2024-03-29 06:18:38 +00:00
board.decrease(posa, posb);
2024-03-22 09:03:01 +00:00
_ = Block_ClearAsync(path);
while (queue.Count() > 0)
queue.Dequeue();
2024-04-19 02:00:37 +00:00
foreach (var item in new[] { posa, posb })
{
if (giftBlock.TryGetValue(item, out (GiftArgs.GiftType, PictureBox) result))
{
iGameMode!.GetGift_Handler(this, new GiftArgs { giftType = result.Item1 });
DeleteGiftPicture(item, result);
}
}
if (doRandomGift)
{
Random random = new();
if (random.Next(0, 10) < 1)
{
SetGiftPicture(board.GetRandomBlock(), (GiftArgs.GiftType)(random.Next(1, 3) - 1));
}
}
2024-03-22 09:03:01 +00:00
}
else
{
2024-03-29 06:18:38 +00:00
if (do_search)
{
de_set_tip();
2024-04-08 13:04:47 +00:00
set_tip();
2024-03-29 06:18:38 +00:00
}
2024-04-19 02:00:37 +00:00
if (doSubScore)
{
iGameMode!.Score_Change(this, new ChangeScoreArgs { add = false, score = 200 });
}
2024-03-22 09:03:01 +00:00
queue.Enqueue((e.position, sender));
sendera.deselect();
}
break;
}
}
else
{
switch (queue.Count)
{
case 0:
break;
case 1:
if (queue.Peek().Item1 == e.position)
2024-04-08 13:04:47 +00:00
{
2024-03-22 09:03:01 +00:00
queue.Dequeue();
2024-03-29 06:18:38 +00:00
if (do_search)
de_set_tip();
}
2024-03-22 09:03:01 +00:00
break;
}
}
});
}
2024-04-19 02:00:37 +00:00
private void DeleteGiftPicture((int, int) item, (GiftArgs.GiftType, PictureBox) result)
{
Invoke(() =>
{
result.Item2.Visible = false;
result.Item2.SendToBack();
result.Item2.Dispose();
});
2024-04-22 05:48:40 +00:00
giftBlock.Remove(item);
2024-04-19 02:00:37 +00:00
}
2024-04-22 05:48:40 +00:00
private void SetGiftPicture((int, int) pos, GiftArgs.GiftType type)
2024-04-19 02:00:37 +00:00
{
var (picWidth, picHeight) = (single_width / 3, single_height / 3);
PictureBox picture = new PictureBox
{
Image = type switch
{
GiftArgs.GiftType.Search => Properties.Resources.w_search,
GiftArgs.GiftType.ReMake => Properties.Resources.w_exchange,
_ => Properties.Resources.trans
},
Size = new Size((int)picWidth, (int)picHeight),
SizeMode = PictureBoxSizeMode.Zoom,
Location = new Point((int)(((pos.Item1 + 1) * single_width) - picWidth), (int)(pos.Item2 * single_height))
};
Invoke(() => Controls.Add(picture));
Invoke(() => picture.BringToFront());
giftBlock.TryAdd(pos, (type, picture));
}
2024-03-22 09:03:01 +00:00
async Task Block_ClearAsync(List<(int, int)>? path)
{
2024-03-29 06:18:38 +00:00
if (do_search == true)
foreach (Single_Block single_Block in hint_blocks)
single_Block.de_path();
List<Single_Block> blocks = new List<Single_Block>();
2024-03-22 09:03:01 +00:00
if (path == null)
return;
2024-03-29 06:18:38 +00:00
await path_drawerAsync(path, 20, blocks, false);
2024-04-19 02:00:37 +00:00
Single_Block? controlA = _index.ContainsKey((path[0].Item1, path[0].Item2)) ? _index[(path[0].Item1, path[0].Item2)] : null;
Single_Block? controlB = _index.ContainsKey((path.Last().Item1, path.Last().Item2)) ? _index[(path.Last().Item1, path.Last().Item2)] : null;
2024-04-02 13:57:04 +00:00
Etcs.info_Audio_Processer.play_random_break_soundScape();
2024-04-19 02:00:37 +00:00
if (controlA != null && controlB != null)
2024-03-22 09:03:01 +00:00
{
2024-04-19 02:00:37 +00:00
controlA.destroyAsync();
controlB.destroyAsync();
2024-03-22 09:03:01 +00:00
}
await Task.Delay(200);
2024-03-29 06:18:38 +00:00
foreach (var control in blocks)
2024-03-22 09:03:01 +00:00
control.de_path();
2024-04-19 02:00:37 +00:00
iGameMode?.Score_Change(this, new ChangeScoreArgs { score = (blocks.Count + 2) * 10 });
2024-03-22 09:03:01 +00:00
}
2024-04-19 02:00:37 +00:00
private int _row = 0, _column = 0;
private double single_width = 0, single_height = 0;
private Dictionary<(int, int), Single_Block> _index = new();
2024-03-22 09:03:01 +00:00
void playPanel_size_change()
{
var (width, height) = board.size;
2024-04-19 02:00:37 +00:00
(_row, _column) = (height + 2, width + 2);
single_width = (double)Width / _column;
single_height = (double)Height / _row;
2024-03-22 09:03:01 +00:00
}
2024-03-29 06:18:38 +00:00
public void Exchange_Handler(object? sender, EventArgs e)
{
2024-04-02 13:57:04 +00:00
int[,] bd = board.remake_board();
2024-04-19 02:00:37 +00:00
SuspendLayout();
2024-03-29 06:18:38 +00:00
for (int i = 0; i < bd.GetLength(0); i++)
for (int j = 0; j < bd.GetLength(1); j++)
if (bd[i, j] == -1)
continue;
else
{
2024-04-19 02:00:37 +00:00
Single_Block? control = _index.ContainsKey((j, i)) ? _index[(j, i)] : null;
if (control != null)
control.Re_create(bd[i, j], null, null, null);
2024-03-29 06:18:38 +00:00
}
2024-04-19 02:00:37 +00:00
int length = giftBlock.Count();
foreach (var item in giftBlock)
{
DeleteGiftPicture(item.Key, item.Value);
}
for (int i = 0; i < length; i++)
{
Random random = new();
SetGiftPicture(board.GetRandomBlock(), (GiftArgs.GiftType)(random.Next(1, 3) - 1));
}
ResumeLayout();
2024-03-29 06:18:38 +00:00
}
public void Search_Handler(object? sender, SearchEventArgs e)
{
if (e.set_search)
{
do_search = true;
set_tip();
}
else
{
do_search = false;
de_set_tip();
}
}
2024-03-22 09:03:01 +00:00
}
}