monsterdestination.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. function MonsterDestination(startx, starty) {
  2. var position = {x : startx, y: starty};
  3. var velocity = {x: 1, y: 1};
  4. var monsterSpeed = 0.25;
  5. var monsterMaxHealth = 2;
  6. var monsterXpValue = 50;
  7. var weaponStrength = 1;
  8. var level = 1;
  9. this.isConsumed = false;
  10. var game = null;
  11. var inventory = {coins: 4, hearts: 2};
  12. var name = "monster";
  13. var delay = 25;
  14. this.init = function(canvas, gameReference) {
  15. game = gameReference;
  16. velocity.x = Math.pow(-1, Math.floor(2 * Math.random()));
  17. velocity.y = Math.pow(-1, Math.floor(2 * Math.random()));
  18. inventory = [];
  19. level = Math.floor(game.getAveragePlayerLevel() * Math.random() + 1);
  20. //level = Math.floor(Math.abs(((6 * Math.random() + 1) + (6 * Math.random() + 1)) - 7)) + 1;
  21. monsterMaxHealth = Math.floor(level * (6 * Math.random() + 1));
  22. inventory.hearts = monsterMaxHealth;
  23. weaponStrength = level;
  24. var carryingCoins = level * 2;
  25. inventory.coins = carryingCoins;
  26. monsterXpValue = level * 25;
  27. name = game.nameGenerator.generateMonster();
  28. };
  29. var calculateLevel = function(xp) {
  30. return Math.floor(0.5 + Math.sqrt(1 + 8*(xp)/(100)) / 2);
  31. }
  32. this.update = function(canvas) {
  33. position.x += monsterSpeed * velocity.x;
  34. position.y += monsterSpeed * velocity.y;
  35. if(position.x < 0) {
  36. position.x = 0;
  37. velocity.x *= -1;
  38. }
  39. if(position.x > canvas.width) {
  40. position.x = canvas.width;
  41. velocity.x *= -1;
  42. }
  43. if(position.y < 0) {
  44. position.y = 0;
  45. velocity.y *= -1;
  46. }
  47. if(position.y > canvas.height) {
  48. position.y = canvas.height;
  49. velocity.y *= -1;
  50. }
  51. };
  52. this.draw = function(context, offset) {
  53. context.beginPath();
  54. context.strokeStyle = "#3D3D3D";
  55. var size = level * 5;
  56. context.rect(position.x - size / 2, position.y - size / 2, size, size);
  57. context.stroke();
  58. };
  59. this.getPosition = function() {
  60. return position;
  61. }
  62. this.visit = function(hero) {
  63. hero.actionDelay(delay);
  64. hero.dealDamage(this);
  65. return this;
  66. }
  67. this.dealDamage = function(hero) {
  68. console.log(""+ hero.getName() +"("+hero.getLevel()+") takes " + weaponStrength + " damage from " + this.getName() + "("+this.getLevel()+")");
  69. if(hero.deductHeart(weaponStrength)) {
  70. console.log("%c"+ hero.getName() +"("+hero.getLevel()+") is killed by " + this.getName() + "("+this.getLevel()+")", 'color: #FF0000');
  71. this.gainXp(hero.getXpValue());
  72. } else {
  73. hero.knockBack(this);
  74. hero.setFocus(this);
  75. }
  76. };
  77. this.getKnockback = function() {
  78. return 30;
  79. }
  80. this.getType = function() {
  81. return "monster";
  82. }
  83. this.mouseMove = function(canvas, x, y) {
  84. };
  85. this.getLevel = function() {
  86. return level;
  87. }
  88. this.getName = function() {
  89. return name;
  90. }
  91. this.gainXp = function(amount) {
  92. /*var prevXP = inventory.xp;
  93. inventory.xp += amount;
  94. triggerXpGain = 30;
  95. xpGainAmount = amount;
  96. if(calculateLevel(inventory.xp) > level) {
  97. triggerLevelUp = 60;
  98. maxHearts++;
  99. inventory.hearts = maxHearts;
  100. level++;
  101. }*/
  102. }
  103. this.deductHeart = function(amount) {
  104. inventory.hearts -= Math.min(amount, inventory.hearts);
  105. triggerHeartDecrement = 30;
  106. if(inventory.hearts <= 0) {
  107. game.spawnCoins(position, inventory.coins);
  108. for(var i = 0; i < level - 1; i++) {
  109. game.spawnHeart(position.x, position.y);
  110. }
  111. this.isConsumed = true;
  112. return true;
  113. }
  114. return false;
  115. }
  116. this.getXpValue = function() {
  117. return monsterXpValue;
  118. }
  119. this.setFocus = function(newTarget) {
  120. this.dealDamage(newTarget);
  121. //MonsterDestination = newTarget;
  122. }
  123. this.knockBack = function(hero) {
  124. //todo: get hero trajectory and move in same direction
  125. }
  126. }