X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcommon%2FHero.cpp;h=4e7b5b91068f08e8db5137fb8f97656afae79bd8;hb=8f4cb4e8ad954ba73fb78a030c969c933a7ed60c;hp=bef60a4c02305f8f0e5a3fc3854663f39fa6c20e;hpb=4bc70f5311dcbcca4e6b9e852bbcb19602f50eeb;p=l2e.git diff --git a/src/common/Hero.cpp b/src/common/Hero.cpp index bef60a4..4e7b5b9 100644 --- a/src/common/Hero.cpp +++ b/src/common/Hero.cpp @@ -1,16 +1,24 @@ -/* - * Hero.cpp - * - * Created on: Oct 7, 2012 - * Author: holy - */ - #include "Hero.h" +#include "Item.h" +#include "Spell.h" +#include "../graphics/Animation.h" +#include "../graphics/Sprite.h" +#include "../loader/Interpreter.h" #include "../loader/TypeDescription.h" +#include "../map/Entity.h" + +#include +using graphics::Animation; +using graphics::Sprite; using loader::FieldDescription; +using loader::Interpreter; using loader::TypeDescription; +using map::Entity; +using std::memset; +using std::vector; + namespace common { @@ -24,19 +32,17 @@ Hero::Hero() , ip(0) , level(0) +, experience(0) +, levelLadder(0) +, numLevels(0) -, weapon(0) -, armor(0) -, shield(0) -, helmet(0) -, ring(0) -, jewel(0) +, useMask(0) , battleSprite(0) , meleeAnimation(0) , attackAnimation(0) , spellAnimation(0) { - + memset(equipment, 0, sizeof(equipment)); } @@ -55,38 +61,76 @@ void Hero::SubtractHealth(int amount) { } +int Hero::NextLevel() const { + int levelOffset(Level() - 1); + if (levelOffset < numLevels) { + return levelLadder[levelOffset] - Experience(); + } else { + return 0; + } +} + +void Hero::AddExperience(int exp, vector &info) { + if (level > numLevels) { + // don't award any experience if at highest level + return; + } + int remain = exp; + while (remain >= NextLevel()) { + int added = NextLevel(); + experience += added; + remain -= added; + ++level; + + info.push_back(UpgradeInfo(UPGRADE_LVL, level)); + + // TODO: upgrade attributes and push info + + if (level > numLevels) { + return; + } + } + experience += remain; +} + + +bool Hero::CanEquip(const Item &item) const { + return useMask & item.HeroMask(); +} + +bool Hero::CanInvoke(const Spell &spell) const { + return useMask & spell.HeroMask(); +} + + void Hero::CreateTypeDescription() { Hero h; - int animationId(TypeDescription::GetTypeId("Animation")); - int entityId(TypeDescription::GetTypeId("Entity")); - int numberId(TypeDescription::GetTypeId("Number")); - int spriteId(TypeDescription::GetTypeId("Sprite")); - int statsId(TypeDescription::GetTypeId("Stats")); - int stringId(TypeDescription::GetTypeId("String")); - - TypeDescription &td(TypeDescription::CreateOrGet("Hero")); + TypeDescription &td(TypeDescription::Create(TYPE_ID, "Hero")); td.SetConstructor(&Construct); td.SetSize(sizeof(Hero)); - td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), stringId).SetReferenced()); + td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), Interpreter::STRING_ID).SetReferenced()); + + td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), Interpreter::NUMBER_ID)); + td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), Interpreter::NUMBER_ID)); + td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), Interpreter::NUMBER_ID)); + td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), Interpreter::NUMBER_ID)); + td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), Interpreter::NUMBER_ID)); - td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), numberId)); - td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), numberId)); - td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), numberId)); - td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), numberId)); - td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), numberId)); + td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), Stats::TYPE_ID)); - td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), statsId)); + td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), Interpreter::NUMBER_ID)); + td.AddField("ladder", FieldDescription(((char *)&h.levelLadder) - ((char *)&h), Interpreter::NUMBER_ID).SetAggregate()); - td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), numberId)); + td.AddField("useMask", FieldDescription(((char *)&h.useMask) - ((char *)&h), Interpreter::NUMBER_ID)); - td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), spriteId).SetReferenced().SetDescription("the sprite used for battle scenes")); - td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), animationId).SetReferenced().SetDescription("the animation played for physical attacks")); - td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), animationId).SetReferenced().SetDescription("the animation played for magical attacks")); - td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), animationId).SetReferenced().SetDescription("the animation played on attacked monsters when the hero has no weapon")); + td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for battle scenes")); + td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for physical attacks")); + td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for magical attacks")); + td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played on attacked monsters when the hero has no weapon")); - td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), entityId).SetDescription("the entity representing the hero on maps")); + td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), Entity::TYPE_ID).SetDescription("the entity representing the hero on maps")); } void Hero::Construct(void *data) {