Implement level rendering
This commit is contained in:
parent
cf0fa0f7dd
commit
2c31af397c
122
game.cpp
122
game.cpp
|
@ -1,5 +1,10 @@
|
|||
#include "game.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
constexpr int cell_length = 20;
|
||||
constexpr int window_side = cell_length * side;
|
||||
|
||||
Game::Game()
|
||||
{
|
||||
// Place the player with 10 initial charges onto x: 1, y: 1
|
||||
|
@ -16,15 +21,9 @@ int Game::run()
|
|||
{
|
||||
clock = std::make_unique<sf::Clock>();
|
||||
|
||||
// To where player moved each step of the game loop
|
||||
Direction direction;
|
||||
|
||||
// On the game loop
|
||||
while (main_window.isOpen())
|
||||
{
|
||||
// By default player doesn't move anywhere
|
||||
direction = Direction::None;
|
||||
|
||||
sf::Event event;
|
||||
while (main_window.pollEvent(event))
|
||||
{
|
||||
|
@ -35,12 +34,12 @@ int Game::run()
|
|||
if (event.type == sf::Event::KeyPressed)
|
||||
{
|
||||
// Move
|
||||
direction = onMoving(event.key.code);
|
||||
onMoving(event.key.code);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw level
|
||||
renderMap(direction);
|
||||
renderMap();
|
||||
|
||||
main_window.display();
|
||||
}
|
||||
|
@ -79,77 +78,85 @@ Direction Game::getDirection(sf::Keyboard::Key &key) const
|
|||
}
|
||||
}
|
||||
|
||||
Direction Game::onMoving(sf::Keyboard::Key &key)
|
||||
void Game::onMoving(sf::Keyboard::Key &key)
|
||||
{
|
||||
// Determine where to move
|
||||
const Direction direction = getDirection(key);
|
||||
|
||||
if (direction != Direction::None)
|
||||
if (direction == Direction::None)
|
||||
return;
|
||||
|
||||
// Save the initial coordinates
|
||||
coordinate initial_x, initial_y;
|
||||
hero->position(initial_x, initial_y);
|
||||
|
||||
// Try to move hero
|
||||
hero->move(direction);
|
||||
|
||||
// Save the new coordinates after moving
|
||||
coordinate attempt_x, attempt_y;
|
||||
hero->position(attempt_x, attempt_y);
|
||||
|
||||
//////////////////////////
|
||||
|
||||
// If the following cell is water
|
||||
if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
|
||||
{
|
||||
// Save the initial coordinates
|
||||
coordinate initial_x, initial_y;
|
||||
hero->position(initial_x, initial_y);
|
||||
|
||||
// Try to move hero
|
||||
hero->move(direction);
|
||||
|
||||
// Save the new coordinates after moving
|
||||
coordinate attempt_x, attempt_y;
|
||||
hero->position(attempt_x, attempt_y);
|
||||
|
||||
//////////////////////////
|
||||
|
||||
// If the following cell is water
|
||||
if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
|
||||
{
|
||||
// Try to use one charge to place a bridge
|
||||
if (hero->useCharge())
|
||||
level->placeBridge(attempt_x, attempt_y);
|
||||
// If hero doesn't have enough charges, we move Hero back
|
||||
else
|
||||
hero->setPosition(initial_x, initial_y);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
// If the following cell is a charge
|
||||
if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge))
|
||||
{
|
||||
// Hero picks up the charge; remove it from the map
|
||||
hero->refillCharges(1);
|
||||
level->removeCharge(attempt_x, attempt_y);
|
||||
}
|
||||
// Try to use one charge to place a bridge
|
||||
if (hero->useCharge())
|
||||
level->placeBridge(attempt_x, attempt_y);
|
||||
// If hero doesn't have enough charges, we move Hero back
|
||||
else
|
||||
hero->setPosition(initial_x, initial_y);
|
||||
}
|
||||
|
||||
return direction;
|
||||
//////////////////////////
|
||||
|
||||
// If the following cell is a charge
|
||||
if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge))
|
||||
{
|
||||
// Hero picks up the charge; remove it from the map
|
||||
hero->refillCharges(1);
|
||||
level->removeCharge(attempt_x, attempt_y);
|
||||
}
|
||||
}
|
||||
|
||||
void Game::renderMap(const Direction direction)
|
||||
void Game::renderMap()
|
||||
{
|
||||
if (direction == Direction::None)
|
||||
return; // Player didn't move this step of loop, so map couldn't change
|
||||
|
||||
const Map &map = level->mapArray();
|
||||
|
||||
coordinate painter_x = 0, painter_y = 0;
|
||||
|
||||
// Brush for cell sprites
|
||||
sf::RectangleShape rectangle_brush;
|
||||
rectangle_brush.setSize({cell_length, cell_length});
|
||||
rectangle_brush.setFillColor(sf::Color::Blue);
|
||||
rectangle_brush.setOutlineThickness(0);
|
||||
rectangle_brush.setPosition(painter_x, painter_y);
|
||||
|
||||
// Draw map from 2D array
|
||||
for (const Row &row : map)
|
||||
{
|
||||
for (const CellType &cell : row)
|
||||
{
|
||||
switch (cell)
|
||||
{ // TO DO!!
|
||||
{
|
||||
case CellType::Ground:
|
||||
; break;
|
||||
rectangle_brush.setFillColor(sf::Color(165, 42, 42)); // Brown
|
||||
break;
|
||||
case CellType::Charge:
|
||||
; break;
|
||||
rectangle_brush.setFillColor(sf::Color::Green);
|
||||
break;
|
||||
case CellType::Bridge:
|
||||
; break;
|
||||
rectangle_brush.setFillColor(sf::Color::Black);
|
||||
break;
|
||||
case CellType::Water:
|
||||
default:
|
||||
;
|
||||
rectangle_brush.setFillColor(sf::Color::Blue);
|
||||
}
|
||||
|
||||
main_window.draw(rectangle_brush);
|
||||
|
||||
// Move painter to next cell of row
|
||||
painter_x += cell_length;
|
||||
}
|
||||
|
@ -158,4 +165,13 @@ void Game::renderMap(const Direction direction)
|
|||
painter_x = 0;
|
||||
painter_y += cell_length;
|
||||
}
|
||||
|
||||
// Where is hero
|
||||
coordinate hero_x, hero_y;
|
||||
hero->position(hero_x, hero_y);
|
||||
|
||||
// Place the hero sprite
|
||||
rectangle_brush.setFillColor(sf::Color::White);
|
||||
rectangle_brush.setPosition(hero_x * cell_length, hero_y * cell_length);
|
||||
main_window.draw(rectangle_brush);
|
||||
}
|
||||
|
|
7
game.h
7
game.h
|
@ -10,9 +10,6 @@
|
|||
#include "hero.h"
|
||||
#include "level.h"
|
||||
|
||||
constexpr int cell_length = 20;
|
||||
constexpr int window_side = cell_length * side;
|
||||
|
||||
/// The main class where all the process happens
|
||||
class Game
|
||||
{
|
||||
|
@ -29,10 +26,10 @@ private:
|
|||
Direction getDirection(sf::Keyboard::Key &key) const;
|
||||
|
||||
/// Move player by pressed key
|
||||
Direction onMoving(sf::Keyboard::Key &key);
|
||||
void onMoving(sf::Keyboard::Key &key);
|
||||
|
||||
/// Render game state
|
||||
void renderMap(const Direction direction);
|
||||
void renderMap();
|
||||
|
||||
public:
|
||||
explicit Game();
|
||||
|
|
Loading…
Reference in New Issue