aboutsummaryrefslogtreecommitdiff
path: root/src/test/ParticleTest.cpp
blob: 9112a3fd65bd323acf101b2b41f701ab2d1c62b7 (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
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
#include "api/Asset.h"
#include <crepe/Particle.h>
#include <crepe/api/Config.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Transform.h>
#include <crepe/manager/ComponentManager.h>
#include <crepe/system/ParticleSystem.h>
#include <gtest/gtest.h>
#include <math.h>

using namespace std;
using namespace std::chrono_literals;
using namespace crepe;

class ParticlesTest : public ::testing::Test {
	Mediator m;

public:
	ComponentManager component_manager{m};
	ParticleSystem particle_system{m};

	void SetUp() override {
		ComponentManager & mgr = this->component_manager;
		std::vector<std::reference_wrapper<Transform>> transforms
			= mgr.get_components_by_id<Transform>(0);
		if (transforms.empty()) {
			GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0);

			Color color(0, 0, 0, 0);
			auto s1 = Asset("asset/texture/img.png");
			Sprite & test_sprite = game_object.add_component<Sprite>(
				s1, Sprite::Data{
						.color = color,
						.flip = Sprite::FlipSettings{true, true},
						.size = {10, 10},
					});

			game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
				.position = {0, 0},
				.max_particles = 100,
				.emission_rate = 0,
				.min_speed = 0,
				.max_speed = 0,
				.min_angle = 0,
				.max_angle = 0,
				.begin_lifespan = 0,
				.end_lifespan = 0,
				.force_over_time = vec2{0, 0},
				.boundary{
					.width = 0,
					.height = 0,
					.offset = vec2{0, 0},
					.reset_on_exit = false,
				},
				.sprite = test_sprite,
			});
		}
		transforms = mgr.get_components_by_id<Transform>(0);
		Transform & transform = transforms.front().get();
		transform.position.x = 0.0;
		transform.position.y = 0.0;
		transform.rotation = 0.0;
		std::vector<std::reference_wrapper<ParticleEmitter>> rigidbodies
			= mgr.get_components_by_id<ParticleEmitter>(0);
		ParticleEmitter & emitter = rigidbodies.front().get();
		emitter.data.position = {0, 0};
		emitter.data.emission_rate = 0;
		emitter.data.min_speed = 0;
		emitter.data.max_speed = 0;
		emitter.data.min_angle = 0;
		emitter.data.max_angle = 0;
		emitter.data.begin_lifespan = 0;
		emitter.data.end_lifespan = 0;
		emitter.data.force_over_time = vec2{0, 0};
		emitter.data.boundary = {0, 0, vec2{0, 0}, false};
		for (auto & particle : emitter.data.particles) {
			particle.active = false;
		}
	}
};

TEST_F(ParticlesTest, spawnParticle) {
	Config::get_instance().physics.gravity = 1;
	ComponentManager & mgr = this->component_manager;
	ParticleEmitter & emitter = mgr.get_components_by_id<ParticleEmitter>(0).front().get();
	emitter.data.end_lifespan = 5;
	emitter.data.boundary.height = 100;
	emitter.data.boundary.width = 100;
	emitter.data.max_speed = 0.1;
	emitter.data.max_angle = 0.1;
	emitter.data.max_speed = 10;
	emitter.data.max_angle = 10;
	particle_system.update();
	//check if nothing happend
	EXPECT_EQ(emitter.data.particles[0].active, false);
	emitter.data.emission_rate = 1;
	//check particle spawnes
	particle_system.update();
	EXPECT_EQ(emitter.data.particles[0].active, true);
	particle_system.update();
	EXPECT_EQ(emitter.data.particles[1].active, true);
	particle_system.update();
	EXPECT_EQ(emitter.data.particles[2].active, true);
	particle_system.update();
	EXPECT_EQ(emitter.data.particles[3].active, true);

	for (auto & particle : emitter.data.particles) {
		// Check velocity range
		EXPECT_GE(particle.velocity.x, emitter.data.min_speed);
		// Speed should be greater than or equal to min_speed
		EXPECT_LE(particle.velocity.x, emitter.data.max_speed);
		// Speed should be less than or equal to max_speed
		EXPECT_GE(particle.velocity.y, emitter.data.min_speed);
		// Speed should be greater than or equal to min_speed
		EXPECT_LE(particle.velocity.y, emitter.data.max_speed);
		// Speed should be less than or equal to max_speed

		// Check angle range
		EXPECT_GE(particle.angle, emitter.data.min_angle);
		// Angle should be greater than or equal to min_angle
		EXPECT_LE(particle.angle, emitter.data.max_angle);
		// Angle should be less than or equal to max_angle
	}
}

