Guest User

Untitled

a guest
Jun 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. var canvas = {
  2.  
  3. _robots: [
  4. ],
  5.  
  6. _ctx: null,
  7.  
  8. init: function() {
  9. var canvas = document.getElementById("canvas");
  10. this._ctx = canvas.getContext("2d");
  11. },
  12.  
  13. hit: function(id, hp) {
  14. var robot = null;
  15. for(var i = 0; i < this._robots.length;i++) {
  16. if(this._robots[i].id == id) {
  17. robot = this._robots[i];
  18. }
  19. }
  20. if(robot) {
  21. robot.hp = hp;
  22.  
  23. this._redraw();
  24. }
  25. },
  26.  
  27. move: function(id, x, y) {
  28. x = x*10;
  29. y = y*5;
  30. var robot = null;
  31. for(var i = 0; i < this._robots.length;i++) {
  32. if(this._robots[i].id == id) {
  33. robot = this._robots[i];
  34. }
  35. }
  36. if(robot) {
  37. var old_pos = robot.pos;
  38. var that = this;
  39. (function _s(current_x, current_y) {
  40. robot.pos = {
  41. x: (current_x+1),
  42. y: (current_y)
  43. };
  44. that._redraw.call(that);
  45. var new_x = current_x;
  46. var new_y = current_y;
  47. if(current_x < x) {
  48. new_x = new_x+1;
  49. } else if(current_x > x) {
  50. new_x = new_x-1;
  51. }
  52. if(current_y < y) {
  53. new_y = new_y+1;
  54. } else if(current_y > y) {
  55. new_y = new_y-1;
  56. }
  57.  
  58. if(new_y != current_y || new_x != current_x) {
  59. setTimeout(function() { _s(new_x, new_y) }, 10);
  60. }
  61. })(old_pos.x, old_pos.y);
  62. } else {
  63. this._robots.push({
  64. id: id,
  65. pos: {x: x, y: y},
  66. hp: 3
  67. });
  68. }
  69. },
  70.  
  71.  
  72. _roundRect: function(ctx, x, y, width, height, radius, fill, stroke) {
  73. ctx.beginPath();
  74. ctx.moveTo(x + radius, y);
  75. ctx.lineTo(x + width - radius, y);
  76. ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  77. ctx.lineTo(x + width, y + height - radius);
  78. ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  79. ctx.lineTo(x + radius, y + height);
  80. ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  81. ctx.lineTo(x, y + radius);
  82. ctx.quadraticCurveTo(x, y, x + radius, y);
  83. ctx.closePath();
  84. if (stroke) {
  85. ctx.stroke();
  86. }
  87. if (fill) {
  88. ctx.fill();
  89. }
  90. },
  91.  
  92.  
  93. _redraw: function() {
  94. var r = this._robots;
  95. this._ctx.clearRect(0,0,1000,500);
  96. var ctx = this._ctx;
  97. for(var i = 0; i < r.length; i++) {
  98.  
  99. var x = r[i].pos.x;
  100. var y = r[i].pos.y;
  101.  
  102. switch(r[i].hp) {
  103. case 3:
  104. ctx.fillStyle = "#0F0";
  105. break;
  106. case 2:
  107. ctx.fillStyle = "rgba(0, 255, 0, .7)";
  108. break;
  109. case 1:
  110. ctx.fillStyle = "rgba(0, 255, 0, .3)";
  111. break;
  112. case 0:
  113. ctx.fillStyle = "#FFF";
  114. break;
  115. }
  116.  
  117. ctx.beginPath();
  118. ctx.arc(r[i].pos.x, r[i].pos.y-16, 13, 0, Math.PI, true);
  119. ctx.closePath();
  120. ctx.fill();
  121.  
  122. this._roundRect(ctx, x-21, y-13, 5, 23, 3, true, false);
  123. this._roundRect(ctx, x+16, y-13, 5, 23, 3, true, false);
  124.  
  125. this._roundRect(ctx, x-8, y+14, 5, 10, 3, true, false);
  126. this._roundRect(ctx, x+3, y+14, 5, 10, 3, true, false);
  127.  
  128. this._roundRect(ctx, x-15, y-15, 30, 30, 6, true, false);
  129. }
  130. }
  131.  
  132. };
Add Comment
Please, Sign In to add comment