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.
 
 
 

98 lines
1.8 KiB

  1. define([
  2. 'js/input',
  3. 'js/system/client',
  4. 'js/misc/physics',
  5. 'js/system/events'
  6. ], function (
  7. input,
  8. client,
  9. physics,
  10. events
  11. ) {
  12. return {
  13. type: 'keyboardMover',
  14. moveCd: 0,
  15. moveCdMax: 8,
  16. init: function () {
  17. this.hookEvent('onCanvasKeyDown', this.onCanvasKeyDown.bind(this));
  18. this.hookEvent('onMoveSpeedChange', this.onMoveSpeedChange.bind(this));
  19. },
  20. update: function () {
  21. if (this.obj.dead)
  22. return;
  23. if (this.moveCd > 0) {
  24. this.moveCd--;
  25. return;
  26. }
  27. this.keyMove();
  28. },
  29. //Changes the moveCdMax variable
  30. // moveSpeed is affected when mounting and unmounting
  31. // moveSpeed: 0 | moveCdMax: 8
  32. // moveSpeed: 200 | moveCdMax: 4
  33. onMoveSpeedChange: function (moveSpeed) {
  34. this.moveCdMax = Math.ceil(4 + (((200 - moveSpeed) / 200) * 4));
  35. },
  36. onCanvasKeyDown: function (keyEvent) {
  37. if (keyEvent.key === 'esc') {
  38. client.request({
  39. cpn: 'player',
  40. method: 'queueAction',
  41. data: {
  42. action: 'clearQueue',
  43. priority: true
  44. }
  45. });
  46. }
  47. },
  48. bump: function (dx, dy) {
  49. if (this.obj.pather.path.length > 0)
  50. return;
  51. if (this.obj.bumpAnimation)
  52. return;
  53. events.emit('onObjCollideBump', this.obj);
  54. this.obj.addComponent('bumpAnimation', {
  55. deltaX: dx,
  56. deltaY: dy
  57. });
  58. },
  59. keyMove: function () {
  60. let delta = {
  61. x: input.getAxis('horizontal'),
  62. y: input.getAxis('vertical')
  63. };
  64. if ((!delta.x) && (!delta.y))
  65. return;
  66. let newX = this.obj.pather.pathPos.x + delta.x;
  67. let newY = this.obj.pather.pathPos.y + delta.y;
  68. if (physics.isTileBlocking(~~newX, ~~newY)) {
  69. this.bump(delta.x, delta.y);
  70. return;
  71. }
  72. this.moveCd = this.moveCdMax;
  73. this.obj.pather.add(newX, newY);
  74. },
  75. destroy: function () {
  76. this.unhookEvents();
  77. }
  78. };
  79. });