Skip to content

Commit

Permalink
merge: Merge pull request #44 from G-Epitech/43-add-snake-music
Browse files Browse the repository at this point in the history
feat: add snake sounds
  • Loading branch information
Yann-Masson authored Apr 6, 2024
2 parents 432ba84 + d5ab746 commit ecce8fd
Show file tree
Hide file tree
Showing 23 changed files with 428 additions and 27 deletions.
Binary file added assets/menu/sounds/music.wav
Binary file not shown.
Binary file added assets/snake/death.wav
Binary file not shown.
Binary file added assets/snake/down.wav
Binary file not shown.
Binary file added assets/snake/eat.wav
Binary file not shown.
Binary file added assets/snake/effects.wav
Binary file not shown.
Binary file added assets/snake/left.wav
Binary file not shown.
Binary file added assets/snake/right.wav
Binary file not shown.
Binary file added assets/snake/up.wav
Binary file not shown.
14 changes: 14 additions & 0 deletions core/src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ void Core::_changeGameProvider(const unsigned char &index)
}
auto newProvider = this->_getGameProvider(index);
this->_gameProvider = newProvider;
this->_textures.clear();
this->_fonts.clear();
this->_sounds.clear();
this->_initGame();
this->_initWindow();
}
Expand Down Expand Up @@ -376,9 +379,18 @@ void Core::_handleMouseMove(std::shared_ptr<events::IMouseEvent> &event, std::sh
component->onMouseHover(this->_game);
}

void Core::_stopAllGraphicsSounds()
{
for (auto &sound : this->_sounds) {
sound.second.sound->setState(ISound::SoundState::PAUSE);
sound.second.previousGameState = components::PAUSE;
}
}

void Core::_handleWindowClose()
{
if (this->_window && this->_window->isOpen()) {
this->_stopAllGraphicsSounds();
this->_window->close();
this->_menu.updateScore(this->_game);
}
Expand Down Expand Up @@ -478,11 +490,13 @@ void Core::_handleSoundComponent(std::shared_ptr<components::ISoundComponent> &g
gameSound->onStateChange(this->_game, components::PAUSE);
if (graphicSoundState == ISound::SoundState::STOP)
gameSound->onStateChange(this->_game, components::STOP);
sound.previousGraphicState = graphicSoundState;
}
if (gameSoundVolume != graphicSoundVolume)
sound.sound->setVolume(gameSoundVolume);
if (gameSoundLoop != graphicSoundLoop)
sound.sound->setLoopState(gameSoundLoop);
this->_sounds[gameSoundPath] = sound;
}

