diff --git a/Sources/Builders/Game_n_common.cpp b/Sources/Builders/Game_n_common.cpp index 6d1891d4d763b2f1ae73b966c4171da037d2afe2..3f7deaa8e13e98296db70d46903f30c83678621e 100644 --- a/Sources/Builders/Game_n_common.cpp +++ b/Sources/Builders/Game_n_common.cpp @@ -37,7 +37,7 @@ void CommonBuilder::create_context(float dynamic_object_ratio, float IP_spawn_pr //располагаем еду for (size_t i = 0; i < buf.size(); ++i) { if (rand() % 100 < IP_spawn_probab * 100) { - m_context.static_objects.emplace_back(std::move(std::make_unique<InvisPoison>())); + m_context.static_objects.emplace_back(std::move(std::make_unique<InvisPotion>())); } else { m_context.static_objects.emplace_back(std::move(std::make_unique<Food>())); } m_context.static_objects.back()->set_location(buf.at(i)); diff --git a/Sources/Entities/Entities.cpp b/Sources/Entities/Entities.cpp index b0791c908b1e4b8f47c4b8f3a7d96665f3788d3b..f7bf641c6d71b2e2203cf83dc2b75cddab96b0c9 100644 --- a/Sources/Entities/Entities.cpp +++ b/Sources/Entities/Entities.cpp @@ -16,26 +16,22 @@ Room* IEntity::get_location() { //ENEMY Enemy::Enemy() { - /*m_rect = sf::RectangleShape({ config::GAME_ENEMY_SIZE ,config::GAME_ENEMY_SIZE }); - m_rect.setOrigin({ config::GAME_ENEMY_SIZE/2, config::GAME_ENEMY_SIZE/2 }); - m_rect.setFillColor(config::GAME_ENEMY_COLOR);*/ size_t ind = rand() % config::PATH_ENEMY_TEXTURES.size(); if (!m_texture.loadFromFile("../../../textures/" + config::PATH_ENEMY_TEXTURES.at(ind))) { - throw std::runtime_error("Can't load texture!\n"); + throw std::runtime_error("Textures loading error\n"); } - m_sprite.setOrigin({ m_texture.getSize().x/2.0f, m_texture.getSize().y/2.0f }); - m_sprite.setScale({ 2.5f,2.5f }); + + m_sprite.setOrigin({ m_texture.getSize().x / 2.0f, m_texture.getSize().y / 2.0f}); m_sprite.setTexture(m_texture); + m_sprite.setScale({ 2.5f,2.5f }); } void Enemy::draw_into(sf::RenderWindow& window) const { - //window.draw(m_rect); window.draw(m_sprite); } void Enemy::prepare_for_drawing() { //prep drawing проходит в конце set_pos - //m_rect.setPosition(m_ptr_room->get_position()); m_sprite.setTexture(m_texture); m_sprite.setPosition(m_ptr_room->get_position()); } @@ -86,36 +82,36 @@ std::unique_ptr<IGameEvent> Food::accept(IVisitor* ptr_visitor) { } -//INVISPOISON -InvisPoison::InvisPoison() { - m_circle = sf::CircleShape(config::GAME_INVIS_POISON_SIZE / 2); - m_circle.setOrigin({ config::GAME_INVIS_POISON_SIZE / 2, config::GAME_INVIS_POISON_SIZE / 2 }); - m_circle.setFillColor(config::GAME_INVIS_POISON_COLOR); +//INVISPOTION +InvisPotion::InvisPotion() { + m_circle = sf::CircleShape(config::GAME_INVIS_POTION_SIZE / 2); + m_circle.setOrigin({ config::GAME_INVIS_POTION_SIZE / 2, config::GAME_INVIS_POTION_SIZE / 2 }); + m_circle.setFillColor(config::GAME_INVIS_POTION_COLOR); - m_rect = sf::RectangleShape({ config::GAME_INVIS_POISON_SIZE * 3 / 8, config::GAME_INVIS_POISON_SIZE * 3 / 4 }); - m_rect.setFillColor(config::GAME_INVIS_POISON_COLOR); - m_rect.setOrigin({ config::GAME_INVIS_POISON_SIZE * 3 / 16, config::GAME_INVIS_POISON_SIZE }); + m_rect = sf::RectangleShape({ config::GAME_INVIS_POTION_SIZE * 3 / 8, config::GAME_INVIS_POTION_SIZE * 3 / 4 }); + m_rect.setFillColor(config::GAME_INVIS_POTION_COLOR); + m_rect.setOrigin({ config::GAME_INVIS_POTION_SIZE * 3 / 16, config::GAME_INVIS_POTION_SIZE }); } -void InvisPoison::catched(Inventory& inventory) { +void InvisPotion::catched(Inventory& inventory) { inventory.increase_IP(); } -void InvisPoison::draw_into(sf::RenderWindow& window) const { +void InvisPotion::draw_into(sf::RenderWindow& window) const { window.draw(m_circle); window.draw(m_rect); } -void InvisPoison::prepare_for_drawing() { +void InvisPotion::prepare_for_drawing() { m_circle.setPosition(m_ptr_room->get_position()); sf::Vector2f circle_pos = m_circle.getPosition(); - m_rect.setPosition({ circle_pos.x, circle_pos.y + config::GAME_INVIS_POISON_SIZE * 0.1f }); + m_rect.setPosition({ circle_pos.x, circle_pos.y + config::GAME_INVIS_POTION_SIZE * 0.1f }); } -std::unique_ptr<IStaticEntity> InvisPoison::clone() const { - return std::make_unique<InvisPoison>(*this); +std::unique_ptr<IStaticEntity> InvisPotion::clone() const { + return std::make_unique<InvisPotion>(*this); } -std::unique_ptr<IGameEvent> InvisPoison::accept(IVisitor* ptr_visitor) { +std::unique_ptr<IGameEvent> InvisPotion::accept(IVisitor* ptr_visitor) { return ptr_visitor->visit(this); } \ No newline at end of file diff --git a/Sources/Entities/Entities.h b/Sources/Entities/Entities.h index d2df213265533746c9a3ba519e94ab51e4cb32bb..77f87cd3f0760e36513191f1839b85abc27e9fcf 100644 --- a/Sources/Entities/Entities.h +++ b/Sources/Entities/Entities.h @@ -7,7 +7,7 @@ class Room; class Enemy; class Food; -class InvisPoison; +class InvisPotion; class Inventory; class IEntity: public IPreparable { //мб entity отсюда надо убрать @@ -24,7 +24,7 @@ class IVisitor { public: virtual std::unique_ptr<IGameEvent> visit(Food* ptr_food) = 0; virtual std::unique_ptr<IGameEvent> visit(Enemy* ptr_enemy) = 0; - virtual std::unique_ptr<IGameEvent> visit(InvisPoison* ptr_poison) = 0; + virtual std::unique_ptr<IGameEvent> visit(InvisPotion* ptr_potion) = 0; }; @@ -84,9 +84,9 @@ private: sf::CircleShape m_circle; }; -class InvisPoison: public IStaticEntity { +class InvisPotion: public IStaticEntity { public: - InvisPoison(); + InvisPotion(); void catched(Inventory& inventory) override; void draw_into(sf::RenderWindow& window) const override; diff --git a/Sources/Entities/Inventory.cpp b/Sources/Entities/Inventory.cpp index fd4e240bfd3b385795be3a25b8fd65f9073e1f25..b658b2ed183f12a4abf48dd636986904b8615c65 100644 --- a/Sources/Entities/Inventory.cpp +++ b/Sources/Entities/Inventory.cpp @@ -54,12 +54,12 @@ Inventory::Inventory() { m_texts.emplace_back(food_count); - sf::Text poison_count = sf::Text(std::to_string(m_count_of_IP), MyFont::Instance(), FS); - poison_count.setPosition({ pos_cell2.x + CS / 2 - FS + 0.2f * FS, pos_cell2.y + CS / 2 - FS - 0.2f * FS }); - poison_count.setFillColor(config::INVENTORY_COLOR_TEXT); - poison_count.setStyle(sf::Text::Bold); + sf::Text potion_count = sf::Text(std::to_string(m_count_of_IP), MyFont::Instance(), FS); + potion_count.setPosition({ pos_cell2.x + CS / 2 - FS + 0.2f * FS, pos_cell2.y + CS / 2 - FS - 0.2f * FS }); + potion_count.setFillColor(config::INVENTORY_COLOR_TEXT); + potion_count.setStyle(sf::Text::Bold); - m_texts.emplace_back(poison_count); + m_texts.emplace_back(potion_count); //food auto food_circle = sf::CircleShape(config::GAME_FOOD_SIZE / 1.5f); @@ -70,19 +70,19 @@ Inventory::Inventory() { m_circles.emplace_back(food_circle); //bottle - auto bottle_circle = sf::CircleShape(config::GAME_INVIS_POISON_SIZE / 1.5f); - bottle_circle.setOrigin({ config::GAME_INVIS_POISON_SIZE / 1.5f, 0 }); + auto bottle_circle = sf::CircleShape(config::GAME_INVIS_POTION_SIZE / 1.5f); + bottle_circle.setOrigin({ config::GAME_INVIS_POTION_SIZE / 1.5f, 0 }); bottle_circle.setPosition({ pos_cell2.x ,pos_cell2.y }); - bottle_circle.setFillColor(config::GAME_INVIS_POISON_COLOR); + bottle_circle.setFillColor(config::GAME_INVIS_POTION_COLOR); m_circles.emplace_back(bottle_circle); sf::Vector2f bottle_pos = bottle_circle.getPosition(); - auto bottle_neck = sf::RectangleShape({ config::GAME_INVIS_POISON_SIZE/2, config::GAME_INVIS_POISON_SIZE }); - bottle_neck.setFillColor(config::GAME_INVIS_POISON_COLOR); - bottle_neck.setOrigin({ config::GAME_INVIS_POISON_SIZE / 4, config::GAME_INVIS_POISON_SIZE }); - bottle_neck.setPosition({ bottle_pos.x, bottle_pos.y + config::GAME_INVIS_POISON_SIZE*0.1f }); //сдвинули на 10%, чтобы не было зазора + auto bottle_neck = sf::RectangleShape({ config::GAME_INVIS_POTION_SIZE/2, config::GAME_INVIS_POTION_SIZE }); + bottle_neck.setFillColor(config::GAME_INVIS_POTION_COLOR); + bottle_neck.setOrigin({ config::GAME_INVIS_POTION_SIZE / 4, config::GAME_INVIS_POTION_SIZE }); + bottle_neck.setPosition({ bottle_pos.x, bottle_pos.y + config::GAME_INVIS_POTION_SIZE*0.1f }); //сдвинули на 10%, чтобы не было зазора m_rects.emplace_back(bottle_neck); } @@ -129,7 +129,7 @@ void Inventory::get_item(IStaticEntity* item) { item->catched(*this); } -bool Inventory::use_poison() { +bool Inventory::use_potion() { if (m_count_of_IP > 0) { --m_count_of_IP; return true; diff --git a/Sources/Entities/Inventory.h b/Sources/Entities/Inventory.h index 0acac1f0a0ee270eba4edd16e85281752da3e9e4..6935bd07a2dd6f53be2792726bc1e94f79fac5be 100644 --- a/Sources/Entities/Inventory.h +++ b/Sources/Entities/Inventory.h @@ -18,7 +18,7 @@ public: Inventory clone(); void get_item(IStaticEntity* item); - bool use_poison(); + bool use_potion(); void increase_IP(); void increase_food(); diff --git a/Sources/Entities/Pacman.cpp b/Sources/Entities/Pacman.cpp index 69bf9c1f86f08036455622b1c864330ff734a6d3..39719677fef68d5b480f95f5cbf04b260f67d3dc 100644 --- a/Sources/Entities/Pacman.cpp +++ b/Sources/Entities/Pacman.cpp @@ -73,49 +73,37 @@ std::unique_ptr<IGameEvent> Pacman::visit(Enemy* ptr_enemy) { return std::make_unique<LostGame>(); } -std::unique_ptr<IGameEvent> Pacman::visit(InvisPoison* ptr_poison) { - if (ptr_poison->get_location() != this->get_location()) +std::unique_ptr<IGameEvent> Pacman::visit(InvisPotion* ptr_potion) { + if (ptr_potion->get_location() != this->get_location()) return {}; - return std::make_unique<CatchItem>(std::move(std::unique_ptr<IStaticEntity>(ptr_poison))); + return std::make_unique<CatchItem>(std::move(std::unique_ptr<IStaticEntity>(ptr_potion))); } void Pacman::become_invisible(GameContext& context) { - if (context.m_inventory.use_poison()) { //если можно применить зелье - m_clock.restart(); + if (context.m_inventory.use_potion()) { //если можно применить зелье + m_ticks = 0; m_visible = 0; m_circle.setFillColor(config::GAME_COLOR_PACMAN_INVISIBLE); } } void Pacman::update_color() noexcept { - //std::cout << m_clock.getElapsedTime().asMilliseconds() << '\n'; if (m_visible) return; - if (m_clock.getElapsedTime().asMilliseconds() > config::INVIS_POISON_DURATION) { + if (m_ticks > config::INVIS_POTION_DURATION) { m_visible = 1; m_circle.setFillColor(config::GAME_COLOR_PACMAN); return; } size_t start = config::START_BLINKING_TIME; size_t gap = config::BLINKING_GAP_DURATION; - size_t time = m_clock.getElapsedTime().asMilliseconds(); //создаем эффект мигания - if (m_circle.getFillColor()!= config::GAME_COLOR_PACMAN_INVISIBLE && time>start && ((time-start)/gap)%2==0) { //если время принадлежит 0,2,4,6 промежутку времени + if (m_circle.getFillColor()!= config::GAME_COLOR_PACMAN_INVISIBLE && m_ticks>start && ((m_ticks - start)/gap)%2==0) { //если время принадлежит 0,2,4,6 промежутку времени m_circle.setFillColor(config::GAME_COLOR_PACMAN_INVISIBLE); return; } - if (m_circle.getFillColor() != config::GAME_COLOR_PACMAN && time>start && ((time-start)/gap)%2==1) { + if (m_circle.getFillColor() != config::GAME_COLOR_PACMAN && m_ticks>start && ((m_ticks-start)/gap)%2==1) { m_circle.setFillColor(config::GAME_COLOR_PACMAN); return; } -} - -Pacman Pacman::clone() { - Pacman pacman; - pacman.set(m_clock, m_visible); - return pacman; -} - -void Pacman::set(sf::Clock clock, bool visible) { - m_clock = clock; - m_visible = visible; + ++m_ticks; } \ No newline at end of file diff --git a/Sources/Entities/Pacman.h b/Sources/Entities/Pacman.h index 56ea8c2d629e9f603828b52a762757d810df6456..6d5417b0d43e75ab2ba5269fccef626e4684d4a9 100644 --- a/Sources/Entities/Pacman.h +++ b/Sources/Entities/Pacman.h @@ -52,17 +52,12 @@ public: void prepare_for_drawing() override; std::unique_ptr<IGameEvent> visit(Food* ptr_food) override; std::unique_ptr<IGameEvent> visit(Enemy* ptr_enemy) override; - std::unique_ptr<IGameEvent> visit(InvisPoison* ptr_poison) override; + std::unique_ptr<IGameEvent> visit(InvisPotion* ptr_potion) override; void become_invisible(GameContext& context); - void update_color() noexcept; - Pacman clone(); - -private: - void set(sf::Clock clock, bool visible); + void update_color() noexcept; private: sf::CircleShape m_circle; - //sf::Clock m_initial_clock; - sf::Clock m_clock; + int m_ticks; bool m_visible = 1; }; diff --git a/Sources/States/States.cpp b/Sources/States/States.cpp index 342da60fd0c8e131ef28158a6132a5f7699ebc01..6df74a44419547daf28c9e5de6eb25b222b5c604 100644 --- a/Sources/States/States.cpp +++ b/Sources/States/States.cpp @@ -47,7 +47,7 @@ static void process_key_pressed(sf::Event event, ContextManager& manager) { manager.get_current_context().m_inventory.change_visibility(); break; - case config::KEY_POISON: + case config::KEY_POTION: manager.save_current_context(); manager.get_current_context().pacman.become_invisible(manager.get_current_context()); break; diff --git a/config.h b/config.h index d152570556177e3c3cbedabfed1e934f9d0897cc..465b1af06419b6343a4bca36278f2bcd13c079c0 100644 --- a/config.h +++ b/config.h @@ -39,15 +39,15 @@ namespace config { const sf::Keyboard::Key KEY_UP = sf::Keyboard::W; const sf::Keyboard::Key KEY_DOWN = sf::Keyboard::S; const sf::Keyboard::Key KEY_INVENTORY = sf::Keyboard::E; - const sf::Keyboard::Key KEY_POISON = sf::Keyboard::V; + const sf::Keyboard::Key KEY_POTION = sf::Keyboard::V; //Дополнительно: - const float GAME_INVIS_POISON_SIZE = ROOM_SIZE * 0.35; + const float GAME_INVIS_POTION_SIZE = ROOM_SIZE * 0.35; - const size_t INVIS_POISON_DURATION = 5000; //millisec - const size_t START_BLINKING_TIME = 2900; //millisec - const size_t BLINKING_GAP_DURATION = 300; //millisec - const float EASY_IP_SPAWN_PROBAB = 0; //INVIS_POISON + const size_t INVIS_POTION_DURATION = 300; //ticks + const size_t START_BLINKING_TIME = 160; //ticks + const size_t BLINKING_GAP_DURATION = 20; //ticks + const float EASY_IP_SPAWN_PROBAB = 0; //INVIS_POTION const float MEDIUM_IP_SPAWN_PROBAB = 0.005; const float HARD_IP_SPAWN_PROBAB = 0.02; @@ -77,7 +77,7 @@ namespace config { const sf::Color GAME_FOOD_COLOR{ 0, 200, 100 }; const sf::Color GAME_ENEMY_COLOR{ 255, 50, 0 }; - const sf::Color GAME_INVIS_POISON_COLOR{ 0, 179, 255 }; + const sf::Color GAME_INVIS_POTION_COLOR{ 0, 179, 255 }; const sf::Color CELL_COLOR = sf::Color{ 255,255,255, 255 }; const sf::Color CELL_OUTLINE_COLOR = sf::Color{ 0,0,0 }; const sf::Color INVENTORY_COLOR = sf::Color{ 230,230,230, 225 }; @@ -99,7 +99,7 @@ namespace config { const sf::Color GAME_FOOD_COLOR{ 0, 200, 100 }; const sf::Color GAME_ENEMY_COLOR{ 255, 50, 0 }; - const sf::Color GAME_INVIS_POISON_COLOR{ 0, 179, 255 }; + const sf::Color GAME_INVIS_POTION_COLOR{ 0, 179, 255 }; const sf::Color CELL_COLOR = sf::Color{ 0,0,0, 255 }; const sf::Color CELL_OUTLINE_COLOR = sf::Color{ 0,0, 255 }; const sf::Color INVENTORY_COLOR = sf::Color{ 0,0,0, 150 };