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