You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

147 line
2.5 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/rendering/renderer',
  4. 'js/system/client',
  5. 'js/input',
  6. 'js/objects/objects'
  7. ], function(
  8. events,
  9. renderer,
  10. client,
  11. input,
  12. objects
  13. ) {
  14. var scale = 40;
  15. return {
  16. type: 'mouseMover',
  17. hoverTile: {
  18. x: 0,
  19. y: 0
  20. },
  21. path: [],
  22. pathColor: 'rgba(255, 255, 255, 0.5)',
  23. mouseDown: false,
  24. opacityCounter: 0,
  25. sprite: null,
  26. init: function() {
  27. this.sprite = renderer.buildObject({
  28. layerName: 'effects',
  29. x: 0,
  30. y: 0,
  31. w: scale,
  32. h: scale,
  33. sheetName: 'ui',
  34. cell: 7
  35. });
  36. },
  37. clearPath: function() {
  38. this.path.forEach(function(p) {
  39. if (p.sprite) {
  40. renderer.destroyObject({
  41. sprite: p.sprite,
  42. layerName: 'effects'
  43. })
  44. }
  45. });
  46. this.path = [];
  47. },
  48. showPath: function(e) {
  49. if ((e.button != null) && (e.button != 0))
  50. return;
  51. var tileX = ~~(e.x / scale);
  52. var tileY = ~~(e.y / scale);
  53. if ((tileX == this.hoverTile.x) && (tileY == this.hoverTile.y))
  54. return;
  55. events.emit('onChangeHoverTile', tileX, tileY);
  56. this.hoverTile.x = ~~(e.x / scale);
  57. this.hoverTile.y = ~~(e.y / scale);
  58. this.sprite.x = (this.hoverTile.x * scale);
  59. this.sprite.y = (this.hoverTile.y * scale);
  60. return;
  61. if ((!e.down) && (!this.mouseDown))
  62. return;
  63. this.mouseDown = true;
  64. var obj = this.obj;
  65. this.clearPath();
  66. //We floor the position in case we're charging (subpixel position)
  67. var path = physics.getPath({
  68. x: ~~this.obj.pather.pathPos.x,
  69. y: ~~this.obj.pather.pathPos.y
  70. }, {
  71. x: this.hoverTile.x,
  72. y: this.hoverTile.y
  73. });
  74. this.path = path.map(function(p) {
  75. return {
  76. x: p.x,
  77. y: p.y,
  78. sprite: renderer.buildRectangle({
  79. layerName: 'effects',
  80. x: (p.x * scale) + 4,
  81. y: (p.y * scale) + 4,
  82. w: 24,
  83. h: 24,
  84. color: '0x48edff',
  85. alpha: 0.2
  86. })
  87. };
  88. });
  89. },
  90. queuePath: function(e) {
  91. this.mouseDown = false;
  92. if ((this.path.length == 0) || (e.down))
  93. return;
  94. client.request({
  95. cpn: 'player',
  96. method: 'moveList',
  97. data: this.path.map(function(p) {
  98. return {
  99. x: p.x,
  100. y: p.y
  101. }
  102. })
  103. });
  104. this.obj.pather.setPath(this.path);
  105. this.path = [];
  106. },
  107. update: function() {
  108. this.opacityCounter++;
  109. if (this.sprite)
  110. this.sprite.alpha = 0.35 + Math.abs(Math.sin(this.opacityCounter / 20) * 0.35);
  111. this.showPath(input.mouse);
  112. },
  113. destroy: function() {
  114. renderer.destroyObject({
  115. sprite: this.sprite,
  116. layerName: 'effects'
  117. });
  118. }
  119. };
  120. });