add initial ghost logic

This commit is contained in:
2025-11-15 20:56:25 -05:00
parent 20eb36945b
commit e7d999109d
6 changed files with 104 additions and 5 deletions
+60 -1
View File
@@ -1,5 +1,64 @@
#include "Blinky.h"
#include "Entity.h"
#include "MazeMap.h"
#include <limits>
void Blinky::setTargetTile(Entity& pacman) {
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);
}
};
+4 -1
View File
@@ -2,10 +2,13 @@
#define BLINKY_H
#include "Ghost.h"
#include <SFML/System/Vector2.hpp>
class Blinky : public Ghost {
public:
void setTargetTile(Entity& pacman);
void setTargetTile(sf::Vector2i target);
void setBestMove(Entity& pacman);
};
#endif
+18 -1
View File
@@ -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
+7 -1
View File
@@ -10,12 +10,12 @@
#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);
@@ -330,6 +330,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);
if (map.hasPellet(currentPacmanTile)) {
+10
View File
@@ -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,
@@ -265,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;
};
+5 -1
View File
@@ -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,
@@ -56,6 +58,8 @@ public:
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; }