2020-02-19 12:50:09 -05:00
|
|
|
#include "game.h"
|
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
#include <SFML/Graphics/RectangleShape.hpp>
|
2020-03-15 11:02:37 -04:00
|
|
|
#include <SFML/Graphics/ConvexShape.hpp>
|
2020-02-21 16:55:13 -05:00
|
|
|
#include <SFML/Graphics/Text.hpp>
|
2020-02-21 12:34:28 -05:00
|
|
|
|
2020-03-15 11:02:37 -04:00
|
|
|
constexpr int cell_width = 60;
|
|
|
|
constexpr int cell_height = 35;
|
|
|
|
constexpr int cell_deviation = 25;
|
|
|
|
|
2020-03-15 17:57:39 -04:00
|
|
|
constexpr int window_side = cell_width * 4;
|
2020-02-21 12:34:28 -05:00
|
|
|
|
2020-02-19 12:50:09 -05:00
|
|
|
Game::Game()
|
|
|
|
{
|
2020-02-20 13:34:41 -05:00
|
|
|
// Place the player with 10 initial charges onto x: 1, y: 1
|
2020-02-21 16:55:13 -05:00
|
|
|
hero = std::make_unique<Hero>(1, 1, 2);
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
// Generate level
|
|
|
|
level = std::make_unique<Level>();
|
|
|
|
|
2020-03-16 10:30:30 -04:00
|
|
|
main_window.create(sf::VideoMode(window_side * 3, window_side * 3), "SFML-Test Application", sf::Style::Default);
|
2020-02-21 10:24:03 -05:00
|
|
|
main_window.setActive();
|
2020-02-25 11:53:57 -05:00
|
|
|
|
2020-03-16 15:57:42 -04:00
|
|
|
level->mapArray()[0][0]->setHeightShift(15);
|
|
|
|
level->mapArray()[0][1]->setHeightShift(10);
|
|
|
|
|
2020-02-25 11:53:57 -05:00
|
|
|
current_level = 1;
|
2020-03-15 17:57:39 -04:00
|
|
|
//loadLevel(current_level);
|
2020-02-19 12:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-02-20 13:34:41 -05:00
|
|
|
// Handling keyboard activity
|
|
|
|
if (event.type == sf::Event::KeyPressed)
|
|
|
|
{
|
|
|
|
// Move
|
2020-02-21 12:34:28 -05:00
|
|
|
onMoving(event.key.code);
|
2020-02-20 13:34:41 -05:00
|
|
|
}
|
2020-02-19 12:50:09 -05:00
|
|
|
}
|
2020-02-21 10:24:03 -05:00
|
|
|
|
|
|
|
// Draw level
|
2020-02-21 12:34:28 -05:00
|
|
|
renderMap();
|
2020-02-21 10:24:03 -05:00
|
|
|
|
|
|
|
main_window.display();
|
2020-02-19 12:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
////////////////////////////////////////////////////
|
2020-02-20 13:34:41 -05:00
|
|
|
Direction Game::getDirection(sf::Keyboard::Key &key) const
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case sf::Keyboard::A:
|
|
|
|
case sf::Keyboard::Left:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num4:
|
|
|
|
return Direction::Left;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
case sf::Keyboard::W:
|
|
|
|
case sf::Keyboard::Up:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num8:
|
|
|
|
return Direction::Up;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
case sf::Keyboard::D:
|
|
|
|
case sf::Keyboard::Right:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num6:
|
|
|
|
return Direction::Right;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
case sf::Keyboard::S:
|
|
|
|
case sf::Keyboard::Down:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num2:
|
|
|
|
return Direction::Down;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
default:
|
2020-02-21 09:13:12 -05:00
|
|
|
return Direction::None;
|
2020-02-20 13:34:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
void Game::onMoving(sf::Keyboard::Key &key)
|
2020-02-20 13:34:41 -05:00
|
|
|
{
|
|
|
|
// Determine where to move
|
|
|
|
const Direction direction = getDirection(key);
|
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
if (direction == Direction::None)
|
|
|
|
return;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
// Save the initial coordinates
|
2020-03-15 17:57:39 -04:00
|
|
|
coordinate initial_row, initial_col;
|
|
|
|
hero->position(initial_row, initial_col);
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
// Try to move hero
|
|
|
|
hero->move(direction);
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
// Save the new coordinates after moving
|
2020-03-15 17:57:39 -04:00
|
|
|
coordinate attempt_row, attempt_col;
|
|
|
|
hero->position(attempt_row, attempt_col);
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
//////////////////////////
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-03-15 17:57:39 -04:00
|
|
|
if (!level->mapArray()[attempt_row][attempt_col]->onMovingTo(hero, level))
|
|
|
|
hero->setPosition(initial_row, initial_col);
|
2020-02-21 10:24:03 -05:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
void Game::renderMap()
|
2020-02-21 10:24:03 -05:00
|
|
|
{
|
|
|
|
const Map &map = level->mapArray();
|
|
|
|
|
2020-03-16 15:57:42 -04:00
|
|
|
float painter_x = 60, painter_y = 60;
|
2020-03-16 12:54:42 -04:00
|
|
|
float horizontal_shift = 0, vertical_shift = 0;
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
// Brush for cell sprites
|
2020-03-15 11:02:37 -04:00
|
|
|
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));
|
2020-03-16 10:30:30 -04:00
|
|
|
convex_brush.setFillColor(palette::Blue);
|
2020-03-15 11:02:37 -04:00
|
|
|
convex_brush.setOutlineThickness(0);
|
|
|
|
convex_brush.setPosition(painter_x, painter_y);
|
2020-02-21 12:34:28 -05:00
|
|
|
|
2020-02-21 16:55:13 -05:00
|
|
|
// Counter for available charges
|
|
|
|
sf::Text text;
|
|
|
|
sf::Font font;
|
|
|
|
font.loadFromFile("font/VeraMono.ttf");
|
|
|
|
text.setFont(font);
|
2020-03-16 10:30:30 -04:00
|
|
|
text.setFillColor(palette::White);
|
2020-02-21 16:55:13 -05:00
|
|
|
text.setCharacterSize(25);
|
|
|
|
text.setPosition(50, 350);
|
|
|
|
text.setString("Available bridge cells: " + std::to_string(hero->charges()));
|
|
|
|
|
2020-03-15 11:02:37 -04:00
|
|
|
// Where is hero
|
2020-03-15 17:57:39 -04:00
|
|
|
coordinate hero_row, hero_col;
|
|
|
|
hero->position(hero_row, hero_col);
|
2020-03-15 11:02:37 -04:00
|
|
|
|
2020-02-21 12:34:28 -05:00
|
|
|
// Draw map from 2D array
|
2020-03-17 00:27:30 -04:00
|
|
|
for (coordinate x = 0; x < level->cols(); ++x)
|
2020-02-21 09:13:12 -05:00
|
|
|
{
|
2020-03-17 17:35:03 -04:00
|
|
|
horizontal_shift = static_cast<float>(level->cols()) * cell_deviation;
|
2020-03-15 11:02:37 -04:00
|
|
|
|
2020-03-17 00:27:30 -04:00
|
|
|
for (coordinate y = 0; y < level->rows(); ++y)
|
2020-02-21 10:24:03 -05:00
|
|
|
{
|
2020-03-16 12:54:42 -04:00
|
|
|
vertical_shift = static_cast<float>(map[y][x]->heightShift());
|
|
|
|
|
|
|
|
// If cell has any height value, we should draw walls for it
|
2020-03-16 15:57:42 -04:00
|
|
|
if (vertical_shift > 0)
|
2020-03-16 12:54:42 -04:00
|
|
|
{
|
|
|
|
// Brush for vertical walls
|
|
|
|
sf::ConvexShape convex_wall_brush;
|
2020-03-16 15:57:42 -04:00
|
|
|
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));
|
2020-03-16 12:54:42 -04:00
|
|
|
convex_wall_brush.setOutlineThickness(0);
|
|
|
|
|
2020-03-16 15:57:42 -04:00
|
|
|
sf::Color wall_color(sf::Uint8(map[y][x]->color().r - 40), sf::Uint8(map[y][x]->color().g - 40), sf::Uint8(map[y][x]->color().b - 40));
|
|
|
|
convex_wall_brush.setFillColor(wall_color);
|
2020-03-16 12:54:42 -04:00
|
|
|
|
2020-03-16 15:57:42 -04:00
|
|
|
convex_wall_brush.setPosition(painter_x + horizontal_shift, painter_y);
|
2020-03-16 12:54:42 -04:00
|
|
|
|
|
|
|
main_window.draw(convex_wall_brush);
|
2020-03-16 15:57:42 -04:00
|
|
|
}
|
2020-03-16 12:54:42 -04:00
|
|
|
|
|
|
|
// 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);
|
2020-03-15 17:57:39 -04:00
|
|
|
convex_brush.setFillColor(map[y][x]->color());
|
2020-03-16 12:54:42 -04:00
|
|
|
|
2020-03-15 11:02:37 -04:00
|
|
|
main_window.draw(convex_brush);
|
|
|
|
|
2020-03-15 17:57:39 -04:00
|
|
|
if (hero_row == y && hero_col == x)
|
2020-03-15 11:02:37 -04:00
|
|
|
{
|
2020-03-16 12:54:42 -04:00
|
|
|
// Draw the hero sprite
|
2020-03-16 10:30:30 -04:00
|
|
|
convex_brush.setFillColor(palette::White);
|
2020-03-15 11:02:37 -04:00
|
|
|
main_window.draw(convex_brush);
|
|
|
|
}
|
2020-02-21 12:34:28 -05:00
|
|
|
|
2020-03-16 12:54:42 -04:00
|
|
|
// Move painter to next cell of current column
|
2020-03-15 11:02:37 -04:00
|
|
|
painter_y += cell_height;
|
2020-03-16 12:54:42 -04:00
|
|
|
horizontal_shift -= cell_deviation;
|
2020-02-21 10:24:03 -05:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:54:42 -04:00
|
|
|
// Move painter to next column
|
2020-03-16 15:57:42 -04:00
|
|
|
painter_y = 60;
|
2020-03-15 11:02:37 -04:00
|
|
|
painter_x += cell_width;
|
2020-02-21 09:13:12 -05:00
|
|
|
}
|
2020-02-21 12:34:28 -05:00
|
|
|
|
2020-02-21 16:55:13 -05:00
|
|
|
main_window.draw(text);
|
2020-02-20 13:34:41 -05:00
|
|
|
}
|