-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
208 lines (196 loc) · 9.05 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections;
using System.Numerics;
using System.Reflection;
namespace THBSimulate
{
public partial class MainForm : Form
{
Thread? nowGame;
decimal sleepTime = 4000;
public MainForm()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;//允许子线程访问控件,该方法需要改进
}
private void startBotton_Click(object sender, EventArgs e)
{
if (startBotton.Text == "开始")
{
nowGame = new Thread(StartGame)
{
IsBackground = true
};
nowGame.Start();
//StartGame();
}
else if(startBotton.Text == "停止")
{
nowGame?.Interrupt();
}
}
public void OutLine(string message)
{
OutPutBox.Text+=">>>\r\n"+message+"\r\n";
}
public void Out(string message)
{
OutPutBox.Text += ">>>\r\n" + message;
}
void StartGame()
{
startBotton.Text = "停止";
try
{
Scene nowScene=InitGame();
var p1 = nowScene.players[0];
var p2 = nowScene.players[1];
nameP1.Text = p1.name;
nameP2.Text = p2.name;
OutLine("游戏开始");
cardStack.Text = nowScene.stackManager.cardsStack.Count.ToString();
OutPutBox.Text = "";
hpP1.Text = p1.Hp.ToString();
handCardP1.Text = p1.handCardsArea.Count.ToString();
hpP2.Text = p2.Hp.ToString();
handCardP2.Text = p1.handCardsArea.Count.ToString();
p1.Draw(nowScene.stackManager.Draw(4));
p2.Draw(nowScene.stackManager.Draw(4));
//游戏主循环
while (true)
{
nowScene.HasToken = p1;
while (p1.stage != Stage.Wait) { p1.TakeTurn();}
nowScene.HasToken = p2;
while (p2.stage != Stage.Wait) { p2.TakeTurn(); }
//一个死亡后跳出循环的好办法:把玩家的行动全部改为事件向GameManager发送。由GameManager执行
}
}catch (Exception error) when (error is GameOver||error is ThreadInterruptedException)
{
startBotton.Text = "开始";
}
catch (Exception error)
{
OutLine("游戏逻辑发生了崩溃!" + error.Message);
}
finally
{
startBotton.Text = "开始";
}
}
Scene InitGame()
{
//进行一些全局设置
Player p1 = new(1, characterP1.Text switch { "小恶魔" => new LittleDemon(), "妖梦" => new Youmu(), "白板" => new BaiBan(), _ => new BaiBan() }), p2 = new(2, characterP2.Text switch { "小恶魔" => new LittleDemon(), "妖梦" => new Youmu(), "白板" => new BaiBan(),_=>new BaiBan() });
GameManager.Instance.Scene = new Scene() { players = new List<Player> { p1, p2 } };
GameManager.Instance.Scene.stackManager.StackShuffled += () =>
{
cardStack.Text = cardStack.Text = GameManager.Instance.Scene.stackManager.cardsStack.Count.ToString();
OutLine("牌堆刷新"); Thread.Sleep((int)(sleepTime / speedControl.Value));
};
GameManager.Instance.Scene.stackManager.CardsStackChanged += amount =>
{
cardStack.Text = (int.Parse(cardStack.Text) + amount).ToString();
};
foreach (var player in GameManager.Instance.Scene.players)
{
player.SendMessage += message =>
{
OutLine(message);
Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.Dead += () =>
{
OutLine($"【{player.name}】死亡!"); Thread.Sleep((int)(sleepTime / speedControl.Value));
if (GameManager.Instance.Scene.IsGameEnds()) { throw new GameOver(); }
};
player.OnDying += amount =>
{
OutLine($"【{player.name}】进入濒死状态。"); Thread.Sleep((int)(sleepTime / speedControl.Value));
for (int i = 0; i < amount; i++)
{
if (player.handCardsArea.Contains<Heal>(out Heal heal)) { player.Play(heal, player); }
else { break; }
}
};
player.OnGetDamage += (source, damage) =>
{
if (player.id == 1) { hpP1.Text = (int.Parse(hpP1.Text) - damage).ToString(); }
else if (player.id == 2) { hpP2.Text = (int.Parse(hpP2.Text) - damage).ToString(); }
if (source != null)
{
OutLine($"【{player.name}】受到【{source.name}】造成的{damage}点伤害,当前体力:{player.Hp - damage}");
}
else
{
OutLine($"【{player.name}】受到{damage}点无来源伤害,当前体力:{player.Hp - damage}");
}
Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.OnGetHeal += (source, amount) =>
{
if (player.id == 1) { hpP1.Text = (int.Parse(hpP1.Text) + amount).ToString(); }
else if (player.id == 2) { hpP2.Text = (int.Parse(hpP2.Text) + amount).ToString(); }
OutLine($"【{player.name}】回复{amount}点体力,当前体力:{player.Hp + amount}"); Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.DrawCard += num =>
{
if (player.id == 1) { handCardP1.Text = (int.Parse(handCardP1.Text) + num).ToString(); }
else if (player.id == 2) { handCardP2.Text = (int.Parse(handCardP2.Text) + num).ToString(); }
OutLine($"【{player.name}】摸了{num}张牌。"); Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.AfterDiscard += cards =>
{
if (player.id == 1) { handCardP1.Text = (int.Parse(handCardP1.Text) - cards.Count).ToString(); }
else if (player.id == 2) { handCardP2.Text = (int.Parse(handCardP2.Text) - cards.Count).ToString(); }
var names = new List<string>(); foreach (var card in cards) { names.Add(card.Name); }
OutLine($"【{player.name}】弃置了{cards.Count}张牌:{string.Join("、", names)}。");
Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.PlayCard += (card, target) =>
{
if (player.id == 1) { handCardP1.Text = (int.Parse(handCardP1.Text) - 1).ToString(); }
else if (player.id == 2) { handCardP2.Text = (int.Parse(handCardP2.Text) - 1).ToString(); }
OutLine($"【{player.name}】对【{target.name}】使用了一张{card.Name}。");
card.Use(player, target);
Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.RespondCard += card =>
{
if (player.id == 1) { handCardP1.Text = (int.Parse(handCardP1.Text) - 1).ToString(); }
else if (player.id == 2) { handCardP2.Text = (int.Parse(handCardP2.Text) - 1).ToString(); }
OutLine( $"【{player.name}】打出了一张{card.Name}。");
Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.StageStart += () =>
{
OutLine($"【{player.name}】回合开始。"); Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.StageEnd += () =>
{
OutLine($"【{player.name}】回合结束。"); Thread.Sleep((int)(sleepTime / speedControl.Value));
};
player.TriggerSkill += skill =>
{
OutLine($"【{player.name}】{skill.strategy switch{SkillStrategy.Positive => "发动",SkillStrategy.Passive => "触发", _ => throw new NotImplementedException()}}了技能{skill.name}!");
Thread.Sleep((int)(sleepTime / speedControl.Value));
};
//对每个玩家进行单独设置
player.skillArea.Add(player.character.skills.ToArray());//绑定技能
player.ai =AI.BindAI(player,player.character.name);//绑定ai
}
return GameManager.Instance.Scene;
}
private void OutPutBox_TextChanged(object sender, EventArgs e)
{
OutPutBox.SelectionStart=OutPutBox.Text.Length;
OutPutBox.ScrollToCaret();
}
}
class GameOver : Exception
{
public GameOver()
{
}
}
}