player.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. function Player(camera, data) {
  2. this.id = 0;
  3. this.position = data.position || {x: 60, y: 60};
  4. this.velocity = data.velocity || {x: 0, y: 0};
  5. this.width = 100;
  6. this.height = 100;
  7. this.maxSpeed = 3;
  8. this.turnRate = Math.PI / 16;
  9. this.moveToTarget = data.moveToTarget || false;
  10. this.targetDestination = data.targetDestination || {x: 60, y: 60};
  11. this.targetAngle = data.targetAngle || 0;
  12. this.angleRadians = data.angleRadians || 0;
  13. this.shipColor = data.shipColor || "#DD0000";
  14. this.playerName = "Player";
  15. this.spriteCanvas = null;
  16. this.spriteContext = null;
  17. this.sync = function(timestamp, data) {
  18. if(timestamp + 25 >= new Date().getTime()) {
  19. for(var key in data) {
  20. if(this.hasOwnProperty(key)) {
  21. this[key] = data[key];
  22. if(key == "shipColor") {
  23. this.spriteContext = null;
  24. this.spriteCanvas = null;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. this.update = function(delta) {
  31. if(this.angleRadians != this.targetAngle) {
  32. //this.angleRadians += Math.min(this.targetAngle - this.angleRadians, this.turnRate);
  33. this.angleRadians = this.targetAngle;
  34. }
  35. if(this.moveToTarget) {
  36. if(this.targetDestination.x > this.position.x) { //we're moving right
  37. if(this.position.x + this.velocity.x * delta > this.targetDestination.x) {
  38. this.position.x = this.targetDestination.x;
  39. this.velocity.x = 0;
  40. } else {
  41. this.position.x += this.velocity.x * delta;
  42. }
  43. } else if(this.targetDestination.x < this.position.x) { //we're moving left
  44. if(this.position.x + this.velocity.x * delta < this.targetDestination.x) {
  45. this.position.x = this.targetDestination.x;
  46. this.velocity.x = 0;
  47. } else {
  48. this.position.x += this.velocity.x * delta;
  49. }
  50. } else {
  51. this.position.x += this.velocity.x * delta;
  52. }
  53. if(this.targetDestination.y > this.position.y) { //we're moving down
  54. if(this.position.y + this.velocity.y * delta > this.targetDestination.y) {
  55. this.position.y = this.targetDestination.y;
  56. this.velocity.y = 0;
  57. } else {
  58. this.position.y += this.velocity.y * delta;
  59. }
  60. } else if(this.targetDestination.y < this.position.y) { //we're moving up
  61. if(this.position.y + this.velocity.y * delta < this.targetDestination.y) {
  62. this.position.y = this.targetDestination.y;
  63. this.velocity.y = 0;
  64. } else {
  65. this.position.y += this.velocity.y * delta;
  66. }
  67. } else {
  68. this.position.y += this.velocity.y * delta;
  69. }
  70. if(this.targetDestination.x == this.position.x && this.targetDestination.y == this.position.y) { //we're moving right
  71. this.moveToTarget = false;
  72. }
  73. } else {
  74. this.position.x += this.velocity.x * delta;
  75. this.position.y += this.velocity.y * delta;
  76. }
  77. }
  78. this.preRender = function() {
  79. if(this.spriteContext == null) {
  80. this.spriteCanvas = document.createElement("canvas");
  81. this.spriteCanvas.width = this.width;
  82. this.spriteCanvas.height = this.height;
  83. this.spriteContext = this.spriteCanvas.getContext("2d");
  84. this.spriteContext.fillStyle = this.shipColor;
  85. this.spriteContext.strokeStyle = this.shipColor;
  86. this.spriteContext.lineWidth = 1;
  87. this.spriteContext.save();
  88. this.spriteContext.translate(this.width / 2, this.height / 2);
  89. this.spriteContext.beginPath();
  90. //this.spriteContext.rotate(this.angleRadians);
  91. this.spriteContext.moveTo(this.width / 2, 0);
  92. this.spriteContext.lineTo(-(this.width / 2), -(this.height / 3));
  93. this.spriteContext.lineTo(-(this.width / 2), this.height / 3);
  94. this.spriteContext.lineTo(this.width / 2, 0);
  95. this.spriteContext.fill();
  96. this.spriteContext.stroke();
  97. this.spriteContext.restore();
  98. }
  99. }
  100. this.draw = function(context) {
  101. this.preRender();
  102. var truePos = {x: 0, y: 0};
  103. truePos.x = camera.x + this.position.x;
  104. truePos.y = camera.y + this.position.y;
  105. context.save();
  106. context.translate(truePos.x, truePos.y);
  107. context.rotate(this.angleRadians);
  108. context.drawImage(this.spriteCanvas, 0, 0, this.spriteCanvas.width, this.spriteCanvas.height,
  109. -this.spriteCanvas.width / 2, -this.spriteCanvas.height /2 , this.width, this.height);
  110. context.restore();
  111. context.font = "16px sans-serif";
  112. context.textAlign = "center";
  113. context.fillStyle = "#DDDDDD";
  114. context.strokeStyle = "#333333";
  115. context.lineWidth = 2;
  116. var textMetrics = context.measureText(this.playerName);
  117. var textWidth = textMetrics.width;
  118. context.fillText(this.playerName,
  119. camera.x + this.position.x,
  120. camera.y + this.position.y);
  121. //context.beginPath();
  122. //context.fillRect(truePos.x, truePos.y, this.width, this.height);
  123. //context.fillStyle= null;
  124. //context.rect(truePos.x - this.width / 2, truePos.y - this.height / 2, this.width, this.height);
  125. }
  126. }