Player.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Net.WebSockets;
  3. namespace gameapi.Library
  4. {
  5. public class Point
  6. {
  7. public double X { get; set; }
  8. public double Y { get; set; }
  9. public Point(double x, double y)
  10. {
  11. X = x;
  12. Y = y;
  13. }
  14. }
  15. public class Player
  16. {
  17. private WebSocket Connection;
  18. public string Id { get; set; }
  19. private Tuple<int, int> Sector { get; set; }
  20. public Point Position { get; set; }
  21. public Point Velocity { get; set; }
  22. public Point Acceleration { get; set; }
  23. public double Angle { get; set; }
  24. public bool IsThrusting { get; set; }
  25. public double maxThrust;
  26. public double maxVelocity;
  27. public string Color {get; set;}
  28. public Player(WebSocket socket)
  29. {
  30. Connection = socket;
  31. Id = "" + socket.GetHashCode();
  32. Sector = new Tuple<int, int>(0, 0);
  33. Position = new Point(5000, 5000);
  34. Velocity = new Point(0, 0);
  35. Acceleration = new Point(0, 0);
  36. Angle = 0;
  37. maxThrust = 0.1;
  38. maxVelocity = 5;
  39. Color = "green";
  40. }
  41. public void Update(double delta)
  42. {
  43. Acceleration.X = IsThrusting ? maxThrust * Math.Cos(Angle) : 0;
  44. Acceleration.Y = IsThrusting ? maxThrust * Math.Sin(Angle) : 0;
  45. Velocity.X += Acceleration.X;
  46. Velocity.Y += Acceleration.Y;
  47. Position.X += Velocity.X * delta;
  48. Position.Y += Velocity.Y * delta;
  49. }
  50. public void SetPosition(int x, int y)
  51. {
  52. this.Position.X = x;
  53. this.Position.Y = y;
  54. }
  55. public string GetKey()
  56. {
  57. return "" + this.Connection.GetHashCode();
  58. }
  59. public WebSocket GetConnection()
  60. {
  61. return this.Connection;
  62. }
  63. public Tuple<int, int> GetSector()
  64. {
  65. return this.Sector;
  66. }
  67. public void SetSector(Tuple<int, int> sector)
  68. {
  69. this.Sector = sector;
  70. }
  71. public void SetPositionData(double x, double y, double angle, double velocityX, double velocityY, bool isThrusting)
  72. {
  73. Position.X = x;
  74. Position.Y = y;
  75. Velocity.X = velocityX;
  76. Velocity.Y = velocityY;
  77. this.Angle = angle;
  78. this.IsThrusting = isThrusting;
  79. Acceleration.X = this.IsThrusting ? this.maxThrust * Math.Cos(this.Angle) : 0;
  80. Acceleration.Y = this.IsThrusting ? this.maxThrust * Math.Sin(this.Angle) : 0;
  81. }
  82. public string GetShipData()
  83. {
  84. return string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}",
  85. this.GetKey(),
  86. this.Position.X,
  87. this.Position.Y,
  88. this.Angle,
  89. this.Velocity.X,
  90. this.Velocity.Y,
  91. this.IsThrusting ? this.Acceleration.X : 0,
  92. this.IsThrusting ? this.Acceleration.Y : 0,
  93. this.IsThrusting,
  94. this.Color
  95. );
  96. }
  97. }
  98. }