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.
 
 
 

87 lines
1.6 KiB

  1. define([
  2. 'js/rendering/renderer',
  3. 'js/system/events',
  4. 'js/misc/physics',
  5. 'js/sound/sound'
  6. ], function (
  7. renderer,
  8. events,
  9. physics,
  10. sound
  11. ) {
  12. return {
  13. type: 'player',
  14. oldPos: {
  15. x: 0,
  16. y: 0
  17. },
  18. init: function () {
  19. const obj = this.obj;
  20. obj.addComponent('keyboardMover');
  21. obj.addComponent('mouseMover');
  22. if (isMobile)
  23. obj.addComponent('touchMover');
  24. obj.addComponent('serverActions');
  25. obj.addComponent('pather');
  26. this.hookEvent('onRespawn', this.onRespawn.bind(this));
  27. events.emit('onGetPortrait', obj.portrait);
  28. },
  29. extend: function (blueprint) {
  30. const { collisionChanges, mapChanges } = blueprint;
  31. delete blueprint.collisionChanges;
  32. delete blueprint.mapChanges;
  33. if (collisionChanges)
  34. collisionChanges.forEach(c => physics.setCollision(c));
  35. if (mapChanges) {
  36. mapChanges.forEach(({ x, y, mapCellString }) => {
  37. renderer.updateMapAtPosition(x, y, mapCellString);
  38. });
  39. }
  40. },
  41. update: function () {
  42. const obj = this.obj;
  43. const x = obj.x;
  44. const y = obj.y;
  45. let oldPos = this.oldPos;
  46. if ((oldPos.x === x) && (oldPos.y === y))
  47. return;
  48. oldPos.x = x;
  49. oldPos.y = y;
  50. sound.update(x, y);
  51. this.positionCamera(x, y);
  52. },
  53. positionCamera: function (x, y, instant) {
  54. renderer.setPosition({
  55. x: (x - (renderer.width / (scale * 2))) * scale,
  56. y: (y - (renderer.height / (scale * 2))) * scale
  57. }, instant);
  58. },
  59. onRespawn: function ({ x, y }) {
  60. this.positionCamera(x, y, true);
  61. sound.update(x, y);
  62. },
  63. destroy: function () {
  64. this.unhookEvents();
  65. }
  66. };
  67. });