#include "Character.hpp"
|
|
#include <cstring>
|
|
const int Character::maxLevel = 0xffff;
|
|
const int Character::maxExperience = 0x0fffffff;
|
|
Character::Character()
|
|
{
|
|
this->level = 1;
|
|
this->experience = 0;
|
|
this->maxHP = 10;
|
|
this->curHP = maxHP;
|
|
this->maxMP = 5;
|
|
this->curMP = maxMP;
|
|
this->atk = 1;
|
|
this->def = 1;
|
|
memset(this->name, 0, 16);
|
|
}
|
|
|
|
Character::Character(char *n) : Character::Character()
|
|
{
|
|
strcpy(this->name, n);
|
|
this->name[15] = 0;
|
|
}
|
|
|
|
Character::Character(const char *n) : Character::Character()
|
|
{
|
|
strcpy(this->name, n);
|
|
this->name[15] = 0;
|
|
}
|
|
|
|
Character::Character(char *n, int l, int e, int h, int m, int a, int d)
|
|
{
|
|
strcpy(this->name, n);
|
|
this->name[15] = 0;
|
|
this->level = l;
|
|
this->experience = e;
|
|
this->maxHP = h;
|
|
this->curHP = maxHP;
|
|
this->maxMP = m;
|
|
this->curMP = maxMP;
|
|
this->atk = a;
|
|
this->def = d;
|
|
}
|
|
|
|
Character::Character(const char *n, int l, int e, int h, int m, int a, int d)
|
|
{
|
|
strcpy(this->name, n);
|
|
this->name[15] = 0;
|
|
this->level = l;
|
|
this->experience = e;
|
|
this->maxHP = h;
|
|
this->curHP = maxHP;
|
|
this->maxMP = m;
|
|
this->curMP = maxMP;
|
|
this->atk = a;
|
|
this->def = d;
|
|
}
|
|
|
|
char *Character::getName()
|
|
{
|
|
return this->name;
|
|
}
|
|
|
|
int Character::getLevel()
|
|
{
|
|
if (this->level > Character::maxLevel)
|
|
{
|
|
exit(-1);
|
|
}
|
|
return this->level;
|
|
}
|
|
|
|
int Character::getExperience()
|
|
{
|
|
if (this->experience > Character::maxExperience)
|
|
{
|
|
exit(-1);
|
|
}
|
|
return this->experience;
|
|
}
|
|
|
|
int Character::getMaxHP()
|
|
{
|
|
return this->maxHP;
|
|
}
|
|
|
|
int Character::getCurHP()
|
|
{
|
|
if (this->curHP > this->maxHP)
|
|
{
|
|
exit(-1);
|
|
}
|
|
return this->curHP;
|
|
}
|
|
|
|
int Character::getMaxMP()
|
|
{
|
|
return this->maxMP;
|
|
}
|
|
|
|
int Character::getCurMP()
|
|
{
|
|
if (this->curMP > this->maxMP)
|
|
{
|
|
exit(-1);
|
|
}
|
|
return this->curMP;
|
|
}
|
|
|
|
int Character::getAtk()
|
|
{
|
|
return this->atk;
|
|
}
|
|
|
|
int Character::getDef()
|
|
{
|
|
return this->def;
|
|
}
|
|
|
|
void Character::levelUp()
|
|
{
|
|
if ((this->level <= maxLevel) && this->readytoLevelUp())
|
|
{
|
|
this->experience -= this->toNextLevel();
|
|
this->level++;
|
|
this->maxHP += 10;
|
|
this->maxMP += 5;
|
|
this->curHP = this->maxHP;
|
|
this->curMP = this->maxMP;
|
|
}
|
|
}
|
|
|
|
void Character::restoreHP(int n)
|
|
{
|
|
this->curHP += n;
|
|
if (this->curHP > this->maxHP)
|
|
{
|
|
this->curHP = this->maxHP;
|
|
}
|
|
}
|
|
|
|
void Character::restoreMP(int n)
|
|
{
|
|
this->curMP += n;
|
|
if (this->curMP > this->maxMP)
|
|
{
|
|
this->curMP = this->maxMP;
|
|
}
|
|
}
|
|
|
|
void Character::reduceHP(int n)
|
|
{
|
|
this->curHP -= n;
|
|
}
|
|
|
|
void Character::reduceMP(int n)
|
|
{
|
|
this->curMP -= n;
|
|
}
|
|
|
|
bool Character::isAlive()
|
|
{
|
|
return this->curHP > 0;
|
|
}
|
|
|
|
int Character::toNextLevel()
|
|
{
|
|
return (this->level + 1) * (this->level + 1);
|
|
}
|
|
|
|
bool Character::readytoLevelUp()
|
|
{
|
|
return (this->toNextLevel() <= this->experience);
|
|
}
|
|
|
|
void Character::increaseExperience(int n)
|
|
{
|
|
this->experience += n;
|
|
if (this->experience > maxExperience)
|
|
{
|
|
this->experience = maxExperience;
|
|
}
|
|
while (this->readytoLevelUp())
|
|
{
|
|
this->levelUp();
|
|
}
|
|
}
|
|
|
|
bool Character::canCastSpell(Spell *s)
|
|
{
|
|
return this->curMP >= s->getCost();
|
|
}
|
|
|
|
void Character::castSpell(Spell *s, Character *t)
|
|
{
|
|
this->reduceMP(s->getCost());
|
|
t->reduceHP(s->getAmount());
|
|
}
|
|
|
|
void Character::rest()
|
|
{
|
|
this->curHP = this->maxHP;
|
|
this->curMP = this->maxMP;
|
|
}
|
|
|
|
void Character::kill()
|
|
{
|
|
this->curHP = 0;
|
|
}
|
|
|
|
void Character::revive()
|
|
{
|
|
if (this->curHP <= 0)
|
|
{
|
|
this->curHP = 1;
|
|
}
|
|
}
|