This commit is contained in:
Crizomb 2025-06-15 19:22:33 +02:00
parent adcd6146ed
commit f30a490593
8 changed files with 93 additions and 4 deletions

View file

@ -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

View file

@ -1,12 +1,13 @@
#pragma once
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/Vector2.hpp>
#include <vector>
class AnimatedSprite : public sf::Sprite {
private:
std::vector<sf::Texture> 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<std::string> &texture_paths);
void update(float dtime);
// draw will be same as sf::Sprite
void setTimeBeetwenFrames(float new_time) { time_beetwen_frames = new_time; };
};

View file

@ -0,0 +1,32 @@
#include "AnimatedSprite.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Vector2.hpp>
#include <string>
#include <vector>
enum CarrotState { Angry, Happy, Neutral };
class Carrot {
private:
AnimatedSprite happyAnimation;
AnimatedSprite neutralAnimation;
AnimatedSprite angryAnimation;
CarrotState carrotState;
public:
Carrot(const std::vector<std::string> &angry_paths,
const std::vector<std::string> &neutral_paths,
const std::vector<std::string> &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<std::string> ANGRY_PATHS = {
"media/sprites/carrot_angry_0.png", "media/sprites/carrot_angry_1.png"};
static std::vector<std::string> NEUTRAL_PATHS = {
"media/sprites/carrot_neutral_0.png", "media/sprites/carrot_neutral_1.png"};
static std::vector<std::string> HAPPY_PATHS = {
"media/sprites/carrot_happy_0.png", "media/sprites/carrot_happy_1.png"};

View file

@ -7,6 +7,7 @@
#define BOOK_GAME_HPP
#include "AudioEmitter.hpp"
#include <Carrot.hpp>
#include <GameData.hpp>
#include <SFML/Graphics.hpp>
@ -47,6 +48,7 @@ private:
int score_multiplier = 1; // number of good press without misses
sf::Sprite backGround;
Carrot carrot;
};
#endif // BOOK_GAME_HPP

View file

@ -1,6 +1,7 @@
#pragma once
#include <SFML/System/Vector2.hpp>
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;

View file

@ -18,7 +18,7 @@ AnimatedSprite::AnimatedSprite(const std::vector<std::string> &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) {

View file

@ -0,0 +1,39 @@
#include "Carrot.hpp"
#include <SFML/System/Vector2.hpp>
Carrot::Carrot(const std::vector<std::string> &angry_paths,
const std::vector<std::string> &neutral_paths,
const std::vector<std::string> &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; };

View file

@ -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);