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.
 
 
 

143 lines
2.7 KiB

  1. module.exports = {
  2. type: 'follower',
  3. master: null,
  4. lifetime: -1,
  5. maxDistance: 10,
  6. lastMasterPos: {
  7. x: 0,
  8. y: 0
  9. },
  10. noNeedMaster: false,
  11. fGetHighest: {
  12. inCombat: null,
  13. outOfCombat: null
  14. },
  15. bindEvents: function () {
  16. let master = this.master;
  17. this.lastMasterPos.x = master.x;
  18. this.lastMasterPos.y = master.y;
  19. this.obj.aggro.faction = master.aggro.faction;
  20. this.fGetHighest.inCombat = master.aggro.getHighest.bind(master.aggro);
  21. this.fGetHighest.outOfCombat = this.returnNoAggro.bind(this);
  22. },
  23. returnNoAggro: function () {
  24. let master = this.master;
  25. let obj = this.obj;
  26. let mob = obj.mob;
  27. mob.originX = master.x + ~~((Math.random() * 2) * 2) - 1;
  28. mob.originY = master.y + ~~((Math.random() * 2) * 2) - 1;
  29. return null;
  30. },
  31. despawn: function () {
  32. let obj = this.obj;
  33. obj.destroyed = true;
  34. this.obj.instance.syncer.queue('onGetObject', {
  35. x: obj.x,
  36. y: obj.y,
  37. components: [{
  38. type: 'attackAnimation',
  39. row: 0,
  40. col: 4
  41. }]
  42. }, -1);
  43. },
  44. teleport: function () {
  45. let obj = this.obj;
  46. let physics = obj.instance.physics;
  47. let syncer = obj.syncer;
  48. let master = this.master;
  49. let newPosition = physics.getOpenCellInArea(master.x - 1, master.y - 1, master.x + 1, master.y + 1);
  50. if (!newPosition)
  51. return;
  52. physics.removeObject(obj, obj.x, obj.y);
  53. obj.x = newPosition.x;
  54. obj.y = newPosition.y;
  55. syncer.o.x = obj.x;
  56. syncer.o.y = obj.y;
  57. physics.addObject(obj, obj.x, obj.y);
  58. obj.instance.syncer.queue('onGetObject', {
  59. x: obj.x,
  60. y: obj.y,
  61. components: [{
  62. type: 'attackAnimation',
  63. row: 0,
  64. col: 4
  65. }]
  66. }, -1);
  67. },
  68. update: function () {
  69. if (this.lifetime > 0) {
  70. this.lifetime--;
  71. if (this.lifetime <= 0) {
  72. this.despawn();
  73. return;
  74. }
  75. }
  76. let obj = this.obj;
  77. let master = this.master;
  78. if ((master.destroyed) && (!this.noNeedMaster)) {
  79. this.despawn();
  80. return;
  81. }
  82. let attacker = this.fGetHighest.inCombat();
  83. let maxDistance = this.maxDistance;
  84. let distance = Math.max(Math.abs(obj.x - master.x), Math.abs(obj.y - master.y));
  85. //When we're too far, just teleport
  86. if ((!attacker) && (distance >= maxDistance * 2)) {
  87. this.teleport();
  88. return;
  89. }
  90. let doMove = false;
  91. //If we're not too far from the master but the master is not in combat, move anyway
  92. if (!attacker) {
  93. let lastMasterPos = this.lastMasterPos;
  94. if ((master.x !== lastMasterPos.x) || (master.y !== lastMasterPos.y)) {
  95. doMove = true;
  96. lastMasterPos.x = master.x;
  97. lastMasterPos.y = master.y;
  98. }
  99. }
  100. if (doMove) {
  101. this.obj.clearQueue();
  102. obj.mob.target = obj;
  103. }
  104. obj.aggro.getHighest = doMove ? this.fGetHighest.outOfCombat : this.fGetHighest.inCombat;
  105. },
  106. simplify: function () {
  107. return {
  108. type: 'follower',
  109. master: this.master.id
  110. };
  111. }
  112. };