aboutsummaryrefslogtreecommitdiffstats
path: root/tests/entitiesBenchmark.h
blob: e046fe359eb42606c7c0ac4877e053b92c6b9ca9 (plain)
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
#ifndef ENTITIESBENCHMARK_H_
#define ENTITIESBENCHMARK_H_

#include <string>
#include <vector>
#include <memory>
#include <random>
#include <numeric>
#include <functional>

#include "../entities.hpp"

class EntitiesBenchmark {
    public:

    struct PositionComponent : public Component {
        float x = 0.0f;
        float y = 0.0f;
    };

    struct VelocityComponent : public Component {
        float x = 0.0f;
        float y = 0.0f;
    };

    struct ComflabulationComponent : public Component {
        float thingy = 0.0;
        int dingy = 0;
        bool mingy = false;
        std::string stringy;
    };

    class MovementSystem : public System {
        public:
        MovementSystem() = default;

        void update(EntityManager &es, DeltaTime dt) {
			es.each<PositionComponent, VelocityComponent>(
				[dt](Entity e) {
					auto& pos = *e.component<PositionComponent>();
					auto& vel = *e.component<VelocityComponent>();
					pos.x = vel.x * dt;
					pos.y = vel.y * dt;
				}
			);
        }
    };

    class ComflabSystem : public System {
        public:
        ComflabSystem() = default;

        void update(EntityManager &es, DeltaTime dt) {
   			es.each<ComflabulationComponent>(
				[dt](Entity e) {
					auto comflab = e.component<ComflabulationComponent>();
	                comflab->thingy *= 1.000001f;
	                comflab->mingy = !comflab->mingy;
	                comflab->dingy++;
	                //comflab.stringy = std::to_string(comflab.dingy);
	            }
			);
        }
    };

    #ifdef USE_MORECOMPLEX_SYSTEM
    class MoreComplexSystem : public System {
        private:
        int random(int min, int max){
            // Seed with a real random value, if available
            static std::random_device r;
        
            // Choose a random mean between min and max
            static std::default_random_engine e1(r());

            std::uniform_int_distribution<int> uniform_dist(min, max);

            return uniform_dist(e1);
        }

        public:
        MoreComplexSystem() = default;

        void update(EntityManager &es, DeltaTime dt) {
			es.each<PositionComponent, DirectionComponent, ComflabulationComponent>(
				[dt](Entity e) {
					auto comflab = e.component<ComflabulationComponent>();
					if(comflab) {
						std::vector<double> vec;
						for(size_t i = 0; i < comflab->dingy && i < 100; i++)
							vec.push_back(i * comflab0>thingy);
						int sum = std::accumulate(vec.begin(), vec.end(), 0);
						int product = std::accumulate(vec.begin(), vec.end(),
							1, std::multiplies<double>());
						comflab->stringy = std::to_string(comflab->dingy);
						
						auto pos = e.component<PositionComponent>();
						auto vel = e.component<VelocityComponent>();
						if (pos && vel && comflab->dingy % 10000 == 0) {
							if (pos->x > pos->y) {
								vel->x = random(0, 5);
								vel->y = random(0, 10);
							} else {
								vel->x = random(0, 10);
								vel->y = random(0, 5);
							}
						}
					}
				}
			);
        }
    };
    #endif

    class Application {
        public:
		EntityManager em;
		SystemManager sm;

        Application() : sm(em) {
            sm.add<MovementSystem>();
            sm.add<ComflabSystem>();
            #ifdef USE_MORECOMPLEX_SYSTEM
            sm.add<MoreComplexSystem>();
            #endif
        }

        void update(DeltaTime dt) {
            sm.update<MovementSystem>(dt);
            sm.update<ComflabSystem>(dt);
            #ifdef USE_MORECOMPLEX_SYSTEM
            sm.update<MoreComplexSystem>(dt);
            #endif
        }
    };

    static constexpr double fakeDeltaTime = 1.0 / 60;
};

#endif // ENTITIESBENCHMARK_H_