-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
137 lines (107 loc) · 3.66 KB
/
example.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
// ECS.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ComponentSystem.h"
#include <iostream>
#define CFID_HEALTH 1
#define CFID_ARMOR 2
#define CFID_ATTACK 3
#define CFID_NAME 4
// component holding entity's name
struct Name : public Component {
Name() : name("") { mFamilyId = CFID_NAME; }
std::string name;
};
// component holding entity's health
struct Health : public Component {
Health() : health(10) { mFamilyId = CFID_HEALTH; }
int health;
};
// component holding entity's armor
struct Armor : public Component {
Armor() : armor(3) { mFamilyId = CFID_ARMOR; }
int armor;
};
// component holding entity's attack power
struct Attack : public Component {
Attack() : strength(2) { mFamilyId = CFID_ATTACK; }
int strength;
};
// System responsible of creating tanks
class TankFactory : public ComponentSystem {
public:
entity_t Create( const std::string& name ) {
// create new entity
entity_t eid = entitySystem.CreateNewEntity();
// add attack, health, name and armor components to entity
CreateComponent<Attack>(eid);
ComponentPtr com_armor = CreateComponent<Armor>(eid);
ComponentPtr com_health = CreateComponent<Health>(eid);
// randomize a bit health and armor
smart_cast<Health*>(com_health)->health = 5+rand()%5;
smart_cast<Armor*>(com_armor)->armor += (rand()%2)-1;
smart_cast<Name*>( CreateComponent<Name>(eid) )->name = name;
return eid;
}
};
// system responsible of battling tanks
class TankBattleSystem : public ComponentSystem {
public:
bool MakeAttack( entity_t attacker, entity_t defender ) {
// get attackers attack ppower and defender's armor
Attack* attack = Get<Attack*>( attacker, CFID_ATTACK );
Armor* armor = Get<Armor*>( defender, CFID_ARMOR );
int attack_power = rand()%6;
// make some kind of attack
if( attack_power < attack->strength ) {
// attack succeeded
// reduce defender's health
Health* defender_health = Get<Health*>( defender, CFID_HEALTH );
defender_health->health--;
// print some stat messages
std::cout << Get<Name*>( attacker, CFID_NAME )->name << " reduces " <<
Get<Name*>( defender, CFID_NAME )->name << "'s health with 1 damage to " <<
defender_health->health << " health." <<
std::endl;
// and return true if defender died.
if( defender_health->health <= 0 )
return true;
} else {
std::cout << Get<Name*>( attacker, CFID_NAME )->name << " misses " <<
Get<Name*>( defender, CFID_NAME )->name <<std::endl;
}
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
TankFactory tankFactory;
// create two tanks
entity_t tank1 = tankFactory.Create("Sherman");
entity_t tank2 = tankFactory.Create("Panzer");
// fetch all components of two tanks based on their enity id
component_vector vec_tank1_components, vec_tank2_components;
tankFactory.GetComponentsByEntity(tank1, vec_tank1_components );
tankFactory.GetComponentsByEntity(tank2, vec_tank2_components );
// instantiate battle system
TankBattleSystem battleSystem;
// and add to it two tanks previously defined
battleSystem.
AttachArray(vec_tank1_components)->
AttachArray(vec_tank2_components);
// loop a battle between two tanks until one is dead.
bool battle_ongoing = true;
while( battle_ongoing ) {
// if make attack returns true, then the defender is dead
if( battleSystem.MakeAttack( tank1, tank2 ) == false ) {
if( battleSystem.MakeAttack( tank2, tank1 ) ) {
std::cout << tankFactory.Get<Name*>(tank2, CFID_NAME)->name << " wins." << std::endl;
battle_ongoing = false;
}
} else {
std::cout << tankFactory.Get<Name*>(tank1, CFID_NAME)->name << " wins." << std::endl;
battle_ongoing = false;
}
}
return 0;
}