aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/ReplaySystem.cpp
blob: 39b5c14f57bf1ca16925a913772f318b7c2202df (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
#include "../manager/ReplayManager.h"
#include "../manager/SystemManager.h"

#include "RenderSystem.h"
#include "ReplaySystem.h"

using namespace crepe;
using namespace std;

void ReplaySystem::fixed_update() {
	ReplayManager & replay = this->mediator.replay_manager;
	ReplayManager::State state = replay.get_state();
	ReplayManager::State last_state = this->last_state;
	this->last_state = state;

	switch (state) {
		case ReplayManager::IDLE: break;
		case ReplayManager::RECORDING: {
			replay.frame_record();
			break;
		}
		case ReplayManager::PLAYING: {
			if (last_state != ReplayManager::PLAYING) this->playback_begin();
			bool last = replay.frame_step();
			if (last) this->playback_end();
			break;
		}
	}
}

void ReplaySystem::playback_begin() {
	SystemManager & systems = this->mediator.system_manager;
	ComponentManager & components = this->mediator.component_manager;

	this->playback = {
		.components = components.save(),
		.systems = systems.save(),
	};

	systems.disable_all();
	systems.get_system<RenderSystem>().active = true;
	systems.get_system<ReplaySystem>().active = true;
}

void ReplaySystem::playback_end() {
	SystemManager & systems = this->mediator.system_manager;
	ComponentManager & components = this->mediator.component_manager;

	components.restore(this->playback.components);
	systems.restore(this->playback.systems);
}