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.
 
 
 

101 lines
2.5 KiB

  1. module.exports = {
  2. config: null,
  3. state: -1,
  4. cd: 0,
  5. repeat: 0,
  6. init: function () {
  7. this.update();
  8. },
  9. update: function () {
  10. if (this.cd === 0) {
  11. if (this.state < this.config.length - 1) {
  12. this.state++;
  13. let stateConfig = this.config[this.state];
  14. if (stateConfig.repeat) {
  15. if (!stateConfig.oldRepeat)
  16. stateConfig.oldRepeat = stateConfig.repeat;
  17. stateConfig.repeat--;
  18. }
  19. this.cd = (stateConfig.delay instanceof Array) ? stateConfig.delay[stateConfig.oldRepeat - stateConfig.repeat - 1] : stateConfig.delay;
  20. this.events[stateConfig.type].call(this, stateConfig);
  21. if (stateConfig.repeat > 0)
  22. this.state--;
  23. else
  24. stateConfig.repeat = stateConfig.oldRepeat;
  25. //Sometimes (Like when we make a mob attackable, then check if they're alive in a new phase), the next phase doesn't
  26. // trigger soon enough. So if there's no delay, make sure to switch phases asap.
  27. if (!this.cd)
  28. this.end = true;
  29. } else
  30. this.end = true;
  31. } else
  32. this.cd--;
  33. },
  34. events: {
  35. mobTalk: function (config) {
  36. let mob = this.instance.objects.objects.find(o => (o.id === config.id));
  37. let text = (config.text instanceof Array) ? config.text[config.oldRepeat - config.repeat - 1] : config.text;
  38. if (config.zone) {
  39. this.instance.syncer.queue('onGetMessages', {
  40. messages: {
  41. class: 'q4',
  42. message: mob.name + ': ' + text
  43. }
  44. }, -1);
  45. } else {
  46. mob.addComponent('chatter');
  47. mob.syncer.set(false, 'chatter', 'msg', text);
  48. }
  49. },
  50. addComponents: function (config) {
  51. let objects = this.instance.objects.objects;
  52. let components = config.components;
  53. if (!components.push)
  54. components = [components];
  55. let cLen = components.length;
  56. let mobs = config.mobs;
  57. if (!mobs.push)
  58. mobs = [mobs];
  59. let mLen = mobs.length;
  60. for (let i = 0; i < mLen; i++) {
  61. let mob = objects.find(o => (o.id === mobs[i]));
  62. for (let j = 0; j < cLen; j++) {
  63. let c = components[j];
  64. mob.addComponent(c.type, components[j]);
  65. }
  66. }
  67. },
  68. removeComponents: function (config) {
  69. let objects = this.instance.objects.objects;
  70. let components = config.components;
  71. if (!components.push)
  72. components = [components];
  73. let cLen = components.length;
  74. let mobs = config.mobs;
  75. if (!mobs.push)
  76. mobs = [mobs];
  77. let mLen = mobs.length;
  78. for (let i = 0; i < mLen; i++) {
  79. let mob = objects.find(o => (o.id === mobs[i]));
  80. for (let j = 0; j < cLen; j++)
  81. mob.removeComponent(components[j]);
  82. }
  83. }
  84. }
  85. };