Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

200 řádky
4.1 KiB

  1. let herbs = require('../config/herbs');
  2. module.exports = {
  3. nodes: [],
  4. objects: null,
  5. syncer: null,
  6. zone: null,
  7. physics: null,
  8. map: null,
  9. cdMax: 171,
  10. init: function (instance) {
  11. Object.assign(this, {
  12. objects: instance.objects,
  13. syncer: instance.syncer,
  14. physics: instance.physics,
  15. map: instance.map,
  16. zone: instance.zone
  17. });
  18. },
  19. register: function (name, blueprint) {
  20. let exists = this.nodes.find(n => (n.blueprint.name === name));
  21. if (exists) {
  22. if (!exists.blueprint.positions) {
  23. exists.blueprint.positions = [{
  24. x: exists.blueprint.x,
  25. y: exists.blueprint.y,
  26. width: exists.blueprint.width,
  27. height: exists.blueprint.height
  28. }];
  29. }
  30. exists.blueprint.positions.push({
  31. x: blueprint.x,
  32. y: blueprint.y,
  33. width: blueprint.width,
  34. height: blueprint.height
  35. });
  36. return;
  37. }
  38. blueprint = extend({}, blueprint, herbs[name], {
  39. name: name
  40. });
  41. let max = blueprint.max;
  42. delete blueprint.max;
  43. let chance = blueprint.chance;
  44. delete blueprint.chance;
  45. let cdMax = blueprint.cdMax;
  46. delete blueprint.cdMax;
  47. this.nodes.push({
  48. cd: 0,
  49. max: max,
  50. chance: chance,
  51. cdMax: cdMax,
  52. blueprint: blueprint,
  53. spawns: []
  54. });
  55. },
  56. getRandomSpawnPosition: function (node, blueprint) {
  57. //Get an accessible position
  58. let w = this.physics.width;
  59. let h = this.physics.height;
  60. let x = blueprint.x;
  61. let y = blueprint.y;
  62. let position = null;
  63. if (blueprint.type === 'herb') {
  64. x = ~~(Math.random() * w);
  65. y = ~~(Math.random() * h);
  66. if (this.physics.isTileBlocking(x, y))
  67. return false;
  68. let spawn = this.map.spawn[0];
  69. let path = this.physics.getPath(spawn, {
  70. x: x,
  71. y: y
  72. });
  73. let endTile = path[path.length - 1];
  74. if (!endTile)
  75. return false;
  76. else if ((endTile.x !== x) || (endTile.y !== y))
  77. return false;
  78. //Don't spawn in rooms or on objects/other resources
  79. let cell = this.physics.getCell(x, y);
  80. if (cell.length > 0)
  81. return false;
  82. position = { x, y };
  83. } else if (blueprint.positions) {
  84. //Find all possible positions in which a node hasn't spawned yet
  85. position = blueprint.positions.filter(f => !node.spawns.some(s => ((s.x === f.x) && (s.y === f.y))));
  86. if (position.length === 0)
  87. return false;
  88. position = position[~~(Math.random() * position.length)];
  89. }
  90. return position;
  91. },
  92. spawn: function (node) {
  93. let blueprint = node.blueprint;
  94. let position = this.getRandomSpawnPosition(node, blueprint);
  95. if (!position)
  96. return false;
  97. let quantity = 1;
  98. if (blueprint.quantity)
  99. quantity = blueprint.quantity[0] + ~~(Math.random() * (blueprint.quantity[1] - blueprint.quantity[0]));
  100. let zoneLevel = this.zone.level;
  101. zoneLevel = ~~(zoneLevel[0] + ((zoneLevel[1] - zoneLevel[0]) / 2));
  102. let objBlueprint = extend({}, blueprint, position);
  103. objBlueprint.properties = {
  104. cpnResourceNode: {
  105. nodeType: blueprint.type,
  106. ttl: blueprint.ttl,
  107. xp: zoneLevel * zoneLevel,
  108. blueprint: extend({}, blueprint),
  109. quantity: quantity
  110. }
  111. };
  112. let obj = this.objects.buildObjects([objBlueprint]);
  113. delete obj.ttl;
  114. if (blueprint.type === 'herb') {
  115. this.syncer.queue('onGetObject', {
  116. x: obj.x,
  117. y: obj.y,
  118. components: [{
  119. type: 'attackAnimation',
  120. row: 0,
  121. col: 4
  122. }]
  123. }, -1);
  124. }
  125. let inventory = obj.addComponent('inventory');
  126. obj.layerName = 'objects';
  127. node.spawns.push(obj);
  128. let item = {
  129. material: true,
  130. type: node.type || null,
  131. sprite: node.blueprint.itemSprite,
  132. name: node.blueprint.name,
  133. quantity: (blueprint.type !== 'fish') ? 1 : null,
  134. quality: 0
  135. };
  136. if (blueprint.itemSheet)
  137. item.spritesheet = blueprint.itemSheet;
  138. if (blueprint.type === 'fish')
  139. item.noStack = true;
  140. inventory.getItem(item);
  141. return true;
  142. },
  143. update: function () {
  144. let nodes = this.nodes;
  145. let nLen = nodes.length;
  146. for (let i = 0; i < nLen; i++) {
  147. let node = nodes[i];
  148. let spawns = node.spawns;
  149. spawns.spliceWhere(f => f.destroyed);
  150. if (spawns.length < node.max) {
  151. if (node.cd > 0)
  152. node.cd--;
  153. else if ((!node.chance || Math.random() < node.chance) && this.spawn(node))
  154. node.cd = node.cdMax || this.cdMax;
  155. }
  156. }
  157. }
  158. };