meta_system.cpp 5.36 KB
Newer Older
1
2
3
4
#include "meta_system.hpp"

#include "audio/audio_asset.hpp"
#include "game_engine.hpp"
Florian Oetke's avatar
Florian Oetke committed
5
#include "gameplay/beat_system.hpp"
6
#include "gameplay/camera_system.hpp"
7
#include "gameplay/movement_system.hpp"
8
#include "helper/attachment_system.hpp"
Florian Oetke's avatar
Florian Oetke committed
9
#include "input/input_system.hpp"
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "ui/hud_system.hpp"

#include <context.hpp>
#include <mirrage/ecs/components/transform_comp.hpp>
#include <mirrage/renderer/deferred_renderer.hpp>
#include <mirrage/renderer/loading_system.hpp>
#include <settings.hpp>
#include <sinks/sdl_sink.hpp>
#include <sources/opus.hpp>


namespace phase_shifter {
	using namespace mirrage;
	using namespace mirrage::ecs::components;

	Meta_system::Meta_system(Game_engine& engine)
	  : _entities(engine.assets(), this)
	  , _audio()
	  , _renderer(engine.renderer_factory().create_renderer(_entities, engine.render_pass_mask()))
	  , _model_loading(std::make_unique<renderer::Loading_system>(_entities, engine.assets()))
Florian Oetke's avatar
Florian Oetke committed
30
	  , _beat_system(std::make_unique<gameplay::Beat_system>())
Florian Oetke's avatar
Florian Oetke committed
31
32
	  , _movement_system(std::make_unique<gameplay::Movement_system>(_entities, *_beat_system))
	  , _input_system(std::make_unique<input::Input_system>(engine.bus(), _entities))
33
	  , _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
34
	  , _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities, *_beat_system))
35
	  , _camera_system(std::make_unique<gameplay::Camera_system>(_entities))
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
	{
		_entities.register_component_type<ecs::components::Transform_comp>();

		//auto s                     = _renderer->settings();
		//s.scene_luminance_override = 300.f;
		//_renderer->settings(s, false);

		eam::settings setting{{16, true, false, false}, 2, 48000, 1024};
		_audio.set_sink<eam::sdl_sink>(setting);
		_audio.set_master_volume(35);
		/*auto bg_music =
		        engine.assets().load<std::shared_ptr<eam::opus>>("audio:bg_music_01"_aid).get_blocking();
		auto bg_context = _audio.generate_context(bg_music);
		bg_context.get_object()->set_loops(-1);
		bg_context.get_object()->set_volume(50);
		bg_context.play_fade(2);
*/
		_commands.add("reload | Reloads most assets", [&] { engine.assets().reload(); });

		_commands.add("ecs.emplace <blueprint> | Creates a new entity in front of the current camera",
		              [&](std::string blueprint) {
			              auto  pos  = glm::vec3(0, 0, 0);
			              float prio = -1.f;
			              for(auto&& [transform, cam] :
			                  _entities.list<Transform_comp, renderer::Camera_comp>()) {
				              if(cam.priority() > prio) {
					              prio = cam.priority();
					              pos  = transform.position + transform.direction() * 2.f;
				              }
			              }
			              _entities.entity_builder(blueprint).position(pos).create();
		              });

		_commands.add("mem.renderer | Prints memory usage of renderer", [&] {
			IF_LOG(plog::info)
			{
				auto msg = std::stringstream();
				_renderer->device().print_memory_usage(msg);
				LOG(plog::info) << "Renderer Memory usage: " << msg.str();
			}
		});


		// TODO: replace with level-loading / generation code
Florian Oetke's avatar
Florian Oetke committed
80
81
		auto cam_elevation = 0.3f * glm::pi<float>();
		auto cam_azimuth   = 0.0f;
82
83
84
85
86
87
88
89
90
91
92
93
94
		auto cam_dir       = glm::quat(glm::vec3(
                (cam_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * cam_azimuth, 0.f));

		auto sun_elevation = 0.05f * glm::pi<float>();
		auto sun_azimuth   = 0.7f;
		auto sun_dir       = glm::quat(glm::vec3(
                (sun_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * sun_azimuth, 0.f));
		entities()
		        .entity_builder("sun")
		        .rotation(sun_dir)
		        .position(glm::rotate(sun_dir, glm::vec3(0, 0, 1)) * 40.f)
		        .create();

95
96
		mirrage::ecs::Entity_facet player = entities().entity_builder("player").position({0, 1, 0}).create();
		auto                       playerhandle = player.handle();
97
		entities()
98
99
100
101
102
103
104
105
		        .entity_builder("camera")
		        .rotation(cam_dir)
		        .position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f)
		        .post_create([=](ecs::Entity_facet entity) {
			        entity.process<Transform_comp>([&](auto& transform) {
				        transform.position = transform.direction() * -10.f;
				        transform.look_at({0, 0, 0});
			        });
106
107
			        entity.process(
			                [=](gameplay::Viewtarget_comp& viewtarget) { viewtarget.target = playerhandle; });
108
109
		        })
		        .create();
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

		entities()
		        .entity_builder("cube")
		        .position({0, 0, 0})
		        .post_create([=](ecs::Entity_facet entity) {
			        entity.process<Transform_comp>([&](auto& transform) {
				        transform.scale = glm::vec3{10.f, 0.01f, 10.f};
			        });
		        })
		        .create();
	}

	Meta_system::~Meta_system()
	{
		_renderer->device().wait_idle();
		_entities.clear();
	}

	void Meta_system::update(mirrage::util::Time dt)
	{
		_entities.process_queued_actions();

Florian Oetke's avatar
Florian Oetke committed
132
		_beat_system->update(dt);
Florian Oetke's avatar
Florian Oetke committed
133
134
		_input_system->update(dt);
		_movement_system->update(dt);
135
		_camera_system->update(dt);
Florian Oetke's avatar
Florian Oetke committed
136

137
138
139
140
141
142
143
144
145
146
147
148
149
150
		_attachment_system->update(dt);

		_hud_system->update(dt);

		_model_loading->update(dt);
		_renderer->update(dt);
	}
	void Meta_system::draw()
	{
		_hud_system->draw();
		_renderer->draw();
	}


Florian Oetke's avatar
Florian Oetke committed
151
152
	void Meta_system::pause() { _input_system->disable(); }
	void Meta_system::resume() { _input_system->enable(); }
153
154
155
156

	void Meta_system::shrink_to_fit() { _renderer->shrink_to_fit(); }

} // namespace phase_shifter