-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.cpp
More file actions
258 lines (218 loc) · 6.63 KB
/
Factory.cpp
File metadata and controls
258 lines (218 loc) · 6.63 KB
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
#include<iostream>
#include<memory>
#include <random>
#include <iostream>
using namespace std;
/*
---------------------------------------------------DESIGN PATTERN "FACTORY OBJECT"-------------------------------------------------
You have 5 types of enemies:
* robot
* manticore
* griffon
* ogre
* goblin
Each enemy has HP, attack damage and attack range. Each enemy activates his passive after being created. You want them to be able to damage the player, and vice versa. You also want enemies not be able to damage each other.
*/
enum EnemyType {ROBOT, MANTICORE, GRIFFON, OGRE, GOBLIN};
class Creature{
protected:
float hp;
float atk_dmg;
float atk_range;
public:
//pure virtual functions
virtual void getDamaged(int damage) = 0;
virtual void dealDamage(std::shared_ptr<Creature> target) = 0;
};
class Player : public Creature{
public:
Player(int hp){
this->hp = hp;
this->atk_dmg = 30;
this->atk_range = 0;
}
int getHP(){
return this->hp;
}
void getDamaged(int dmg) override{
this->hp = this->hp - dmg;
}
void dealDamage(std::shared_ptr<Creature> target) override{
target->getDamaged(atk_dmg);
}
};
class Enemy : public Creature{
protected:
EnemyType type;
public:
//pure virtual functions
virtual void passive() = 0;
int random_number(int min, int max){
return (std::rand() % (max - min)) + min;
}
void getDamaged(int dmg) override{
this->hp = this->hp - dmg;
}
void dealDamage(std::shared_ptr<Creature> p) override{
if(dynamic_pointer_cast<Player>(p)){
p->getDamaged(atk_dmg);
}
}
float getHP(){
return this->hp;
}
float getAtkDmg(){
return this->atk_dmg;
}
float getAtkRange(){
return this->atk_range;
}
EnemyType getType(){
return this->type;
}
};
class Robot : public Enemy{
public:
Robot(){
this->hp = random_number(100, 200);
this->atk_dmg = random_number(10,20);
this->atk_range = 0;
this->type = EnemyType::ROBOT;
}
void passive() override{
atk_dmg = atk_dmg + 10;
}
};
class Manticore : public Enemy{
public:
Manticore(){
this->hp = random_number(200, 300);
this->atk_dmg = random_number(20,25);
this->atk_range = 10;
this->type = EnemyType::MANTICORE;
}
void passive() override{
atk_range = atk_range + 10;
}
};
class Griffon : public Enemy{
public:
Griffon(){
this->hp = random_number(50, 200);
this->atk_dmg = random_number(40,60);
this->atk_range = random_number(10,30);
this->type = EnemyType::GRIFFON;
}
void passive() override{
hp = hp + 20;
}
};
class Ogre : public Enemy{
public:
Ogre(){
this->hp = random_number(400, 700);
this->atk_dmg = 20;
this->atk_range = 0;
this->type = EnemyType::OGRE;
}
void passive() override{
hp = hp + 50;
}
};
class Goblin : public Enemy{
public:
Goblin(){
this->hp = random_number(10, 30);
this->atk_dmg = random_number(5, 10);
this->atk_range = 0;
this->type = EnemyType::GOBLIN;
}
void passive() override{
hp = hp + 10;
atk_dmg = atk_dmg + 10;
}
};
class EnemyFactory{
public:
std::shared_ptr<Enemy> createEnemy(EnemyType type){
std::shared_ptr<Enemy> enemy(nullptr);
switch (type){
case EnemyType::GOBLIN:
{
std::shared_ptr<Goblin> temp(new Goblin());
enemy = dynamic_pointer_cast<Enemy>(temp);
break;
}
case EnemyType::GRIFFON:
{
std::shared_ptr<Griffon> temp(new Griffon());
enemy = dynamic_pointer_cast<Enemy>(temp);
break;
}
case EnemyType::MANTICORE:
{
std::shared_ptr<Manticore> temp(new Manticore());
enemy = dynamic_pointer_cast<Enemy>(temp);
break;
}
case EnemyType::OGRE:
{
std::shared_ptr<Ogre> temp(new Ogre());
enemy = dynamic_pointer_cast<Enemy>(temp);
break;
}
case EnemyType::ROBOT:
{
std::shared_ptr<Robot> temp(new Robot());
enemy = dynamic_pointer_cast<Enemy>(temp);
break;
}
}
return enemy;
}
};
class EnemyCreator{
private:
std::shared_ptr<EnemyFactory> factory;
public:
EnemyCreator(std::shared_ptr<EnemyFactory> factory){
this->factory = factory;
}
std::shared_ptr<Enemy> getEnemy(EnemyType type){
std::shared_ptr<Enemy> enemy;
enemy = factory->createEnemy(type);
enemy->passive();
return enemy;
}
};
void printStats(std::shared_ptr<Enemy> enemy){
cout << "Enemy: " << enemy->getType() << ". HP: " << enemy->getHP() << ", attack damage: " << enemy->getAtkDmg() << ", attack range: " << enemy->getAtkRange() << endl;
return ;
}
int main(){
//create enemy factory
std::shared_ptr<EnemyFactory> factory(new EnemyFactory());
std::unique_ptr<EnemyCreator> creator(new EnemyCreator(factory));
//create player
std::shared_ptr<Player> player(new Player(100));
//create 2 goblins and ogre
std::shared_ptr<Enemy> goblin1 = creator->getEnemy(EnemyType::GOBLIN);
printStats(goblin1);
std::shared_ptr<Enemy> goblin2 = creator->getEnemy(EnemyType::GOBLIN);
printStats(goblin2);
std::shared_ptr<Enemy> ogre1 = creator->getEnemy(EnemyType::OGRE);
printStats(ogre1);
//deal damage to player
goblin1->dealDamage(player);
goblin2->dealDamage(player);
ogre1->dealDamage(player);
//show player's health
cout << "Player HP: " << player->getHP() << endl;
//player attacks goblin1
player->dealDamage(goblin1);
printStats(goblin1);
//no damage dealt
goblin2->dealDamage(goblin1);
printStats(goblin1);
return 0;
}