Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ECS/include/components/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ namespace comp {
};

using sound = sf::Sound;

struct volume_display {};
}

void draw_system(registry const &,
Expand Down Expand Up @@ -212,4 +214,13 @@ void text_field_system(registry &,
sparse_array<comp::position> const &,
sparse_array<comp::text_field> &,
sparse_array<comp::window> &,
sparse_array<comp::enabled> const &);
sparse_array<comp::enabled> const &);

void sound_system(registry &,
sparse_array<comp::sound> &,
sparse_array<comp::enabled> const &);

void volume_display_system(registry const &,
sparse_array<comp::text> &,
sparse_array<comp::volume_display> const &,
sparse_array<comp::enabled> const &);
1 change: 1 addition & 0 deletions client/include/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct KeyBindings {
};

extern KeyBindings g_key_bindings;
extern float g_audio_volume;
namespace comp {
struct player {};

Expand Down
123 changes: 122 additions & 1 deletion client/src/create_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

static assets _assets;
KeyBindings g_key_bindings;
float g_audio_volume = 50.0f;

template <typename... Extra>
static auto add(Extra &&...extra, const auto &&f) {
Expand Down Expand Up @@ -79,7 +80,7 @@ namespace comp {

auto &s = reg.get_components<sound>()[enti];
s->setPlayingOffset(sf::seconds(.15f));
s->setVolume(25.0f);
s->setVolume(g_audio_volume);
s->play();
}

Expand Down Expand Up @@ -324,6 +325,14 @@ namespace comp {
for (auto &&[button, _]: zipper(buttons, settingsButtons)) button.visible = !button.visible;
}

void volume_up(registry &, window &, size_t &) {
g_audio_volume = std::min(100.0f, g_audio_volume + 10.0f);
}

void volume_down(registry &, window &, size_t &) {
g_audio_volume = std::max(0.0f, g_audio_volume - 10.0f);
}

void set_bg(registry &reg, window &MainWindow) {
sprite bg(_assets.safe_get<sf::Texture>("assets/background.png"), {1134, 207});
bg.setScale({5.2f, 5.2f});
Expand Down Expand Up @@ -408,6 +417,19 @@ namespace comp {
enabled()
);

text upLabel("UP:", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 20, {0, -30});
upLabel.setOutlineColor({255, 255, 255});
upLabel.visible = false;

entity upLabelEntity(reg.spawn_entity());
reg.add_components(upLabelEntity,
position(140.f, 120.f),
Mainwindow,
upLabel,
settingsButton(),
enabled()
);

RectButton bindUpBtn = createDefaultButton("assets/arial_bold.ttf",
get_key_name(g_key_bindings.keys[MOVE_UP]), {80.f, 50.f}, {140.f, 120.f});
entity bindUpEntity(reg.spawn_entity());
Expand All @@ -419,6 +441,19 @@ namespace comp {
enabled()
);

text downLabel("DOWN:", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 20, {0, -30});
downLabel.setOutlineColor({255, 255, 255});
downLabel.visible = false;

entity downLabelEntity(reg.spawn_entity());
reg.add_components(downLabelEntity,
position(230.f, 120.f),
Mainwindow,
downLabel,
settingsButton(),
enabled()
);

RectButton bindDownBtn = createDefaultButton("assets/arial_bold.ttf",
get_key_name(g_key_bindings.keys[MOVE_DOWN]), {80.f, 50.f}, {230.f, 120.f});
entity bindDownEntity(reg.spawn_entity());
Expand All @@ -430,6 +465,19 @@ namespace comp {
enabled()
);

text leftLabel("LEFT:", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 20, {0, -30});
leftLabel.setOutlineColor({255, 255, 255});
leftLabel.visible = false;

entity leftLabelEntity(reg.spawn_entity());
reg.add_components(leftLabelEntity,
position(320.f, 120.f),
Mainwindow,
leftLabel,
settingsButton(),
enabled()
);

RectButton bindLeftBtn = createDefaultButton("assets/arial_bold.ttf",
get_key_name(g_key_bindings.keys[MOVE_LEFT]), {80.f, 50.f}, {320.f, 120.f});
entity bindLeftEntity(reg.spawn_entity());
Expand All @@ -441,6 +489,19 @@ namespace comp {
enabled()
);

text rightLabel("RIGHT:", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 20, {0, -30});
rightLabel.setOutlineColor({255, 255, 255});
rightLabel.visible = false;

entity rightLabelEntity(reg.spawn_entity());
reg.add_components(rightLabelEntity,
position(410.f, 120.f),
Mainwindow,
rightLabel,
settingsButton(),
enabled()
);

RectButton bindRightBtn = createDefaultButton("assets/arial_bold.ttf",
get_key_name(g_key_bindings.keys[MOVE_RIGHT]), {80.f, 50.f}, {410.f, 120.f});
entity bindRightEntity(reg.spawn_entity());
Expand All @@ -452,6 +513,19 @@ namespace comp {
enabled()
);

text shootLabel("SHOOT:", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 20, {0, -30});
shootLabel.setOutlineColor({255, 255, 255});
shootLabel.visible = false;

entity shootLabelEntity(reg.spawn_entity());
reg.add_components(shootLabelEntity,
position(500.f, 120.f),
Mainwindow,
shootLabel,
settingsButton(),
enabled()
);

RectButton bindShootBtn = createDefaultButton("assets/arial_bold.ttf",
get_key_name(g_key_bindings.keys[SHOOT]), {80.f, 50.f}, {500.f, 120.f});
entity bindShootEntity(reg.spawn_entity());
Expand All @@ -463,6 +537,53 @@ namespace comp {
enabled()
);

text volumeLabel("Volume:", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 24, {0, 0});
volumeLabel.setOutlineColor({255, 255, 255});
volumeLabel.visible = false;

entity volumeLabelEntity(reg.spawn_entity());
reg.add_components(volumeLabelEntity,
position(60.f, 200.f),
Mainwindow,
volumeLabel,
settingsButton(),
enabled()
);

text volumeDisplayText("50", _assets.safe_get<sf::Font>("assets/arial_bold.ttf"), 24, {0, 0});
volumeDisplayText.setOutlineColor({255, 255, 255});
volumeDisplayText.visible = false;

entity volumeDisplayEntity(reg.spawn_entity());
reg.add_components(volumeDisplayEntity,
position(200.f, 200.f),
Mainwindow,
volumeDisplayText,
volume_display(),
settingsButton(),
enabled()
);

RectButton volumeDownBtn = createDefaultButton("assets/arial_bold.ttf", "-", {50.f, 50.f}, {250.f, 200.f});
entity volumeDownEntity(reg.spawn_entity());
reg.add_components(volumeDownEntity,
position(250.f, 200.f),
Mainwindow,
button(volumeDownBtn, volume_down, false),
settingsButton(),
enabled()
);

RectButton volumeUpBtn = createDefaultButton("assets/arial_bold.ttf", "+", {50.f, 50.f}, {310.f, 200.f});
entity volumeUpEntity(reg.spawn_entity());
reg.add_components(volumeUpEntity,
position(310.f, 200.f),
Mainwindow,
button(volumeUpBtn, volume_up, false),
settingsButton(),
enabled()
);

entity rebindState(reg.spawn_entity());
reg.add_components(rebindState,
rebinding_state(),
Expand Down
6 changes: 6 additions & 0 deletions client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ namespace comp {
reg.register_component<sound>();
reg.register_component<settingsButton>();
reg.register_component<rebinding_state>();
reg.register_component<volume_display>();

reg.safe_add_system<position, velocity, network, enabled>(sync_position_system);
reg.safe_add_system<clock, enabled>(clock_system);
Expand All @@ -351,6 +352,7 @@ namespace comp {
reg.safe_add_system<position, velocity, enabled>(position_system);
reg.safe_add_system<window, sprite, position, enabled>(refresh_bg);
reg.safe_add_system<text, score, enabled>(update_score);
reg.safe_add_system<sound, enabled>(sound_system);
reg.register_component<animation>();

new_window(reg, scoreValue, Mainwindow);
Expand Down Expand Up @@ -378,23 +380,27 @@ namespace comp {
reg.register_component<settingsButton>();
reg.register_component<player_count>();
reg.register_component<rebinding_state>();
reg.register_component<volume_display>();

reg.safe_add_system<position, drawable, window, enabled>(draw_system);
reg.safe_add_system<position, sprite, window, enabled>(sprite_system);
reg.safe_add_system<position, button, window, enabled>(button_system);
reg.safe_add_system<position, text, window, enabled>(text_system);
reg.safe_add_system<text, volume_display, enabled>(volume_display_system);

waiting_setup_entity(reg, MainWindow, nb_player);
}

void setup_settings(registry &reg, window &MainWindow) {
reg.register_component<settingsButton>();
reg.register_component<rebinding_state>();
reg.register_component<volume_display>();

reg.safe_add_system<position, drawable, window, enabled>(draw_system);
reg.safe_add_system<position, sprite, window, enabled>(sprite_system);
reg.safe_add_system<position, button, window, enabled>(button_system);
reg.safe_add_system<position, text, window, enabled>(text_system);
reg.safe_add_system<text, volume_display, enabled>(volume_display_system);

setup_blur(reg, MainWindow);
setup_settings_button(reg, MainWindow);
Expand Down
17 changes: 17 additions & 0 deletions client/src/systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ void refresh_bg(registry const &,
bg.draw(pos.x + bg.size.x * scale, pos.y, window);
}
}

void sound_system(registry &,
sparse_array<comp::sound> &sounds,
sparse_array<comp::enabled> const &enabled) {
for (auto &&[sound, _]: zipper(sounds, enabled)) {
sound.setVolume(g_audio_volume);
}
}

void volume_display_system(registry const &,
sparse_array<comp::text> &texts,
sparse_array<comp::volume_display> const &volume_displays,
sparse_array<comp::enabled> const &enabled) {
for (auto &&[text, _, __]: zipper(texts, volume_displays, enabled)) {
text.setString(std::to_string((int)g_audio_volume));
}
}