]> git.localhorst.tv Git - l2e.git/blob - src/common/Hero.cpp
moved Hero and Stats to common
[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 , mapSprite(0) {
41
42 }
43
44
45 void Hero::SubtractHealth(int amount) {
46         if (amount > Health()) {
47                 health = 0;
48         } else {
49                 health -= amount;
50                 int ipGain(amount * 255 / health);
51                 if (ip + ipGain > 255) {
52                         ip = 255;
53                 } else {
54                         ip += ipGain;
55                 }
56         }
57 }
58
59
60 void Hero::CreateTypeDescription() {
61         Hero h;
62
63         int animationId(TypeDescription::GetTypeId("Animation"));
64         int numberId(TypeDescription::GetTypeId("Number"));
65         int spriteId(TypeDescription::GetTypeId("Sprite"));
66         int statsId(TypeDescription::GetTypeId("Stats"));
67         int stringId(TypeDescription::GetTypeId("String"));
68
69         TypeDescription &td(TypeDescription::CreateOrGet("Hero"));
70         td.SetConstructor(&Construct);
71         td.SetSize(sizeof(Hero));
72
73         td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), stringId, true));
74
75         td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), numberId, false));
76         td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), numberId, false));
77         td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), numberId, false));
78         td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), numberId, false));
79         td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), numberId, false));
80
81         td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), statsId, false));
82
83         td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), numberId, false));
84
85         td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), spriteId, true));
86         td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), animationId, true));
87         td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), animationId, true));
88         td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), animationId, true));
89
90         td.AddField("mapSprite", FieldDescription(((char *)&h.mapSprite) - ((char *)&h), spriteId, true));
91 }
92
93 void Hero::Construct(void *data) {
94         new (data) Hero;
95 }
96
97 }