]> git.localhorst.tv Git - l2e.git/blob - src/common/Hero.cpp
ab73a6be70d3ee91fbc003d4918342ebba28638c
[l2e.git] / src / common / Hero.cpp
1 /*
2  * Hero.cpp
3  *
4  *  Created on: Oct 7, 2012
5  *      Author: holy
6  */
7
8 #include "Hero.h"
9
10 #include "Item.h"
11 #include "Spell.h"
12 #include "../graphics/Animation.h"
13 #include "../graphics/Sprite.h"
14 #include "../loader/Interpreter.h"
15 #include "../loader/TypeDescription.h"
16 #include "../map/Entity.h"
17
18 using graphics::Animation;
19 using graphics::Sprite;
20 using loader::FieldDescription;
21 using loader::Interpreter;
22 using loader::TypeDescription;
23 using map::Entity;
24
25 namespace common {
26
27 Hero::Hero()
28 : name(0)
29
30 , maxHealth(0)
31 , health(0)
32 , maxMana(0)
33 , mana(0)
34 , ip(0)
35
36 , level(0)
37 , experience(0)
38 , levelLadder(0)
39 , numLevels(0)
40
41 , useMask(0)
42
43 , weapon(0)
44 , armor(0)
45 , shield(0)
46 , helmet(0)
47 , ring(0)
48 , jewel(0)
49
50 , battleSprite(0)
51 , meleeAnimation(0)
52 , attackAnimation(0)
53 , spellAnimation(0) {
54
55 }
56
57
58 void Hero::SubtractHealth(int amount) {
59         if (amount > Health()) {
60                 health = 0;
61         } else {
62                 health -= amount;
63                 int ipGain(amount * 255 / health);
64                 if (ip + ipGain > 255) {
65                         ip = 255;
66                 } else {
67                         ip += ipGain;
68                 }
69         }
70 }
71
72
73 int Hero::NextLevel() const {
74         int levelOffset(Level() - 1);
75         if (levelOffset < numLevels) {
76                 return levelLadder[levelOffset] - Experience();
77         } else {
78                 return 0;
79         }
80 }
81
82
83 bool Hero::CanEquip(const Item &item) const {
84         return useMask & item.HeroMask();
85 }
86
87 bool Hero::CanInvoke(const Spell &spell) const {
88         return useMask & spell.HeroMask();
89 }
90
91
92 void Hero::CreateTypeDescription() {
93         Hero h;
94
95         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Hero"));
96         td.SetConstructor(&Construct);
97         td.SetSize(sizeof(Hero));
98
99         td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), Interpreter::STRING_ID).SetReferenced());
100
101         td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), Interpreter::NUMBER_ID));
102         td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), Interpreter::NUMBER_ID));
103         td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), Interpreter::NUMBER_ID));
104         td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), Interpreter::NUMBER_ID));
105         td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), Interpreter::NUMBER_ID));
106
107         td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), Stats::TYPE_ID));
108
109         td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), Interpreter::NUMBER_ID));
110         td.AddField("ladder", FieldDescription(((char *)&h.levelLadder) - ((char *)&h), Interpreter::NUMBER_ID).SetReferenced().SetAggregate());
111
112         td.AddField("useMask", FieldDescription(((char *)&h.useMask) - ((char *)&h), Interpreter::NUMBER_ID));
113
114         td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for battle scenes"));
115         td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for physical attacks"));
116         td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for magical attacks"));
117         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"));
118
119         td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), Entity::TYPE_ID).SetDescription("the entity representing the hero on maps"));
120 }
121
122 void Hero::Construct(void *data) {
123         new (data) Hero;
124 }
125
126 }