-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
113 lines (96 loc) · 3.06 KB
/
Copy pathmain.cpp
File metadata and controls
113 lines (96 loc) · 3.06 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <print>
#include <string>
import ddge.prelude;
import ddge.modules.ecs;
import ddge.modules.scheduler;
import ddge.utility.containers.OptionalRef;
import demo.Window;
using namespace ddge::scheduler::accessors;
using namespace ddge::ecs::query_filter_tags;
struct WindowClosed {};
using Position = float;
using Health = int;
struct EnemyTag {};
struct Renderable {
int hi{};
};
struct Collider {
std::string message{};
};
constexpr static auto initialize = //
[](const Resource<Window> window, const Registry registry) -> void //
{
window->open();
registry->create(Position{}, EnemyTag{}, Collider{ .message = "Hi! 👋" });
};
constexpr static auto process_events = //
[](const Processor& events_processor) -> void //
{ //
events_processor.process_events();
};
[[nodiscard]]
auto update_world()
{
return ddge::scheduler::query(
+[]( //
const ddge::ecs::ID id,
const Position position,
const Optional<const Collider> optional_collider,
With<EnemyTag>,
Without<Renderable>
) -> void {
if (optional_collider.has_value()) {
std::println(
"Collider #{} at position {} says \"{}\"",
id.underlying(),
position,
optional_collider->message
);
}
}
);
}
constexpr static auto record_window_events =
[](const Recorder<WindowClosed>& window_closed_event_recorder) -> void //
{ //
window_closed_event_recorder.record();
};
constexpr static auto game_is_running =
[](const Reader<WindowClosed>& window_closed_event_reader) -> bool //
{ //
return window_closed_event_reader.read().size() == 0;
};
[[nodiscard]]
auto run_game_loop()
{
return ddge::scheduler::loop_until(
ddge::scheduler::start_as(
ddge::scheduler::group(
update_world(), //
ddge::scheduler::as_task(record_window_events)
)
)
.then(ddge::scheduler::as_task(process_events)),
ddge::scheduler::as_task(game_is_running)
);
}
constexpr static auto shut_down = //
[](const Resource<Window> window) -> void //
{ //
window->close();
};
auto main() -> int
{
ddge::app::create()
.plug_in(ddge::resources::Plugin{})
.insert_resource(Window{})
.plug_in(ddge::events::Plugin{})
.register_event<WindowClosed>()
.plug_in(ddge::ecs::Plugin{})
.plug_in(ddge::scheduler::Plugin{})
.run(
ddge::scheduler::start_as(ddge::scheduler::as_task(initialize)) //
.then(run_game_loop())
.then(ddge::scheduler::as_task(shut_down))
);
}