91 lines
3.2 KiB
C#

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
{
/// <summary>
/// Data class containing information about a player. Drawn directly to the screen, hence the inheritance
/// of Grid, which allows this entire object to be used in the display
/// </summary>
public class Player : Grid
{
// Gradient that denotes the background of the player sprite
private static readonly 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)
};
private static readonly 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 string Sprite { get; private set; }
public byte[] UnhandledBuffer { get; set; }
public Image PlayerSprite { get; set; } = new();
private readonly 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, string sprite, byte[] identifier, int row, int column) : base() // Call the base constructor at the same time; inits a Grid()
{
PlayerName = name;
hashCode = identifier;
Sprite = sprite;
Row = row;
Column = column;
LastTime = DateTime.Now.Ticks;
Connected = true;
UnhandledBuffer = Array.Empty<byte>();
spriteShadow.Fill = shadowBrush;
_ = Children.Add(spriteShadow);
_ = Children.Add(PlayerSprite);
}
public new bool Equals(object obj)
{
return (obj as Player).hashCode == hashCode;
}
private byte[] hashCode;
/// <summary>
/// TODO:
/// The simplest way to auth a PC is using the computer name.
/// We don't need the textual version; just using the hash-code of the PC name is fine
/// </summary>
/// <returns></returns>
public new int GetHashCode()
{
return hashCode.GetHashCode();
}
}
}