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.
 
 
 

102 lines
1.6 KiB

  1. define([
  2. 'js/input',
  3. 'js/system/client',
  4. 'js/misc/physics'
  5. ], function (
  6. input,
  7. client,
  8. physics
  9. ) {
  10. return {
  11. type: 'keyboardMover',
  12. moveCd: 0,
  13. moveCdMax: 8,
  14. direction: {
  15. x: 0,
  16. y: 0
  17. },
  18. update: function () {
  19. if (this.obj.dead)
  20. return;
  21. if (this.obj.moveAnimation)
  22. this.obj.pather.clearPath();
  23. if (input.isKeyDown('esc')) {
  24. client.request({
  25. cpn: 'player',
  26. method: 'queueAction',
  27. data: {
  28. action: 'clearQueue',
  29. priority: true
  30. }
  31. });
  32. }
  33. if (this.moveCd > 0) {
  34. this.moveCd--;
  35. return;
  36. }
  37. this.keyMove();
  38. },
  39. bump: function (dx, dy) {
  40. if (this.obj.pather.path.length > 0)
  41. return;
  42. this.obj.addComponent('bumpAnimation', {
  43. deltaX: dx,
  44. deltaY: dy
  45. });
  46. },
  47. keyMove: function () {
  48. let delta = {
  49. x: input.getAxis('horizontal'),
  50. y: input.getAxis('vertical')
  51. };
  52. if ((!delta.x) && (!delta.y))
  53. return;
  54. this.direction.x = delta.x;
  55. this.direction.y = delta.y;
  56. let newX = this.obj.pather.pathPos.x + delta.x;
  57. let newY = this.obj.pather.pathPos.y + delta.y;
  58. if (physics.isTileBlocking(~~newX, ~~newY)) {
  59. this.bump(delta.x, delta.y);
  60. return;
  61. }
  62. this.moveCd = this.moveCdMax;
  63. this.addQueue(newX, newY);
  64. },
  65. addQueue: function (x, y) {
  66. if (this.obj.moveAnimation)
  67. return;
  68. else if (!this.obj.pather.add(x, y))
  69. return;
  70. this.obj.dirty = true;
  71. this.obj.pather.pathPos.x = x;
  72. this.obj.pather.pathPos.y = y;
  73. client.request({
  74. cpn: 'player',
  75. method: 'move',
  76. data: {
  77. x: x,
  78. y: y
  79. }
  80. });
  81. }
  82. };
  83. });