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.
 
 
 

86 lines
1.5 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. let scale = 40;
  13. return {
  14. type: 'player',
  15. oldPos: {
  16. x: 0,
  17. y: 0
  18. },
  19. init: function () {
  20. this.obj.addComponent('keyboardMover');
  21. this.obj.addComponent('mouseMover');
  22. this.obj.addComponent('serverActions');
  23. this.obj.addComponent('pather');
  24. events.emit('onGetPortrait', this.obj.portrait);
  25. },
  26. update: function () {
  27. let obj = this.obj;
  28. let oldPos = this.oldPos;
  29. if ((oldPos.x == obj.x) && (oldPos.y == obj.y))
  30. return;
  31. let dx = obj.x - oldPos.x;
  32. let dy = obj.y - oldPos.y;
  33. let instant = false;
  34. if ((dx > 5) || (dy > 5))
  35. instant = true;
  36. if (dx != 0)
  37. dx = dx / Math.abs(dx);
  38. if (dy != 0)
  39. dy = dy / Math.abs(dy);
  40. this.oldPos.x = this.obj.x;
  41. this.oldPos.y = this.obj.y;
  42. this.canvasFollow({
  43. x: dx,
  44. y: dy
  45. }, instant);
  46. sound.update(obj.x, obj.y);
  47. },
  48. extend: function (blueprint) {
  49. if (blueprint.collisionChanges) {
  50. blueprint.collisionChanges.forEach(function (c) {
  51. physics.setCollision(c.x, c.y, c.collides);
  52. });
  53. delete blueprint.collisionChanges;
  54. }
  55. },
  56. canvasFollow: function (delta, instant) {
  57. let obj = this.obj;
  58. delta = delta || {
  59. x: 0,
  60. y: 0
  61. };
  62. renderer.setPosition({
  63. x: (obj.x - (renderer.width / (scale * 2))) * scale,
  64. y: (obj.y - (renderer.height / (scale * 2))) * scale
  65. }, instant);
  66. }
  67. };
  68. });