Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

160 рядки
3.0 KiB

  1. let mobBuilder = require('../../world/mobBuilder');
  2. const buildMob = (objects, mobConfig, x, y, mobIndex) => {
  3. const { id, sheetName, cell, name, properties, originX, originY, maxChaseDistance, dialogue, trade, chats, events } = mobConfig;
  4. let mob = objects.buildObjects([{
  5. x: x,
  6. y: y,
  7. sheetName: sheetName || 'mobs',
  8. cell: cell,
  9. name: name,
  10. properties: properties
  11. }]);
  12. mobBuilder.build(mob, mobConfig);
  13. if (id)
  14. mob.id = id.split('$').join(mobIndex);
  15. if (originX) {
  16. mob.mob.originX = originX;
  17. mob.mob.originY = originY;
  18. mob.mob.goHome = true;
  19. mob.mob.maxChaseDistance = maxChaseDistance;
  20. //This is a hack to make mobs that run somewhere able to take damage
  21. delete mob.mob.events.beforeTakeDamage;
  22. }
  23. if (dialogue) {
  24. mob.addComponent('dialogue', {
  25. config: dialogue.config
  26. });
  27. if (dialogue.auto) {
  28. mob.dialogue.trigger = objects.buildObjects([{
  29. properties: {
  30. x: mob.x - 1,
  31. y: mob.y - 1,
  32. width: 3,
  33. height: 3,
  34. cpnNotice: {
  35. actions: {
  36. enter: {
  37. cpn: 'dialogue',
  38. method: 'talk',
  39. args: [{
  40. targetName: mob.name.toLowerCase()
  41. }]
  42. },
  43. exit: {
  44. cpn: 'dialogue',
  45. method: 'stopTalk'
  46. }
  47. }
  48. }
  49. }
  50. }]);
  51. }
  52. }
  53. if (trade)
  54. mob.addComponent('trade', trade);
  55. if (chats)
  56. mob.addComponent('chatter', chats);
  57. if (events) {
  58. mob.addBuiltComponent({
  59. type: 'eventComponent',
  60. events: events
  61. });
  62. }
  63. return mob;
  64. };
  65. const spawnAnimation = (syncer, { x, y }) => {
  66. syncer.queue('onGetObject', {
  67. x: x,
  68. y: y,
  69. components: [{
  70. type: 'attackAnimation',
  71. row: 0,
  72. col: 4
  73. }]
  74. }, -1);
  75. };
  76. module.exports = {
  77. spawnRect: null,
  78. mobs: null,
  79. init: function () {
  80. const { spawnRect, instance: { objects, syncer } } = this;
  81. if (!this.mobs.push)
  82. this.mobs = [this.mobs];
  83. let usedSpots = ['-1,-1'];
  84. this.mobs.forEach(function (l) {
  85. let amount = l.amount || 1;
  86. delete l.amount;
  87. l.walkDistance = 0;
  88. for (let i = 0; i < amount; i++) {
  89. let x = -1;
  90. let y = -1;
  91. let pos = l.pos;
  92. if (pos) {
  93. if (pos instanceof Array) {
  94. x = pos[i].x;
  95. y = pos[i].y;
  96. } else {
  97. x = pos.x;
  98. y = pos.y;
  99. }
  100. if (spawnRect) {
  101. x += spawnRect.x;
  102. y += spawnRect.y;
  103. }
  104. } else {
  105. while (usedSpots.indexOf(x + ',' + y) > -1) {
  106. x = spawnRect.x + ~~(Math.random() * spawnRect.w);
  107. y = spawnRect.y + ~~(Math.random() * spawnRect.h);
  108. }
  109. usedSpots.push(x + ',' + y);
  110. }
  111. if (l.exists) {
  112. let mob = objects.objects.find(o => (o.name === l.name));
  113. mob.mob.walkDistance = 0;
  114. this.spawnAnimation(mob);
  115. mob.performMove({
  116. force: true,
  117. data: {
  118. x: x,
  119. y: y
  120. }
  121. });
  122. this.spawnAnimation(mob);
  123. this.event.objects.push(mob);
  124. } else {
  125. const mob = buildMob(objects, l, x, y, i);
  126. this.event.objects.push(mob);
  127. mob.event = this.event;
  128. spawnAnimation(syncer, mob);
  129. }
  130. }
  131. }, this);
  132. if (!this.endMark)
  133. this.end = true;
  134. }
  135. };