diff --git a/SimpleGame/src/CMakeLists.txt b/SimpleGame/src/CMakeLists.txt index bdb1e10..5117e0c 100644 --- a/SimpleGame/src/CMakeLists.txt +++ b/SimpleGame/src/CMakeLists.txt @@ -18,7 +18,8 @@ else() endif() add_executable(simpleGame - Source/Game.cpp Include/Game.hpp Source/Main.cpp + Source/Game.cpp Include/Game.hpp Include/GameData.hpp + Source/Main.cpp Source/AudioEmitter.cpp Include/AudioEmitter.hpp Source/NoteSprite.cpp Include/NoteSprite.hpp Source/NoteTile.cpp Include/NoteTile.hpp Include/NotePlaceEnum.hpp diff --git a/SimpleGame/src/Include/Game.hpp b/SimpleGame/src/Include/Game.hpp index 8e341d4..7faedba 100644 --- a/SimpleGame/src/Include/Game.hpp +++ b/SimpleGame/src/Include/Game.hpp @@ -1,9 +1,12 @@ #pragma once +#include +#include #ifndef BOOK_GAME_HPP #define BOOK_GAME_HPP #include "AudioEmitter.hpp" +#include #include class Game { @@ -15,6 +18,7 @@ public: private: void processEvents(AudioEmitter &audioEmitter); + void start(); void update(sf::Time elapsedTime); void render(); @@ -22,12 +26,17 @@ private: static const sf::Time TimePerFrame; - sf::RenderWindow mWindow{sf::VideoMode({640, 480}), "SFML Application"}; + sf::RenderWindow mWindow{sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), + "SFML Application"}; sf::Texture mTexture; sf::Font mFont; sf::Text mStatisticsText{mFont}; sf::Time mStatisticsUpdateTime; + sf::RectangleShape leftPressZone; + sf::RectangleShape middlePressZone; + sf::RectangleShape rightPressZone; + std::size_t mStatisticsNumFrames{0}; }; diff --git a/SimpleGame/src/Include/GameData.hpp b/SimpleGame/src/Include/GameData.hpp new file mode 100644 index 0000000..9a241b0 --- /dev/null +++ b/SimpleGame/src/Include/GameData.hpp @@ -0,0 +1,12 @@ + +#pragma once + +static constexpr unsigned int SCREEN_WIDTH = 1080; +static constexpr unsigned int SCREEN_HEIGHT = 720; +static constexpr unsigned int NOTE_PRESS_HEIGHT = SCREEN_HEIGHT * 0.9; + +static constexpr unsigned int MIDDLE_WIDTH = SCREEN_WIDTH / 2; +static constexpr unsigned int MIDDLE_HEIGHT = SCREEN_HEIGHT / 2; + +static constexpr unsigned int NOTE_PLACE_X_POS[3] = { + SCREEN_WIDTH * 3 / 10, SCREEN_WIDTH * 5 / 10, SCREEN_WIDTH * 7 / 10}; diff --git a/SimpleGame/src/Source/Game.cpp b/SimpleGame/src/Source/Game.cpp index b7cc4d3..720a85a 100644 --- a/SimpleGame/src/Source/Game.cpp +++ b/SimpleGame/src/Source/Game.cpp @@ -1,9 +1,13 @@ #include "Game.hpp" #include "AudioEmitter.hpp" +#include "GameData.hpp" #include "NoteSprite.hpp" #include "NoteTile.hpp" #include "TilePattern.hpp" #include +#include +#include +#include #include #include #include @@ -14,6 +18,21 @@ Game::Game() { assert(mFont.openFromFile("media/Sansation.ttf")); mStatisticsText.setPosition({5.f, 5.f}); mStatisticsText.setCharacterSize(10); + + leftPressZone = sf::RectangleShape(sf::Vector2f(50, 10)); + middlePressZone = sf::RectangleShape(sf::Vector2f(50, 10)); + rightPressZone = sf::RectangleShape(sf::Vector2f(50, 10)); + + leftPressZone.setPosition( + sf::Vector2f(NOTE_PLACE_X_POS[0], NOTE_PRESS_HEIGHT)); + middlePressZone.setPosition( + sf::Vector2f(NOTE_PLACE_X_POS[1], NOTE_PRESS_HEIGHT)); + rightPressZone.setPosition( + sf::Vector2f(NOTE_PLACE_X_POS[2], NOTE_PRESS_HEIGHT)); + + leftPressZone.setFillColor(sf::Color::Black); + middlePressZone.setFillColor(sf::Color::Black); + rightPressZone.setFillColor(sf::Color::Black); } void generateTilePatternAndMusic(AudioEmitter &audio_emitter) { @@ -38,6 +57,7 @@ void Game::run() { AudioEmitter audioEmitter = AudioEmitter(); generateTilePatternAndMusic(audioEmitter); bool generated = false; + mWindow.setFramerateLimit(60); while (mWindow.isOpen()) { @@ -55,6 +75,21 @@ void Game::processEvents(AudioEmitter &audioEmitter) { if (event->is()) { audioEmitter.audioEnd(); mWindow.close(); + break; + } + if (const auto *keyPressed = event->getIf()) { + sf::Keyboard::Scancode key_scan_code = keyPressed->scancode; + switch (key_scan_code) { + case sf::Keyboard::Scancode::Left: + NoteTile::checkPress(audioEmitter.getTime(), NotePlaceEnum::Left); + break; + case sf::Keyboard::Scancode::Up: + NoteTile::checkPress(audioEmitter.getTime(), NotePlaceEnum::Middle); + break; + case sf::Keyboard::Scancode::Right: + NoteTile::checkPress(audioEmitter.getTime(), NotePlaceEnum::Right); + break; + } } } } @@ -63,6 +98,9 @@ void Game::update(const sf::Time elapsedTime) {} void Game::render() { mWindow.clear(sf::Color::Yellow); + mWindow.draw(leftPressZone); + mWindow.draw(middlePressZone); + mWindow.draw(rightPressZone); NoteTile::update(1.0 / 60, mWindow); mWindow.draw(mStatisticsText); mWindow.display(); diff --git a/SimpleGame/src/Source/NoteTile.cpp b/SimpleGame/src/Source/NoteTile.cpp index f376b1d..d67505c 100644 --- a/SimpleGame/src/Source/NoteTile.cpp +++ b/SimpleGame/src/Source/NoteTile.cpp @@ -1,10 +1,10 @@ #include "NoteTile.hpp" +#include "GameData.hpp" #include "NoteSprite.hpp" #include #include #include -const int NotePlaceXPos[3] = {200, 400, 600}; std::vector> NoteTile::existing_tiles{}; // private NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place, @@ -13,9 +13,9 @@ NoteTile::NoteTile(float play_time, float good_interval, NotePlaceEnum place, note_sprite() { // TODO do real note_sprite init, make it fall at good speed far enough... note_sprite.fall_speed = 100; - note_sprite.sprite.setPosition( - sf::Vector2f(NotePlaceXPos[place], - 480 - note_sprite.fall_speed * (play_time - current_time))); + note_sprite.sprite.setPosition(sf::Vector2f( + NOTE_PLACE_X_POS[place], + NOTE_PRESS_HEIGHT - note_sprite.fall_speed * (play_time - current_time))); } // public @@ -27,18 +27,33 @@ void NoteTile::create(float play_time, float good_interval, NotePlaceEnum place, } bool NoteTile::checkPress(float press_time, NotePlaceEnum key_pressed) { - for (const auto ¬e_tile : NoteTile::existing_tiles) { + auto &tiles = NoteTile::existing_tiles; + for (auto it = tiles.begin(); it != tiles.end(); ++it) { + const auto ¬e_tile = *it; if (note_tile->play_time - note_tile->good_interval / 2 < press_time && note_tile->play_time + note_tile->good_interval / 2 > press_time && note_tile->place == key_pressed) { + printf("good touch \n"); + tiles.erase(it); return true; } } + printf("badd touch \n"); return false; } void NoteTile::update(float dtime, sf::RenderWindow &window) { - for (const auto ¬e_tile : NoteTile::existing_tiles) { - note_tile->note_sprite.update(dtime, window); + auto &tiles = NoteTile::existing_tiles; + + for (auto it = tiles.begin(); it != tiles.end();) { + auto ¬e_tile = *it; + + if (note_tile->note_sprite.sprite.getPosition().y > SCREEN_HEIGHT) { + printf("missed tile \n"); + it = tiles.erase(it); + } else { + note_tile->note_sprite.update(dtime, window); + ++it; + } } } diff --git a/SimpleGame/src/Source/TilePattern.cpp b/SimpleGame/src/Source/TilePattern.cpp index abfd19d..a4382af 100644 --- a/SimpleGame/src/Source/TilePattern.cpp +++ b/SimpleGame/src/Source/TilePattern.cpp @@ -7,11 +7,13 @@ void generateTilePattern(std::vector> new_notes, const AudioEmitter &audio_emitter) { - int i = 0; + int i = -1; for (auto note : new_notes) { + i++; + if (i % 4 != 0) // skip les 3/4 des notes (je suis trop nul sinon) + continue; float start_time = note.first; NotePlaceEnum notePlace = static_cast(i % 3); - NoteTile::create(start_time, 1.0, notePlace, audio_emitter.getTime()); - i++; + NoteTile::create(start_time, 0.5, notePlace, audio_emitter.getTime()); } }