From 5ec9165351cc766252fe1351dc1981c7568d851f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B2=D0=B4=D0=B5=D0=B5=D0=B2=20=D0=95=D0=B2=D0=B3?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= <evavdeev@edu.hse.ru> Date: Sun, 23 Mar 2025 14:44:35 +0300 Subject: [PATCH] fixed sound for ultimate video --- source/Builder/Builders.cpp | 4 ++-- source/Config/Config.h | 6 +++++ source/Context/Context.cpp | 4 ++-- source/Event/IGameEvent.cpp | 19 ++++++++-------- source/Game/IEntity.cpp | 4 ++-- source/Game/Pacman.cpp | 12 +++++----- source/State/States.cpp | 45 +++++++++++++++++++------------------ 7 files changed, 51 insertions(+), 43 deletions(-) diff --git a/source/Builder/Builders.cpp b/source/Builder/Builders.cpp index e9f8406..b1ac5c6 100644 --- a/source/Builder/Builders.cpp +++ b/source/Builder/Builders.cpp @@ -50,8 +50,8 @@ void CommonBuilder::create_context(float dynamic_object_ratio) { //располагаем еду for (size_t i = 0; i < buf.size(); ++i) { - size_t choise = rand() % 100; - if (choise >= 80) { + size_t choise = rand() % 1000; + if (choise >= config::POTION_RATE) { m_context.static_obj.emplace_back(std::move(std::make_unique<MPpotion>())); } else { diff --git a/source/Config/Config.h b/source/Config/Config.h index dbba3ec..653a96a 100644 --- a/source/Config/Config.h +++ b/source/Config/Config.h @@ -31,6 +31,12 @@ namespace config { const sf::Vector2f BAR_SIZE = { 200, 15 }; const float PACMAN_MAX_MP = 300; const float PACMAN_START_MP = 100; + const float FIREBALL_COST = 50; + const float ULTIMATE_COST = 300; + const float MP_REGEN = 1.0f/60; + const float POTION_MP = 25; + const float SPAWN_INV = 5; + const size_t POTION_RATE = 900; // Пакмэн: const float GAME_PACMAN_SIZE = ROOM_SIZE * 0.8; const sf::Keyboard::Key KEY_LEFT = sf::Keyboard::A; diff --git a/source/Context/Context.cpp b/source/Context/Context.cpp index 4083f28..de2adb7 100644 --- a/source/Context/Context.cpp +++ b/source/Context/Context.cpp @@ -14,7 +14,7 @@ GameContext GameContext::clone() { return context; } -//CONTEXTMANAGER + void ContextManager::reset(GameContext&& context) { while (!m_contexts.empty()) { m_contexts.pop(); @@ -35,5 +35,5 @@ GameContext& ContextManager::get_context() { } void ContextManager::save_context() { - m_contexts.emplace(m_contexts.top().clone()); //записываем клонированием текущий контекст в историю + m_contexts.emplace(m_contexts.top().clone()); } \ No newline at end of file diff --git a/source/Event/IGameEvent.cpp b/source/Event/IGameEvent.cpp index cec346e..a9d31dd 100644 --- a/source/Event/IGameEvent.cpp +++ b/source/Event/IGameEvent.cpp @@ -42,7 +42,7 @@ void Potion_take::handle(GameContext& context) const{ for (auto it = context.static_obj.begin(); it != context.static_obj.end(); ++it) { if (m_ptr_entity == (*it)) { context.static_obj.erase(it); - context.m_bar.regen_MP(25); + context.m_bar.regen_MP(config::POTION_MP); return; } } @@ -58,27 +58,28 @@ static void kill_all(GameContext& context) { } void Ultimate::handle(GameContext& context) const { - //m_sound.play(); - cv::VideoCapture cap(std::string(VIDEO_PATH) + "ultimate.mp4"); // Открываем файл + cv::VideoCapture cap(std::string(VIDEO_PATH) + "boom.mp4"); if (!cap.isOpened()) { std::cerr << "ERROR!!!!!!!!!!!!!!!!!\n" << std::endl; } + m_sound.play(); cv::Mat frame; - cv::namedWindow("Видео", cv::WINDOW_NORMAL); // Позволяет менять размер окна - cv::resizeWindow("Видео", 1200, 800); + cv::namedWindow("BOOM", cv::WINDOW_NORMAL); + cv::resizeWindow("BOOM", 1920, 1080); bool flag = true; while (true) { - cap >> frame; // Считываем кадр - if (frame.empty()) break; // Выход, если видео закончилось + cap >> frame; + if (frame.empty()) break; if (flag) { kill_all(context); } - cv::imshow("Видео", frame); - if (cv::waitKey(30) == 27) break; // Выход по ESC + cv::imshow("BOOM", frame); + if (cv::waitKey(60) == 27) break; } cap.release(); cv::destroyAllWindows(); + m_sound.resetBuffer(); } diff --git a/source/Game/IEntity.cpp b/source/Game/IEntity.cpp index 2d9791a..2257903 100644 --- a/source/Game/IEntity.cpp +++ b/source/Game/IEntity.cpp @@ -102,8 +102,8 @@ void MPpotion::draw_into(sf::RenderWindow& window) const { void MPpotion::prepare_for_drawing() { m_sprite.setTexture(m_ptr_tex->get_texture()); - m_sprite.setScale(0.01, 0.01); - m_sprite.setPosition(m_ptr_room->get_position() + sf::Vector2f(-10, -10)); + m_sprite.setScale(0.02, 0.02); + m_sprite.setPosition(m_ptr_room->get_position() + sf::Vector2f(-20, -20)); } std::unique_ptr<IStaticEntity> MPpotion::clone() const { diff --git a/source/Game/Pacman.cpp b/source/Game/Pacman.cpp index 1db0fff..ff1bb65 100644 --- a/source/Game/Pacman.cpp +++ b/source/Game/Pacman.cpp @@ -12,22 +12,22 @@ void Pacman::move(Direction direction) { } void Pacman::spell(){ - if (m_mp >= 50) { - m_mp -= 50; + if (m_mp >= config::FIREBALL_COST) { + m_mp -= config::FIREBALL_COST; m_spells.emplace_back(Fireball(m_dir, m_ptr_room)); } } std::unique_ptr<IGameEvent> Pacman::ultimate(){ - if (m_mp >= 300) { - m_mp -= 300; + if (m_mp >= config::ULTIMATE_COST) { + m_mp -= config::ULTIMATE_COST; return std::make_unique<Ultimate>(); } return {}; } void Pacman::draw_into(sf::RenderWindow& window) const { - m_mp += 1.0 / 60; + m_mp += config::MP_REGEN; if (m_mp > config::PACMAN_MAX_MP) { m_mp = config::PACMAN_MAX_MP; } @@ -64,7 +64,7 @@ std::unique_ptr<IGameEvent> Fireball::visit(Enemy* ptr_enemy){ } std::unique_ptr<IGameEvent> Pacman::visit(Enemy* ptr_enemy) { - if (ptr_enemy->get_location() != this->get_location() || m_clock.getElapsedTime().asSeconds() < 5) { + if (ptr_enemy->get_location() != this->get_location() || m_clock.getElapsedTime().asSeconds() < config::SPAWN_INV) { return {}; } return std::make_unique<LostGame>(); diff --git a/source/State/States.cpp b/source/State/States.cpp index 1c285d6..bdb2b01 100644 --- a/source/State/States.cpp +++ b/source/State/States.cpp @@ -32,21 +32,6 @@ void GameState::event_handling() { } m_context_manager.restore_previous_context(); } - if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) { - m_context_manager.save_context(); - if (m_context_manager.get_context().m_bar.m_cur_mp >= 50) { - m_context_manager.get_context().pacman.spell(); - m_context_manager.get_context().m_bar.use_MP(50); - } - } - if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::E) { - m_context_manager.save_context(); - if (m_context_manager.get_context().m_bar.m_cur_mp == 300) { - m_events.emplace_back(m_context_manager.get_context().pacman.ultimate()); - m_context_manager.get_context().m_bar.use_MP(300); - } - - } if (event.type == sf::Event::KeyPressed && m_context_manager.get_context().state == GameContext::INGAME) { process_key_pressed(event.key.code); } @@ -102,9 +87,9 @@ void GameState::update() { } for (int i = 0; i < m_events.size(); i++) { - m_events.at(i)->handle(m_context_manager.get_context()); + m_events.at(i)->handle(m_context_manager.get_context()); } - m_context_manager.get_context().m_bar.regen_MP(1.0f / 60); + m_context_manager.get_context().m_bar.regen_MP(config::MP_REGEN); m_events.clear(); } @@ -121,22 +106,24 @@ void GameState::render() { m_window.clear(config::GAME_COLOR_BACKGROUND_WIN); if (m_flag) { - cv::VideoCapture cap(std::string(VIDEO_PATH) + "WIN.mp4"); // Открываем файл + cv::VideoCapture cap(std::string(VIDEO_PATH) + "WIN.mp4"); if (!cap.isOpened()) { std::cerr << "ERROR!!!!!!!!!!!!!!!!!\n" << std::endl; } cv::Mat frame; + cv::namedWindow("WIN", cv::WINDOW_NORMAL); + cv::resizeWindow("WIN", 1920, 1080); while (true) { if (m_sound.getStatus() == 0) { m_sound.play(); } - cap >> frame; // Считываем кадр - if (frame.empty()) break; // Выход, если видео закончилось + cap >> frame; + if (frame.empty()) break; - cv::imshow("Видео", frame); - if (cv::waitKey(30) == 27) break; // Выход по ESC + cv::imshow("WIN", frame); + if (cv::waitKey(30) == 27) break; } cap.release(); @@ -181,6 +168,20 @@ void GameState::process_key_pressed(sf::Keyboard::Key code) { m_context_manager.save_context(); m_context_manager.get_context().pacman.move(Direction::RIGHT); break; + case sf::Keyboard::Space: + m_context_manager.save_context(); + if (m_context_manager.get_context().m_bar.m_cur_mp >= config::FIREBALL_COST) { + m_context_manager.get_context().pacman.spell(); + m_context_manager.get_context().m_bar.use_MP(config::FIREBALL_COST); + } + break; + case sf::Keyboard::E: + m_context_manager.save_context(); + if (m_context_manager.get_context().m_bar.m_cur_mp == config::PACMAN_MAX_MP) { + m_events.emplace_back(m_context_manager.get_context().pacman.ultimate()); + m_context_manager.get_context().m_bar.use_MP(config::PACMAN_MAX_MP); + } + break; } } -- GitLab