function MonsterDestination(startx, starty) { var position = {x : startx, y: starty}; var velocity = {x: 1, y: 1}; var monsterSpeed = 0.25; var monsterMaxHealth = 2; var monsterXpValue = 50; var weaponStrength = 1; var level = 1; this.isConsumed = false; var game = null; var inventory = {coins: 4, hearts: 2}; var name = "monster"; var delay = 25; this.init = function(canvas, gameReference) { game = gameReference; velocity.x = Math.pow(-1, Math.floor(2 * Math.random())); velocity.y = Math.pow(-1, Math.floor(2 * Math.random())); inventory = []; level = Math.floor(game.getAveragePlayerLevel() * Math.random() + 1); //level = Math.floor(Math.abs(((6 * Math.random() + 1) + (6 * Math.random() + 1)) - 7)) + 1; monsterMaxHealth = Math.floor(level * (6 * Math.random() + 1)); inventory.hearts = monsterMaxHealth; weaponStrength = level; var carryingCoins = level * 2; inventory.coins = carryingCoins; monsterXpValue = level * 25; name = game.nameGenerator.generateMonster(); }; var calculateLevel = function(xp) { return Math.floor(0.5 + Math.sqrt(1 + 8*(xp)/(100)) / 2); } this.update = function(canvas) { position.x += monsterSpeed * velocity.x; position.y += monsterSpeed * velocity.y; if(position.x < 0) { position.x = 0; velocity.x *= -1; } if(position.x > canvas.width) { position.x = canvas.width; velocity.x *= -1; } if(position.y < 0) { position.y = 0; velocity.y *= -1; } if(position.y > canvas.height) { position.y = canvas.height; velocity.y *= -1; } }; this.draw = function(context, offset) { context.beginPath(); context.strokeStyle = "#3D3D3D"; var size = level * 5; context.rect(position.x - size / 2, position.y - size / 2, size, size); context.stroke(); }; this.getPosition = function() { return position; } this.visit = function(hero) { hero.actionDelay(delay); hero.dealDamage(this); return this; } this.dealDamage = function(hero) { console.log(""+ hero.getName() +"("+hero.getLevel()+") takes " + weaponStrength + " damage from " + this.getName() + "("+this.getLevel()+")"); if(hero.deductHeart(weaponStrength)) { console.log("%c"+ hero.getName() +"("+hero.getLevel()+") is killed by " + this.getName() + "("+this.getLevel()+")", 'color: #FF0000'); this.gainXp(hero.getXpValue()); } else { hero.knockBack(this); hero.setFocus(this); } }; this.getKnockback = function() { return 30; } this.getType = function() { return "monster"; } this.mouseMove = function(canvas, x, y) { }; this.getLevel = function() { return level; } this.getName = function() { return name; } this.gainXp = function(amount) { /*var prevXP = inventory.xp; inventory.xp += amount; triggerXpGain = 30; xpGainAmount = amount; if(calculateLevel(inventory.xp) > level) { triggerLevelUp = 60; maxHearts++; inventory.hearts = maxHearts; level++; }*/ } this.deductHeart = function(amount) { inventory.hearts -= Math.min(amount, inventory.hearts); triggerHeartDecrement = 30; if(inventory.hearts <= 0) { game.spawnCoins(position, inventory.coins); for(var i = 0; i < level - 1; i++) { game.spawnHeart(position.x, position.y); } this.isConsumed = true; return true; } return false; } this.getXpValue = function() { return monsterXpValue; } this.setFocus = function(newTarget) { this.dealDamage(newTarget); //MonsterDestination = newTarget; } this.knockBack = function(hero) { //todo: get hero trajectory and move in same direction } }