Extract Pellets and SuperPellets

All around refactor
This commit is contained in:
Patricia Aas 2020-11-28 14:10:25 +01:00
parent bf9aaa5cfc
commit 4789e93d62
20 changed files with 129 additions and 83 deletions

View file

@ -1,4 +1,4 @@
#include "Board.h"
#include "Board.hpp"
// Legend
// 0 - wall
@ -7,9 +7,6 @@
// 3 - door
// 4 - superpower
// 16 pixels per square
// Maze in pixels - width: 448 - height - 496
static const uint8_t board[ROWS][COLUMNS] = {
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0
@ -46,17 +43,12 @@ static const uint8_t board[ROWS][COLUMNS] = {
};
Board::Board() {
resetBoardState();
}
void Board::resetBoardState() {
for (uint8_t row = 0; row < ROWS; row++)
for (uint8_t column = 0; column < COLUMNS; column++)
board_state[row][column] = board[row][column];
}
bool Board::isWalkable(Position point, float_t position_delta, Direction direction) const {
switch (direction) {
case Direction::LEFT:
return board_state[int(point.y)][int(point.x - position_delta)] != 0;
@ -72,15 +64,7 @@ bool Board::isWalkable(Position point, float_t position_delta, Direction directi
}
}
SDL_Rect Board::pelletSprite() {
return pellet;
}
SDL_Rect Board::superPelletSprite() {
return super_pellet;
}
std::vector<SDL_Point> Board::pelletPositions() {
std::vector<SDL_Point> Board::initialPelletPositions() const {
std::vector<SDL_Point> positions;
for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t column = 0; column < COLUMNS; column++) {
@ -91,8 +75,7 @@ std::vector<SDL_Point> Board::pelletPositions() {
return positions;
}
std::vector<SDL_Point> Board::superPelletPositions() {
// Hard coded is probably better than this
std::vector<SDL_Point> Board::initialSuperPelletPositions() const {
std::vector<SDL_Point> positions;
for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t column = 0; column < COLUMNS; column++) {

View file

@ -1,8 +1,9 @@
#ifndef PACMAN_BOARD_H
#define PACMAN_BOARD_H
#include "Direction.h"
#include "Position.h"
#include "Direction.hpp"
#include "Position.hpp"
#include <SDL2/SDL_rect.h>
#include <cstdint>
#include <vector>
@ -16,20 +17,14 @@ public:
[[nodiscard]] bool isWalkable(Position point, float_t d, Direction direction) const;
SDL_Rect pelletSprite();
[[nodiscard]] std::vector<SDL_Point> initialPelletPositions() const;
SDL_Rect superPelletSprite();
[[nodiscard]] std::vector<SDL_Point> initialSuperPelletPositions() const;
std::vector<SDL_Point> pelletPositions();
std::vector<SDL_Point> superPelletPositions();
static Position initialPacManPosition() { return {14, 23}; }
private:
uint8_t board_state[ROWS][COLUMNS]{};
const SDL_Rect super_pellet = {0 * 32, 9 * 32, 32, 32};
const SDL_Rect pellet = {1 * 32, 9 * 32, 32, 32};
void resetBoardState();
};
#endif //PACMAN_BOARD_H

View file

@ -1,12 +1,13 @@
#include "Game.h"
#include <SDL2/SDL.h>
#include "Game.hpp"
#include <chrono>
Game::Game()
: window(448, 496) {
}
Game::Game() :
window(448, 496),
board(),
pacMan(board),
pellets(board),
superPellets(board) {}
auto Game::now() {
return std::chrono::system_clock::now();
@ -21,7 +22,7 @@ void Game::run() {
auto milli_delta = std::chrono::duration_cast<std::chrono::milliseconds>(time_delta);
pacMan.update(milli_delta, inputState, board);
current_time += time_delta;
window.update(pacMan, board);
window.update(pacMan, pellets, superPellets);
}
}

View file

@ -1,10 +1,13 @@
#ifndef PACMAN_GAME_H
#define PACMAN_GAME_H
#include "Board.h"
#include "GameWindow.h"
#include "InputState.h"
#include "PacMan.h"
#include "Board.hpp"
#include "GameWindow.hpp"
#include "PacMan.hpp"
#include "Pellets.hpp"
#include "SuperPellets.hpp"
class InputState;
class Game {
public:
@ -14,8 +17,10 @@ public:
private:
GameWindow window;
PacMan pacMan;
Board board;
PacMan pacMan;
Pellets pellets;
SuperPellets superPellets;
static void processEvents(InputState & inputState);

View file

@ -1,10 +1,13 @@
#include "GameWindow.h"
#include "PacMan.h"
#include "GameWindow.hpp"
#include <SDL2/SDL_image.h>
#include <iostream>
#include <vector>
#include "PacMan.hpp"
#include "Pellets.hpp"
#include "SuperPellets.hpp"
GameWindow::GameWindow(int width, int height) {
initSDL();
initSDLImage();
@ -16,11 +19,12 @@ GameWindow::GameWindow(int width, int height) {
sprite_texture = loadTexture(sdl_renderer, "sprites32.png");
}
void GameWindow::update(const PacMan & pacMan, Board board) {
void GameWindow::update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets) {
SDL_RenderClear(renderer.get());
renderMaze();
renderBoard(board);
renderPellets(pellets);
renderSuperPellets(superPellets);
renderPacMan(pacMan);
SDL_RenderPresent(renderer.get());
@ -30,23 +34,18 @@ void GameWindow::renderMaze() const {
renderTexture(maze_texture.get(), nullptr, nullptr);
}
void GameWindow::renderBoard(Board board) {
renderPellets(board);
renderSuperPellets(board);
}
void GameWindow::renderSuperPellets(Board & board) const {
SDL_Rect sprite_rect = board.superPelletSprite();
std::vector<SDL_Point> superPelletPositions = board.superPelletPositions();
void GameWindow::renderSuperPellets(const SuperPellets & superPellets) const {
SDL_Rect sprite_rect = superPellets.currentSprite();
std::vector<SDL_Point> superPelletPositions = superPellets.currentPositions();
for (const auto & pos : superPelletPositions) {
SDL_Rect maze_rect = targetRect({float_t(pos.x), float_t(pos.y)}, 8 * SCALE_FACTOR);
renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect);
}
}
void GameWindow::renderPellets(Board & board) const {
SDL_Rect sprite_rect = board.pelletSprite();
std::vector<SDL_Point> pelletPositions = board.pelletPositions();
void GameWindow::renderPellets(const Pellets & pellets) const {
SDL_Rect sprite_rect = pellets.currentSprite();
std::vector<SDL_Point> pelletPositions = pellets.currentPositions();
for (const auto & pos : pelletPositions) {
SDL_Rect maze_rect = targetRect({float_t(pos.x), float_t(pos.y)}, 8 * SCALE_FACTOR);
renderTexture(sprite_texture.get(), &sprite_rect, &maze_rect);

View file

@ -3,8 +3,6 @@
#include <memory>
#include <string>
#include "PacMan.h"
#include <SDL2/SDL.h>
struct SDL_Window_Deleter {
@ -32,12 +30,15 @@ struct SDL_Texture_Deleter {
};
class PacMan;
class Pellets;
class Position;
class SuperPellets;
class GameWindow {
public:
explicit GameWindow(int width, int height);
void update(const PacMan & pacMan, Board board);
void update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets);
private:
static const int16_t SCALE_FACTOR = 1;
@ -70,11 +71,9 @@ private:
void renderPacMan(const PacMan & pac_man) const;
void renderBoard(Board board);
void renderPellets(const Pellets & pellets) const;
void renderPellets(Board & board) const;
void renderSuperPellets(Board & board) const;
void renderSuperPellets(const SuperPellets & superPellets) const;
static SDL_Rect targetRect(const Position & position, int pixel_increase);

View file

@ -1 +0,0 @@
#include "InputState.h"

View file

@ -1,5 +1,7 @@
#include "PacMan.h"
#include "PacManAnimation.hpp"
#include "PacMan.hpp"
PacMan::PacMan(const Board & board) :
pos(board.initialPacManPosition()) {}
SDL_Rect PacMan::currentSprite() const {
return pacManAnimation.animationFrame(direction);

View file

@ -1,17 +1,20 @@
#ifndef PACMAN_PACMAN_H
#define PACMAN_PACMAN_H
#include "Board.h"
#include "Direction.h"
#include "InputState.h"
#include "Position.h"
#include "Direction.hpp"
#include "Position.hpp"
#include "PacManAnimation.hpp"
#include <SDL2/SDL_rect.h>
#include <chrono>
class Board;
class InputState;
class PacMan {
public:
explicit PacMan(const Board & board);
[[nodiscard]] SDL_Rect currentSprite() const;
[[nodiscard]] Position currentPosition() const;
@ -22,7 +25,7 @@ private:
Direction direction = Direction::NONE;
Direction desired_direction = Direction::NONE;
Position pos = {14, 23};
Position pos;
PacManAnimation pacManAnimation;
void setDirection(const InputState & state);

View file

@ -1,10 +1,10 @@
#ifndef PACMAN_PACMAN_ANIMATION_HPP
#define PACMAN_PACMAN_ANIMATION_HPP
#include "Board.h"
#include "Direction.h"
#include "InputState.h"
#include "Position.h"
#include "Board.hpp"
#include "Direction.hpp"
#include "InputState.hpp"
#include "Position.hpp"
#include <SDL2/SDL_rect.h>
#include <chrono>
@ -12,7 +12,7 @@
class PacManAnimation {
public:
SDL_Rect animationFrame(Direction direction) const;
[[nodiscard]] SDL_Rect animationFrame(Direction direction) const;
void updateAnimationPosition(std::chrono::milliseconds time_delta);

4
pacman/lib/Pellets.cpp Normal file
View file

@ -0,0 +1,4 @@
#include "Pellets.hpp"
Pellets::Pellets(const Board & board) :
positions(board.initialPelletPositions()) {}

26
pacman/lib/Pellets.hpp Normal file
View file

@ -0,0 +1,26 @@
#ifndef PACMAN_PELLETS_HPP
#define PACMAN_PELLETS_HPP
#include "Position.hpp"
#include "Board.hpp"
#include <SDL2/SDL_rect.h>
class Pellets {
public:
explicit Pellets(const Board & board);
[[nodiscard]] SDL_Rect currentSprite() const {
return pellet;
};
[[nodiscard]] std::vector<SDL_Point> currentPositions() const {
return positions;
}
private:
const SDL_Rect pellet = {1 * 32, 9 * 32, 32, 32};
std::vector<SDL_Point> positions;
};
#endif //PACMAN_PELLETS_HPP

View file

@ -1,7 +1,7 @@
#ifndef PACMAN_POSITION_H
#define PACMAN_POSITION_H
#include <math.h>
#include <cmath>
struct Position {
float_t x;

View file

@ -0,0 +1,4 @@
#include "SuperPellets.hpp"
SuperPellets::SuperPellets(const Board & board) :
positions(board.initialSuperPelletPositions()) {}

View file

@ -0,0 +1,26 @@
#ifndef PACMAN_SUPERPELLETS_HPP
#define PACMAN_SUPERPELLETS_HPP
#include "Position.hpp"
#include "Board.hpp"
#include <SDL2/SDL_rect.h>
class SuperPellets {
public:
explicit SuperPellets(const Board & board);
[[nodiscard]] SDL_Rect currentSprite() const {
return super_pellet;
}
[[nodiscard]] std::vector<SDL_Point> currentPositions() const {
return positions;
}
private:
const SDL_Rect super_pellet = {0 * 32, 9 * 32, 32, 32};
std::vector<SDL_Point> positions;
};
#endif //PACMAN_SUPERPELLETS_HPP

View file

@ -1,4 +1,4 @@
#include "../lib/Game.h"
#include "../lib/Game.hpp"
extern "C" int main([[maybe_unused]] int argc, [[maybe_unused]] char * argv[]) {
Game game;

View file

@ -6,6 +6,5 @@ include(GoogleTest)
add_executable(tests tests.cpp)
target_link_libraries(tests GTest::GTest libpacman)
#gtest_add_tests(TARGET tests TEST_PREFIX old_pacman:)
gtest_discover_tests(tests TEST_PREFIX pacman:)
add_test(NAME monolithic COMMAND tests)

View file

@ -1,8 +1,9 @@
#include <gtest/gtest.h>
#include "../lib/PacMan.h"
#include "../lib/PacMan.hpp"
TEST(PacManTest, InitialPosition) {
PacMan pacMan;
Board board;
PacMan pacMan(board);
EXPECT_EQ(pacMan.currentPosition().x, 14);
EXPECT_EQ(pacMan.currentPosition().y, 23);
}