#include "game.h" #include #include #include constexpr int cell_width = 60; constexpr int cell_height = 35; constexpr int cell_deviation = 25; constexpr int window_side = cell_width * 4; Game::Game() { // Place the player with 10 initial charges onto x: 1, y: 1 hero = std::make_unique(1, 1, 2); // Generate level level = std::make_unique(); main_window.create(sf::VideoMode(window_side * 3, window_side * 3), "SFML-Test Application", sf::Style::Default); main_window.setActive(); } int Game::run() { // On the game loop while (main_window.isOpen()) { sf::Event event; while (main_window.pollEvent(event)) { if (event.type == sf::Event::Closed) main_window.close(); // Handling keyboard activity if (event.type == sf::Event::KeyPressed) { // Move onMoving(event.key.code); } } // Draw level renderMap(); main_window.display(); } return EXIT_SUCCESS; } //////////////////////////////////////////////////// Direction Game::getDirection(sf::Keyboard::Key &key) const { switch (key) { case sf::Keyboard::A: case sf::Keyboard::Left: case sf::Keyboard::Num4: return Direction::Left; case sf::Keyboard::W: case sf::Keyboard::Up: case sf::Keyboard::Num8: return Direction::Up; case sf::Keyboard::D: case sf::Keyboard::Right: case sf::Keyboard::Num6: return Direction::Right; case sf::Keyboard::S: case sf::Keyboard::Down: case sf::Keyboard::Num2: return Direction::Down; default: return Direction::None; } } void Game::onMoving(sf::Keyboard::Key &key) { // Determine where to move const Direction direction = getDirection(key); if (direction == Direction::None) return; // Save the initial coordinates coordinate initial_row, initial_col; hero->position(initial_row, initial_col); // Try to move hero hero->move(direction); // Save the new coordinates after moving coordinate attempt_row, attempt_col; hero->position(attempt_row, attempt_col); ////////////////////////// if (!level->getCellAt(attempt_row, attempt_col)->onMovingTo(hero, level)) hero->setPosition(initial_row, initial_col); } void Game::renderMap() { float painter_x = 60, painter_y = 60; float horizontal_shift = 0, vertical_shift = 0; // Brush for cell sprites sf::ConvexShape convex_brush; convex_brush.setPointCount(4); convex_brush.setPoint(0, sf::Vector2f(cell_deviation, 0.f)); convex_brush.setPoint(1, sf::Vector2f(cell_deviation + cell_width, 0.f)); convex_brush.setPoint(2, sf::Vector2f(cell_width, cell_height)); convex_brush.setPoint(3, sf::Vector2f(0.f, cell_height)); convex_brush.setFillColor(palette::Blue); convex_brush.setOutlineThickness(0); convex_brush.setPosition(painter_x, painter_y); // Counter for available charges sf::Text text; sf::Font font; font.loadFromFile("font/VeraMono.ttf"); text.setFont(font); text.setFillColor(palette::White); text.setCharacterSize(25); text.setPosition(50, 350); text.setString("Available bridge cells: " + std::to_string(hero->charges())); // Where is hero coordinate hero_row, hero_col; hero->position(hero_row, hero_col); // Draw map from 2D array for (coordinate x = 0; x < level->cols(); ++x) { horizontal_shift = static_cast(level->cols()) * cell_deviation; for (coordinate y = 0; y < level->rows(); ++y) { vertical_shift = static_cast(level->getCellAt(y, x)->heightShift()); // If cell has any height value, we should draw walls for it if (vertical_shift > 0) { // Brush for vertical walls sf::ConvexShape convex_wall_brush; convex_wall_brush.setPointCount(6); convex_wall_brush.setPoint(0, sf::Vector2f(cell_deviation + cell_width, -vertical_shift)); convex_wall_brush.setPoint(1, sf::Vector2f(cell_deviation + cell_width, 0.f)); convex_wall_brush.setPoint(2, sf::Vector2f(cell_width, cell_height)); convex_wall_brush.setPoint(3, sf::Vector2f(0.f, cell_height)); convex_wall_brush.setPoint(4, sf::Vector2f(0.f, cell_height - vertical_shift)); convex_wall_brush.setPoint(5, sf::Vector2f(cell_width, cell_height)); convex_wall_brush.setOutlineThickness(0); sf::Color wall_color(sf::Uint8(level->getCellAt(y, x)->color().r - 40), sf::Uint8(level->getCellAt(y, x)->color().g - 40), sf::Uint8(level->getCellAt(y, x)->color().b - 40)); convex_wall_brush.setFillColor(wall_color); convex_wall_brush.setPosition(painter_x + horizontal_shift, painter_y); main_window.draw(convex_wall_brush); } // Draw the top surface of the cell itself float final_x = painter_x + horizontal_shift; float final_y = painter_y - vertical_shift; convex_brush.setPosition(final_x, final_y); convex_brush.setFillColor(level->getCellAt(y, x)->color()); main_window.draw(convex_brush); if (hero_row == y && hero_col == x) { // Draw the hero sprite convex_brush.setFillColor(palette::White); main_window.draw(convex_brush); } // Move painter to next cell of current column painter_y += cell_height; horizontal_shift -= cell_deviation; } // Move painter to next column painter_y = 60; painter_x += cell_width; } main_window.draw(text); }