void Core::_handleComponentEvents(std::vector<events::EventPtr> &events, std::shared_ptr<components::IComponent> &component)
Expand Down
6 changes: 6 additions & 0 deletions core/src/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ class Core {
*/
void _handleWindowClose();

/**
* @brief Stop all sounds
*
*/
void _stopAllGraphicsSounds();

/**
* @brief Handle the window resize event
*
Expand Down
70 changes: 54 additions & 16 deletions core/src/menu/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Menu::Menu(GameProviders &gameProviders, GraphicsProviders &graphicsProviders,
this->_score.score = 0;
this->_readScores();
this->_textType = GAME;
this->_music = nullptr;
}

Menu::~Menu()
Expand Down Expand Up @@ -148,11 +149,16 @@ void Menu::_initNoGameFound()

void Menu::_initCheckBoxesGames()
{
auto texture = this->_graphicsProvider->createTexture("assets/menu/img/space-invader.png", "assets/menu/img/space-invader.ascii");
std::shared_ptr<shared::graphics::ITexture> texture = nullptr;
auto font = this->_font;
float index = 9.0;
int count = 0;

try {
texture = this->_graphicsProvider->createTexture("assets/menu/img/space-invader.png", "assets/menu/img/space-invader.ascii");
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
for (auto gameProvider : this->_gameProviders) {
if (count > 5)
break;
Expand Down Expand Up @@ -206,11 +212,16 @@ void Menu::_initHiddenGraphics(const GraphicsManifest &graphicsManifest, std::sh

void Menu::_initCheckBoxesGraphics()
{
auto texture = this->_graphicsProvider->createTexture("assets/menu/img/space-invader.png", "assets/menu/img/space-invader.ascii");
std::shared_ptr<shared::graphics::ITexture> texture = nullptr;
auto font = this->_font;
float index = 17.0;
int count = 0;

try {
texture = this->_graphicsProvider->createTexture("assets/menu/img/space-invader.png", "assets/menu/img/space-invader.ascii");
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
for (auto graphicsProvider : this->_graphicsProviders) {
if (count > 5)
break;
Expand All @@ -230,12 +241,16 @@ void Menu::_initCheckBoxesGraphics()

void Menu::_initTextures()
{
auto backgroundTexture = this->_graphicsProvider->createTexture("assets/menu/img/background.png", "assets/menu/img/background.ascii");
this->_textures.push_back(std::make_shared<Texture>(backgroundTexture, Vector2f{12, 12}, Vector2u{0, 0}, Vector2u{50, 25}, Vector2f{0, 0}));
auto titleTexture = this->_graphicsProvider->createTexture("assets/menu/img/title.png", "assets/menu/img/title.ascii");
this->_textures.push_back(std::make_shared<Texture>(titleTexture, Vector2f{17, 17}, Vector2u{0, 0}, Vector2u{31, 5}, Vector2f{10, 1}));
auto middleTexture = this->_graphicsProvider->createTexture("assets/menu/img/middle.png", "assets/menu/img/middle.ascii");
this->_textures.push_back(std::make_shared<Texture>(middleTexture, Vector2f{12, 12}, Vector2u{0, 0}, Vector2u{1, 15}, Vector2f{24, 7}));
try {
auto backgroundTexture = this->_graphicsProvider->createTexture("assets/menu/img/background.png", "assets/menu/img/background.ascii");
this->_textures.push_back(std::make_shared<Texture>(backgroundTexture, Vector2f{12, 12}, Vector2u{0, 0}, Vector2u{50, 25}, Vector2f{0, 0}));
auto titleTexture = this->_graphicsProvider->createTexture("assets/menu/img/title.png", "assets/menu/img/title.ascii");
this->_textures.push_back(std::make_shared<Texture>(titleTexture, Vector2f{17, 17}, Vector2u{0, 0}, Vector2u{31, 5}, Vector2f{10, 1}));
auto middleTexture = this->_graphicsProvider->createTexture("assets/menu/img/middle.png", "assets/menu/img/middle.ascii");
this->_textures.push_back(std::make_shared<Texture>(middleTexture, Vector2f{12, 12}, Vector2u{0, 0}, Vector2u{1, 15}, Vector2f{24, 7}));
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
}

void Menu::_initTexts()
Expand Down Expand Up @@ -269,6 +284,14 @@ void Menu::_preventGraphicsProvider()
std::cout << "No graphics provider found, using default provider" << std::endl;
}

void Menu::_initMusic()
{
this->_music = this->_graphicsProvider->createSound("assets/menu/sounds/music.wav");
this->_music->setVolume(50);
this->_music->setLoopState(true);
this->_music->setState(ISound::SoundState::PLAY);
}

void Menu::_initWindow()
{
IWindow::WindowInitProps windowInitProps {
Expand All @@ -279,13 +302,18 @@ void Menu::_initWindow()
.icon = "assets/menu/img/icon.png"
};

this->_clearLists();
this->_preventGraphicsProvider();
this->_initTexts();
this->_initTextures();
this->_initCheckBoxesGames();
this->_initCheckBoxesGraphics();
this->_window = this->_graphicsProvider->createWindow(windowInitProps);
try {
this->_window = this->_graphicsProvider->createWindow(windowInitProps);
this->_clearLists();
this->_preventGraphicsProvider();
this->_initTexts();
this->_initTextures();
this->_initCheckBoxesGames();
this->_initCheckBoxesGraphics();
this->_initMusic();
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
}

void Menu::_handleSelectUpperCheckBox()
Expand Down Expand Up @@ -342,7 +370,7 @@ void Menu::_handleSelectLowerCheckBox()
this->_checkBoxType = GRAPHICS_CHECKBOX;
} else {
this->_gamesCheckBoxes.at(index + 1)->hover();
}
}
break;
}
}
Expand Down Expand Up @@ -512,6 +540,8 @@ void Menu::_handleEvents()

void Menu::_renderField()
{
if (!this->_font)
return;
if (this->_score.player.empty()) {
auto placeholder = std::make_shared<Text>(this->_font, 30, "Guest", TextAlign::CENTER, TextVerticalAlign::MIDDLE, Color{255, 255, 255, 255}, Vector2u{16, 1}, Vector2f{17, 23});
this->_nameField = placeholder;
Expand Down Expand Up @@ -560,6 +590,8 @@ void Menu::_render()
void Menu::_previousSelectedGame()
{
this->_checkBoxType = GAME_CHECKBOX;
if (this->_gamesCheckBoxes.empty())
return;
for (auto gameProvider : this->_gameProviders) {
if (this->_gameProvider == gameProvider.second) {
auto index = std::distance(this->_gameProviders.begin(), std::find(this->_gameProviders.begin(), this->_gameProviders.end(), gameProvider));
Expand All @@ -579,6 +611,8 @@ void Menu::_previousSelectedGame()

void Menu::_previousSelectedGraphics()
{
if (this->_graphicsCheckBoxes.empty())
return;
for (auto graphicsProvider : this->_graphicsProviders) {
if (this->_graphicsProvider == graphicsProvider.second) {
auto index = std::distance(this->_graphicsProviders.begin(), std::find(this->_graphicsProviders.begin(), this->_graphicsProviders.end(), graphicsProvider));
Expand Down Expand Up @@ -608,10 +642,14 @@ void Menu::run()
this->_initWindow();
this->_previousSelectedGame();
this->_previousSelectedGraphics();
if (!this->_window)
throw ArcadeError("Can't create window");
while (this->_window->isOpen()) {
this->_handleEvents();
this->_render();
}
if (this->_music)
this->_music->setState(ISound::SoundState::STOP);
}

void Menu::_readScores()
Expand Down
7 changes: 7 additions & 0 deletions core/src/menu/Menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Menu {
std::map<std::shared_ptr<CheckBox>, std::vector<std::shared_ptr<Text>>> _hiddenAuthors;
std::shared_ptr<IFont> _font;
std::shared_ptr<Text> _nameField;
std::shared_ptr<ISound> _music;

/**
* @brief Initialize the window
Expand Down Expand Up @@ -193,6 +194,12 @@ class Menu {
*/
void _initTextures();

/**
* @brief Initialize Background Music
*
*/
void _initMusic();

/**
* @brief Initialize hidden textures
*
Expand Down
6 changes: 5 additions & 1 deletion core/src/menu/entity/text/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ void Text::draw(std::shared_ptr<IWindow> window)
.position = this->_position
};

window->render(textProps);
try {
window->render(textProps);
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
}

void Text::setColor(Color color)
Expand Down
6 changes: 5 additions & 1 deletion core/src/menu/entity/texture/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ void Texture::draw(std::shared_ptr<IWindow> window)
.position = this->_position
};

window->render(textureProps);
try {
window->render(textureProps);
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
}

void Texture::setOrigin(Vector2u origin)
Expand Down
2 changes: 2 additions & 0 deletions games/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ target_sources(${PROJECT_NAME} PUBLIC
components/CollidableComponent.cpp
components/TextureComponent.hpp
components/TextureComponent.cpp
components/SoundComponent.hpp
components/SoundComponent.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..)
50 changes: 50 additions & 0 deletions games/common/components/SoundComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
** EPITECH PROJECT, 2024
** arcade
** File description:
** SoundComponent
*/

#include "SoundComponent.hpp"

using namespace shared::games::components;
using namespace arcade::games::common::components;

SoundComponent::SoundComponent(shared::games::entity::IEntity &entity, const std::string &path,
shared::games::components::SoundState state, shared::games::components::SoundVolume volume, bool loop, onChangeFunction onChange)
: AComponent(SOUND, entity), _path(path), _state(state), _volume(volume), _loop(loop), _onChange(onChange)
{
this->_type = SOUND;
}

const std::string &SoundComponent::getPath() const noexcept
{
return this->_path;
}

SoundState &SoundComponent::getState() noexcept
{
return this->_state;
}

SoundVolume &SoundComponent::getVolume() noexcept
{
return this->_volume;
}

bool &SoundComponent::getLoop() noexcept
{
return this->_loop;
}

void SoundComponent::onStateChange(std::shared_ptr<shared::games::IGame> ctx,
shared::games::components::SoundState state)
{
if (this->_onChange)
this->_onChange(ctx, state);
}

void SoundComponent::setState(SoundState state)
{
this->_state = state;
}
78 changes: 78 additions & 0 deletions games/common/components/SoundComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
** EPITECH PROJECT, 2024
** arcade
** File description:
** SoundComponent
*/

#pragma once

#include "AComponent.hpp"
#include "shared/games/components/ISoundComponent.hpp"

namespace arcade::games::common::components
{
class SoundComponent;
}

class arcade::games::common::components::SoundComponent
: public virtual shared::games::components::ISoundComponent, public AComponent
{
public:
typedef void (*onChangeFunction)(std::shared_ptr<shared::games::IGame> ctx, shared::games::components::SoundState state);

~SoundComponent() override = default;

explicit SoundComponent(shared::games::entity::IEntity &entity, const std::string &path,
shared::games::components::SoundState state, shared::games::components::SoundVolume volume, bool loop, onChangeFunction onChange = nullptr);

/**
* @brief Get path of the sound
*
* @return Sound path
*/
const std::string &getPath() const noexcept override;

/**
* @brief Get state of the sound
*
* @return Sound state
*/
shared::games::components::SoundState &getState() noexcept override;

/**
* @brief Get volume of the sound
*
* @return Sound volume
*/
shared::games::components::SoundVolume &getVolume() noexcept override;

/**
* @brief Get loop of the sound
*
* @return Sound loop
*/
bool &getLoop() noexcept override;

/**
* @brief On state change event handler for the component
*
* @param ctx Context of the game
* @param state New state of the sound
*/
void onStateChange(std::shared_ptr<shared::games::IGame> ctx, shared::games::components::SoundState state) override;

/**
* @brief Set the state of the sound
*
* @param state New state of the sound
*/
void setState(shared::games::components::SoundState state);

protected:
std::string _path;
shared::games::components::SoundState _state;
shared::games::components::SoundVolume _volume;
onChangeFunction _onChange;
bool _loop;
};
Loading

0 comments on commit ecce8fd

Please sign in to comment.