forked from tsoj/ecs_prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
140 lines (120 loc) · 4.03 KB
/
main.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
#include <iostream>
#include "ecs.hpp"
#include "benchmark.hpp"
#include "e.hpp"
using namespace ecs;
struct Position
{
double x;
double y;
};
struct Mass
{
double m;
};
struct SomeEvent
{
int value = 0;
};
struct AnotherEvent
{
double value = 0.0;
};
void handleSomeEvent(const SomeEvent& event)
{
std::cout << "HANDLING SOME EVENT !!: " << event.value << std::endl;
}
void handleAnotherEvent(const AnotherEvent& event)
{
std::cout << "HANDLING ANOTHER EVENT !!: " << event.value << std::endl;
}
void outputSystem()
{
for(auto currentEntity : Iterator<void>())
{
std::cout << "-----------------------" << std::endl;
std::cout << "ID: \t" << currentEntity.getID() << std::endl;
if(currentEntity.hasComponents<Position>())
{
std::cout << "Position:" << std::endl;
std::cout << " x: \t" << currentEntity.getComponent<Position>().x << std::endl;
std::cout << " y: \t" << currentEntity.getComponent<Position>().y << std::endl;
}
if(currentEntity.hasComponents<Mass>())
{
std::cout << "Mass: \t" << currentEntity.getComponent<Mass>().m << std::endl;
}
std::cout << "-----------------------" << std::endl;
}
}
void gravitySystem()
{
for(auto currentEntity : Iterator<Mass, Position>())
{
for(auto currentEntity2_iter = ++Iterator<Mass, Position>(currentEntity); currentEntity2_iter != Iterator<Mass, Position>().end(); ++currentEntity2_iter)
{
auto currentEntity2 = *currentEntity2_iter;
double dx = currentEntity2.getComponent<Position>().x - currentEntity.getComponent<Position>().x;
double dy = currentEntity2.getComponent<Position>().y - currentEntity.getComponent<Position>().y;
double distanceSquared = dx*dx + dy*dy;
if(distanceSquared<0)
{
distanceSquared*=-1;
}
double F = 6.67259e-11 * ((currentEntity2.getComponent<Mass>().m * currentEntity.getComponent<Mass>().m) / (distanceSquared));
std::cout<<"Force between " << currentEntity.getID() <<" and "<< currentEntity2.getID() << ": "<< F<<"\n";
}
}
}
void removeMeSystem()
{
std::cout << "Remove me." << std::endl;
}
int main()
{
std::cout << "Hello\n" << std::endl;
SystemManager::addSystem(&outputSystem, std::chrono::milliseconds(0));
SystemManager::addSystem(&gravitySystem, std::chrono::milliseconds(0));
SystemManager::addSystem(&removeMeSystem, std::chrono::milliseconds(0));
SystemManager::addSystem(&handleSomeEvent);
SystemManager::addSystem(&handleAnotherEvent);
Entity a = Entity::createEntity();
Entity b = Entity::createEntity();
Entity c = Entity::createEntity();
Entity d = Entity::createEntity();
Entity e = Entity::createEntity();
a.createComponent<Position>(Position{ 0.2, 0.3 });
b.createComponent<Position>(Position{ 500.0, 600.0 });
c.createComponent<Position>(Position{ 42.0, 7890.3 });
d.createComponent<Position>(Position{ 6.0, 6.3 });
a.createComponent<Mass>(Mass{ 0.7 });
b.createComponent<Mass>(Mass{ 7.3 });
c.createComponent<Mass>(Mass{ 1200000.0 });
SystemManager::throwEvent(SomeEvent{12504});
SystemManager::throwEvent(SomeEvent{12505});
SystemManager::throwEvent(SomeEvent{12506});
SystemManager::throwEvent(SomeEvent{12507});
SystemManager::throwEvent(AnotherEvent{0.6});
SystemManager::throwEvent(AnotherEvent{0.05});
SystemManager::throwEvent(SomeEvent{12508});
SystemManager::throwEvent(SomeEvent{12509});
SystemManager::throwEvent(AnotherEvent{10.6});
SystemManager::throwEvent(AnotherEvent{10.05});
SystemManager::runSystems();
SystemManager::throwEvent(AnotherEvent{10.05});
SystemManager::removeSystem(&handleAnotherEvent);
SystemManager::removeSystem(&removeMeSystem);
std::cout << std::endl;
d.removeEntity();
a.removeEntity();
c.removeComponent<Position>();
e.createComponent<Position>(Position{2000.0, 3000.0});
e.createComponent<Mass>(Mass{1000.0});
a = Entity::createEntity();
a.createComponent<Position>(Position{1000.0, 1000.0});
SystemManager::runSystems();
extra();
//ecsTest(10'000, 1'000'000, 3);
std::cout << "\nGood Bye" << std::endl;
return 0;
}