Commit 43dd0474 authored by Печенин Данила Михайлович's avatar Печенин Данила Михайлович
Browse files

Add textures to food

parent b1cf0133
No related merge requests found
Showing with 69 additions and 67 deletions
+69 -67
assets/enemy_blue.png

2.44 KB

assets/enemy_pink.png

2.4 KB

assets/enemy_red.png

3.06 KB

assets/enemy_yellow.png

2.83 KB

#include "Assets.h"
#include <array>
#include <random>
#include <Configuration.h>
sf::Font& Assets::GetFont() {
static sf::Font font;
......@@ -16,38 +19,42 @@ sf::Texture& Assets::GetPacmanTexture() {
return texture;
}
sf::Texture& Assets::GetEnemyRedTexture() {
static sf::Texture texture;
if (texture.getSize() != sf::Vector2u{0, 0})
return texture;
if (!texture.loadFromFile(config::ENEMY_RED_FILE))
throw std::runtime_error("Failed to load texture: enemy red");
return texture;
}
sf::Texture& Assets::GetEnemyPinkTexture() {
static sf::Texture texture;
if (texture.getSize() != sf::Vector2u{0, 0})
return texture;
if (!texture.loadFromFile(config::ENEMY_PINK_FILE))
throw std::runtime_error("Failed to load texture: enemy pink");
return texture;
sf::Texture& Assets::GetEnemyTexture() {
static std::array<sf::Texture, config::ENEMY_TEXTURES_NUMBER> textures;
if (textures[0].getSize() == sf::Vector2u{0, 0}) {
if (!textures[0].loadFromFile(config::ENEMY_RED_FILE))
throw std::runtime_error("Failed to load texture: enemy red");
if (!textures[1].loadFromFile(config::ENEMY_BLUE_FILE))
throw std::runtime_error("Failed to load texture: enemy blue");
if (!textures[2].loadFromFile(config::ENEMY_YELLOW_FILE))
throw std::runtime_error("Failed to load texture: enemy yellow");
if (!textures[3].loadFromFile(config::ENEMY_PINK_FILE))
throw std::runtime_error("Failed to load texture: enemy pink");
}
static std::mt19937 gen{std::random_device{}()};
static std::uniform_int_distribution<size_t> dist{0, config::ENEMY_TEXTURES_NUMBER-1};
return textures[dist(gen)];
}
sf::Texture& Assets::GetEnemyYellowTexture() {
static sf::Texture texture;
if (texture.getSize() != sf::Vector2u{0, 0})
return texture;
if (!texture.loadFromFile(config::ENEMY_YELLOW_FILE))
throw std::runtime_error("Failed to load texture: enemy yellow");
return texture;
}
sf::Texture& Assets::GetEnemyBlueTexture() {
static sf::Texture texture;
if (texture.getSize() != sf::Vector2u{0, 0})
return texture;
if (!texture.loadFromFile(config::ENEMY_BLUE_FILE))
throw std::runtime_error("Failed to load texture: enemy blue");
return texture;
sf::Texture& Assets::GetFoodTexture() {
static std::array<sf::Texture, config::FOOD_TEXTURES_NUMBER> textures;
if (textures[0].getSize() == sf::Vector2u{0, 0}) {
if (!textures[0].loadFromFile(config::FOOD_1))
throw std::runtime_error("Failed to load texture: food 1");
if (!textures[1].loadFromFile(config::FOOD_2))
throw std::runtime_error("Failed to load texture: food 2");
if (!textures[2].loadFromFile(config::FOOD_3))
throw std::runtime_error("Failed to load texture: food 3");
if (!textures[3].loadFromFile(config::FOOD_4))
throw std::runtime_error("Failed to load texture: food 4");
if (!textures[4].loadFromFile(config::FOOD_5))
throw std::runtime_error("Failed to load texture: food 5");
if (!textures[5].loadFromFile(config::FOOD_6))
throw std::runtime_error("Failed to load texture: food 6");
if (!textures[6].loadFromFile(config::FOOD_7))
throw std::runtime_error("Failed to load texture: food 7");
}
static std::mt19937 gen{std::random_device{}()};
static std::uniform_int_distribution<size_t> dist{0, config::FOOD_TEXTURES_NUMBER-1};
return textures[dist(gen)];
}
\ No newline at end of file
#pragma once
#include <Configuration.h>
#include <SFML/Graphics.hpp>
struct Assets final {
Assets() = delete;
static sf::Font& GetFont();
static sf::Texture& GetPacmanTexture();
static sf::Texture& GetEnemyRedTexture();
static sf::Texture& GetEnemyYellowTexture();
static sf::Texture& GetEnemyBlueTexture();
static sf::Texture& GetEnemyPinkTexture();
static sf::Texture& GetEnemyTexture();
static sf::Texture& GetFoodTexture();
};
\ No newline at end of file
......@@ -3,7 +3,7 @@
namespace config {
// Общее:
constexpr unsigned int FRAME_RATE_LIMIT = 10;
constexpr unsigned int FRAME_RATE_LIMIT = 60;
// Меню:
const sf::VideoMode SELECT_LEVEL_VIDEO_MODE{ 400, 600 };
const sf::Vector2f BUTTON_SIZE = { static_cast<float>(SELECT_LEVEL_VIDEO_MODE.width)/1.6f,
......@@ -25,11 +25,20 @@ namespace config {
// Игра:
const sf::VideoMode GAME_VIDEO_MODE{ 1080, 720 };
constexpr char PACMAN_FILE[] = ASSETS_PATH "pacman.png";
constexpr char ENEMY_PINK_FILE[] = ASSETS_PATH "enemy_pink.png";
constexpr char ENEMY_RED_FILE[] = ASSETS_PATH "enemy_red.png";
constexpr char ENEMY_BLUE_FILE[] = ASSETS_PATH "enemy_blue.png";
constexpr char ENEMY_YELLOW_FILE[] = ASSETS_PATH "enemy_yellow.png";
constexpr char ENEMY_PINK_FILE[] = ASSETS_PATH "enemies/enemy_pink.png";
constexpr char ENEMY_RED_FILE[] = ASSETS_PATH "enemies/enemy_red.png";
constexpr char ENEMY_BLUE_FILE[] = ASSETS_PATH "enemies/enemy_blue.png";
constexpr char ENEMY_YELLOW_FILE[] = ASSETS_PATH "enemies/enemy_yellow.png";
constexpr size_t ENEMY_TEXTURES_NUMBER = 4;
constexpr size_t ENEMY_ON_SPRITE_COUNT = 8;
constexpr char FOOD_1[] = ASSETS_PATH "food/onigiri.png";
constexpr char FOOD_2[] = ASSETS_PATH "food/sushi_roll_1.png";
constexpr char FOOD_3[] = ASSETS_PATH "food/sushi_roll_2.png";
constexpr char FOOD_4[] = ASSETS_PATH "food/sushi_roll_3.png";
constexpr char FOOD_5[] = ASSETS_PATH "food/sushi_roll_4.png";
constexpr char FOOD_6[] = ASSETS_PATH "food/sushi_roll_5.png";
constexpr char FOOD_7[] = ASSETS_PATH "food/sushi_roll_6.png";
constexpr size_t FOOD_TEXTURES_NUMBER = 7;
constexpr char EASY_GAME_TITLE[] = "Level: Easy";
constexpr char MEDIUM_GAME_TITLE[] = "Level: Medium";
constexpr char HARD_GAME_TITLE[] = "Level: Hard";
......@@ -39,7 +48,7 @@ namespace config {
constexpr float HARD_GAME_ENEMY_RATIO = 0.07f;
constexpr float ROOM_SIZE = 50;
constexpr float GAME_ENEMY_SIZE = ROOM_SIZE * 0.8;
constexpr float GAME_FOOD_SIZE = ROOM_SIZE * 0.1;
constexpr float GAME_FOOD_SIZE = ROOM_SIZE * 0.2;
constexpr float ENEMY_SPEED_MEDIEUM = 100;
constexpr float ENEMY_SPEED_HARD = 150;
constexpr float ENEMY_SPEED_EXTREME = 250;
......@@ -72,7 +81,6 @@ namespace config {
const sf::Color SELECT_LEVEL_BACKGROUND_COLOR{ 230,230,230 };
const sf::Color GAME_COLOR_ROOM{ 255, 255, 255 };
const sf::Color GAME_COLOR_WALL{ 0, 0, 0 };
const sf::Color GAME_FOOD_COLOR{ 0, 200, 100 };
const sf::Color GAME_COLOR_BACKGROUND_INGAME{ 230,230,230 };
const sf::Color TEXT_COLOR_NOTIFICATION{ 0, 0, 0, 255};
const sf::Color NOTIFICATION_BANNER_COLOR_PLAY{ 0, 100, 255, 200 };
......@@ -95,7 +103,6 @@ namespace config {
const sf::Color SELECT_LEVEL_BACKGROUND_COLOR_DARK{ 30, 30, 30 };
const sf::Color GAME_COLOR_ROOM_DARK{ 50, 50, 50 };
const sf::Color GAME_COLOR_WALL_DARK{ 200, 200, 200 };
const sf::Color GAME_FOOD_COLOR_DARK{ 0, 150, 75 };
const sf::Color GAME_COLOR_BACKGROUND_INGAME_DARK{ 20, 20, 20 };
const sf::Color TEXT_COLOR_NOTIFICATION_DARK{ 255, 255, 255, 255 };
const sf::Color NOTIFICATION_BANNER_COLOR_PLAY_DARK{ 0, 50, 150, 200 };
......
......@@ -5,19 +5,12 @@
Enemy::Enemy(const float speed) : m_speed(speed) {
std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution<size_t> dist{0, 3};
const sf::Texture* texture;
switch (dist(gen)) {
case 0: texture = &Assets::GetEnemyRedTexture(); break;
case 1: texture = &Assets::GetEnemyYellowTexture(); break;
case 2: texture = &Assets::GetEnemyBlueTexture(); break;
case 3: texture = &Assets::GetEnemyPinkTexture(); break;
default: texture = &Assets::GetEnemyRedTexture(); break;;
}
const sf::Texture& texture = Assets::GetEnemyTexture();
m_sprite.setTexture(*texture);
m_sprite.setTexture(texture);
m_sprite.setTextureRect(sf::IntRect(0, 0,
static_cast<int>(texture->getSize().x / config::ENEMY_ON_SPRITE_COUNT), static_cast<int>(texture->getSize().y)));
m_sprite.setOrigin(static_cast<float>(texture->getSize().x) / (2.f * config::ENEMY_ON_SPRITE_COUNT), static_cast<float>(texture->getSize().y) / 2.f);
static_cast<int>(texture.getSize().x / config::ENEMY_ON_SPRITE_COUNT), static_cast<int>(texture.getSize().y)));
m_sprite.setOrigin(static_cast<float>(texture.getSize().x) / (2.f * config::ENEMY_ON_SPRITE_COUNT), static_cast<float>(texture.getSize().y) / 2.f);
m_distance_limit = m_speed / config::ROOM_SIZE;
}
......
#include <States/GameState/Entities/StaticEntities/StaticEntities.h>
#include <Configuration.h>
#include <BasicAbstractions/Assets.h>
#include <Themes/Themes.h>
Food::Food() : m_circle(config::GAME_FOOD_SIZE, 6) {
m_circle.setFillColor(Themes::Instance().get_game_food_color());
m_circle.setOrigin({config::GAME_FOOD_SIZE, config::GAME_FOOD_SIZE});
Food::Food() : m_rect({ config::GAME_FOOD_SIZE, config::GAME_FOOD_SIZE }) {
m_rect.setOrigin({config::GAME_FOOD_SIZE, config::GAME_FOOD_SIZE});
m_rect.setTexture(&Assets::GetFoodTexture());
}
std::unique_ptr<IStaticEntity> Food::clone() const {
......@@ -12,7 +13,7 @@ std::unique_ptr<IStaticEntity> Food::clone() const {
}
void Food::draw_into(sf::RenderWindow& window) const {
window.draw(m_circle);
window.draw(m_rect);
}
void Food::prepare_for_first_drawing() {
......@@ -20,5 +21,5 @@ void Food::prepare_for_first_drawing() {
}
void Food::prepare_for_drawing() {
m_circle.setPosition(m_ptr_room->get_position());
m_rect.setPosition(m_ptr_room->get_position());
}
\ No newline at end of file
......@@ -13,8 +13,8 @@ public:
void draw_into(sf::RenderWindow& window) const override;
void prepare_for_first_drawing() override;
void prepare_for_drawing() override;
sf::FloatRect getBounds() const { return m_circle.getGlobalBounds(); }
sf::FloatRect getBounds() const { return m_rect.getGlobalBounds(); }
std::unique_ptr<IGameEvent> accept(IVisitor* ptr_visitor) override { return ptr_visitor->visit(this); }
private:
sf::CircleShape m_circle;
sf::RectangleShape m_rect;
};
\ No newline at end of file
......@@ -14,7 +14,6 @@ void Themes::change_theme() {
SELECT_LEVEL_BACKGROUND_COLOR = config::SELECT_LEVEL_BACKGROUND_COLOR;
GAME_COLOR_ROOM = config::GAME_COLOR_ROOM;
GAME_COLOR_WALL = config::GAME_COLOR_WALL;
GAME_FOOD_COLOR = config::GAME_FOOD_COLOR;
GAME_COLOR_BACKGROUND_INGAME = config::GAME_COLOR_BACKGROUND_INGAME;
TEXT_COLOR_NOTIFICATION = config::TEXT_COLOR_NOTIFICATION;
NOTIFICATION_BANNER_COLOR_PLAY = config::NOTIFICATION_BANNER_COLOR_PLAY;
......@@ -39,7 +38,6 @@ void Themes::change_theme() {
SELECT_LEVEL_BACKGROUND_COLOR = config::SELECT_LEVEL_BACKGROUND_COLOR_DARK;
GAME_COLOR_ROOM = config::GAME_COLOR_ROOM_DARK;
GAME_COLOR_WALL = config::GAME_COLOR_WALL_DARK;
GAME_FOOD_COLOR = config::GAME_FOOD_COLOR_DARK;
GAME_COLOR_BACKGROUND_INGAME = config::GAME_COLOR_BACKGROUND_INGAME_DARK;
TEXT_COLOR_NOTIFICATION = config::TEXT_COLOR_NOTIFICATION_DARK;
NOTIFICATION_BANNER_COLOR_PLAY = config::NOTIFICATION_BANNER_COLOR_PLAY_DARK;
......
......@@ -15,7 +15,6 @@ public:
[[nodiscard]] sf::Color get_select_level_background_color() const { return SELECT_LEVEL_BACKGROUND_COLOR; }
[[nodiscard]] sf::Color get_game_color_room() const { return GAME_COLOR_ROOM; }
[[nodiscard]] sf::Color get_game_color_wall() const { return GAME_COLOR_WALL; }
[[nodiscard]] sf::Color get_game_food_color() const { return GAME_FOOD_COLOR; }
[[nodiscard]] sf::Color get_game_color_background_in_game() const { return GAME_COLOR_BACKGROUND_INGAME; }
[[nodiscard]] sf::Color get_text_color_notification() const { return TEXT_COLOR_NOTIFICATION; }
[[nodiscard]] sf::Color get_notification_banner_color_play() const { return NOTIFICATION_BANNER_COLOR_PLAY; }
......@@ -41,7 +40,6 @@ private:
sf::Color SELECT_LEVEL_BACKGROUND_COLOR = config::SELECT_LEVEL_BACKGROUND_COLOR_DARK;
sf::Color GAME_COLOR_ROOM = config::GAME_COLOR_ROOM_DARK;
sf::Color GAME_COLOR_WALL = config::GAME_COLOR_WALL_DARK;
sf::Color GAME_FOOD_COLOR = config::GAME_FOOD_COLOR_DARK;
sf::Color GAME_COLOR_BACKGROUND_INGAME = config::GAME_COLOR_BACKGROUND_INGAME_DARK;
sf::Color TEXT_COLOR_NOTIFICATION = config::TEXT_COLOR_NOTIFICATION_DARK;
sf::Color NOTIFICATION_BANNER_COLOR_PLAY = config::NOTIFICATION_BANNER_COLOR_PLAY_DARK;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment