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.
 
 
 

80 regels
1.3 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. events.on('onCanvasKeyDown', this.onCanvasKeyDown.bind(this));
  18. },
  19. update: function () {
  20. if (this.obj.dead)
  21. return;
  22. if (this.moveCd > 0) {
  23. this.moveCd--;
  24. return;
  25. }
  26. this.keyMove();
  27. },
  28. onCanvasKeyDown: function (keyEvent) {
  29. if (keyEvent.key === 'esc') {
  30. client.request({
  31. cpn: 'player',
  32. method: 'queueAction',
  33. data: {
  34. action: 'clearQueue',
  35. priority: true
  36. }
  37. });
  38. }
  39. },
  40. bump: function (dx, dy) {
  41. if (this.obj.pather.path.length > 0)
  42. return;
  43. this.obj.addComponent('bumpAnimation', {
  44. deltaX: dx,
  45. deltaY: dy
  46. });
  47. },
  48. keyMove: function () {
  49. let delta = {
  50. x: input.getAxis('horizontal'),
  51. y: input.getAxis('vertical')
  52. };
  53. if ((!delta.x) && (!delta.y))
  54. return;
  55. let newX = this.obj.pather.pathPos.x + delta.x;
  56. let newY = this.obj.pather.pathPos.y + delta.y;
  57. if (physics.isTileBlocking(~~newX, ~~newY)) {
  58. this.bump(delta.x, delta.y);
  59. return;
  60. }
  61. this.moveCd = this.moveCdMax;
  62. this.obj.pather.addQueue(newX, newY);
  63. }
  64. };
  65. });