Eat pellets
This commit is contained in:
parent
2398bf9c36
commit
f911f6e2a8
9 changed files with 52 additions and 0 deletions
|
@ -19,11 +19,18 @@ void Game::run() {
|
||||||
auto time_delta = now() - current_time;
|
auto time_delta = now() - current_time;
|
||||||
auto milli_delta = std::chrono::duration_cast<std::chrono::milliseconds>(time_delta);
|
auto milli_delta = std::chrono::duration_cast<std::chrono::milliseconds>(time_delta);
|
||||||
pacMan.update(milli_delta, inputState, board);
|
pacMan.update(milli_delta, inputState, board);
|
||||||
|
eatPellets();
|
||||||
current_time += time_delta;
|
current_time += time_delta;
|
||||||
canvas.update(pacMan, pellets, superPellets);
|
canvas.update(pacMan, pellets, superPellets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Game::eatPellets() {
|
||||||
|
const auto pos = pacMan.positionInGrid();
|
||||||
|
pellets.eatPelletAtPosition(pos);
|
||||||
|
superPellets.eatPelletAtPosition(pos);
|
||||||
|
}
|
||||||
|
|
||||||
void Game::processEvents(InputState & inputState) {
|
void Game::processEvents(InputState & inputState) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
|
|
|
@ -21,6 +21,8 @@ private:
|
||||||
Pellets pellets;
|
Pellets pellets;
|
||||||
SuperPellets superPellets;
|
SuperPellets superPellets;
|
||||||
|
|
||||||
|
void eatPellets();
|
||||||
|
|
||||||
static void processEvents(InputState & inputState);
|
static void processEvents(InputState & inputState);
|
||||||
|
|
||||||
static void keyToggle(const SDL_Event & event, InputState & inputState, bool on);
|
static void keyToggle(const SDL_Event & event, InputState & inputState, bool on);
|
||||||
|
|
|
@ -11,6 +11,20 @@ Position PacMan::position() const {
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Position PacMan::positionInGrid() const {
|
||||||
|
switch (direction) {
|
||||||
|
case Direction::LEFT:
|
||||||
|
case Direction::RIGHT:
|
||||||
|
return { floor(pos.x), round(pos.y) };
|
||||||
|
case Direction::UP:
|
||||||
|
case Direction::DOWN:
|
||||||
|
return { floor(pos.x), round(pos.y) };
|
||||||
|
default:
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) {
|
void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) {
|
||||||
setDirection(state);
|
setDirection(state);
|
||||||
updateAnimationPosition(time_delta);
|
updateAnimationPosition(time_delta);
|
||||||
|
|
|
@ -18,6 +18,8 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] Position position() const;
|
[[nodiscard]] Position position() const;
|
||||||
|
|
||||||
|
[[nodiscard]] Position positionInGrid() const;
|
||||||
|
|
||||||
void update(std::chrono::milliseconds time_delta, InputState state, const Board & board);
|
void update(std::chrono::milliseconds time_delta, InputState state, const Board & board);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
#include "Pellets.hpp"
|
#include "Pellets.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
Pellets::Pellets(const Board & board)
|
Pellets::Pellets(const Board & board)
|
||||||
: positions(board.initialPelletPositions()) {}
|
: positions(board.initialPelletPositions()) {}
|
||||||
|
|
||||||
|
bool Pellets::eatPelletAtPosition(Position p) {
|
||||||
|
auto it = std::find(positions.begin(), positions.end(), p);
|
||||||
|
if (it == positions.end())
|
||||||
|
return false;
|
||||||
|
positions.erase(it);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ public:
|
||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool eatPelletAtPosition(Position p);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const SDL_Point sprite = { 1, 9 };
|
const SDL_Point sprite = { 1, 9 };
|
||||||
std::vector<SDL_Point> positions;
|
std::vector<SDL_Point> positions;
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL_rect.h>
|
||||||
|
|
||||||
struct Position {
|
struct Position {
|
||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline bool operator==(const SDL_Point & b, const Position & a) {
|
||||||
|
return a.x == b.x && a.y == b.y;
|
||||||
|
}
|
||||||
|
|
|
@ -2,3 +2,11 @@
|
||||||
|
|
||||||
SuperPellets::SuperPellets(const Board & board)
|
SuperPellets::SuperPellets(const Board & board)
|
||||||
: positions(board.initialSuperPelletPositions()) {}
|
: positions(board.initialSuperPelletPositions()) {}
|
||||||
|
|
||||||
|
bool SuperPellets::eatPelletAtPosition(Position p) {
|
||||||
|
auto it = std::find(positions.begin(), positions.end(), p);
|
||||||
|
if (it == positions.end())
|
||||||
|
return false;
|
||||||
|
positions.erase(it);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ public:
|
||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool eatPelletAtPosition(Position p);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const SDL_Point sprite = { 0, 9 };
|
const SDL_Point sprite = { 0, 9 };
|
||||||
std::vector<SDL_Point> positions;
|
std::vector<SDL_Point> positions;
|
||||||
|
|
Loading…
Reference in a new issue