level_system.cpp 24.5 KB
Newer Older
Georg Schäfer's avatar
Georg Schäfer committed
1
2
#include "level_system.hpp"

Georg Schäfer's avatar
Georg Schäfer committed
3
#include "../gameplay/movement_comp.hpp"
Kevin Balz's avatar
Kevin Balz committed
4
5
#include "../gameplay/fixed_path_comp.hpp"
#include "../gameplay/shooting_comp.hpp"
Georg Schäfer's avatar
Georg Schäfer committed
6
7
#include "../gameplay/player_comp.hpp"
#include "../gameplay/rigid_body_comp.hpp"
8
#include "../gameplay/viewtarget_comp.hpp"
Tim Scheiber's avatar
Tim Scheiber committed
9
#include "../gameplay/spring_comp.hpp"
10
#include "../helper/attachment_comp.hpp"
11
12

#include <mirrage/ecs/components/transform_comp.hpp>
Georg Schäfer's avatar
Georg Schäfer committed
13
14
#include <mirrage/ecs/ecs.hpp>

Florian Oetke's avatar
Florian Oetke committed
15
16
17
#include <mirrage/renderer/model_comp.hpp>


Georg Schäfer's avatar
Georg Schäfer committed
18
namespace phase_shifter::level {
19
20
	using namespace mirrage;
	using namespace mirrage::ecs::components;
Florian Oetke's avatar
Florian Oetke committed
21
	using namespace mirrage::util;
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
	namespace {
		using action = std::function<auto(const Tileset&     tileset,
		                                  const std::string& key,
		                                  const Tile&        tile,
		                                  const glm::vec3&   position)
		                                     ->void>;

