Source

Target

Showing with 107 additions and 21 deletions
+107 -21
#include "Texture.h"
PacmanTexture::PacmanTexture() {
if (!m_tex.loadFromFile(std::string(TEXTURE_PATH) + "pacman.png"))
throw std::runtime_error("Can't added Texture for Pacman");
if (!m_right.loadFromFile(std::string(TEXTURE_PATH) + "pacman.png"))
throw std::runtime_error("Can't added Texture for pacman");
if (!m_down.loadFromFile(std::string(TEXTURE_PATH) + "pacman_down.png"))
throw std::runtime_error("Can't added Texture for pacman");
if (!m_left.loadFromFile(std::string(TEXTURE_PATH) + "pacman_left.png"))
throw std::runtime_error("Can't added Texture for pacman");
if (!m_up.loadFromFile(std::string(TEXTURE_PATH) + "pacman_up.png"))
throw std::runtime_error("Can't added Texture for pacman");
}
sf::Texture& PacmanTexture::get_texture() {
switch (m_dir) {
case Direction::UP:
return m_up;
case Direction::DOWN:
return m_down;
case Direction::RIGHT:
return m_right;
case Direction::LEFT:
return m_left;
}
}
EnemyTexture::EnemyTexture() {
if (!m_tex.loadFromFile(std::string(TEXTURE_PATH) + "spider.png"))
throw std::runtime_error("Can't added Texture for Enemy");
......
......@@ -18,9 +18,9 @@ protected:
class PacmanTexture : public Texture {
public:
static PacmanTexture& Instance() { static PacmanTexture tex; return tex; }
sf::Texture& get_texture() override { return m_tex; }
sf::Texture& get_texture() override;
private:
sf::Texture m_tex;
sf::Texture m_up, m_right, m_down, m_left;
PacmanTexture();
};
......
#include "IGameEvent.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include "../Context/Context.h"
#include "../Game/IEntity.h"
//DELETESTATIC
DeleteStaticEntity::DeleteStaticEntity(std::unique_ptr<IStaticEntity> ptr) : m_ptr_entity(std::move(ptr)) {}
void DeleteStaticEntity::handle(GameContext& context) const {
......@@ -41,8 +42,43 @@ void Potion_take::handle(GameContext& context) const{
for (auto it = context.static_obj.begin(); it != context.static_obj.end(); ++it) {
if (m_ptr_entity == (*it)) {
context.static_obj.erase(it);
context.m_bar.regen_MP(50);
context.m_bar.regen_MP(25);
return;
}
}
}
Ultimate::Ultimate(){
set_sound(&UltimateSound::Instance());
m_sound.setBuffer(m_ptr_sound->get_sound());
}
static void kill_all(GameContext& context) {
context.dynamic_obj.clear();
}
void Ultimate::handle(GameContext& context) const {
//m_sound.play();
cv::VideoCapture cap(std::string(VIDEO_PATH) + "ultimate.mp4"); //
if (!cap.isOpened()) {
std::cerr << "ERROR!!!!!!!!!!!!!!!!!\n" << std::endl;
}
cv::Mat frame;
cv::namedWindow("", cv::WINDOW_NORMAL); //
cv::resizeWindow("", 1200, 800);
bool flag = true;
while (true) {
cap >> frame; //
if (frame.empty()) break; // ,
if (flag) {
kill_all(context);
}
cv::imshow("", frame);
if (cv::waitKey(30) == 27) break; // ESC
}
cap.release();
cv::destroyAllWindows();
}
#pragma once
#include <memory>
#include "Sound.h"
class GameContext;
class IStaticEntity;
class IDynamicEntity;
......@@ -44,3 +46,12 @@ public:
void handle(GameContext& context) const override;
};
class Ultimate : public IGameEvent {
public:
Ultimate();
void handle(GameContext& context) const override;
void set_sound(MySound* ptr_sound) { m_ptr_sound = ptr_sound; }
private:
MySound* m_ptr_sound;
mutable sf::Sound m_sound;
};
......@@ -2,6 +2,7 @@
#include <iostream>
#include "../Config/Config.h"
Pacman::Pacman() {
m_clock.restart();
set_texture(&PacmanTexture::Instance());
}
......@@ -17,10 +18,12 @@ void Pacman::spell(){
}
}
void Pacman::ultimate(){
if (m_mp >= 100) {
m_mp -= 100;
std::unique_ptr<IGameEvent> Pacman::ultimate(){
if (m_mp >= 300) {
m_mp -= 300;
return std::make_unique<Ultimate>();
}
return {};
}
void Pacman::draw_into(sf::RenderWindow& window) const {
......@@ -35,6 +38,7 @@ void Pacman::draw_into(sf::RenderWindow& window) const {
}
void Pacman::prepare_for_drawing() {
m_ptr_tex->set_dir(m_dir);
m_sprite.setTexture(m_ptr_tex->get_texture());
m_sprite.setScale(0.07, 0.07);
m_sprite.setPosition(m_ptr_room->get_position() + sf::Vector2f(-20, -20));
......@@ -49,7 +53,7 @@ std::unique_ptr<IGameEvent> Pacman::visit(Food* ptr_food) {
std::unique_ptr<IGameEvent> Pacman::visit(MPpotion* ptr_food) {
if (ptr_food->get_location() != this->get_location())
return {};
m_mp += 50;
m_mp += 25;
return std::make_unique<Potion_take>(std::move(std::unique_ptr<MPpotion>(ptr_food)));
}
......@@ -60,8 +64,9 @@ std::unique_ptr<IGameEvent> Fireball::visit(Enemy* ptr_enemy){
}
std::unique_ptr<IGameEvent> Pacman::visit(Enemy* ptr_enemy) {
if (ptr_enemy->get_location() != this->get_location())
if (ptr_enemy->get_location() != this->get_location() || m_clock.getElapsedTime().asSeconds() < 5) {
return {};
}
return std::make_unique<LostGame>();
}
......@@ -73,7 +78,6 @@ Fireball::Fireball(Direction direction, Room* room) {
}
void Fireball::draw_into(sf::RenderWindow& window) const {
//std::cout << "x = " << m_sprite.getPosition().x << "; y = " << m_sprite.getPosition().y << '\n';
window.draw(m_sprite);
}
......
......@@ -25,7 +25,7 @@ public:
Pacman();
void move(Direction direction);
void spell();
void ultimate();
std::unique_ptr<IGameEvent> ultimate();
void draw_into(sf::RenderWindow& window) const override;
void prepare_for_drawing() override;
std::unique_ptr<IGameEvent> visit(Food* ptr_food) override;
......@@ -35,9 +35,10 @@ public:
std::vector<Fireball> m_spells;
private:
mutable float m_mp = config::PACMAN_START_MP;
Direction m_dir;
Direction m_dir = Direction::RIGHT;
sf::Sprite m_sprite;
Texture* m_ptr_tex;
sf::Clock m_clock;
};
......
......@@ -136,19 +136,22 @@ void Wall::enter(IEntity* entity) {
MPbar::MPbar(){
m_back.setSize(config::BAR_SIZE);
m_back.setFillColor(sf::Color(50, 50, 50));
m_back.setPosition(0, 0);
m_back.setOrigin(config::BAR_SIZE.x / 2.0f, config::BAR_SIZE.y / 2.0f);
m_back.setPosition(config::GAME_VIDEO_MODE.width/2.0f, 1);
float percentage = m_cur_mp / m_max_mp;
m_bar.setSize(sf::Vector2f(m_back.getSize().x * percentage, m_back.getSize().y));
m_bar.setFillColor(sf::Color(0, 0, 255)); // MP
m_bar.setPosition(0, 0);
m_bar.setFillColor(sf::Color(0, 0, 255));
m_bar.setOrigin(config::BAR_SIZE.x / 2.0f, config::BAR_SIZE.y / 2.0f);
m_bar.setPosition(config::GAME_VIDEO_MODE.width / 2.0f, 1);
m_text.setFont(MyFont::Instance());
m_text.setString(std::to_string((size_t)(m_cur_mp)) + "/" + std::to_string((size_t)m_max_mp));
m_text.setCharacterSize(0.90*15.0f);
m_text.setLetterSpacing(1);
auto textRect = m_text.getLocalBounds();
m_text.setPosition(0, 0);
m_text.setOrigin(textRect.width / 2.0f, textRect.height / 2.0f);
m_text.setPosition(m_back.getPosition().x, m_back.getPosition().y);
m_text.setFillColor(config::BUTTON_COLOR_TEXT);
m_text.setOutlineColor(config::BUTTON_COLOR_FRAME);
m_text.setOutlineThickness(config::BUTTON_FRAME_THICKNESS);
......
......@@ -83,8 +83,8 @@ public:
void use_MP(float mp);
void regen_MP(float mp);
void update();
private:
float m_cur_mp = config::PACMAN_START_MP;
private:
float m_max_mp = config::PACMAN_MAX_MP;
sf::RectangleShape m_back;
sf::RectangleShape m_bar;
......
......@@ -34,8 +34,18 @@ void GameState::event_handling() {
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
m_context_manager.save_context();
m_context_manager.get_context().pacman.spell();
m_context_manager.get_context().m_bar.use_MP(50);
if (m_context_manager.get_context().m_bar.m_cur_mp >= 50) {
m_context_manager.get_context().pacman.spell();
m_context_manager.get_context().m_bar.use_MP(50);
}
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::E) {
m_context_manager.save_context();
if (m_context_manager.get_context().m_bar.m_cur_mp == 300) {
m_events.emplace_back(m_context_manager.get_context().pacman.ultimate());
m_context_manager.get_context().m_bar.use_MP(300);
}
}
if (event.type == sf::Event::KeyPressed && m_context_manager.get_context().state == GameContext::INGAME) {
process_key_pressed(event.key.code);
......