TEST_F(ParticlesTest, moveParticleHorizontal) {
	Config::get_instance().physics.gravity = 1;
	ComponentManager & mgr = this->component_manager;
	ParticleEmitter & emitter = mgr.get_components_by_id<ParticleEmitter>(0).front().get();
	emitter.data.end_lifespan = 100;
	emitter.data.boundary.height = 100;
	emitter.data.boundary.width = 100;
	emitter.data.min_speed = 1;
	emitter.data.max_speed = 1;
	emitter.data.max_angle = 0;
	emitter.data.emission_rate = 1;
	for (int a = 1; a < emitter.data.boundary.width / 2; a++) {
		particle_system.update();
		EXPECT_EQ(emitter.data.particles[0].position.x, a);
	}
}

TEST_F(ParticlesTest, moveParticleVertical) {
	Config::get_instance().physics.gravity = 1;
	ComponentManager & mgr = this->component_manager;
	ParticleEmitter & emitter = mgr.get_components_by_id<ParticleEmitter>(0).front().get();
	emitter.data.end_lifespan = 100;
	emitter.data.boundary.height = 100;
	emitter.data.boundary.width = 100;
	emitter.data.min_speed = 1;
	emitter.data.max_speed = 1;
	emitter.data.min_angle = 90;
	emitter.data.max_angle = 90;
	emitter.data.emission_rate = 1;
	for (int a = 1; a < emitter.data.boundary.width / 2; a++) {
		particle_system.update();
		EXPECT_EQ(emitter.data.particles[0].position.y, a);
	}
}

TEST_F(ParticlesTest, boundaryParticleReset) {
	Config::get_instance().physics.gravity = 1;
	ComponentManager & mgr = this->component_manager;
	ParticleEmitter & emitter = mgr.get_components_by_id<ParticleEmitter>(0).front().get();
	emitter.data.end_lifespan = 100;
	emitter.data.boundary.height = 10;
	emitter.data.boundary.width = 10;
	emitter.data.boundary.reset_on_exit = true;
	emitter.data.min_speed = 1;
	emitter.data.max_speed = 1;
	emitter.data.min_angle = 90;
	emitter.data.max_angle = 90;
	emitter.data.emission_rate = 1;
	for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) {
		particle_system.update();
	}
	EXPECT_EQ(emitter.data.particles[0].active, false);
}

TEST_F(ParticlesTest, boundaryParticleStop) {
	Config::get_instance().physics.gravity = 1;
	ComponentManager & mgr = this->component_manager;
	ParticleEmitter & emitter = mgr.get_components_by_id<ParticleEmitter>(0).front().get();
	emitter.data.end_lifespan = 100;
	emitter.data.boundary.height = 10;
	emitter.data.boundary.width = 10;
	emitter.data.boundary.reset_on_exit = false;
	emitter.data.min_speed = 1;
	emitter.data.max_speed = 1;
	emitter.data.min_angle = 90;
	emitter.data.max_angle = 90;
	emitter.data.emission_rate = 1;
	for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) {
		particle_system.update();
	}
	const double TOLERANCE = 0.01;
	EXPECT_NEAR(emitter.data.particles[0].velocity.x, 0, TOLERANCE);
	EXPECT_NEAR(emitter.data.particles[0].velocity.y, 0, TOLERANCE);
	if (emitter.data.particles[0].velocity.x != 0)
		EXPECT_NEAR(std::abs(emitter.data.particles[0].position.x),
					emitter.data.boundary.height / 2, TOLERANCE);
	if (emitter.data.particles[0].velocity.y != 0)
		EXPECT_NEAR(std::abs(emitter.data.particles[0].position.y),
					emitter.data.boundary.width / 2, TOLERANCE);
}