-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreature.cpp
321 lines (282 loc) · 8.55 KB
/
Creature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "Creature.h"
#define EXPERIENCE_NECESSAIRE_DEFAUT 100
Creature::Creature() :nom_(""), attaque_(0), defense_(0), pointDeVie_(0),
energie_(0), experience_(0), niveau_(1)
{
energieTotal_ = 0;
experienceNecessaire_ = 0;
pointDeVieTotal_ = 0;
}
Creature::Creature(const std::string& nom, unsigned int attaque,
unsigned int defense, unsigned int pointDeVie, unsigned int energie) :
nom_(nom), attaque_(attaque), defense_(defense), pointDeVie_(pointDeVie),
energie_(energie), experience_(0), niveau_(1)
{
energieTotal_ = energie;
experienceNecessaire_ = EXPERIENCE_NECESSAIRE_DEFAUT;
pointDeVieTotal_ = pointDeVie;
}
Creature::~Creature()
{
while (pouvoirs_.size() != 0)
{
delete pouvoirs_.back();
pouvoirs_.back() = nullptr;
pouvoirs_.pop_back();
}
}
std::string Creature::obtenirNom() const
{
return nom_;
}
unsigned int Creature::obtenirAttaque() const
{
return attaque_;
}
unsigned int Creature::obtenirDefense() const
{
return defense_;
}
unsigned int Creature::obtenirPointDeVie() const
{
return pointDeVie_;
}
unsigned int Creature::obtenirPointDeVieTotal() const
{
return pointDeVieTotal_;
}
unsigned int Creature::obtenirEnergie() const
{
return energie_;
}
unsigned int Creature::obtenirEnergieTotale() const
{
return energieTotal_;
}
unsigned int Creature::obtenirExperience() const
{
return experience_;
}
unsigned int Creature::obtenirExperienceNecessaire() const
{
return experienceNecessaire_;
}
unsigned int Creature::obtenirNiveau() const
{
return niveau_;
}
std::vector<Pouvoir*> Creature::obtenirPouvoirs() const
{
return pouvoirs_;
}
void Creature::attaquer(const Pouvoir & pouvoir, Creature & creature)
{
bool pouvoirEstDansVector = false;
for (unsigned int i = 0; i < pouvoirs_.size(); i++) //S'assurer que votre créature a bien ce pouvoir.
{
if (*pouvoirs_[i] == pouvoir)
{
pouvoirEstDansVector = true;
}
}
if (energie_ >= pouvoir.obtenirEnergieNecessaire() && pouvoirEstDansVector)
{
if (creature.obtenirPointDeVie() >= 0) {
//Calcul du nombre de degat selon une formule
unsigned int degat = abs((int)((pouvoir.obtenirNombreDeDegat())* (attaque_ / 2 - creature.defense_)));
int tentative = rand() % 6;
//l'attaque rate une fois sur 6
if (tentative != 3) {
std::cout << nom_ << " lance " << pouvoir.obtenirNom() << " qui inflige "
<< degat << " degat a " << creature.obtenirNom() << std::endl;
if (degat > creature.obtenirPointDeVie()) {
creature.modifierPointDeVie(0);
int xp = experienceGagnee(creature);
std::cout << nom_ << " a gagné " << xp << " XP" << std::endl;
}
else
creature.modifierPointDeVie(creature.obtenirPointDeVie() - degat);
std::cout << creature.obtenirNom() << " a encore " << creature.obtenirPointDeVie() << " PV" << std::endl;
energie_ -= pouvoir.obtenirEnergieNecessaire();
}
else {
std::cout << "Attaque " << pouvoir.obtenirNom() << " a échouée" << std::endl;
}
}
else
std::cout << "Vous deja avez vaincu " << creature.obtenirNom() << std::endl;
}
}
int Creature::experienceGagnee(const Creature& creature)
{
if (creature.obtenirPointDeVie() <= 0) {
//Calcul de l'experience selon une formule
int experience = (creature.obtenirNiveau() + 1 - niveau_) * ((creature.obtenirAttaque() + 5 - attaque_)
* (creature.obtenirDefense() + 3 - defense_)) + (pointDeVie_ / 2);
if (experience > (experienceNecessaire_ - experience_)) {
int experienceRestante = experience - (experienceNecessaire_ - experience_);
niveau_++;
attaque_ += 1;
defense_ += 1;
pointDeVie_ += 5;
energie_ += 5;
energieTotal_ += 5;
pointDeVieTotal_ += 5;
experience_ = experienceRestante;
experienceNecessaire_ = experienceNecessaire_ * 15 / 10;
}
else {
experience_ += experience;
}
return experience;
}
return 0;
}
bool Creature::apprendrePouvoir(Pouvoir* pouvoir)
{
for (unsigned int i = 0; i < pouvoirs_.size(); i++)
{
if (*pouvoirs_[i] == *pouvoir)
{
return false;
}
}
pouvoirs_.push_back(new Pouvoir(*pouvoir));
return true;
}
bool Creature::oublierPouvoir(Pouvoir* pouvoir)
{
for (int i = 0; i < pouvoirs_.size(); i++)
{
if (*(pouvoirs_[i]) == *pouvoir)
{
delete pouvoirs_[i];
pouvoirs_[i] = nullptr;
pouvoirs_[i] = pouvoirs_.back();
pouvoirs_.pop_back();
std::cout << "Le pouvoir " << pouvoir->obtenirNom() << " a bien ete retire." << std::endl;
return true;
}
}
std::cout << "Le pouvoir " << pouvoir->obtenirNom() << " n'a pas ete retire." << std::endl;
return false;
}
void Creature::modifierNom(const std::string& nom)
{
nom_ = nom;
}
void Creature::modifierAttaque(unsigned int attaque)
{
attaque_ = attaque;
}
void Creature::modifierDefense(unsigned int defense)
{
defense_ = defense;
}
void Creature::modifierPointDeVie(unsigned int pointDeVie)
{
pointDeVie_ = pointDeVie;
}
void Creature::modifierEnergie(unsigned int energie)
{
energie_ = energie;
}
void Creature::modifierExperience(unsigned int experience)
{
experience_ = experience;
}
void Creature::modifierNiveau(unsigned int niveau)
{
niveau_ = niveau;
}
void Creature::modifierPouvoirs(std::vector<Pouvoir*> pouvoirs)
{
while (pouvoirs_.size() != 0)
{
delete pouvoirs_.back();
pouvoirs_.back() = nullptr;
pouvoirs_.pop_back();
}
for (unsigned int i = 0; i < pouvoirs.size(); i++)
{
pouvoirs_.push_back( new Pouvoir(*pouvoirs[i]) );
}
}
Creature::Creature(const Creature& creature)
: nom_(creature.nom_), attaque_(creature.attaque_), defense_(creature.defense_), pointDeVie_(creature.pointDeVie_),
pointDeVieTotal_(creature.pointDeVieTotal_), energie_(creature.energie_), energieTotal_(creature.energieTotal_),
experience_(creature.experience_), experienceNecessaire_(creature.experienceNecessaire_), niveau_(creature.niveau_)
{
for(Pouvoir* pouvoir : creature.pouvoirs_)
{
pouvoirs_.push_back(new Pouvoir(*pouvoir));
}
}
Creature& Creature::operator=(const Creature& creature)
{
if (this != &creature)
{
nom_ = creature.nom_;
attaque_ = creature.attaque_;
defense_ = creature.defense_;
pointDeVie_ = creature.pointDeVie_;
pointDeVieTotal_ = creature.pointDeVieTotal_;
energie_ = creature.energie_;
energieTotal_ = creature.energieTotal_;
experience_ = creature.experience_;
experienceNecessaire_ = creature.experienceNecessaire_;
niveau_ = creature.niveau_;
while (pouvoirs_.size() != 0)
{
delete pouvoirs_.back();
pouvoirs_.back() = nullptr;
pouvoirs_.pop_back();
}
for(Pouvoir* pouvoir : creature.pouvoirs_)
{
pouvoirs_.push_back(new Pouvoir(*pouvoir));
}
}
return *this;
}
bool Creature::operator==(const Creature& creature) const
{
return (nom_ == creature.nom_ && attaque_ == creature.attaque_ && defense_ == creature.defense_ && pointDeVie_ == creature.pointDeVie_
&& pointDeVieTotal_ == creature.pointDeVieTotal_ && energie_ == creature.energie_ && energieTotal_ == creature.energieTotal_
&& experience_ == creature.experience_ && experienceNecessaire_ == creature.experienceNecessaire_ && niveau_ == creature.niveau_);
}
bool Creature::operator==(const std::string& nom) const
{
return (nom_ == nom);
}
bool operator==(const std::string& nom, const Creature& creature)
{
return (creature == nom);
}
std::ostream& operator<<(std::ostream& os, const Creature& creature) // TODO
{
os << creature.nom_ << " a " << creature.attaque_ << " en attaque et " << creature.defense_ << " en defense, " << std::endl
<< "Il a " << creature.pointDeVie_ << "/" << creature.pointDeVieTotal_ << " PV et " << creature.energie_ << "/"
<< creature.energieTotal_ << " Energie" << std::endl
<< "Il est au niveau " << creature.niveau_ << " avec " << creature.experience_ << "d'XP" << std::endl
<< "Il lui manque " << creature.experienceNecessaire_ - creature.experience_ << " jusqu'au prochain niveau " << std::endl;
os << "Pouvoirs : " << std::endl;
if (!creature.pouvoirs_.empty())
{
for(Pouvoir* pouvoir : creature.pouvoirs_)
{
std::cout << *pouvoir << std::endl;
}
}
else
os << creature.nom_ << " ne connait aucun pouvoir";
os << std::endl;
return os;
}
std::string Creature::obtenirTypeCreature() const
{
return (typeid(*this).name());
}