2015.03.07-08
This commit is contained in:
parent
efa2dac012
commit
278292e43a
9 changed files with 313 additions and 80 deletions
19
SnakeGame/Form1.Designer.cs
generated
19
SnakeGame/Form1.Designer.cs
generated
|
@ -38,6 +38,7 @@
|
||||||
this.scoreLabel = new System.Windows.Forms.Label();
|
this.scoreLabel = new System.Windows.Forms.Label();
|
||||||
this.livesLabel = new System.Windows.Forms.Label();
|
this.livesLabel = new System.Windows.Forms.Label();
|
||||||
this.DialogPanel = new System.Windows.Forms.Panel();
|
this.DialogPanel = new System.Windows.Forms.Panel();
|
||||||
|
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
|
||||||
this.menuStrip1.SuspendLayout();
|
this.menuStrip1.SuspendLayout();
|
||||||
this.SuspendLayout();
|
this.SuspendLayout();
|
||||||
//
|
//
|
||||||
|
@ -48,9 +49,9 @@
|
||||||
| System.Windows.Forms.AnchorStyles.Right)));
|
| System.Windows.Forms.AnchorStyles.Right)));
|
||||||
this.panel1.BackColor = System.Drawing.Color.Black;
|
this.panel1.BackColor = System.Drawing.Color.Black;
|
||||||
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||||
this.panel1.Location = new System.Drawing.Point(12, 64);
|
this.panel1.Location = new System.Drawing.Point(12, 83);
|
||||||
this.panel1.Name = "panel1";
|
this.panel1.Name = "panel1";
|
||||||
this.panel1.Size = new System.Drawing.Size(600, 357);
|
this.panel1.Size = new System.Drawing.Size(600, 338);
|
||||||
this.panel1.TabIndex = 0;
|
this.panel1.TabIndex = 0;
|
||||||
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
|
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
|
||||||
//
|
//
|
||||||
|
@ -129,7 +130,7 @@
|
||||||
// livesLabel
|
// livesLabel
|
||||||
//
|
//
|
||||||
this.livesLabel.AutoSize = true;
|
this.livesLabel.AutoSize = true;
|
||||||
this.livesLabel.Location = new System.Drawing.Point(14, 48);
|
this.livesLabel.Location = new System.Drawing.Point(14, 53);
|
||||||
this.livesLabel.Name = "livesLabel";
|
this.livesLabel.Name = "livesLabel";
|
||||||
this.livesLabel.Size = new System.Drawing.Size(32, 13);
|
this.livesLabel.Size = new System.Drawing.Size(32, 13);
|
||||||
this.livesLabel.TabIndex = 3;
|
this.livesLabel.TabIndex = 3;
|
||||||
|
@ -146,12 +147,23 @@
|
||||||
this.DialogPanel.TabIndex = 0;
|
this.DialogPanel.TabIndex = 0;
|
||||||
this.DialogPanel.Visible = false;
|
this.DialogPanel.Visible = false;
|
||||||
//
|
//
|
||||||
|
// flowLayoutPanel1
|
||||||
|
//
|
||||||
|
this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||||
|
| System.Windows.Forms.AnchorStyles.Right)));
|
||||||
|
this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
|
||||||
|
this.flowLayoutPanel1.Location = new System.Drawing.Point(239, 31);
|
||||||
|
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
|
||||||
|
this.flowLayoutPanel1.Size = new System.Drawing.Size(373, 46);
|
||||||
|
this.flowLayoutPanel1.TabIndex = 4;
|
||||||
|
//
|
||||||
// Form1
|
// Form1
|
||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
this.BackColor = System.Drawing.Color.Black;
|
this.BackColor = System.Drawing.Color.Black;
|
||||||
this.ClientSize = new System.Drawing.Size(624, 442);
|
this.ClientSize = new System.Drawing.Size(624, 442);
|
||||||
|
this.Controls.Add(this.flowLayoutPanel1);
|
||||||
this.Controls.Add(this.DialogPanel);
|
this.Controls.Add(this.DialogPanel);
|
||||||
this.Controls.Add(this.livesLabel);
|
this.Controls.Add(this.livesLabel);
|
||||||
this.Controls.Add(this.scoreLabel);
|
this.Controls.Add(this.scoreLabel);
|
||||||
|
@ -187,6 +199,7 @@
|
||||||
private System.Windows.Forms.Label livesLabel;
|
private System.Windows.Forms.Label livesLabel;
|
||||||
private System.Windows.Forms.Panel DialogPanel;
|
private System.Windows.Forms.Panel DialogPanel;
|
||||||
private System.Windows.Forms.ToolStripTextBox toolStripTextBox1;
|
private System.Windows.Forms.ToolStripTextBox toolStripTextBox1;
|
||||||
|
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,13 +28,16 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
if (!MSGBox.Shown)
|
if (!MSGBox.Shown)
|
||||||
{
|
{
|
||||||
if (value && !timerenabled) //Only start if not running already
|
Instance.Invoke(new Action(delegate
|
||||||
{
|
{
|
||||||
Timer.Start();
|
if (value && !timerenabled) //Only start if not running already
|
||||||
SpeedTimer.Start();
|
{
|
||||||
}
|
Timer.Start();
|
||||||
timerenabled = value;
|
SpeedTimer.Start();
|
||||||
Instance.toolStripTextBox1.Enabled = !value;
|
}
|
||||||
|
timerenabled = value;
|
||||||
|
Instance.toolStripTextBox1.Enabled = !value;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,6 +131,8 @@ namespace SnakeGame
|
||||||
private bool formdeactivated = false;
|
private bool formdeactivated = false;
|
||||||
private void Form1_Activated(object sender, EventArgs e)
|
private void Form1_Activated(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
if (Network.ConnectedMatch != null)
|
||||||
|
return;
|
||||||
if (formdeactivated)
|
if (formdeactivated)
|
||||||
Game.Paused = false;
|
Game.Paused = false;
|
||||||
formdeactivated = false;
|
formdeactivated = false;
|
||||||
|
@ -135,6 +140,8 @@ namespace SnakeGame
|
||||||
|
|
||||||
private void Form1_Deactivate(object sender, EventArgs e)
|
private void Form1_Deactivate(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
if (Network.ConnectedMatch != null)
|
||||||
|
return;
|
||||||
formdeactivated = !Game.Paused;
|
formdeactivated = !Game.Paused;
|
||||||
Game.Paused = true;
|
Game.Paused = true;
|
||||||
}
|
}
|
||||||
|
@ -171,6 +178,35 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
Network.Leave(); //Stop threads and such
|
Network.Leave(); //Stop threads and such
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void RefreshPlayerList()
|
||||||
|
{
|
||||||
|
if (Instance == null)
|
||||||
|
return;
|
||||||
|
Action action = new Action(() =>
|
||||||
|
{
|
||||||
|
Instance.flowLayoutPanel1.Controls.Clear();
|
||||||
|
/*if (Network.ConnectedMatch == null)
|
||||||
|
{
|
||||||
|
Instance.flowLayoutPanel1.Controls.Add(new Label { Text = Game.Player.Name, ForeColor = Game.Player.Color });
|
||||||
|
Instance.flowLayoutPanel1.Controls.Add(new Label { Text = "Score: " + Game.Player.Score, ForeColor = Game.Player.Color });
|
||||||
|
}*/
|
||||||
|
if (Network.ConnectedMatch != null)
|
||||||
|
{
|
||||||
|
foreach (Player player in Network.ConnectedMatch.Players)
|
||||||
|
{
|
||||||
|
if (player.Name == Game.Player.Name)
|
||||||
|
continue; //The current player's score/lives value is shown already
|
||||||
|
Instance.flowLayoutPanel1.Controls.Add(new Label { Text = player.Name, ForeColor = Game.Player.Color });
|
||||||
|
Instance.flowLayoutPanel1.Controls.Add(new Label { Text = "Score: " + Game.Player.Score, ForeColor = Game.Player.Color });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (Instance.InvokeRequired)
|
||||||
|
Instance.Invoke(action);
|
||||||
|
else
|
||||||
|
action();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static class Ext
|
public static class Ext
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,6 +24,7 @@ namespace SnakeGame
|
||||||
//public static string UserName;
|
//public static string UserName;
|
||||||
public static Player Player = new Player("Player", true);
|
public static Player Player = new Player("Player", true);
|
||||||
private static int score;
|
private static int score;
|
||||||
|
[Obsolete("Use Game.Player.Score")]
|
||||||
public static int Score
|
public static int Score
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -37,6 +38,7 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private static int lives;
|
private static int lives;
|
||||||
|
[Obsolete("Use Game.Player.Lives")]
|
||||||
public static int Lives
|
public static int Lives
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -51,6 +53,8 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Opposite of Form1.TimerEnabled
|
/// Opposite of Form1.TimerEnabled
|
||||||
|
/// TODO: Send pause/resume event
|
||||||
|
/// (View -> Task List -> Comments
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool Paused
|
public static bool Paused
|
||||||
{
|
{
|
||||||
|
@ -104,7 +108,7 @@ namespace SnakeGame
|
||||||
match.Players.Add(Game.Player);
|
match.Players.Add(Game.Player);
|
||||||
Game.Reset();
|
Game.Reset();
|
||||||
Network.CreateGame(match);
|
Network.CreateGame(match);
|
||||||
Game.Paused = false;
|
//Game.Paused = false; - Start in thread
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Game.Paused = true;
|
Game.Paused = true;
|
||||||
|
@ -131,7 +135,7 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
Game.Reset();
|
Game.Reset();
|
||||||
Network.Connect(Network.Matches[num]);
|
Network.Connect(Network.Matches[num]);
|
||||||
Game.Paused = false;
|
//Game.Paused = false; - Start in thread
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Game.Paused = true;
|
Game.Paused = true;
|
||||||
|
@ -226,7 +230,7 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Point nextcoord = MovePlayerPre(Player, MoveDirection);
|
Point nextcoord = MovePlayerPre(Player, MoveDirection);
|
||||||
Network.SyncUpdate(NetUpdateType.Move);
|
Network.SyncUpdate(NetUpdateType.Move, MoveDirection);
|
||||||
/*if (nextcoord.X >= GameField.GetLength(0) || nextcoord.Y >= GameField.GetLength(1))
|
/*if (nextcoord.X >= GameField.GetLength(0) || nextcoord.Y >= GameField.GetLength(1))
|
||||||
{
|
{
|
||||||
MessageBox.Show("Error!");
|
MessageBox.Show("Error!");
|
||||||
|
@ -235,6 +239,7 @@ namespace SnakeGame
|
||||||
if (Game.GameField[nextcoord.X, nextcoord.Y].Tick != 0 && Game.GameField[nextcoord.X, nextcoord.Y].Type != SquareType.Point)
|
if (Game.GameField[nextcoord.X, nextcoord.Y].Tick != 0 && Game.GameField[nextcoord.X, nextcoord.Y].Type != SquareType.Point)
|
||||||
{
|
{
|
||||||
Lives--;
|
Lives--;
|
||||||
|
LivesLabel.ForeColor = Color.Red;
|
||||||
if (Lives <= 0)
|
if (Lives <= 0)
|
||||||
Stop();
|
Stop();
|
||||||
else
|
else
|
||||||
|
@ -242,11 +247,15 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
LivesLabel.ForeColor = Color.White;
|
||||||
if (GameField[nextcoord.X, nextcoord.Y].Type == SquareType.Point)
|
if (GameField[nextcoord.X, nextcoord.Y].Type == SquareType.Point)
|
||||||
{
|
{
|
||||||
Score += 1000;
|
Score += 1000;
|
||||||
|
ScoreLabel.ForeColor = Color.Blue;
|
||||||
Game.AddPoint();
|
Game.AddPoint();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
ScoreLabel.ForeColor = Color.White;
|
||||||
if (Score > 0)
|
if (Score > 0)
|
||||||
Score -= new Random().Next(1, 20);
|
Score -= new Random().Next(1, 20);
|
||||||
MovePlayerPost(Player, nextcoord);
|
MovePlayerPost(Player, nextcoord);
|
||||||
|
@ -328,6 +337,22 @@ namespace SnakeGame
|
||||||
colors = colorlist.ToArray();
|
colors = colorlist.ToArray();
|
||||||
return Color.FromKnownColor(colors[new Random().Next(colors.Length)]);
|
return Color.FromKnownColor(colors[new Random().Next(colors.Length)]);
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// TODO
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename"></param>
|
||||||
|
public static void Load(string filename)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// TODO
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename"></param>
|
||||||
|
public static void Save(string filename)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public enum GameStartMode
|
public enum GameStartMode
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,7 +13,12 @@ namespace SnakeGame
|
||||||
public string Name = "";
|
public string Name = "";
|
||||||
public int MaxPlayers = 0;
|
public int MaxPlayers = 0;
|
||||||
//public List<string> PlayerNames = new List<string>();
|
//public List<string> PlayerNames = new List<string>();
|
||||||
public List<Player> Players = new List<Player>();
|
/// <summary>
|
||||||
|
/// TODO: Send player join/leave to master server
|
||||||
|
/// </summary>
|
||||||
|
//public List<Player> Players = new List<Player>();
|
||||||
|
public PlayerCollection Players = new PlayerCollection();
|
||||||
|
|
||||||
private string ownername = "";
|
private string ownername = "";
|
||||||
public string OwnerName
|
public string OwnerName
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,16 +35,23 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
for (int j = 0; j < Game.GameSize.Y; j++)
|
for (int j = 0; j < Game.GameSize.Y; j++)
|
||||||
{
|
{
|
||||||
Game.GameField[i, j].PlayerName = (string)readdata["GameField"][i][j]["PlayerName"];
|
Game.GameField[i, j].PlayerName = (string)readdata["GameField"][i + ""][j + ""]["PlayerName"];
|
||||||
Game.GameField[i, j].Tick = (int)readdata["GameField"][i][j]["Tick"];
|
Game.GameField[i, j].Tick = (int)readdata["GameField"][i + ""][j + ""]["Tick"];
|
||||||
Game.GameField[i, j].Type = (SquareType)Enum.Parse(typeof(SquareType), (string)readdata["GameField"][i][j]["Type"]);
|
Game.GameField[i, j].Type = (SquareType)Enum.Parse(typeof(SquareType), (string)readdata["GameField"][i + ""][j + ""]["Type"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach(JObject item in readdata["Players"])
|
foreach(JProperty item in readdata["Players"])
|
||||||
{
|
{
|
||||||
MessageBox.Show(item.ToString());
|
ConnectedMatch.Players.Add(new Player(item.Name, color: Color.FromArgb((int)item.Value["Color"]), x: (int)item.Value["Position"]["X"], y: (int)item.Value["Position"]["Y"]));
|
||||||
|
}
|
||||||
|
Game.Paused = false;
|
||||||
|
SendUpdate = true;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
string playername = sr.ReadString();
|
||||||
|
Player player = ConnectedMatch.Players.Single(entry => entry.Name == playername);
|
||||||
|
ReceiveAndProcessData(player, sr);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,71 +71,45 @@ namespace SnakeGame
|
||||||
BinaryWriter bwp = new BinaryWriter(ns);
|
BinaryWriter bwp = new BinaryWriter(ns);
|
||||||
var senddata = new JObject();
|
var senddata = new JObject();
|
||||||
senddata["Length"] = Game.Length;
|
senddata["Length"] = Game.Length;
|
||||||
|
senddata["GameSize"] = new JObject();
|
||||||
senddata["GameSize"]["X"] = Game.GameSize.X;
|
senddata["GameSize"]["X"] = Game.GameSize.X;
|
||||||
senddata["GameSize"]["Y"] = Game.GameSize.Y;
|
senddata["GameSize"]["Y"] = Game.GameSize.Y;
|
||||||
|
senddata["GameField"] = new JObject();
|
||||||
for (int i = 0; i < Game.GameSize.X; i++)
|
for (int i = 0; i < Game.GameSize.X; i++)
|
||||||
{
|
{
|
||||||
|
senddata["GameField"][i + ""] = new JObject();
|
||||||
for (int j = 0; j < Game.GameSize.Y; j++)
|
for (int j = 0; j < Game.GameSize.Y; j++)
|
||||||
{
|
{
|
||||||
senddata["GameField"][i][j]["PlayerName"] = Game.GameField[i, j].PlayerName;
|
senddata["GameField"][i + ""][j + ""] = new JObject();
|
||||||
senddata["GameField"][i][j]["Tick"] = Game.GameField[i, j].Tick;
|
senddata["GameField"][i + ""][j + ""]["PlayerName"] = Game.GameField[i, j].PlayerName;
|
||||||
senddata["GameField"][i][j]["Type"] = Game.GameField[i, j].Type.ToString();
|
senddata["GameField"][i + ""][j + ""]["Tick"] = Game.GameField[i, j].Tick;
|
||||||
|
senddata["GameField"][i + ""][j + ""]["Type"] = Game.GameField[i, j].Type.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
senddata["Players"] = new JObject();
|
||||||
foreach (Player joinedplayer in ConnectedMatch.Players)
|
foreach (Player joinedplayer in ConnectedMatch.Players)
|
||||||
{
|
{
|
||||||
|
if (joinedplayer.Name == player.Name)
|
||||||
|
continue;
|
||||||
|
senddata["Players"][joinedplayer.Name] = new JObject();
|
||||||
|
senddata["Players"][joinedplayer.Name]["Position"] = new JObject();
|
||||||
senddata["Players"][joinedplayer.Name]["Position"]["X"] = joinedplayer.Position.X;
|
senddata["Players"][joinedplayer.Name]["Position"]["X"] = joinedplayer.Position.X;
|
||||||
senddata["Players"][joinedplayer.Name]["Position"]["Y"] = joinedplayer.Position.Y;
|
senddata["Players"][joinedplayer.Name]["Position"]["Y"] = joinedplayer.Position.Y;
|
||||||
senddata["Players"][joinedplayer.Name]["Color"] = joinedplayer.Color.ToArgb();
|
senddata["Players"][joinedplayer.Name]["Color"] = joinedplayer.Color.ToArgb();
|
||||||
}
|
}
|
||||||
bwp.Write(senddata.ToString());
|
bwp.Write(senddata.ToString());
|
||||||
|
Game.Paused = false;
|
||||||
|
SendUpdate = true;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
NetUpdateType updatetype = (NetUpdateType)br.ReadInt32();
|
if (!ReceiveAndProcessData(player, br))
|
||||||
switch (updatetype)
|
break;
|
||||||
{
|
|
||||||
case NetUpdateType.Name:
|
|
||||||
string newname = br.ReadString();
|
|
||||||
player.Name = newname;
|
|
||||||
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
|
||||||
{ //ForwardMessage prepares each send and then here the only thing to do is to send the extra data
|
|
||||||
bw.Write(newname);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case NetUpdateType.Color:
|
|
||||||
Color color = Color.FromArgb(br.ReadInt32());
|
|
||||||
player.Color = color;
|
|
||||||
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
|
||||||
{
|
|
||||||
bw.Write(color.ToArgb());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case NetUpdateType.Move:
|
|
||||||
Direction direction = (Direction)br.ReadInt32();
|
|
||||||
Game.MovePlayerPost(player, Game.MovePlayerPre(player, direction));
|
|
||||||
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
|
||||||
{
|
|
||||||
bw.Write((int)direction);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
/*case NetUpdateType.Login:
|
|
||||||
ConnectedMatch.Players.Add(new Player(playername, ConnectedMatch.NextID));
|
|
||||||
break;*/
|
|
||||||
case NetUpdateType.Leave:
|
|
||||||
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
Network.ConnectedMatch.Players.RemoveAll(entry => entry.Name == playername);
|
|
||||||
client.Close();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype)
|
private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype)
|
||||||
{
|
{
|
||||||
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
||||||
{
|
{
|
||||||
//foreach (Player p in ConnectedMatch.Players)
|
|
||||||
Player p;
|
Player p;
|
||||||
while (ConnectedMatch.Players.GetEnumerator().MoveNext())
|
while (ConnectedMatch.Players.GetEnumerator().MoveNext())
|
||||||
{
|
{
|
||||||
|
@ -143,8 +117,7 @@ namespace SnakeGame
|
||||||
if (p == player)
|
if (p == player)
|
||||||
continue;
|
continue;
|
||||||
var bw = new BinaryWriter(p.Client.GetStream());
|
var bw = new BinaryWriter(p.Client.GetStream());
|
||||||
//bw.Write(52); - It should only send it once
|
bw.Write(playername);
|
||||||
//bw.Write(playername); - It should only send it once
|
|
||||||
bw.Write(updatetype);
|
bw.Write(updatetype);
|
||||||
yield return bw;
|
yield return bw;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,9 +19,45 @@ namespace SnakeGame
|
||||||
public static partial class Network
|
public static partial class Network
|
||||||
{
|
{
|
||||||
public const int Port = 12885;
|
public const int Port = 12885;
|
||||||
public static void SyncUpdate(NetUpdateType updatetype) //If we are a server, forward every valid data we get
|
public static void SyncUpdate(NetUpdateType updatetype, object data)
|
||||||
{
|
{
|
||||||
|
if (ConnectedMatch == null)
|
||||||
|
return;
|
||||||
|
BinaryWriter bw;
|
||||||
|
foreach (Player player in ConnectedMatch.Players)
|
||||||
|
{
|
||||||
|
if (player.Name == Game.Player.Name)
|
||||||
|
continue; //Don't send to ourselves
|
||||||
|
bool isserver = ConnectedMatch.OwnerName == Game.Player.Name;
|
||||||
|
if (!isserver)
|
||||||
|
{
|
||||||
|
bw = new BinaryWriter(ConnectedMatch.GetPlayerByName(ConnectedMatch.OwnerName).Client.GetStream());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bw = new BinaryWriter(player.Client.GetStream());
|
||||||
|
}
|
||||||
|
bw.Write((int)updatetype);
|
||||||
|
switch (updatetype)
|
||||||
|
{
|
||||||
|
case NetUpdateType.Name:
|
||||||
|
string newname = (string)data;
|
||||||
|
bw.Write(newname);
|
||||||
|
break;
|
||||||
|
case NetUpdateType.Color:
|
||||||
|
int color = ((Color)data).ToArgb();
|
||||||
|
bw.Write(color);
|
||||||
|
break;
|
||||||
|
case NetUpdateType.Move:
|
||||||
|
int direction = (int)data; //Converting to enum and back to int is unnecessary
|
||||||
|
bw.Write(direction);
|
||||||
|
break;
|
||||||
|
case NetUpdateType.Leave:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!isserver)
|
||||||
|
break; //If not server, only send to the server
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static List<NetMatch> Matches = new List<NetMatch>();
|
public static List<NetMatch> Matches = new List<NetMatch>();
|
||||||
public static NetMatch ConnectedMatch { get; private set; }
|
public static NetMatch ConnectedMatch { get; private set; }
|
||||||
|
@ -111,6 +147,8 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
if (ConnectedMatch == null)
|
if (ConnectedMatch == null)
|
||||||
return;
|
return;
|
||||||
|
SendUpdate = false;
|
||||||
|
SyncUpdate(NetUpdateType.Leave, null);
|
||||||
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
||||||
{
|
{
|
||||||
using (var client = new WebClient())
|
using (var client = new WebClient())
|
||||||
|
@ -170,6 +208,57 @@ namespace SnakeGame
|
||||||
(ReceiverThread = new Thread(new ThreadStart(ClientListenerThreadRun))).Start();
|
(ReceiverThread = new Thread(new ThreadStart(ClientListenerThreadRun))).Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static bool SendUpdate = false;
|
||||||
|
private static bool ReceiveAndProcessData(Player player, BinaryReader br)
|
||||||
|
{
|
||||||
|
string playername = player.Name;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NetUpdateType updatetype = (NetUpdateType)br.ReadInt32();
|
||||||
|
switch (updatetype)
|
||||||
|
{
|
||||||
|
case NetUpdateType.Name:
|
||||||
|
string newname = br.ReadString();
|
||||||
|
player.Name = newname;
|
||||||
|
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
||||||
|
{ //ForwardMessage prepares each send and then here the only thing to do is to send the extra data
|
||||||
|
bw.Write(newname);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case NetUpdateType.Color:
|
||||||
|
Color color = Color.FromArgb(br.ReadInt32());
|
||||||
|
player.Color = color;
|
||||||
|
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
||||||
|
{
|
||||||
|
bw.Write(color.ToArgb());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case NetUpdateType.Move:
|
||||||
|
Direction direction = (Direction)br.ReadInt32();
|
||||||
|
Game.MovePlayerPost(player, Game.MovePlayerPre(player, direction));
|
||||||
|
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
||||||
|
{
|
||||||
|
bw.Write((int)direction);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
/*case NetUpdateType.Login:
|
||||||
|
ConnectedMatch.Players.Add(new Player(playername, ConnectedMatch.NextID));
|
||||||
|
break;*/
|
||||||
|
case NetUpdateType.Leave:
|
||||||
|
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Network.ConnectedMatch.Players.RemoveAll(entry => entry.Name == playername);
|
||||||
|
player.Client.Close();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public enum NetUpdateType
|
public enum NetUpdateType
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -20,9 +21,10 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
name = value;
|
|
||||||
if (Own)
|
if (Own)
|
||||||
Network.SyncUpdate(NetUpdateType.Name);
|
Network.SyncUpdate(NetUpdateType.Name, value);
|
||||||
|
name = value; //Only set name after sending update (which sends old name too)
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private Color color;
|
private Color color;
|
||||||
|
@ -36,14 +38,17 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
color = value;
|
color = value;
|
||||||
if (Own)
|
if (Own)
|
||||||
Network.SyncUpdate(NetUpdateType.Color);
|
Network.SyncUpdate(NetUpdateType.Color, value);
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Point Position;
|
public Point Position;
|
||||||
public TcpClient Client;
|
public TcpClient Client;
|
||||||
|
public int Score;
|
||||||
|
public int Lives;
|
||||||
//public readonly int ID;
|
//public readonly int ID;
|
||||||
public readonly bool Own;
|
public readonly bool Own;
|
||||||
public Player(string name, bool own = false, Color color = default(Color))
|
public Player(string name, bool own = false, Color color = default(Color), int x = 0, int y = 0)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
if (color == default(Color))
|
if (color == default(Color))
|
||||||
|
@ -55,11 +60,101 @@ namespace SnakeGame
|
||||||
//ID = NextID;
|
//ID = NextID;
|
||||||
//NextID++;
|
//NextID++;
|
||||||
Own = own;
|
Own = own;
|
||||||
Position = new Point(0, 0);
|
//Position = new Point(0, 0);
|
||||||
|
Position = new Point(x, y);
|
||||||
|
Score = 0;
|
||||||
|
Lives = 3;
|
||||||
}
|
}
|
||||||
/*public static void Reset()
|
/*public static void Reset()
|
||||||
{
|
{
|
||||||
NextID = 0;
|
NextID = 0;
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
public class PlayerCollection : IList<Player>
|
||||||
|
{
|
||||||
|
private List<Player> _list = new List<Player>();
|
||||||
|
|
||||||
|
public int IndexOf(Player item)
|
||||||
|
{
|
||||||
|
return _list.IndexOf(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert(int index, Player item)
|
||||||
|
{
|
||||||
|
_list.Insert(index, item);
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveAt(int index)
|
||||||
|
{
|
||||||
|
_list.RemoveAt(index);
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player this[int index]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _list[index];
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_list[index] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(Player item)
|
||||||
|
{
|
||||||
|
_list.Add(item);
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
_list.Clear();
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains(Player item)
|
||||||
|
{
|
||||||
|
return _list.Contains(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CopyTo(Player[] array, int arrayIndex)
|
||||||
|
{
|
||||||
|
_list.CopyTo(array, arrayIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get { return _list.Count; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsReadOnly
|
||||||
|
{
|
||||||
|
get { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(Player item)
|
||||||
|
{
|
||||||
|
bool ret = _list.Remove(item);
|
||||||
|
Form1.RefreshPlayerList();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int RemoveAll(Predicate<Player> match)
|
||||||
|
{
|
||||||
|
return _list.RemoveAll(match);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<Player> GetEnumerator()
|
||||||
|
{
|
||||||
|
return _list.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return _list.GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,20 +33,10 @@
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Microsoft.Threading.Tasks">
|
|
||||||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Microsoft.Threading.Tasks.Extensions">
|
|
||||||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
|
|
||||||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
<SpecificVersion>False</SpecificVersion>
|
<SpecificVersion>False</SpecificVersion>
|
||||||
<HintPath>bin\Debug\Newtonsoft.Json.dll</HintPath>
|
<HintPath>bin\Debug\Newtonsoft.Json.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="PresentationCore" />
|
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Net" />
|
<Reference Include="System.Net" />
|
||||||
|
|
Loading…
Reference in a new issue