var Unit = function(hp, name)
{
this.fullHealth = hp;
this.health = hp;
this.name = name;
this.power = 1;
this.speed = 1000;
this.level = 1;
this.target = null;
this.attackID = null;
var self = this;
this.isDead = function()
{
return (self.health <= 0);
};
this.attack = function()
{
if(self.target)
{
self.target.damage(self.power);
self.board.innerHTML = [
self.name, '의 공격대상 : ', self.target.name,
'<br>데미지 : ', self.power, ' 대상의 남은 체력 : ', self.target.hp, '<br>'
].join(' ') + self.board.innerHTML;
if(self.target.isDead())
{
self.board.innerHTML = self.target.name + ' is dead ! ' +
'<br> Winner is ' + self.name + ' !' + self.board.innerHTML;
self.stopAttack();
self.target = null;
self.target.target = null;
}
}
console.log(self.name + " attack " + self.target);
};
this.damage = function(power)
{
self.health -= power;
if(self.isDead())
{
self.stopAttack();
self.board.innerHtml = 'You are dead !<br>' + self.board.innerHTML;
}
};
this.levelUp = function(hp, power, speed)
{
self.level++;
self.fullHealth += hp;
self.hp = self.fullHealth;
self.power +=power;
self.speed -=speed;
self.board.innerHTML = "Level up ! " + (self.level -1) + " => " + self.level + "<br>" + self.board.innerHTML;
};
};
var Human = function(name)
{
Unit.apply(this, [50, name]);
this.board = document.getElementById('fightlog1');
var self = this;
this.expFull = function()
{
self.levelUp(40,2,50);
};
this.startAttack = function(target)
{
self.target = target;
self.attackID = setInterval(self.attack, self.speed);
};
this.stopAttack = function()
{
self.target = null;
clearInterval(self.attackID);
};
console.log("Human is created\n" + self.hp);
};
var Magician = function(name)
{
Unit.apply(this, [40, name]);
this.skillSpeed = 3000;
this.skillID = null;
this.skillPower = 2;
this.board = document.getElementById('fightlog2');
var self = this;
this.skill = function()
{
if(!self.target || self.target.isDead())
{
self.stopAttack();
}
else
{
self.target.damage(self.skillPower);
self.board.innerHTML = [
self.name, '의 공격대상 : ', self.target.name,
'<br>스킬사용 ! 데미지 : ', self.skillPower, ' 대상의 남은 체력 : ', self.target.hp, '<br>'
].join(' ') + self.board.innerHTML;
}
};
this.expFull = function()
{
self.levelUp(30,1,50);
self.skillPower += 2;
self.skillSpeed -= 30;
};
this.startAttack = function(target)
{
self.target = target;
self.attackID = setInterval(self.attack, self.speed);
self.skillID = setInterval(self.skill, self.skillSpeed);
};
this.stopAttack = function()
{
self.target = null;
clearInterval(self.attackID);
clearInterval(self.skillID);
};
console.log("Magician is created\n" + this.name + this.hp );
};
window.onload = function()
{
man = new Human('Man');
woman = new Magician('Woman');
}
function startWar()
{
man.stopAttack();
woman.stopAttack();
man.board.innerHTML = '';
woman.board.innerHTML = '';
man.name = document.getElementById('humanName').value;
woman.name = document.getElementById('magicianName').value;
man.startAttack(woman);
woman.startAttack(man);
}
function stopWar()
{
man.stopAttack();
woman.stopAttack();
man.board.innerHTML = '';
woman.board.innerHTML = '';
man.name = document.getElementById('humanName').value;
woman.name = document.getElementById('magicianName').value;
}
5-3_simulation.js