-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.cpp
73 lines (61 loc) · 1.82 KB
/
tester.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
#include "tester.h"
#include "gamestate.h"
#include "player.h"
#include <iostream>
Tester::Tester(std::unique_ptr<GameStateFactory>& factory)
: state(factory->createGameState()),
paused(false) {}
Tester::~Tester()
{
delete state;
}
void Tester::run(int num_frames) {
std::srand(std::time(nullptr));
for (int i = 0; i < num_frames; ++i) {
if (paused) {
simulateTogglePause();
} else {
// Jump (1/100) or pause (1/250) randomly
int roll = std::rand();
if (roll < RAND_MAX / 100.0) {
simulateJump();
} else if (roll > RAND_MAX * 249.0 / 250.0) {
simulateTogglePause();
}
}
simulateUpdate();
assertCollisionBehaviour();
std::cout << std::endl;
}
}
void Tester::simulateUpdate() {
state->update(paused);
std::cout << "Update simulated" << std::endl;
}
void Tester::simulateJump() {
state->getPlayer()->jump();
std::cout << "Jump simulated" << std::endl;
}
void Tester::simulateTogglePause() {
paused = !paused;
if (paused) {
std::cout << "Paused" << std::endl;
} else {
std::cout << "Unpaused" << std::endl;
}
}
bool Tester::assertCollisionBehaviour() {
// Ensure that collisions stop movement of background and obstacles
if (state->getPlayerColliding()) {
std::vector<Entity*> obstacles = state->findEntitiesByNameContains("obstacle");
for (auto* obstacle : obstacles) {
Obstacle* o = dynamic_cast<Obstacle*>(obstacle);
if (o->isMoving()) {
std::cerr << "Invalid obstacle collision behaviour" << std::endl;
return false;
}
}
}
std::cout << "Passed obstacle collision behaviour test." << std::endl;
return true;
}