R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
level_editor.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/math.hpp>
5#include <aegis2d/ecs/waves.hpp>
6
7#include <cstddef>
8#include <filesystem>
9#include <optional>
10#include <span>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace rtype {
16
18struct DraftEnemy {
20 std::string type{};
22 aegis2d::Vec2 position{};
24 float at = 0.0F;
25
28 bool operator==(const DraftEnemy&) const = default;
29};
30
32struct DraftWave {
34 float delay = 0.0F;
36 std::vector<DraftEnemy> enemies{};
37};
38
42struct LevelDraft {
44 bool loop = false;
46 std::vector<DraftWave> waves{};
47
52 [[nodiscard]] static LevelDraft from_file(const std::filesystem::path& path);
53
59 [[nodiscard]] std::string to_json(std::size_t first_wave = 0) const;
60
68 void save(const std::filesystem::path& path) const;
69
74 [[nodiscard]] aegis2d::ecs::WaveSchedule schedule(std::size_t first_wave = 0) const;
75};
76
80 aegis2d::Vec2 position{};
82 bool pressed = false;
84 bool held = false;
86 bool remove = false;
87};
88
90struct WaveSpan {
92 float start = 0.0F;
94 float end = 0.0F;
95};
96
99struct EditorView {
101 static constexpr float strip = 96.0F;
103 float scale = 0.74F;
105 aegis2d::Vec2 origin{131.0F, 48.0F};
107 aegis2d::Vec2 timeline{131.0F, 608.0F};
109 aegis2d::Vec2 timeline_size{1018.0F, 28.0F};
110
115 [[nodiscard]] static float timeline_seconds(float end) noexcept;
116
120 [[nodiscard]] bool on_timeline(aegis2d::Vec2 screen) const noexcept;
121
125 [[nodiscard]] aegis2d::Vec2 to_screen(aegis2d::Vec2 level) const noexcept;
126
130 [[nodiscard]] aegis2d::Vec2 to_level(aegis2d::Vec2 screen) const noexcept;
131};
132
141public:
143 static constexpr float pick_radius = 20.0F;
144
148 explicit LevelEditor(LevelDraft draft, std::string type = "bydos_slave");
149
152 [[nodiscard]] const LevelDraft& draft() const noexcept;
153
156 [[nodiscard]] std::size_t wave() const noexcept;
157
159 void next_wave() noexcept;
160
162 void previous_wave() noexcept;
163
167 [[nodiscard]] std::vector<WaveSpan> timeline() const;
168
170 void add_wave();
171
175 void shift_delay(float seconds);
176
181 void move_wave(bool later);
182
185 [[nodiscard]] bool dragging_wave() const noexcept;
186
189 [[nodiscard]] std::span<const DraftEnemy> enemies() const noexcept;
190
194 void update(const EditorPointer& pointer, const EditorView& view = {});
195
198 [[nodiscard]] std::optional<std::size_t> hovered() const noexcept;
199
202 [[nodiscard]] std::optional<std::size_t> dragged() const noexcept;
203
206 [[nodiscard]] bool changed() const noexcept;
207
209 void mark_saved() noexcept;
210
211private:
212 [[nodiscard]] std::vector<DraftEnemy>& wave_enemies();
213 [[nodiscard]] std::optional<std::size_t> enemy_at(aegis2d::Vec2 level) const;
214
215 LevelDraft draft_;
216 std::string type_;
217 std::size_t wave_ = 0;
218 std::optional<std::size_t> hovered_;
219 std::optional<std::size_t> dragged_;
220 aegis2d::Vec2 grab_{};
221 // A wave dragged on the timeline: where the mouse took it, its delay then, and the scale
222 // of the timeline then, so that it does not change under the mouse.
223 std::optional<float> timeline_grab_;
224 float grabbed_delay_ = 0.0F;
225 float seconds_per_pixel_ = 0.0F;
226 bool changed_ = false;
227};
228
229} // namespace rtype
Places and moves the enemies of a wave with the mouse, and the waves in time.
Definition level_editor.hpp:140
std::optional< std::size_t > dragged() const noexcept
The enemy following the mouse.
std::size_t wave() const noexcept
Index of the wave edited.
bool dragging_wave() const noexcept
Tells whether the wave edited follows the mouse on the timeline.
const LevelDraft & draft() const noexcept
The waves edited.
void mark_saved() noexcept
Tells the editor that its waves were saved: changed() is false again.
std::vector< WaveSpan > timeline() const
When each wave plays, as the server plays them: a wave starts its delay after the last enemy of the w...
void next_wave() noexcept
Edits the next wave, or the first after the last.
static constexpr float pick_radius
Distance from an enemy's position within which the mouse is over it.
Definition level_editor.hpp:143
void move_wave(bool later)
Moves the wave edited before or after its neighbour, which takes its place; the wave stays the one ed...
std::optional< std::size_t > hovered() const noexcept
The enemy under the mouse, at the last update().
void shift_delay(float seconds)
Moves the wave edited in time: changes its delay, and so moves the waves after.
bool changed() const noexcept
Tells whether the waves changed since the editor opened or they were saved.
LevelEditor(LevelDraft draft, std::string type="bydos_slave")
Edits a draft, from its first wave.
void update(const EditorPointer &pointer, const EditorView &view={})
Applies the mouse of a frame.
void previous_wave() noexcept
Edits the wave before, or the last before the first.
std::span< const DraftEnemy > enemies() const noexcept
The enemies of the wave edited.
void add_wave()
Adds an empty wave after the one edited, 2 seconds after it, and edits it.
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
An enemy of a wave, where and when the editor places it.
Definition level_editor.hpp:18
aegis2d::Vec2 position
Where it appears, in logical pixels of the play area.
Definition level_editor.hpp:22
std::string type
Its type, as a waves file names it.
Definition level_editor.hpp:20
float at
Seconds after the start of its wave.
Definition level_editor.hpp:24
bool operator==(const DraftEnemy &) const =default
Field-by-field equality.
A wave of the editor.
Definition level_editor.hpp:32
std::vector< DraftEnemy > enemies
Its enemies, in the order they were placed.
Definition level_editor.hpp:36
float delay
Seconds between the last enemy of the wave before and the start of this one.
Definition level_editor.hpp:34
The mouse of a frame, in logical pixels of the screen.
Definition level_editor.hpp:78
bool pressed
The left button went down: place an enemy, or take the one under the mouse.
Definition level_editor.hpp:82
bool remove
The right button went down: remove the enemy under the mouse.
Definition level_editor.hpp:86
bool held
The left button is down: the enemy taken follows the mouse.
Definition level_editor.hpp:84
aegis2d::Vec2 position
Where it is.
Definition level_editor.hpp:80
Where the editor draws the play area, shrunk to show the strip beyond its right edge where the enemie...
Definition level_editor.hpp:99
float scale
Size of a logical pixel of the play area on screen.
Definition level_editor.hpp:103
aegis2d::Vec2 timeline_size
Size of the timeline on screen.
Definition level_editor.hpp:109
bool on_timeline(aegis2d::Vec2 screen) const noexcept
Tells whether a point of the screen is on the timeline.
static float timeline_seconds(float end) noexcept
Seconds of the waves the timeline shows across its width.
aegis2d::Vec2 to_level(aegis2d::Vec2 screen) const noexcept
The point of the play area under a point of the screen.
aegis2d::Vec2 to_screen(aegis2d::Vec2 level) const noexcept
A point of the play area on screen.
static constexpr float strip
Width of the strip beyond the right edge of the play area that is shown.
Definition level_editor.hpp:101
aegis2d::Vec2 origin
Where the top-left corner of the play area is on screen.
Definition level_editor.hpp:105
aegis2d::Vec2 timeline
Where the timeline of the waves is on screen: its top-left corner.
Definition level_editor.hpp:107
A waves file opened in the editor.
Definition level_editor.hpp:42
std::vector< DraftWave > waves
The waves, in order.
Definition level_editor.hpp:46
bool loop
Whether the waves play again from the first after the last.
Definition level_editor.hpp:44
aegis2d::ecs::WaveSchedule schedule(std::size_t first_wave=0) const
The draft as the server plays it, from one of its waves on, for a playtest.
std::string to_json(std::size_t first_wave=0) const
The draft as a waves file, from one of its waves on.
static LevelDraft from_file(const std::filesystem::path &path)
Reads a waves file, as the server does (load_waves()).
void save(const std::filesystem::path &path) const
Writes the draft to a waves file, once the server would read it.
When a wave plays, in seconds from the start of the waves.
Definition level_editor.hpp:90
float start
When its delay is over: its first enemies may appear.
Definition level_editor.hpp:92
float end
When its last enemy appears.
Definition level_editor.hpp:94