player.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. module.exports = class Player {
  2. constructor() {
  3. this.id = 0;
  4. this.socket = null;
  5. this.position = {x: 60, y: 60};
  6. this.velocity = {x: 0, y: 0};
  7. this.maxSpeed = 3;
  8. this.turnRate = Math.PI / 8;
  9. this.moveToTarget = false;
  10. this.targetDestination = {x: 60, y: 60};
  11. this.targetAngle = 0;
  12. this.angleRadians = 0;
  13. this.shipColor = null;
  14. this.playerName = "Player";
  15. this.shipColor = "#DD3333";
  16. }
  17. getData(keys) {
  18. var response = {};
  19. for(var i in keys) {
  20. if(this.hasOwnProperty(keys[i])) {
  21. response[keys[i]] = this[keys[i]];
  22. }
  23. }
  24. return response;
  25. }
  26. sendMessage(data) {
  27. data.timestamp = new Date().getTime();
  28. this.socket.sendUTF(JSON.stringify(data, null, "\t"));
  29. }
  30. update(delta) {
  31. //this.angleRadians = this.targetAngle;
  32. /*if(this.angleRadians != this.targetAngle) {
  33. if(this.angleRadians > this.targetAngle) {
  34. this.angleRadians += Math.min(this.angleRadians - this.targetAngle, -this.turnRate);
  35. }
  36. if(this.angleRadians < this.targetAngle) {
  37. this.angleRadians += Math.min(this.angleRadians + this.targetAngle, this.turnRate);
  38. }
  39. }*/
  40. if(this.moveToTarget) {
  41. if(this.targetDestination.x > this.position.x) { //we're moving right
  42. if(this.position.x + this.velocity.x * delta > this.targetDestination.x) {
  43. this.position.x = this.targetDestination.x;
  44. this.velocity.x = 0;
  45. } else {
  46. this.position.x += this.velocity.x * delta;
  47. }
  48. } else if(this.targetDestination.x < this.position.x) { //we're moving left
  49. if(this.position.x + this.velocity.x * delta < this.targetDestination.x) {
  50. this.position.x = this.targetDestination.x;
  51. this.velocity.x = 0;
  52. } else {
  53. this.position.x += this.velocity.x * delta;
  54. }
  55. } else {
  56. this.position.x += this.velocity.x * delta;
  57. }
  58. if(this.targetDestination.y > this.position.y) { //we're moving down
  59. if(this.position.y + this.velocity.y * delta > this.targetDestination.y) {
  60. this.position.y = this.targetDestination.y;
  61. this.velocity.y = 0;
  62. } else {
  63. this.position.y += this.velocity.y * delta;
  64. }
  65. } else if(this.targetDestination.y < this.position.y) { //we're moving up
  66. if(this.position.y + this.velocity.y * delta < this.targetDestination.y) {
  67. this.position.y = this.targetDestination.y;
  68. this.velocity.y = 0;
  69. } else {
  70. this.position.y += this.velocity.y * delta;
  71. }
  72. } else {
  73. this.position.y += this.velocity.y * delta;
  74. }
  75. if(this.targetDestination.x == this.position.x && this.targetDestination.y == this.position.y) { //we're moving right
  76. this.moveToTarget = false;
  77. this.sendUpdate(this);
  78. }
  79. } else {
  80. this.position.x += this.velocity.x * delta;
  81. this.position.y += this.velocity.y * delta;
  82. }
  83. }
  84. sendUpdate(sender) {
  85. var keys = ["id", "position", "velocity", "targetDestination", "shipColor", "moveToTarget", "targetAngle", "angleRadians", "playerName"];
  86. if(sender == this) {
  87. this.sendMessage({player: this.getData(keys)});
  88. } else {
  89. this.sendMessage({other: sender.getData(keys)});
  90. }
  91. }
  92. logout() {
  93. this.socket.close();
  94. }
  95. };