SnakeGame/SnakeGame/Player.cs

192 lines
4.4 KiB
C#
Raw Normal View History

2017-01-07 23:18:03 +01:00
using System;
2017-01-07 23:22:27 +01:00
using System.Collections;
2017-01-07 23:18:03 +01:00
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
2017-01-07 23:20:57 +01:00
using System.Net.Sockets;
2017-01-07 23:18:03 +01:00
using System.Text;
using System.Threading.Tasks;
namespace SnakeGame
{
public class Player
{
2017-01-07 23:20:57 +01:00
//public static int NextID = 0;
private string name = "";
public string Name
{
get
{
return name;
}
set
{
if (Own)
2017-01-07 23:22:27 +01:00
Network.SyncUpdate(NetUpdateType.Name, value);
name = value; //Only set name after sending update (which sends old name too)
Form1.RefreshPlayerList();
2017-01-07 23:20:57 +01:00
}
}
private Color color;
public Color Color
{
get
{
return color;
}
set
{
color = value;
if (Own)
2017-01-07 23:22:27 +01:00
Network.SyncUpdate(NetUpdateType.Color, value);
Form1.RefreshPlayerList();
2017-01-07 23:20:57 +01:00
}
}
public Point Position;
public TcpClient Client;
2017-01-07 23:23:59 +01:00
private int score;
public int Score
{
get
{
return score;
}
set
{
score = value;
if (Own)
Network.SyncUpdate(NetUpdateType.Score, value);
Form1.RefreshPlayerList();
}
}
private int lives;
public int Lives
{
get
{
return lives;
}
set
{
lives = value;
if (Own)
Network.SyncUpdate(NetUpdateType.Lives, value);
Form1.RefreshPlayerList();
}
}
2017-01-07 23:21:48 +01:00
//public readonly int ID;
2017-01-07 23:20:57 +01:00
public readonly bool Own;
2017-01-07 23:22:27 +01:00
public Player(string name, bool own = false, Color color = default(Color), int x = 0, int y = 0)
2017-01-07 23:18:03 +01:00
{
Name = name;
2017-01-07 23:21:48 +01:00
if (color == default(Color))
{
Color = Game.GetRandomColor();
}
else
Color = color;
2017-01-07 23:20:57 +01:00
//ID = NextID;
//NextID++;
Own = own;
2017-01-07 23:22:27 +01:00
//Position = new Point(0, 0);
2017-01-07 23:23:59 +01:00
if (x != 0 && y != 0)
Position = new Point(x, y);
else
Position = new Point(Game.GameSize.X / 2, 1);
2017-01-07 23:22:27 +01:00
Score = 0;
Lives = 3;
2017-01-07 23:18:03 +01:00
}
2017-01-07 23:20:57 +01:00
/*public static void Reset()
{
NextID = 0;
}*/
2017-01-07 23:18:03 +01:00
}
2017-01-07 23:22:27 +01:00
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();
}
}
2017-01-07 23:18:03 +01:00
}