62 lines
2.1 KiB
C#
62 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Controls;
|
|
|
|
namespace RBG_Server
|
|
{
|
|
/// <summary>
|
|
/// Data class containing information about a player. Drawn directly to the screen, hence the inheritance
|
|
/// of Image, which allows this entire object to be
|
|
/// </summary>
|
|
public class Player : Image
|
|
{
|
|
// C# uses implicit field definitions; i.e. declaring a property creates an implicit private field
|
|
// Note that it is also possible to assign values, and specify access modifiers for the get & set independantly
|
|
public string PlayerName { get; set; }
|
|
public int Row { get; set; }
|
|
public int Column { get; set; }
|
|
public long LastTime { get; set; } = DateTime.Now.Ticks;
|
|
public bool Connected { get; private set; }
|
|
public byte[] UnhandledBuffer { get; set; }
|
|
private int processing;
|
|
|
|
public int ObtainLock()
|
|
{
|
|
return System.Threading.Interlocked.CompareExchange(ref processing, 1, 0);
|
|
}
|
|
|
|
public void ReleaseLock()
|
|
{
|
|
_ = System.Threading.Interlocked.Exchange(ref processing, 0);
|
|
}
|
|
|
|
// public Image sprite; // Sprite is now set as the implementation of this class
|
|
|
|
public Player(string name, int row, int column) : base() // Call the base constructor at the same time
|
|
{
|
|
PlayerName = name;
|
|
Row = row;
|
|
Column = column;
|
|
LastTime = DateTime.Now.Ticks;
|
|
Connected = true;
|
|
UnhandledBuffer = Array.Empty<byte>();
|
|
}
|
|
|
|
public new bool Equals(object obj)
|
|
{
|
|
return (obj as Player).GetHashCode() == GetHashCode();
|
|
|
|
}
|
|
|
|
public new int GetHashCode()
|
|
{
|
|
int res = (base.GetHashCode() + PlayerName.GetHashCode()) >> 8; // Right-shift the original hashcode by one byte & use our row & column
|
|
int rcByte = ((Row & 0xF) << 4) | (Column & 0xF);
|
|
return (rcByte << 24) | res;
|
|
}
|
|
}
|
|
}
|