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.
 
 
 

115 lines
2.0 KiB

  1. define([
  2. 'js/system/client',
  3. 'js/misc/physics',
  4. 'js/system/events'
  5. ], function (
  6. client,
  7. physics,
  8. events
  9. ) {
  10. return {
  11. type: 'touchMover',
  12. lastNode: null,
  13. nodes: [],
  14. hoverTile: null,
  15. minSqrDistance: 1650,
  16. init: function () {
  17. ['onTouchStart', 'onTouchMove', 'onTouchEnd', 'onTouchCancel'].forEach(e => {
  18. this.hookEvent(e, this[e].bind(this));
  19. });
  20. this.obj.on('onShake', this.onShake.bind(this));
  21. },
  22. onTouchStart: function (e) {
  23. this.lastNode = e;
  24. let tileX = ~~(e.worldX / scale);
  25. let tileY = ~~(e.worldY / scale);
  26. this.hoverTile = {
  27. x: tileX,
  28. y: tileY
  29. };
  30. events.emit('onChangeHoverTile', tileX, tileY);
  31. },
  32. onTouchMove: function (e) {
  33. const lastNode = this.lastNode;
  34. let sqrDistance = Math.pow(lastNode.x - e.x, 2) + Math.pow(lastNode.y - e.y, 2);
  35. if (sqrDistance < this.minSqrDistance)
  36. return;
  37. let dx = e.x - lastNode.x;
  38. let dy = e.y - lastNode.y;
  39. if (e.touches > 1) {
  40. dx = ~~(dx / Math.abs(dx));
  41. dy = ~~(dy / Math.abs(dy));
  42. } else if (Math.abs(dx) > Math.abs(dy)) {
  43. dx = ~~(dx / Math.abs(dx));
  44. dy = 0;
  45. } else {
  46. dx = 0;
  47. dy = ~~(dy / Math.abs(dy));
  48. }
  49. this.lastNode = e;
  50. let newX = this.obj.pather.pathPos.x + dx;
  51. let newY = this.obj.pather.pathPos.y + dy;
  52. if (physics.isTileBlocking(~~newX, ~~newY)) {
  53. this.bump(dx, dy);
  54. return;
  55. }
  56. this.obj.pather.add(newX, newY);
  57. },
  58. onTouchEnd: function (e) {
  59. this.lastNode = null;
  60. },
  61. onTouchCancel: function () {
  62. this.lastNode = null;
  63. },
  64. onShake: function () {
  65. if (!this.obj.pather.path.length)
  66. return;
  67. client.request({
  68. cpn: 'player',
  69. method: 'queueAction',
  70. data: {
  71. action: 'clearQueue',
  72. priority: true
  73. }
  74. });
  75. window.navigator.vibrate(150);
  76. },
  77. bump: function (dx, dy) {
  78. if (this.obj.pather.path.length > 0)
  79. return;
  80. this.obj.addComponent('bumpAnimation', {
  81. deltaX: dx,
  82. deltaY: dy
  83. });
  84. },
  85. destroy: function () {
  86. this.unhookEvents();
  87. }
  88. };
  89. });