aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/manager/SystemManager.cpp
blob: c8f4f3d2377d77e84c741cee039b44947e14b3a8 (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
#include "../system/AISystem.h"
#include "../system/AnimatorSystem.h"
#include "../system/AudioSystem.h"
#include "../system/CollisionSystem.h"
#include "../system/EventSystem.h"
#include "../system/InputSystem.h"
#include "../system/ParticleSystem.h"
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ReplaySystem.h"
#include "../system/ScriptSystem.h"

#include "SystemManager.h"

using namespace crepe;
using namespace std;

SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) {
	this->load_system<InputSystem>();
	this->load_system<EventSystem>();
	this->load_system<ScriptSystem>();
	this->load_system<ParticleSystem>();
	this->load_system<AISystem>();
	this->load_system<PhysicsSystem>();
	this->load_system<CollisionSystem>();
	this->load_system<AudioSystem>();
	this->load_system<AnimatorSystem>();
	this->load_system<RenderSystem>();
	this->load_system<ReplaySystem>();

	this->mediator.system_manager = *this;
}

void SystemManager::fixed_update() noexcept {
	for (SystemEntry & entry : this->system_order) {
		if (!entry.system.active) continue;
		try {
			entry.system.fixed_update();
		} catch (const exception & e) {
			Log::logf(Log::Level::WARNING, "Uncaught exception in {} fixed update: {}", entry.name, e.what());
		}
	}
}

void SystemManager::frame_update() noexcept {
	for (SystemEntry & entry : this->system_order) {
		if (!entry.system.active) continue;
		try {
			entry.system.frame_update();
		} catch (const exception & e) {
			Log::logf(Log::Level::WARNING, "Uncaught exception in {} frame update: {}", entry.name, e.what());
		}
	}
}

SystemManager::Snapshot SystemManager::save() {
	Snapshot snapshot;
	for (auto & [type, system] : this->systems) {
		snapshot[type] = system->active;
	}
	return snapshot;
}

void SystemManager::restore(const Snapshot & snapshot) {
	for (auto & [type, active] : snapshot) {
		this->systems[type]->active = active;
	}
}

void SystemManager::disable_all() {
	for (auto & [type, system] : this->systems) {
		system->active = false;
	}
}