using System; using System.Net.WebSockets; namespace gameapi.Library { public class Point { public double X { get; set; } public double Y { get; set; } public Point(double x, double y) { X = x; Y = y; } } public class Player { private WebSocket Connection; public string Id { get; set; } private Tuple Sector { get; set; } public Point Position { get; set; } public Point Velocity { get; set; } public Point Acceleration { get; set; } public double Angle { get; set; } public bool IsThrusting { get; set; } public double maxThrust; public double maxVelocity; public string Color {get; set;} public Player(WebSocket socket) { Connection = socket; Id = "" + socket.GetHashCode(); Sector = new Tuple(0, 0); Position = new Point(5000, 5000); Velocity = new Point(0, 0); Acceleration = new Point(0, 0); Angle = 0; maxThrust = 0.1; maxVelocity = 5; Color = "green"; } public void Update(double delta) { Acceleration.X = IsThrusting ? maxThrust * Math.Cos(Angle) : 0; Acceleration.Y = IsThrusting ? maxThrust * Math.Sin(Angle) : 0; Velocity.X += Acceleration.X; Velocity.Y += Acceleration.Y; Position.X += Velocity.X * delta; Position.Y += Velocity.Y * delta; } public void SetPosition(int x, int y) { this.Position.X = x; this.Position.Y = y; } public string GetKey() { return "" + this.Connection.GetHashCode(); } public WebSocket GetConnection() { return this.Connection; } public Tuple GetSector() { return this.Sector; } public void SetSector(Tuple sector) { this.Sector = sector; } public void SetPositionData(double x, double y, double angle, double velocityX, double velocityY, bool isThrusting) { Position.X = x; Position.Y = y; Velocity.X = velocityX; Velocity.Y = velocityY; this.Angle = angle; this.IsThrusting = isThrusting; Acceleration.X = this.IsThrusting ? this.maxThrust * Math.Cos(this.Angle) : 0; Acceleration.Y = this.IsThrusting ? this.maxThrust * Math.Sin(this.Angle) : 0; } public string GetShipData() { return string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}", this.GetKey(), this.Position.X, this.Position.Y, this.Angle, this.Velocity.X, this.Velocity.Y, this.IsThrusting ? this.Acceleration.X : 0, this.IsThrusting ? this.Acceleration.Y : 0, this.IsThrusting, this.Color ); } } }