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.
 
 
 

177 lines
3.5 KiB

  1. define([
  2. 'items/generator'
  3. ], function(
  4. itemGenerator
  5. ) {
  6. var abs = Math.abs.bind(Math);
  7. var rnd = Math.random.bind(Math);
  8. var max = Math.max.bind(Math);
  9. return {
  10. type: 'mob',
  11. target: null,
  12. physics: null,
  13. originX: 0,
  14. originY: 0,
  15. walkDistance: 1,
  16. init: function(blueprint) {
  17. this.physics = this.obj.instance.physics;
  18. this.originX = this.obj.x;
  19. this.originY = this.obj.y;
  20. },
  21. update: function() {
  22. var target = null;
  23. if (this.obj.aggro)
  24. target = this.obj.aggro.getHighest();
  25. var goHome = false;
  26. if ((target) && (target != this.obj)) {
  27. this.fight(target);
  28. return;
  29. } else if (this.target) {
  30. this.target = null;
  31. this.obj.clearQueue();
  32. goHome = true;
  33. }
  34. if (this.obj.actionQueue.length > 0)
  35. return;
  36. if ((!goHome) && (rnd() < 0.85))
  37. return;
  38. var walkDistance = this.walkDistance;
  39. if ((!goHome) && (walkDistance <= 0))
  40. return;
  41. var obj = this.obj;
  42. var toX = this.originX + ~~(rnd() * (walkDistance * 2)) - walkDistance;
  43. var toY = this.originY + ~~(rnd() * (walkDistance * 2)) - walkDistance;
  44. if (!this.physics.isCellOpen(toX, toY))
  45. return;
  46. var path = this.physics.getPath({
  47. x: obj.x,
  48. y: obj.y
  49. }, {
  50. x: toX,
  51. y: toY
  52. }, false);
  53. var pLen = path.length;
  54. for (var i = 0; i < pLen; i++) {
  55. var p = path[i];
  56. obj.queue({
  57. action: 'move',
  58. data: {
  59. x: p.x,
  60. y: p.y
  61. }
  62. });
  63. }
  64. },
  65. fight: function(target) {
  66. if (this.target != target) {
  67. this.obj.clearQueue();
  68. this.target = target;
  69. }
  70. //If the target is true, it means we can't reach the target and should wait for a new one
  71. if (this.target == true)
  72. return;
  73. var obj = this.obj;
  74. var x = obj.x;
  75. var y = obj.y;
  76. var tx = ~~target.x;
  77. var ty = ~~target.y;
  78. var distance = max(abs(x - tx), abs(y - ty));
  79. var furthestRange = obj.spellbook.getFurthestRange();
  80. var doesCollide = null;
  81. var hasLos = null;
  82. if (distance <= furthestRange) {
  83. doesCollide = this.physics.mobsCollide(x, y, obj);
  84. if (!doesCollide) {
  85. hasLos = this.physics.hasLos(x, y, tx, ty);
  86. if (hasLos) {
  87. if (rnd() < 0.65) {
  88. var spell = obj.spellbook.getRandomSpell(target);
  89. var success = obj.spellbook.cast({
  90. spell: spell,
  91. target: target
  92. });
  93. //null means we don't have LoS
  94. if (success != null)
  95. return;
  96. else
  97. hasLos = false;
  98. } else
  99. return;
  100. }
  101. }
  102. }
  103. var targetPos = this.physics.getClosestPos(x, y, tx, ty, target);
  104. if (!targetPos) {
  105. //Find a new target
  106. obj.aggro.ignore(target);
  107. //TODO: Don't skip a turn
  108. return;
  109. }
  110. var newDistance = max(abs(targetPos.x - tx), abs(targetPos.y - ty));
  111. if ((newDistance >= distance) && (newDistance > furthestRange)) {
  112. if (hasLos == null)
  113. hasLos = this.physics.hasLos(x, y, tx, ty);
  114. if (hasLos) {
  115. if (doesCollide == null)
  116. doesCollide = this.physics.mobsCollide(x, y, obj);
  117. if (!doesCollide) {
  118. obj.aggro.ignore(target);
  119. return;
  120. }
  121. } else {
  122. if (doesCollide == null)
  123. doesCollide = this.physics.mobsCollide(x, y, obj);
  124. if (!doesCollide) {
  125. obj.aggro.ignore(target);
  126. return;
  127. }
  128. }
  129. }
  130. var path = this.physics.getPath({
  131. x: x,
  132. y: y
  133. }, {
  134. x: targetPos.x,
  135. y: targetPos.y
  136. });
  137. if (path.length == 0) {
  138. obj.aggro.ignore(target);
  139. //TODO: Don't skip a turn
  140. return;
  141. }
  142. var p = path[0];
  143. obj.queue({
  144. action: 'move',
  145. data: {
  146. x: p.x,
  147. y: p.y
  148. }
  149. });
  150. }
  151. };
  152. });