mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
Merge branch 'master' into master
This commit is contained in:
+1
-1
@@ -20,7 +20,7 @@ FetchContent_Declare(json
|
||||
|
||||
FetchContent_MakeAvailable(SFML json)
|
||||
|
||||
add_executable(main src/main.cpp src/Entity.cpp src/Game.cpp src/MazeMap.cpp src/ResourceManager.cpp src/Ghost.cpp src/Pacman.cpp)
|
||||
add_executable(main src/main.cpp src/Entity.cpp src/Game.cpp src/MazeMap.cpp src/ResourceManager.cpp src/Blinky.cpp src/Pinky.cpp src/Inky.cpp src/Clyde.cpp)
|
||||
target_compile_features(main PRIVATE cxx_std_17)
|
||||
target_link_libraries(main PRIVATE SFML::Graphics SFML::Audio nlohmann_json::nlohmann_json)
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "Blinky.h"
|
||||
#include "Entity.h"
|
||||
#include "MazeMap.h"
|
||||
#include <limits>
|
||||
|
||||
void Blinky::setTargetTile(sf::Vector2i target) {
|
||||
targetTile = target;
|
||||
}
|
||||
|
||||
void Blinky::setBestMove(Entity& pacman) {
|
||||
if (!mazeMap) return;
|
||||
|
||||
setTargetTile(pacman.getCurrentMazeTile());
|
||||
|
||||
sf::Vector2i currentTile = getCurrentMazeTile();
|
||||
MovementDir currentDir = getCurrentDirection();
|
||||
|
||||
MovementDir bestDirection = MovementDir::STATIC;
|
||||
float minDistance = std::numeric_limits<float>::max();
|
||||
|
||||
MovementDir directions[] = {MovementDir::UP, MovementDir::DOWN, MovementDir::LEFT, MovementDir::RIGHT};
|
||||
|
||||
for (MovementDir dir : directions) {
|
||||
if ((currentDir == MovementDir::UP && dir == MovementDir::DOWN) ||
|
||||
(currentDir == MovementDir::DOWN && dir == MovementDir::UP) ||
|
||||
(currentDir == MovementDir::LEFT && dir == MovementDir::RIGHT) ||
|
||||
(currentDir == MovementDir::RIGHT && dir == MovementDir::LEFT)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!mazeMap->entityCanMove(*this, dir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sf::Vector2i nextTile = currentTile;
|
||||
switch (dir) {
|
||||
case MovementDir::UP:
|
||||
nextTile.y -= 1;
|
||||
break;
|
||||
case MovementDir::DOWN:
|
||||
nextTile.y += 1;
|
||||
break;
|
||||
case MovementDir::LEFT:
|
||||
nextTile.x -= 1;
|
||||
break;
|
||||
case MovementDir::RIGHT:
|
||||
nextTile.x += 1;
|
||||
break;
|
||||
case MovementDir::STATIC:
|
||||
break;
|
||||
}
|
||||
|
||||
float distance = mazeMap->distanceBetweenTiles(nextTile, targetTile);
|
||||
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
bestDirection = dir;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestDirection != MovementDir::STATIC) {
|
||||
queueDirection(bestDirection);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef BLINKY_H
|
||||
#define BLINKY_H
|
||||
|
||||
#include "Ghost.h"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
class Blinky : public Ghost {
|
||||
public:
|
||||
void setTargetTile(sf::Vector2i target);
|
||||
|
||||
void setBestMove(Entity& pacman);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "Clyde.h"
|
||||
|
||||
void Clyde::setTargetTile(Entity& pacman) {
|
||||
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
#ifndef CLYDE_H
|
||||
#define CLYDE_H
|
||||
|
||||
#include "Ghost.h"
|
||||
|
||||
class Clyde : public Ghost {
|
||||
public:
|
||||
void setTargetTile(Entity& pacman);
|
||||
};
|
||||
|
||||
#endif
|
||||
+18
-1
@@ -16,6 +16,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class MazeMap;
|
||||
|
||||
enum class MovementDir {
|
||||
STATIC,
|
||||
UP,
|
||||
@@ -35,7 +37,8 @@ public:
|
||||
currentDirection(MovementDir::STATIC),
|
||||
queuedDirection(MovementDir::STATIC),
|
||||
targetPosition(std::nullopt),
|
||||
isMoving(false) {};
|
||||
isMoving(false),
|
||||
mazeMap(nullptr) {};
|
||||
|
||||
void setAnimationTiles(sf::Texture& textureSheet, sf::Vector2i pixelLocation, const std::string& animationName, sf::Vector2i tileSize, unsigned int animationTiles, unsigned int pixelGap);
|
||||
|
||||
@@ -61,12 +64,22 @@ public:
|
||||
|
||||
void setMovementSpeed(sf::Vector2f newMovementSpeed) { movementSpeed = newMovementSpeed; }
|
||||
|
||||
void setCurrentMazeTile(sf::Vector2i mazeTile) {
|
||||
currentMazeTile = mazeTile;
|
||||
}
|
||||
|
||||
sf::Vector2i getCurrentMazeTile() { return currentMazeTile; }
|
||||
|
||||
void adjustTargetPosition(sf::Vector2f offset) {
|
||||
if (targetPosition.has_value()) {
|
||||
targetPosition = targetPosition.value() + offset;
|
||||
}
|
||||
}
|
||||
|
||||
void setMazeMap(MazeMap* map) { mazeMap = map; }
|
||||
|
||||
MazeMap* getMazeMap() { return mazeMap; }
|
||||
|
||||
protected:
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
|
||||
@@ -84,7 +97,11 @@ protected:
|
||||
|
||||
std::optional<sf::Vector2f> targetPosition;
|
||||
|
||||
sf::Vector2i currentMazeTile;
|
||||
|
||||
bool isMoving;
|
||||
|
||||
MazeMap* mazeMap;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+67
-63
@@ -8,10 +8,14 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Game.h"
|
||||
#include "Blinky.h"
|
||||
#include "Clyde.h"
|
||||
#include "Entity.h"
|
||||
#include "Inky.h"
|
||||
#include "MazeMap.h"
|
||||
#include "Pinky.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "Pacman.h"
|
||||
#include "Ghost.h"
|
||||
|
||||
json Game::loadConfig(const std::filesystem::path& configPath) {
|
||||
std::ifstream file(configPath);
|
||||
@@ -78,14 +82,16 @@ void Game::run() {
|
||||
resources.loadSound("siren4_firstloop", "assets/sounds/siren4_firstloop.wav");
|
||||
resources.loadSound("start", "assets/sounds/start.wav");
|
||||
|
||||
// Texture layout: [Pellet Maze Image] + gap (16px) + [Base Maze Image]
|
||||
// Texture layout: [Pellet Maze Image] + gap (4px at base scale) + [Base Maze Image]
|
||||
// Original: 224px (28*8) + 4px gap at 8px tiles
|
||||
// Scaled 4x: 896px (28*32) + 16px gap at 32px tiles
|
||||
// Gap scales with tile size: (28 * tileSize) + (4 * scaleFactor)
|
||||
const unsigned int mazePixelWidth = 28 * tileSize;
|
||||
const unsigned int gapWidth = 4 * scaleFactor;
|
||||
map.loadMaze(resources.getMazeMap(),
|
||||
resources.getPelletMap(),
|
||||
resources.getTexture("all_textures"),
|
||||
tileSize,
|
||||
{912, 0},
|
||||
{mazePixelWidth + gapWidth, 0},
|
||||
{0, 0});
|
||||
|
||||
|
||||
@@ -96,57 +102,49 @@ void Game::run() {
|
||||
Ghost inky(Ghost::AIType::INKY);
|
||||
Ghost clyde(Ghost::AIType::CLYDE);
|
||||
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1824, 0}, "right_walking", {60, 60}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1824, 64}, "left_walking", {60, 60}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1824, 128}, "up_walking", {60, 60}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1824, 192}, "down_walking", {60, 60}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1952, 0}, "static", {60, 60}, 1, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {2016, 0}, "death", {60, 60}, 11, 4);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1368, 0}, "right_walking", {45, 45}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1368, 48}, "left_walking", {45, 45}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1368, 96}, "up_walking", {45, 45}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1368, 144}, "down_walking", {45, 45}, 2, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1464, 0}, "static", {45, 45}, 1, 0);
|
||||
pacman.setAnimationTiles(resources.getTexture("all_textures"), {1512, 0}, "death", {45, 45}, 11, 3);
|
||||
pacman.setActiveSprite("static", 0);
|
||||
pacman.setOrigin({30, 30});
|
||||
pacman.setOrigin({22.5, 22.5});
|
||||
// Tile center = tile * tileSize + tileSize/2
|
||||
pacman.setPosition({14.5 * 32.0f - 16.0f, 24 * 32.0f - 16.0f});
|
||||
pacman.setPosition({14.5f * tileSize - (tileSize / 2.0f), 24.0f * tileSize - (tileSize / 2.0f)});
|
||||
pacman.setMovementSpeed({perLoopMove, perLoopMove});
|
||||
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {1824, 256}, "right_walking", {60, 60}, 2, 4);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {1952, 256}, "left_walking", {60, 60}, 2, 4);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {2080, 256}, "up_walking", {60, 60}, 2, 4);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {2208, 256}, "down_walking", {60, 60}, 2, 4);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {2336, 256}, "vulnerable", {60, 60}, 2, 4);
|
||||
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {1368, 192}, "right_walking", {45, 45}, 2, 3);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {1464, 192}, "left_walking", {45, 45}, 2, 3);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {1560, 192}, "up_walking", {45, 45}, 2, 3);
|
||||
blinky.setAnimationTiles(resources.getTexture("all_textures"), {1656, 192}, "down_walking", {45, 45}, 2, 3);
|
||||
blinky.setActiveSprite("left_walking", 0);
|
||||
blinky.setOrigin({30, 30});
|
||||
blinky.setPosition({14.5 * 32.0f - 16.0f, 12 * 32.0f - 16.0f});
|
||||
blinky.setMovementSpeed({perLoopMove * 0.8f, perLoopMove * 0.8f});
|
||||
blinky.setOrigin({22.5, 22.5});
|
||||
blinky.setPosition({14.5f * tileSize - (tileSize / 2.0f), 12.0f * tileSize - (tileSize / 2.0f)});
|
||||
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {1824, 320}, "right_walking", {60, 60}, 2, 4);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {1952, 320}, "left_walking", {60, 60}, 2, 4);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {2080, 320}, "up_walking", {60, 60}, 2, 4);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {2208, 320}, "down_walking", {60, 60}, 2, 4);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {2336, 256}, "vulnerable", {60, 60}, 2, 4);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {1368, 240}, "right_walking", {45, 45}, 2, 3);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {1464, 240}, "left_walking", {45, 45}, 2, 3);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {1560, 240}, "up_walking", {45, 45}, 2, 3);
|
||||
pinky.setAnimationTiles(resources.getTexture("all_textures"), {1656, 240}, "down_walking", {45, 45}, 2, 3);
|
||||
pinky.setActiveSprite("down_walking", 0);
|
||||
pinky.setOrigin({30, 30});
|
||||
pinky.setPosition({14.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
||||
pinky.setMovementSpeed({perLoopMove * 0.8f, perLoopMove * 0.8f});
|
||||
pinky.setOrigin({22.5, 22.5});
|
||||
pinky.setPosition({14.5f * tileSize - (tileSize / 2.0f), 15.0f * tileSize - (tileSize / 2.0f)});
|
||||
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {1824, 384}, "right_walking", {60, 60}, 2, 4);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {1952, 384}, "left_walking", {60, 60}, 2, 4);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {2080, 384}, "up_walking", {60, 60}, 2, 4);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {2208, 384}, "down_walking", {60, 60}, 2, 4);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {2336, 256}, "vulnerable", {60, 60}, 2, 4);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {1368, 288}, "right_walking", {45, 45}, 2, 3);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {1464, 288}, "left_walking", {45, 45}, 2, 3);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {1560, 288}, "up_walking", {45, 45}, 2, 3);
|
||||
inky.setAnimationTiles(resources.getTexture("all_textures"), {1656, 288}, "down_walking", {45, 45}, 2, 3);
|
||||
inky.setActiveSprite("up_walking", 0);
|
||||
inky.setOrigin({30, 30});
|
||||
inky.setPosition({12.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
||||
inky.setMovementSpeed({perLoopMove * 0.8f, perLoopMove * 0.8f});
|
||||
inky.setOrigin({22.5, 22.5});
|
||||
inky.setPosition({12.5f * tileSize - (tileSize / 2.0f), 15.0f * tileSize - (tileSize / 2.0f)});
|
||||
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {1824, 448}, "right_walking", {60, 60}, 2, 4);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {1952, 448}, "left_walking", {60, 60}, 2, 4);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {2080, 448}, "up_walking", {60, 60}, 2, 4);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {2208, 448}, "down_walking", {60, 60}, 2, 4);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {2336, 256}, "vulnerable", {60, 60}, 2, 4);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {1368, 336}, "right_walking", {45, 45}, 2, 3);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {1464, 336}, "left_walking", {45, 45}, 2, 3);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {1560, 336}, "up_walking", {45, 45}, 2, 3);
|
||||
clyde.setAnimationTiles(resources.getTexture("all_textures"), {1656, 336}, "down_walking", {45, 45}, 2, 3);
|
||||
clyde.setActiveSprite("up_walking", 0);
|
||||
clyde.setOrigin({30, 30});
|
||||
clyde.setPosition({16.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
||||
clyde.setMovementSpeed({perLoopMove * 0.8f, perLoopMove * 0.8f});
|
||||
clyde.setOrigin({22.5, 22.5});
|
||||
clyde.setPosition({16.5f * tileSize - (tileSize / 2.0f), 15.0f * tileSize - (tileSize / 2.0f)});
|
||||
|
||||
// Set up ghost box boundaries for all ghosts
|
||||
// Exit tile is at (14, 12), boundary is Y=12 (don't allow ghosts below this Y)
|
||||
@@ -198,7 +196,7 @@ void Game::run() {
|
||||
auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName);
|
||||
|
||||
sf::Sound sound(*resources.getSound("start"));
|
||||
sound.play();
|
||||
//sound.play();
|
||||
|
||||
sf::Sound pellet0(*resources.getSound("eat_dot_0"));
|
||||
sf::Sound pellet1(*resources.getSound("eat_dot_1"));
|
||||
@@ -218,23 +216,23 @@ void Game::run() {
|
||||
sf::Font bitFont = resources.getFont("bitFont");
|
||||
sf::Text scoreText(bitFont);
|
||||
scoreText.setString(std::to_string(score));
|
||||
scoreText.setPosition({16, 32 * 31 + 16});
|
||||
scoreText.setPosition({16.0f, tileSize * 31.0f + 16.0f});
|
||||
|
||||
sf::Sprite pacmanLifeOne(resources.getTexture("all_textures"), {{2340, 68}, {52, 52}});
|
||||
pacmanLifeOne.setOrigin({52, 0});
|
||||
pacmanLifeOne.setPosition({32 * 28, 32 * 31});
|
||||
|
||||
sf::Sprite pacmanLifeTwo(resources.getTexture("all_textures"), {{2340, 68}, {52, 52}});
|
||||
pacmanLifeTwo.setOrigin({52, 0});
|
||||
pacmanLifeTwo.setPosition({32 * 26.5, 32 * 31});
|
||||
sf::Sprite pacmanLifeOne(resources.getTexture("all_textures"), {{1755, 51}, {39, 39}});
|
||||
pacmanLifeOne.setOrigin({39, 0});
|
||||
pacmanLifeOne.setPosition({tileSize * 28.0f, tileSize * 31.0f});
|
||||
|
||||
sf::Sprite fruitOne(resources.getTexture("all_textures"), {{1956, 192}, {60, 60}});
|
||||
fruitOne.setOrigin({60, 0});
|
||||
fruitOne.setPosition({32 * 16, 32 * 31});
|
||||
sf::Sprite pacmanLifeTwo(resources.getTexture("all_textures"), {{1755, 51}, {39, 39}});
|
||||
pacmanLifeTwo.setOrigin({39, 0});
|
||||
pacmanLifeTwo.setPosition({tileSize * 26.5f, tileSize * 31.0f});
|
||||
|
||||
sf::Sprite fruitTwo(resources.getTexture("all_textures"), {{1956, 192}, {60, 60}});
|
||||
fruitTwo.setOrigin({60, 0});
|
||||
fruitTwo.setPosition({32 * 14, 32 * 31});
|
||||
sf::Sprite fruitOne(resources.getTexture("all_textures"), {{1467, 144}, {45, 45}});
|
||||
fruitOne.setOrigin({45, 0});
|
||||
fruitOne.setPosition({tileSize * 16.0f, tileSize * 31.0f});
|
||||
|
||||
sf::Sprite fruitTwo(resources.getTexture("all_textures"), {{1467, 144}, {45, 45}});
|
||||
fruitTwo.setOrigin({45, 0});
|
||||
fruitTwo.setPosition({tileSize * 14.0f, tileSize * 31.0f});
|
||||
|
||||
while (window.isOpen())
|
||||
{
|
||||
@@ -351,7 +349,7 @@ void Game::run() {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!map.isWall(targetTile.x, targetTile.y)) {
|
||||
if (!map.isWall(targetTile)) {
|
||||
map.snapEntityToGrid(pacman);
|
||||
|
||||
switch (queued) {
|
||||
@@ -425,6 +423,12 @@ void Game::run() {
|
||||
|
||||
pacman.update();
|
||||
|
||||
//check if blinky is at an intersection
|
||||
//if yes set target tile to pacmans tile
|
||||
//calculate distance for all legal move options
|
||||
//choose option with least distance for blinky to pacman
|
||||
|
||||
|
||||
map.handleTunnelWrapping(pacman);
|
||||
|
||||
// Update Blinky's AI and movement (Blinky is the red ghost) - released immediately
|
||||
@@ -480,9 +484,9 @@ void Game::run() {
|
||||
scoreText.setString(std::to_string(score));
|
||||
|
||||
if (pelletSoundCount % 2) {
|
||||
pellet1.play();
|
||||
//pellet1.play();
|
||||
} else {
|
||||
pellet0.play();
|
||||
//pellet0.play();
|
||||
}
|
||||
|
||||
pelletSoundCount++;
|
||||
|
||||
+2
-2
@@ -25,9 +25,9 @@ private:
|
||||
const float perPixelMove;
|
||||
const int baseTileSize;
|
||||
|
||||
int scaleFactor = 4;
|
||||
int scaleFactor = 3;
|
||||
|
||||
sf::Vector2u windowRes = {896, 1080};
|
||||
sf::Vector2u windowRes = {672, 810};
|
||||
std::string windowName = "Pacmen";
|
||||
};
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define GHOST_H
|
||||
|
||||
#include "Entity.h"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
class MazeMap;
|
||||
class Pacman;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "Inky.h"
|
||||
|
||||
void Inky::setTargetTile(Entity& pacman, Entity& blinky) {
|
||||
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
#ifndef INKY_H
|
||||
#define INKY_H
|
||||
|
||||
#include "Ghost.h"
|
||||
|
||||
class Inky : public Ghost {
|
||||
public:
|
||||
void setTargetTile(Entity& pacman, Entity& blinky);
|
||||
};
|
||||
|
||||
#endif
|
||||
+74
-13
@@ -1,5 +1,7 @@
|
||||
#include "MazeMap.h"
|
||||
#include "Entity.h"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <cmath>
|
||||
|
||||
bool MazeMap::loadMaze(const std::vector<int>& collisionData,
|
||||
const std::vector<int>& pelletData,
|
||||
@@ -60,10 +62,10 @@ bool MazeMap::loadMaze(const std::vector<int>& collisionData,
|
||||
return true;
|
||||
}
|
||||
|
||||
void MazeMap::eatPellet(int x, int y) {
|
||||
if (!isLegalTile(x, y)) return;
|
||||
void MazeMap::eatPellet(sf::Vector2i tilePos) {
|
||||
if (!isLegalTile(tilePos)) return;
|
||||
|
||||
int tileIndex = x + y * width;
|
||||
int tileIndex = convert2DCoords(tilePos);
|
||||
|
||||
pelletEaten[tileIndex] = true;
|
||||
|
||||
@@ -73,14 +75,35 @@ void MazeMap::eatPellet(int x, int y) {
|
||||
}
|
||||
}
|
||||
|
||||
bool MazeMap::hasPellet(int x, int y) const {
|
||||
if (!isLegalTile(x, y)) return false;
|
||||
//void MazeMap::eatPellet(int x, int y) {
|
||||
// if (!isLegalTile(x, y)) return;
|
||||
//
|
||||
// int tileIndex = x + y * width;
|
||||
//
|
||||
// pelletEaten[tileIndex] = true;
|
||||
//
|
||||
// sf::Vertex* triangles = &pelletVertices[tileIndex * 6];
|
||||
// for (int i = 0; i < 6; ++i) {
|
||||
// triangles[i].color = sf::Color::Transparent;
|
||||
// }
|
||||
//}
|
||||
|
||||
int tileIndex = x + y * width;
|
||||
bool MazeMap::hasPellet(sf::Vector2i tilePos) const {
|
||||
if (!isLegalTile(tilePos)) return false;
|
||||
|
||||
int tileIndex = convert2DCoords(tilePos);
|
||||
|
||||
return !pelletEaten[tileIndex] && pelletMap[tileIndex] > 0;
|
||||
}
|
||||
|
||||
//bool MazeMap::hasPellet(int x, int y) const {
|
||||
// if (!isLegalTile(x, y)) return false;
|
||||
//
|
||||
// int tileIndex = x + y * width;
|
||||
//
|
||||
// return !pelletEaten[tileIndex] && pelletMap[tileIndex] > 0;
|
||||
//}
|
||||
|
||||
PelletType MazeMap::getPelletType(sf::Vector2i tilePos) const {
|
||||
int tileIndex = tilePos.x + tilePos.y * width;
|
||||
int pellet = pelletMap[tileIndex];
|
||||
@@ -91,18 +114,48 @@ PelletType MazeMap::getPelletType(sf::Vector2i tilePos) const {
|
||||
return PelletType::NONE;
|
||||
}
|
||||
|
||||
bool MazeMap::isWall(int x, int y) const {
|
||||
if (!isLegalTile(x, y)) return true;
|
||||
bool MazeMap::isWall(sf::Vector2i tilePos) const {
|
||||
if (!isLegalTile(tilePos)) return true;
|
||||
|
||||
int tileIndex = convert2DCoords(tilePos);
|
||||
|
||||
int tileIndex = x + y * width;
|
||||
return collisionMap[tileIndex] == 1;
|
||||
}
|
||||
|
||||
bool MazeMap::isLegalTile(int x, int y) const {
|
||||
return x >= 0 && x < static_cast<int>(width) &&
|
||||
y >= 0 && y < static_cast<int>(height);
|
||||
//bool MazeMap::isWall(int x, int y) const {
|
||||
// if (!isLegalTile(x, y)) return true;
|
||||
//
|
||||
// int tileIndex = x + y * width;
|
||||
// return collisionMap[tileIndex] == 1;
|
||||
//}
|
||||
|
||||
bool MazeMap::isLegalTile(sf::Vector2i tilePos) const {
|
||||
return tilePos.x >= 0 && tilePos.x < static_cast<int>(width) &&
|
||||
tilePos.y >= 0 && tilePos.y < static_cast<int>(height);
|
||||
}
|
||||
|
||||
//bool MazeMap::isLegalTile(int x, int y) const {
|
||||
// return x >= 0 && x < static_cast<int>(width) &&
|
||||
// y >= 0 && y < static_cast<int>(height);
|
||||
//}
|
||||
|
||||
bool MazeMap::isIntersectionTile(sf::Vector2i tilePos) const {
|
||||
// true if more than two surrounding tiles are not walls
|
||||
|
||||
int surroundingMovementTiles = 0;
|
||||
|
||||
// +x (right)
|
||||
if (!isWall({tilePos.x + 1, tilePos.y})) surroundingMovementTiles++;
|
||||
// -x (left)
|
||||
if (!isWall({tilePos.x - 1, tilePos.y})) surroundingMovementTiles++;
|
||||
// +y (down)
|
||||
if (!isWall({tilePos.x, tilePos.y + 1})) surroundingMovementTiles++;
|
||||
// -y (up)
|
||||
if (!isWall({tilePos.x, tilePos.y - 1})) surroundingMovementTiles++;
|
||||
|
||||
return surroundingMovementTiles > 2;
|
||||
};
|
||||
|
||||
void MazeMap::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||
if (!texture || !baseMazeSprite.has_value()) return;
|
||||
|
||||
@@ -196,7 +249,7 @@ bool MazeMap::entityCanMove(Entity& entity, MovementDir dir) {
|
||||
return true;
|
||||
};
|
||||
|
||||
return !isWall(targetTile.x, targetTile.y);
|
||||
return !isWall(targetTile);
|
||||
};
|
||||
|
||||
void MazeMap::handleTunnelWrapping(Entity& entity) {
|
||||
@@ -214,3 +267,11 @@ void MazeMap::handleTunnelWrapping(Entity& entity) {
|
||||
entity.setPosition({newX, pos.y});
|
||||
}
|
||||
};
|
||||
|
||||
float MazeMap::distanceBetweenTiles(sf::Vector2i t1, sf::Vector2i t2) {
|
||||
sf::Vector2i delta = {t1.x - t2.x, t1.y - t2.y};
|
||||
|
||||
float distance = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||
|
||||
return distance;
|
||||
};
|
||||
|
||||
+18
-6
@@ -6,7 +6,9 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include "Entity.h"
|
||||
|
||||
class Entity;
|
||||
enum class MovementDir;
|
||||
|
||||
enum class PelletType {
|
||||
NONE,
|
||||
@@ -25,16 +27,22 @@ public:
|
||||
sf::Vector2u baseMazeTexturePos,
|
||||
sf::Vector2u pelletMazeTexturePos);
|
||||
|
||||
void eatPellet(int x, int y);
|
||||
//void eatPellet(int x, int y);
|
||||
void eatPellet(sf::Vector2i tilePos);
|
||||
|
||||
bool hasPellet(int x, int y) const;
|
||||
//bool hasPellet(int x, int y) const;
|
||||
bool hasPellet(sf::Vector2i tilePos) const;
|
||||
|
||||
PelletType getPelletType(sf::Vector2i tilePos) const;
|
||||
|
||||
bool isWall(int x, int y) const;
|
||||
//bool isWall(int x, int y) const;
|
||||
bool isWall(sf::Vector2i tilePos) const;
|
||||
|
||||
int convert2DCoords(int x, int y) const { return x + y * width; }
|
||||
bool isLegalTile(int x, int y) const;
|
||||
//int convert2DCoords(int x, int y) const { return x + y * width; }
|
||||
int convert2DCoords(sf::Vector2i tilePos) const { return tilePos.x + tilePos.y * width; }
|
||||
|
||||
//bool isLegalTile(int x, int y) const;
|
||||
bool isLegalTile(sf::Vector2i tilePos) const;
|
||||
|
||||
bool entityCanMove(Entity& entity, MovementDir dir);
|
||||
|
||||
@@ -48,6 +56,10 @@ public:
|
||||
|
||||
void handleTunnelWrapping(Entity& entity);
|
||||
|
||||
bool isIntersectionTile(sf::Vector2i tileCoords) const;
|
||||
|
||||
float distanceBetweenTiles(sf::Vector2i t1, sf::Vector2i t2);
|
||||
|
||||
unsigned int getWidth() const { return width; }
|
||||
unsigned int getHeight() const { return height; }
|
||||
unsigned int getTileSize() const { return tileSize; }
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "Pinky.h"
|
||||
|
||||
void Pinky::setTargetTile(Entity& pacman) {
|
||||
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
#ifndef PINKY_H
|
||||
#define PINKY_H
|
||||
|
||||
#include "Ghost.h"
|
||||
|
||||
class Pinky : public Ghost {
|
||||
public:
|
||||
void setTargetTile(Entity& pacman);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user