From f30a490593673def66dc7d9828b75a63c2a0cebf Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 15 Jun 2025 19:22:33 +0200 Subject: [PATCH] CARROTE --- SimpleGame/src/CMakeLists.txt | 1 + SimpleGame/src/Include/AnimatedSprite.hpp | 4 ++- SimpleGame/src/Include/Carrot.hpp | 32 +++++++++++++++++++ SimpleGame/src/Include/Game.hpp | 2 ++ SimpleGame/src/Include/GameData.hpp | 7 ++++ SimpleGame/src/Source/AnimatedSprite.cpp | 2 +- SimpleGame/src/Source/Carrot.cpp | 39 +++++++++++++++++++++++ SimpleGame/src/Source/Game.cpp | 10 ++++-- 8 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 SimpleGame/src/Include/Carrot.hpp create mode 100644 SimpleGame/src/Source/Carrot.cpp diff --git a/SimpleGame/src/CMakeLists.txt b/SimpleGame/src/CMakeLists.txt index 1433ce2..1ab937c 100644 --- a/SimpleGame/src/CMakeLists.txt +++ b/SimpleGame/src/CMakeLists.txt @@ -26,6 +26,7 @@ add_executable(simpleGame Source/NoteTile.cpp Include/NoteTile.hpp Include/NotePlaceEnum.hpp Source/TilePattern.cpp Include/TilePattern.hpp Source/AnimatedSprite.cpp Include/AnimatedSprite.hpp + Source/Carrot.cpp Include/Carrot.hpp ) target_include_directories(simpleGame PRIVATE diff --git a/SimpleGame/src/Include/AnimatedSprite.hpp b/SimpleGame/src/Include/AnimatedSprite.hpp index 44c6832..b3a0d3d 100644 --- a/SimpleGame/src/Include/AnimatedSprite.hpp +++ b/SimpleGame/src/Include/AnimatedSprite.hpp @@ -1,12 +1,13 @@ #pragma once #include #include +#include #include class AnimatedSprite : public sf::Sprite { private: std::vector frames_textures; - float time_beetwen_frames = 0.5; + float time_beetwen_frames = 0.25; float time_without_change = 0.0; int current_frame = 0; @@ -14,4 +15,5 @@ public: AnimatedSprite(const std::vector &texture_paths); void update(float dtime); // draw will be same as sf::Sprite + void setTimeBeetwenFrames(float new_time) { time_beetwen_frames = new_time; }; }; diff --git a/SimpleGame/src/Include/Carrot.hpp b/SimpleGame/src/Include/Carrot.hpp new file mode 100644 index 0000000..04aec96 --- /dev/null +++ b/SimpleGame/src/Include/Carrot.hpp @@ -0,0 +1,32 @@ +#include "AnimatedSprite.hpp" +#include +#include +#include +#include + +enum CarrotState { Angry, Happy, Neutral }; + +class Carrot { +private: + AnimatedSprite happyAnimation; + AnimatedSprite neutralAnimation; + AnimatedSprite angryAnimation; + CarrotState carrotState; + +public: + Carrot(const std::vector &angry_paths, + const std::vector &neutral_paths, + const std::vector &happy_paths); + + void draw(sf::RenderWindow &RenderWindow, float dtime); + void setPosition(sf::Vector2f); + void setScale(sf::Vector2f); + void changeState(CarrotState new_state); +}; + +static std::vector ANGRY_PATHS = { + "media/sprites/carrot_angry_0.png", "media/sprites/carrot_angry_1.png"}; +static std::vector NEUTRAL_PATHS = { + "media/sprites/carrot_neutral_0.png", "media/sprites/carrot_neutral_1.png"}; +static std::vector HAPPY_PATHS = { + "media/sprites/carrot_happy_0.png", "media/sprites/carrot_happy_1.png"}; diff --git a/SimpleGame/src/Include/Game.hpp b/SimpleGame/src/Include/Game.hpp index b8f737b..251a561 100644 --- a/SimpleGame/src/Include/Game.hpp +++ b/SimpleGame/src/Include/Game.hpp @@ -7,6 +7,7 @@ #define BOOK_GAME_HPP #include "AudioEmitter.hpp" +#include #include #include @@ -47,6 +48,7 @@ private: int score_multiplier = 1; // number of good press without misses sf::Sprite backGround; + Carrot carrot; }; #endif // BOOK_GAME_HPP diff --git a/SimpleGame/src/Include/GameData.hpp b/SimpleGame/src/Include/GameData.hpp index d5166ff..0fba6e0 100644 --- a/SimpleGame/src/Include/GameData.hpp +++ b/SimpleGame/src/Include/GameData.hpp @@ -1,6 +1,7 @@ #pragma once +#include static constexpr unsigned int SCREEN_WIDTH = 1280; static constexpr unsigned int SCREEN_HEIGHT = 720; static constexpr unsigned int NOTE_PRESS_HEIGHT = SCREEN_HEIGHT * 0.8; @@ -12,9 +13,15 @@ static constexpr unsigned int NOTE_PLACE_X_POS[3] = { SCREEN_WIDTH * 6 / 10, SCREEN_WIDTH * 7 / 10, SCREEN_WIDTH * 8 / 10}; static constexpr unsigned int FLOWER_SIZE = SCREEN_WIDTH * 0.05; + +static const sf::Vector2f CARROT_POS = + sf::Vector2f(SCREEN_WIDTH * 9 / 20, SCREEN_HEIGHT * 0.4); + +// Texture2D seems big export I don't want to include it namespace sf { class Texture; }; + // created because sprite doesn't have default constructor // Not optimal but easy to use + initialized in GameData.cpp extern sf::Texture PLACE_HOLDER_TEXTURE; diff --git a/SimpleGame/src/Source/AnimatedSprite.cpp b/SimpleGame/src/Source/AnimatedSprite.cpp index c13fdda..2307e2c 100644 --- a/SimpleGame/src/Source/AnimatedSprite.cpp +++ b/SimpleGame/src/Source/AnimatedSprite.cpp @@ -18,7 +18,7 @@ AnimatedSprite::AnimatedSprite(const std::vector &texture_paths) } assert(frames_textures.size() > 0); // setTexture is frome Sprite - setTexture(frames_textures[0]); + setTexture(frames_textures[0], true); }; void AnimatedSprite::update(float dtime) { diff --git a/SimpleGame/src/Source/Carrot.cpp b/SimpleGame/src/Source/Carrot.cpp new file mode 100644 index 0000000..cc56c67 --- /dev/null +++ b/SimpleGame/src/Source/Carrot.cpp @@ -0,0 +1,39 @@ +#include "Carrot.hpp" +#include + +Carrot::Carrot(const std::vector &angry_paths, + const std::vector &neutral_paths, + const std::vector &happy_paths) + : angryAnimation(angry_paths), neutralAnimation(neutral_paths), + happyAnimation(happy_paths), carrotState(Neutral) {} + +void Carrot::draw(sf::RenderWindow &RenderWindow, float dtime) { + switch (carrotState) { + case Angry: + RenderWindow.draw(angryAnimation); + angryAnimation.update(dtime); + break; + case Neutral: + RenderWindow.draw(neutralAnimation); + neutralAnimation.update(dtime); + break; + case Happy: + RenderWindow.draw(happyAnimation); + happyAnimation.update(dtime); + break; + } +} + +void Carrot::setPosition(sf::Vector2f new_pos) { + angryAnimation.setPosition(new_pos); + neutralAnimation.setPosition(new_pos); + happyAnimation.setPosition(new_pos); +} + +void Carrot::setScale(sf::Vector2f new_scale) { + angryAnimation.setScale(new_scale); + neutralAnimation.setScale(new_scale); + happyAnimation.setScale(new_scale); +} + +void Carrot::changeState(CarrotState new_state) { carrotState = new_state; }; diff --git a/SimpleGame/src/Source/Game.cpp b/SimpleGame/src/Source/Game.cpp index 5586ba5..26ddce7 100644 --- a/SimpleGame/src/Source/Game.cpp +++ b/SimpleGame/src/Source/Game.cpp @@ -18,7 +18,9 @@ const sf::Time Game::TimePerFrame = sf::seconds(1.f / 60.f); sf::Texture bgTexture; -Game::Game() : backGround(PLACE_HOLDER_TEXTURE) { +Game::Game() + : backGround(PLACE_HOLDER_TEXTURE), + carrot(ANGRY_PATHS, NEUTRAL_PATHS, HAPPY_PATHS) { assert(mFont.openFromFile("media/Sansation.ttf")); mStatisticsText.setPosition({5.f, 5.f}); mStatisticsText.setCharacterSize(10); @@ -52,6 +54,9 @@ Game::Game() : backGround(PLACE_HOLDER_TEXTURE) { float sx = backGround.getTexture().getSize().x; float sy = backGround.getTexture().getSize().y; backGround.setScale(sf::Vector2f(SCREEN_WIDTH / sx, SCREEN_HEIGHT / sy)); + + carrot.setPosition(CARROT_POS); + carrot.setScale(sf::Vector2f(5, 5)); } void generateTilePatternAndMusic(AudioEmitter &audio_emitter) { @@ -124,7 +129,8 @@ void Game::render() { mWindow.draw(leftPressZone); mWindow.draw(middlePressZone); mWindow.draw(rightPressZone); - if (!NoteTile::update(1.0 / 60, mWindow)) + carrot.draw(mWindow, TimePerFrame.asSeconds()); + if (!NoteTile::update(TimePerFrame.asSeconds(), mWindow)) update_scores(false); mWindow.draw(mStatisticsText); mWindow.draw(ScoreText);