]> git.localhorst.tv Git - l2e.git/blob - src/common/Hero.cpp
store complete entity in hero
[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 "../loader/TypeDescription.h"
11
12 using loader::FieldDescription;
13 using loader::TypeDescription;
14
15 namespace common {
16
17 Hero::Hero()
18 : name(0)
19
20 , maxHealth(0)
21 , health(0)
22 , maxMana(0)
23 , mana(0)
24 , ip(0)
25
26 , level(0)
27
28 , weapon(0)
29 , armor(0)
30 , shield(0)
31 , helmet(0)
32 , ring(0)
33 , jewel(0)
34
35 , battleSprite(0)
36 , meleeAnimation(0)
37 , attackAnimation(0)
38 , spellAnimation(0) {
39
40 }
41
42
43 void Hero::SubtractHealth(int amount) {
44         if (amount > Health()) {
45                 health = 0;
46         } else {
47                 health -= amount;
48                 int ipGain(amount * 255 / health);
49                 if (ip + ipGain > 255) {
50                         ip = 255;
51                 } else {
52                         ip += ipGain;
53                 }
54         }
55 }
56
57
58 void Hero::CreateTypeDescription() {
59         Hero h;
60
61         int animationId(TypeDescription::GetTypeId("Animation"));
62         int entityId(TypeDescription::GetTypeId("Entity"));
63         int numberId(TypeDescription::GetTypeId("Number"));
64         int spriteId(TypeDescription::GetTypeId("Sprite"));
65         int statsId(TypeDescription::GetTypeId("Stats"));
66         int stringId(TypeDescription::GetTypeId("String"));
67
68         TypeDescription &td(TypeDescription::CreateOrGet("Hero"));
69         td.SetConstructor(&Construct);
70         td.SetSize(sizeof(Hero));
71
72         td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), stringId, true));
73
74         td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), numberId, false));
75         td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), numberId, false));
76         td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), numberId, false));
77         td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), numberId, false));
78         td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), numberId, false));
79
80         td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), statsId, false));
81
82         td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), numberId, false));
83
84         td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), spriteId, true));
85         td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), animationId, true));
86         td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), animationId, true));
87         td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), animationId, true));
88
89         td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), entityId, false));
90 }
91
92 void Hero::Construct(void *data) {
93         new (data) Hero;
94 }
95
96 }