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.
 
 
 
 

228 lines
4.3 KiB

#include "Character.hpp"
const int Character::maxLevel = 0xffff;
const int Character::maxExperience = 0x0fffffff;
Character::Character(std::string characterName,
Element *characterAffinity)
{
this->name = characterName;
this->affinity = characterAffinity;
this->level = 1;
this->experience = 0;
this->maxHP = 100;
this->maxMP = 50;
this->curHP = this->maxHP;
this->curMP = this->maxMP;
}
Character::Character(std::string characterName,
Element *characterAffinity,
int characterLevel)
{
this->name = characterName;
this->affinity = characterAffinity;
this->level = characterLevel;
this->experience = characterLevel;
this->maxHP = characterLevel * 10;
this->curHP = this->maxHP;
this->maxMP = characterLevel * 5;
this->curMP = this->maxMP;
}
Character::Character(std::string characterName,
Element *characterAffinity,
int characterLevel,
int characterHP,
int characterMP)
{
this->name = characterName;
this->affinity = characterAffinity;
this->level = characterLevel;
this->experience = characterLevel;
this->maxHP = characterHP;
this->curHP = this->maxHP;
this->maxMP = characterMP;
this->curMP = this->maxMP;
}
std::string 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;
}
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();
}
bool Character::castSpell(Spell *s, Character *t)
{
if (this->canCastSpell(s))
{
this->reduceMP(s->getCost());
int pwr = s->getPower();
int spellCompatibility = s->getElement()->compatibilityAgainst(t->getAffinity());
if (spellCompatibility == Element::WEAK_COMPATIBILITY)
{
pwr /= 2;
}
else if (spellCompatibility == Element::STRONG_COMPATIBILITY)
{
pwr *= 2;
}
if (s->getType() == Spell::TYPE_DESTRUCTION)
{
t->reduceHP(pwr);
}
else if (s->getType() == Spell::TYPE_RESTORATION)
{
t->restoreHP(pwr);
}
return true;
}
else
{
return false;
}
}
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;
}
}
Element *Character::getAffinity()
{
return this->affinity;
}