		template <typename F>
		auto for_each(const Level& level, F&& action)
		{
			auto&     tiles  = level.tileset->tiles;
			auto      offset = level.tileset->tile_size;
			glm::vec3 position{0.f};
			for(auto&& row : level.tiles) {
				position.x = 0.f;
				for(auto&& tile_key : row) {
					std::string key(1, tile_key);
					if(auto tile_it = tiles.find(key); tile_it != tiles.end()) {
						auto& tile = tile_it->second;
						action(level.tileset.get_blocking(), key, tile, position);
						position.x += offset;
					}
				}
				position.z += offset;
			}
		}
	} // namespace

Florian Oetke's avatar
Florian Oetke committed
51
52
53
54
	Level_system::Level_system(mirrage::ecs::Entity_manager&  entities,
	                           mirrage::asset::Asset_manager& assets,
	                           const gameplay::Beat_system&   beat)
	  : _entities(entities), _assets(assets), _beat(beat)
Georg Schäfer's avatar
Georg Schäfer committed
55
	{
Florian Oetke's avatar
Florian Oetke committed
56
57
58
59
60
61
62
63
64
65
66
		auto create_tile_dummy = [&](glm::vec4 c) {
			auto e = entities.emplace_empty();
			e.emplace<mirrage::renderer::Material_property_comp>(c);
			_tile_dummies.emplace_back(e);
		};

		create_tile_dummy(glm::vec4(0.f, 0.f, 0.f, 0.f));
		create_tile_dummy(glm::vec4(1, 1, 1, 4000.f));
		create_tile_dummy(glm::vec4(1, 0.5f, 0.5f, 10000.f));
		create_tile_dummy(glm::vec4(1, 1, 1, 4000.f));

Georg Schäfer's avatar
Georg Schäfer committed
67
68
		_entities.register_component_type<gameplay::Rigid_body_comp>();

Tim Scheiber's avatar
Tim Scheiber committed
69
		_spawners.emplace("@", [&](const Tile& tile, const glm::vec3& position) {
70
71
72
73
74
75
76
			mirrage::ecs::Entity_facet player =
			        _entities.entity_builder(tile.spawns).position(position).create();
			auto playerhandle  = player.handle();
			auto cam_elevation = 0.3f * glm::pi<float>();
			auto cam_azimuth   = 0.0f;
			auto cam_dir       = glm::quat(glm::vec3(
                    (cam_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * cam_azimuth, 0.f));
77
78
79
80
81
82
83
84
85
86
87
			auto camera        = _entities.entity_builder("camera")
			                      .rotation(cam_dir)
			                      .position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f + position)
			                      .post_create([=](ecs::Entity_facet entity) {
				                      entity.process<Transform_comp>([&](auto& transform) {
					                      transform.position = transform.direction() * -15.f + position;
					                      transform.look_at(position);
				                      });
				                      entity.process([=](gameplay::Viewtarget_comp& viewtarget) {
					                      viewtarget.target = playerhandle;
				                      });
Tim Scheiber's avatar
Tim Scheiber committed
88
89
90
91
				                      entity.process(
				                              [=](gameplay::Spring_comp& spring, Transform_comp& transform) {
					                              spring.position = transform.position;
				                              });
92
93
94
95
96
97
98
99
100
101
102
103
104
105
			                      })
			                      .create();


			auto sun_elevation = 0.2f * glm::pi<float>();
			auto sun_azimuth   = -0.2f;
			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()
			        .emplace<helper::Attachment_comp>(camera.handle(),
			                                          glm::rotate(sun_dir, glm::vec3(0, 0, 1)) * 40.f);
Florian Oetke's avatar
Florian Oetke committed
106
107
108
109
110
111
112
113
114
115


			sun_elevation = 0.3f * glm::pi<float>();
			sun_azimuth   = glm::pi<float>() - 0.2f;
			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("anti_sun")
			        .rotation(sun_dir)
			        .position(glm::rotate(sun_dir, glm::vec3(0, 0, 1)) * 40.f)
			        .create();
116
		});
Kevin Balz's avatar
Kevin Balz committed
117

Tim Scheiber's avatar
Tim Scheiber committed
118
119
120
		//basic turrets

		//turret targeting the player
Tim Scheiber's avatar
Tim Scheiber committed
121
122
123
124
125
126
127
128
129
130
131
132
133
		_spawners.emplace("0", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.attack_radius = 20;
					        shooting.pause_between_shots = 2;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
134
		//turret shooting North
Tim Scheiber's avatar
Tim Scheiber committed
135
136
137
138
139
140
141
142
143
144
145
146
		_spawners.emplace("1", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 0;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
147
		//turret shooting North-East
Tim Scheiber's avatar
Tim Scheiber committed
148
149
150
151
152
153
154
155
156
157
158
159
		_spawners.emplace("2", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 45;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
160
		//turret shooting East
Tim Scheiber's avatar
Tim Scheiber committed
161
162
163
164
165
166
167
168
169
170
171
172
		_spawners.emplace("3", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 90;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
173
		//turret shooting South-East
Tim Scheiber's avatar
Tim Scheiber committed
174
175
176
177
178
179
180
181
182
183
184
185
		_spawners.emplace("4", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 135;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
186
		//turret shootin South
Tim Scheiber's avatar
Tim Scheiber committed
187
188
189
190
191
192
193
194
195
196
197
198
		_spawners.emplace("5", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 180;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
199
		//turret shooting South-West
Tim Scheiber's avatar
Tim Scheiber committed
200
201
202
203
204
205
206
207
208
209
210
211
		_spawners.emplace("6", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 225;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

Tim Scheiber's avatar
Tim Scheiber committed
212
		//turret shooting West
Tim Scheiber's avatar
Tim Scheiber committed
213
214
215
216
217
218
219
220
221
222
223
		_spawners.emplace("7", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 270;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});
Kevin Balz's avatar
Kevin Balz committed
224

Tim Scheiber's avatar
Tim Scheiber committed
225
		//turret shooting North-West
Tim Scheiber's avatar
Tim Scheiber committed
226
227
228
229
		_spawners.emplace("8", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
Kevin Balz's avatar
Kevin Balz committed
230
				        entity.process([&](gameplay::Shooting_comp& shooting) {
Tim Scheiber's avatar
Tim Scheiber committed
231
					        shooting.default_orientation = 315;
Kevin Balz's avatar
Kevin Balz committed
232
233
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
Tim Scheiber's avatar
Tim Scheiber committed
234
235
			        })
			        .create();
Kevin Balz's avatar
Kevin Balz committed
236
237
		});

Tim Scheiber's avatar
Tim Scheiber committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		/*_spawners.emplace("9", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 0;
					        shooting.rotation_per_step   = 360.f / 16.f;
					        shooting.max_rotation        = 999999999.f;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});*/

Tim Scheiber's avatar
Tim Scheiber committed
252
		//turret rotating counter-clockwise
Tim Scheiber's avatar
Tim Scheiber committed
253
254
255
256
257
258
259
260
261
262
263
264
265
		_spawners.emplace("<", [&](const Tile& tile, const glm::vec3& position) {
		    _entities.entity_builder(tile.spawns)
		            .position(position)
		            .post_create([=](auto entity) {
			            entity.process([&](gameplay::Shooting_comp& shooting) {
				            shooting.rotation_per_step   = -360.f / 16.f;
				            shooting.max_rotation        = -1;
				            shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
			            });
		            })
		            .create();
	    });

Tim Scheiber's avatar
Tim Scheiber committed
266
		//turret rotation clockwise
Tim Scheiber's avatar
Tim Scheiber committed
267
268
269
270
271
272
273
274
275
276
277
278
		_spawners.emplace(">", [&](const Tile& tile, const glm::vec3& position) {
		    _entities.entity_builder(tile.spawns)
		            .position(position)
		            .post_create([=](auto entity) {
			            entity.process([&](gameplay::Shooting_comp& shooting) {
				            shooting.rotation_per_step   = 360.f / 16.f;
				            shooting.max_rotation        = -1;
				            shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
			            });
		            })
		            .create();
	    });
Tim Scheiber's avatar
Tim Scheiber committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

		//special turrets

		_spawners.emplace("a", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.rotation_per_step   = 360.f / 16.f;
					        shooting.max_rotation        = -1;
					        shooting.set_patterns(
					                {gameplay::Bulletpattern({{0, 0}, {90, 0}, {180, 0}, {270, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("b", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 180;
					        shooting.set_patterns({gameplay::Bulletpattern(
					                {{0, 0}, {25, -8}, {-25, 8}, {50, -8}, {-50, 8}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("c", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 0;
					        shooting.rotation_per_step   = 15;
					        shooting.max_rotation        = 30;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}, {20, 0}, {-20, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("d", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Fixed_path_comp& fixed_path) {
					        fixed_path.update_path({180});
Kevin Balz's avatar
Kevin Balz committed
329
					        fixed_path.pause_between_steps = 2;
Tim Scheiber's avatar
Tim Scheiber committed
330
331
332
333
				        });
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 270;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
Kevin Balz's avatar
Kevin Balz committed
334
335
336
337
					        shooting.pause_between_shots = 2;
				        });
				        entity.process([&](gameplay::Movement_comp& movement) {
							movement.distance_per_step = 4;
Tim Scheiber's avatar
Tim Scheiber committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
				        });
			        })
			        .create();
		});

		_spawners.emplace("e", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 135;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}, {25, 0}, {-25, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("f", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 225;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}, {25, 0}, {-25, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("g", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 180;
					        shooting.rotation_per_step = 10;
					        shooting.max_rotation      = 20;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("h", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.rotation_per_step   = 360.f / 16.f;
					        shooting.max_rotation        = -1;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0},
					                                                        {20, 0},
					                                                        {-20, 0},
					                                                        {120, 0},
					                                                        {140, 0},
					                                                        {100, 0},
					                                                        {240, 0},
					                                                        {260, 0},
					                                                        {220, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("i", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.rotation_per_step = -360.f / 16.f;
					        shooting.max_rotation      = -1;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0},
					                                                        {20, 0},
					                                                        {-20, 0},
					                                                        {120, 0},
					                                                        {140, 0},
					                                                        {100, 0},
					                                                        {240, 0},
					                                                        {260, 0},
					                                                        {220, 0}})});
				        });
			        })
			        .create();
		});

		_spawners.emplace("j", [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns)
			        .position(position)
			        .post_create([=](auto entity) {
				        entity.process([&](gameplay::Shooting_comp& shooting) {
					        shooting.default_orientation = 0;
					        shooting.set_patterns({gameplay::Bulletpattern({{0, 0},
					                                                        {45, 0},
					                                                        {90, 0},
					                                                        {135, 0},
					                                                        {180, 0},
					                                                        {225, 0},
					                                                        {270, 0},
					                                                        {315, 0}})});
				        });
			        })
			        .create();
		});
Georg Schäfer's avatar
Georg Schäfer committed
441
442
	}

Florian Oetke's avatar
Florian Oetke committed
443
444
445
446
447
448
	auto Level_system::update(const mirrage::util::Time& time) -> void
	{
		if(_beat.beat_state().beat) {
			_beat_light_offset = (_beat_light_offset + 1) % _tile_dummies.size();
		}
	}
Georg Schäfer's avatar
Georg Schäfer committed
449

450
451
452
453
454
	void Level_system::draw(mirrage::renderer::Deferred_renderer& renderer)
	{
		auto cam = renderer.active_camera();
		if(cam.is_nothing())
			return;
Georg Schäfer's avatar
Georg Schäfer committed
455

456
457
458
459
460
		auto& level        = _current_level.get_blocking();
		auto  level_size_x = level.tiles[0].size();
		auto  level_size_y = level.tiles.size();
		auto& render_queue = renderer.low_level_draw_queue();

Florian Oetke's avatar
Florian Oetke committed
461
		auto offset   = level.tileset->tile_size;
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
		auto viewport = cam.get_or_throw().viewport;

		auto screen_to_world_in_plane = [&](glm::vec2 sp) {
			auto wp  = glm::unProject(glm::vec3{sp, 1.f},
                                     glm::mat4{1.f},
                                     cam.get_or_throw().pure_projection,
                                     cam.get_or_throw().viewport);
			wp       = glm::vec3{cam.get_or_throw().inv_view * glm::vec4{wp, 1.f}};
			auto dir = glm::normalize(wp - cam.get_or_throw().eye_position);

			if(dir.y == 0)
				return glm::vec3(0, 0, 0);
			else {
				auto t = (glm::dot(glm::vec3(0, 1, 0), glm::vec3(0, 0, 0))
				          - glm::dot(glm::vec3(0, 1, 0), cam.get_or_throw().eye_position))
				         / glm::dot(glm::vec3(0, 1, 0), dir);
				return cam.get_or_throw().eye_position + dir * t;
			}
		};

		auto top_left     = glm::vec3(999999.f, 999999.f, 999999.f);
		auto bottom_right = glm::vec3(0, 0, 0);

		for(auto& p : {glm::vec2(0, 0),
		               glm::vec2(viewport.z, 0),
		               glm::vec2(0, viewport.w),
		               glm::vec2(viewport.z, viewport.w)}) {
			auto wp    = screen_to_world_in_plane(p);
			top_left.x = std::min(top_left.x, wp.x);
			top_left.y = std::min(top_left.y, wp.y);
			top_left.z = std::min(top_left.z, wp.z);

			bottom_right.x = std::max(bottom_right.x, wp.x);
			bottom_right.y = std::max(bottom_right.y, wp.y);
			bottom_right.z = std::max(bottom_right.z, wp.z);
		}

		top_left /= offset;
		bottom_right /= offset;

		auto min_x = static_cast<std::size_t>(
		        std::clamp(std::floor(top_left.x) - 1, 0.f, static_cast<float>(level_size_x)));
		auto min_y = static_cast<std::size_t>(
		        std::clamp(std::floor(top_left.z) - 1, 0.f, static_cast<float>(level_size_y)));
		auto max_x = static_cast<std::size_t>(
		        std::clamp(std::ceil(bottom_right.x) + 1, 0.f, static_cast<float>(level_size_x)));
		auto max_y = static_cast<std::size_t>(
		        std::clamp(std::ceil(bottom_right.z) + 1, 0.f, static_cast<float>(level_size_y)));

		for(auto y = min_y; y < max_y; y++) {
			for(auto x = min_x; x < max_x; x++) {
				auto tile = level.tiles[y][x];

Florian Oetke's avatar
Florian Oetke committed
515
516
				auto tile_dummy = _tile_dummies[(x + y + _beat_light_offset) % _tile_dummies.size()].handle();

517
518
519
520
521
522
				auto model_ptr = _tile_models.find(tile);
				if(model_ptr != _tile_models.end() && model_ptr->second.ready()) {
					auto& model     = model_ptr->second.get_blocking();
					auto& submeshes = model.sub_meshes();
					for(auto&& submesh_index : mirrage::util::range(submeshes.size())) {
						auto& geometry        = render_queue.emplace_back();
Florian Oetke's avatar
Florian Oetke committed
523
						geometry.entity       = tile_dummy;
524
525
526
						geometry.model        = &model;
						geometry.sub_mesh     = std::uint32_t(submesh_index);
						geometry.position     = glm::vec3(x, 0, y) * offset;
527
						geometry.scale        = glm::vec3(1, 1, 1);
528
529
530
						geometry.orientation  = glm::angleAxis(0.f, glm::vec3{0.f, 1.f, 0.f});
						geometry.substance_id = submeshes[submesh_index].material->substance_id();
						geometry.culling_mask = ~std::uint32_t(0);
Georg Schäfer's avatar
Georg Schäfer committed
531
					}
Georg Schäfer's avatar
Georg Schäfer committed
532
533
534
				}
			}
		}
535
	}
Georg Schäfer's avatar
Georg Schäfer committed
536
537
538
539
540
541
542
543
544
545

	auto Level_system::load(const std::string& name) -> void
	{
		_current_level = _assets.load<Level>(mirrage::asset::AID("level:" + name));
		auto& level    = _current_level.get_blocking();
		for_each(level, [&](auto&, const std::string& key, const Tile& tile, const glm::vec3& position) {
			if(tile.spawn) {
				_spawn_function(key)(tile, position);
			}
		});
546
547
548
549
550
551

		_tile_models.clear();
		for(auto&& [id, tile] : level.tileset->tiles) {
			_tile_models[id[0]] =
			        _assets.load<mirrage::renderer::Model>(mirrage::asset::AID("model:" + tile.blueprint));
		}
Georg Schäfer's avatar
Georg Schäfer committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
	}

	auto Level_system::ray_cast(const glm::vec2& origin, const glm::vec2& direction)
	        -> std::pair<bool, util::Contact>
	{
		auto&                          level = _current_level.get_blocking();
		std::pair<bool, util::Contact> result{false, {}};
		for_each(level,
		         [&](const Tileset&     tileset,
		             const std::string& key,
		             const Tile&        tile,
		             const glm::vec3&   position) {
			         if(!tile.solid) {
				         return;
			         }

			         glm::vec2 tile_origin{position.x, position.z};
			         auto      half_size = tileset.tile_size / 2.f;
			         glm::vec2 half_vector{half_size, half_size};
			         auto [hit, contact] = util::intersect({origin, direction}, {tile_origin, half_vector});
			         if(!hit) {
				         return;
			         }
			         if(contact.distance2 < result.second.distance2) {
				         result.first  = true;
				         result.second = contact;
			         }
		         });
		return result;
Georg Schäfer's avatar
Georg Schäfer committed
581
	}
582

Florian Oetke's avatar
Florian Oetke committed
583
584
585
586
587
588
589
590
591
592
593
594
595
	void Level_system::check_contacts(glm::vec2                           origin,
	                                  float                               radius,
	                                  std::function<void(util ::Contact)> on_contact) const
	{
		auto& level      = _current_level.get_blocking();
		auto& tile_types = level.tileset->tiles;
		auto  tile_size  = level.tileset->tile_size;

		auto level_size_x = level.tiles[0].size();
		auto level_size_y = level.tiles.size();

		auto min_x = std::clamp(
		        static_cast<std::size_t>(std::floor(origin.x / tile_size - radius / tile_size - 0.5f)),
596
		        static_cast<std::size_t>(0),
Florian Oetke's avatar
Florian Oetke committed
597
598
599
		        level_size_x - 1);
		auto min_y = std::clamp(
		        static_cast<std::size_t>(std::floor(origin.y / tile_size - radius / tile_size - 0.5f)),
600
		        static_cast<std::size_t>(0),
Florian Oetke's avatar
Florian Oetke committed
601
602
603
		        level_size_y - 1);
		auto max_x = std::clamp(
		        static_cast<std::size_t>(std::ceil(origin.x / tile_size + radius / tile_size + 0.5f)),
604
		        static_cast<std::size_t>(0),
Florian Oetke's avatar
Florian Oetke committed
605
606
607
		        level_size_x - 1);
		auto max_y = std::clamp(
		        static_cast<std::size_t>(std::ceil(origin.y / tile_size + radius / tile_size + 0.5f)),
608
		        static_cast<std::size_t>(0),
Florian Oetke's avatar
Florian Oetke committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
		        level_size_y - 1);

		for(auto y = min_y; y <= max_y; y++) {
			for(auto x = min_x; x <= max_x; x++) {
				auto tile = level.tiles[y][x];
				auto key  = std::string(1, tile);
				if(auto tile_it = tile_types.find(key);
				   tile_it != tile_types.end() && tile_it->second.solid) {

					auto tile_origin    = glm::vec2{x * tile_size, y * tile_size};
					auto half_size      = tile_size / 2.f * 1.1f;
					auto half_vector    = glm::vec2{half_size, half_size};
					auto [hit, contact] = util::intersect(util::Aabb{tile_origin, half_vector},
					                                      util::Circle{origin, radius});
					if(hit) {
						on_contact(contact);
					}
				}
			}
		}
	}

631
632
633
634
635
636
637
638
639
640
	auto Level_system::_spawn_function(const std::string& key)
	        -> std::function<auto(const Tile&, const glm::vec3&)->void>
	{
		if(auto spawner_it = _spawners.find(key); spawner_it != _spawners.end()) {
			return spawner_it->second;
		}
		return [&](const Tile& tile, const glm::vec3& position) {
			_entities.entity_builder(tile.spawns).position(position).create();
		};
	}
Georg Schäfer's avatar
Georg Schäfer committed
641
} // namespace phase_shifter::level