2021-08-29 15:04:28 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Controls;
|
2021-08-30 19:42:01 +12:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Shapes;
|
2021-08-29 15:04:28 +12:00
|
|
|
|
|
|
|
|
|
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>
|
2021-08-30 19:42:01 +12:00
|
|
|
|
public class Player : Grid
|
2021-08-29 15:04:28 +12:00
|
|
|
|
{
|
2021-08-30 19:42:01 +12:00
|
|
|
|
static GradientStopCollection gradientStops = new(5)
|
|
|
|
|
{
|
|
|
|
|
new GradientStop(Color.FromArgb(255, 0, 255, 255), 0),
|
|
|
|
|
new GradientStop(Color.FromArgb(192, 0, 255, 255), 0.75),
|
|
|
|
|
new GradientStop(Color.FromArgb(128, 0, 255, 255), 0.85),
|
|
|
|
|
new GradientStop(Color.FromArgb(32, 0, 255, 255), 0.95),
|
|
|
|
|
new GradientStop(Color.FromArgb(0, 0, 255, 255), 1)
|
|
|
|
|
};
|
|
|
|
|
static RadialGradientBrush shadowBrush = new(gradientStops);
|
|
|
|
|
|
2021-08-29 15:04:28 +12:00
|
|
|
|
// 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; }
|
2021-08-30 19:42:01 +12:00
|
|
|
|
|
|
|
|
|
public Image PlayerSprite { get; set; } = new();
|
|
|
|
|
private Rectangle spriteShadow = new();
|
|
|
|
|
|
2021-08-29 15:04:28 +12:00
|
|
|
|
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>();
|
2021-08-30 19:42:01 +12:00
|
|
|
|
|
|
|
|
|
spriteShadow.Fill = shadowBrush;
|
|
|
|
|
Children.Add(spriteShadow);
|
|
|
|
|
Children.Add(PlayerSprite);
|
2021-08-29 15:04:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|