Commit 33152a28 authored by Сулимов Игорь Андреевич's avatar Сулимов Игорь Андреевич
Browse files

Almost finished

parent 8067d964
No related merge requests found
Showing with 81 additions and 25 deletions
+81 -25
assets/EnemyTexture.png

174 KB

assets/PacmanTexture.png

206 KB

......@@ -16,6 +16,9 @@ namespace config {
const char FONT_FILE_VARIANT_1[] = "android2.ttf";
const char FONT_FILE_VARIANT_2[] = "Oxygene1_RUS_by_KanycTa.ttf";
const char FONT_FILE[] = "Font Over.otf";
const char PACMAN_TEXTURE_FILE[] = "PacmanTexture.png";
const char ENEMY_TEXTURE_FILE[] = "EnemyTexture.png";
const char FOOD_TEXTURE_FILE[] = "FoodTexture.png";
const char SELECT_LEVEL_TITLE[] = "Select Level";
const sf::VideoMode SELECT_LEVEL_VIDEO_MODE{ 400, 600 };
const char BUTTON_TEXT_EASY[] = "Easy";
......
......@@ -4,9 +4,7 @@
class EnemyTexture : public Texture {
public:
static EnemyTexture& Instance() { static EnemyTexture tex; return tex; }
sf::Texture& get_texture() override { return m_texture; }
static EnemyTexture& Instance();
private:
sf::Texture m_texture;
EnemyTexture();
};
\ No newline at end of file
......@@ -2,11 +2,9 @@
#include "Texture.h"
class Food : public Texture {
class FoodTexture : public Texture {
public:
static Food& Instance() { static Food m_texture; return m_texutre; }
sf::Texture& get_texture() override { return m_texture; }
static FoodTexture& Instance();
private:
sf::Texture m_texture;
Food();
FoodTexture();
};
\ No newline at end of file
......@@ -5,9 +5,6 @@
class PacmanTexture : public Texture {
public:
static PacmanTexture& Instance();
sf::Texture& get_texture() const noexcept override;
private:
PacmanTexture();
private:
sf::Texture m_texture;
};
\ No newline at end of file
......@@ -6,7 +6,7 @@
class Texture {
public:
virtual sf::Texture& get_texture() const noexcept = 0;
const sf::Texture& get_texture() const noexcept { return m_texture; }
void set_direction(config::Direction dir) { m_direction = dir; }
public:
Texture(const Texture& tex) = delete;
......@@ -15,4 +15,5 @@ protected:
Texture() = default;
protected:
config::Direction m_direction = config::Direction::RIGHT;
sf::Texture m_texture;
};
\ No newline at end of file
......@@ -3,6 +3,8 @@
#include "IEntity.h"
#include <Visit/IVisitor.h>
#include "Draw/Textures/PacmanTexture.h"
class Pacman: public IEntity, public IVisitor {
public:
void prepare_for_drawing() override;
......@@ -10,8 +12,16 @@ public:
void move(Room::Direction direction);
std::unique_ptr<IGameEvent> visit(Food* ptr_food) override;
std::unique_ptr<IGameEvent> visit(Enemy* ptr_enemy) override;
void next_animation();
private:
void set_texture(Texture* ptr_texture);
public:
Pacman();
public:
sf::Clock m_clock;
config::Direction m_direction = config::RIGHT;
private:
sf::CircleShape m_circle;
sf::Sprite m_sprite;
Texture* m_ptr_texture;
inline static const size_t m_rect_count = 2;
};
\ No newline at end of file
#include "Draw/Textures/EnemyTexture.h"
\ No newline at end of file
#include "Draw/Textures/EnemyTexture.h"
EnemyTexture::EnemyTexture() {
if (!m_texture.loadFromFile(ASSETS_PATH + std::string(config::ENEMY_TEXTURE_FILE)))
throw std::runtime_error("Could not open the PacmanTexture file!");
}
EnemyTexture& EnemyTexture::Instance() {
static EnemyTexture instance;
return instance;
}
\ No newline at end of file
#include "Draw/Textures/FoodTexture.h"
\ No newline at end of file
#include "Draw/Textures/FoodTexture.h"
FoodTexture::FoodTexture() {
if (!m_texture.loadFromFile(ASSETS_PATH + std::string(config::FOOD_TEXTURE_FILE)))
throw std::runtime_error("Could not open the PacmanTexture file!");
}
FoodTexture& FoodTexture::Instance() {
static FoodTexture instance;
return instance;
}
#include "Draw/Textures/PacmanTexture.h"
PacmanTexture::PacmanTexture() {
if (!m_texture.loadFromFile(ASSETS_PATH + std::string(config::FONT_FILE)))
if (!m_texture.loadFromFile(ASSETS_PATH + std::string(config::PACMAN_TEXTURE_FILE)))
throw std::runtime_error("Could not open the PacmanTexture file!");
}
......@@ -9,7 +9,3 @@ PacmanTexture& PacmanTexture::Instance() {
static PacmanTexture instance;
return instance;
}
sf::Texture& PacmanTexture::get_texture() const noexcept {
return m_texture;
}
......@@ -5,17 +5,41 @@
#include "MazeContent/Entities/StaticEntities/Food.h"
#include "MazeContent/Entities/DynamicEntities/Enemy.h"
Pacman::Pacman() :m_circle(config::GAME_PACMAN_SIZE / 2) {
m_circle.setOrigin(m_circle.getRadius(), m_circle.getRadius());
m_circle.setFillColor(config::GAME_COLOR_PACMAN);
Pacman::Pacman() {
set_texture(&PacmanTexture::Instance());
}
void Pacman::set_texture(Texture* ptr_texture) {
m_ptr_texture = ptr_texture;
}
void Pacman::prepare_for_drawing() {
m_circle.setPosition(m_ptr_room->get_position());
m_sprite.setTexture(m_ptr_texture->get_texture());
if (m_direction == config::RIGHT) {
m_sprite.setTextureRect(sf::IntRect(0, 0, m_ptr_texture->get_texture().getSize().x / m_rect_count,
(m_ptr_texture->get_texture().getSize().y - 2) / 4));
} else if (m_direction == config::DOWN) {
m_sprite.setTextureRect(sf::IntRect(0, (m_ptr_texture->get_texture().getSize().y - 2) / (4),
m_ptr_texture->get_texture().getSize().x / m_rect_count, (m_ptr_texture->get_texture().getSize().y - 2) / (4)));
} else if (m_direction == config::LEFT) {
m_sprite.setTextureRect(sf::IntRect(0, (m_ptr_texture->get_texture().getSize().y - 2) / (m_rect_count),
m_ptr_texture->get_texture().getSize().x / m_rect_count, (m_ptr_texture->get_texture().getSize().y - 2) / 4 ));
} else if (m_direction == config::UP) {
m_sprite.setTextureRect(sf::IntRect(0, 3 * (m_ptr_texture->get_texture().getSize().y - 2) / (2 * m_rect_count),
m_ptr_texture->get_texture().getSize().x / m_rect_count, (m_ptr_texture->get_texture().getSize().y - 2)));
}
m_sprite.setPosition(m_ptr_room->get_position() + sf::Vector2f(-20, -20));
m_sprite.setScale(0.25, 0.25);
}
void Pacman::next_animation() {
sf::IntRect rect = m_sprite.getTextureRect();
rect.left = ((rect.left / (rect.width) + 1) % m_rect_count) * rect.width;
m_sprite.setTextureRect(rect);
}
void Pacman::draw_into(sf::RenderWindow& window) const {
window.draw(m_circle);
window.draw(m_sprite);
}
void Pacman::move(Room::Direction direction) {
......
......@@ -26,18 +26,22 @@ bool GameState::do_step() {
void GameState::process_key_pressed(sf::Keyboard::Key key) {
if (key == config::KEY_LEFT) {
m_context_manager.save_current_context();
m_context_manager.get_current_context().pacman.m_direction = config::LEFT;
m_context_manager.get_current_context().pacman.move(Room::LEFT);
}
else if (key == config::KEY_RIGHT) {
m_context_manager.save_current_context();
m_context_manager.get_current_context().pacman.m_direction = config::RIGHT;
m_context_manager.get_current_context().pacman.move(Room::RIGHT);
}
else if (key == config::KEY_UP) {
m_context_manager.save_current_context();
m_context_manager.get_current_context().pacman.m_direction = config::UP;
m_context_manager.get_current_context().pacman.move(Room::UP);
}
else if (key == config::KEY_DOWN) {
m_context_manager.save_current_context();
m_context_manager.get_current_context().pacman.m_direction = config::DOWN;
m_context_manager.get_current_context().pacman.move(Room::DOWN);
}
}
......@@ -96,6 +100,11 @@ void GameState::update() {
process_entities_interactions(&m_context_manager.get_current_context().pacman);
if (!m_context_manager.get_current_context().static_objects.size())
m_context_manager.get_current_context().state = GameContext::WIN;
auto miliseconds = static_cast<size_t>(m_context_manager.get_current_context().pacman.m_clock.getElapsedTime().asMilliseconds());
if (miliseconds < 200)
return;
m_context_manager.get_current_context().pacman.next_animation();
m_context_manager.get_current_context().pacman.m_clock.restart();
}
void GameState::render() {
......
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