using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace RBG_Server
{
///
/// Data class containing information about a player. Drawn directly to the screen, hence the inheritance
/// of Image, which allows this entire object to be
///
public class Player : Grid
{
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);
// 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; }
public Image PlayerSprite { get; set; } = new();
private Rectangle spriteShadow = new();
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();
spriteShadow.Fill = shadowBrush;
Children.Add(spriteShadow);
Children.Add(PlayerSprite);
}
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;
}
}
}