diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fa88ca..e5c1426 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) +add_executable(main src/main.cpp src/Entity.cpp src/Game.cpp src/MazeMap.cpp src/ResourceManager.cpp src/Ghost.cpp src/Pacman.cpp) target_compile_features(main PRIVATE cxx_std_17) target_link_libraries(main PRIVATE SFML::Graphics SFML::Audio nlohmann_json::nlohmann_json) @@ -28,4 +28,4 @@ add_custom_command(TARGET main POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets - COMMENT "Copying assets to build directory") \ No newline at end of file + COMMENT "Copying assets to build directory") diff --git a/src/Game.cpp b/src/Game.cpp index 8b421e1..16bd1dd 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -91,10 +91,10 @@ void Game::run() { Pacman pacman; - Ghost blinky; - Ghost pinky; - Ghost inky; - Ghost clyde; + Ghost blinky(Ghost::AIType::BLINKY); + Ghost pinky(Ghost::AIType::PINKY); + 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); @@ -115,6 +115,7 @@ void Game::run() { 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}); 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); @@ -123,6 +124,7 @@ void Game::run() { 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}); 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); @@ -131,6 +133,7 @@ void Game::run() { 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}); 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); @@ -139,6 +142,49 @@ void Game::run() { 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}); + + // 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) + blinky.setBoxExitTile({14, 12}); + blinky.setBoxBoundaryY(12); + + pinky.setBoxExitTile({14, 12}); + pinky.setBoxBoundaryY(12); + inky.setBoxExitTile({14, 12}); + inky.setBoxBoundaryY(12); + clyde.setBoxExitTile({14, 12}); + clyde.setBoxBoundaryY(12); + + // Initialize ghost modes (start in Scatter) + blinky.setMode(Ghost::Mode::SCATTER); + pinky.setMode(Ghost::Mode::SCATTER); + inky.setMode(Ghost::Mode::SCATTER); + clyde.setMode(Ghost::Mode::SCATTER); + + // Mode scheduler: simple alternating scatter/chase timer + sf::Clock modeClock; + const float scatterDurationSeconds = 7.0f; + const float chaseDurationSeconds = 20.0f; + bool currentlyScatter = true; + modeClock.restart(); + + // Ghost exit timers (staggered release from ghost box) + sf::Clock blinkyExitClock; + sf::Clock pinkyExitClock; + sf::Clock inkyExitClock; + sf::Clock clydeExitClock; + + // Offset timers by the intro pause (~5s) so ghosts release after intro + const float blinkyExitDelaySeconds = 5.0f; // Blinky exits after 5 seconds (intro offset) + const float pinkyExitDelaySeconds = 10.0f; // Pinky exits after 10 seconds + const float inkyExitDelaySeconds = 15.0f; // Inky exits after 15 seconds + const float clydeExitDelaySeconds = 20.0f; // Clyde exits after 20 seconds + + bool blinkyReleased = true; // Blinky starts immediately + bool pinkyReleased = false; + bool inkyReleased = false; + bool clydeReleased = false; auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName); @@ -199,6 +245,37 @@ void Game::run() { accumulator -= FIXED_TIMESTEP; if (initialGameplayPause > sf::seconds(4.5f)) { + // Check ghost exit timers (staggered release) + if (!pinkyReleased && pinkyExitClock.getElapsedTime() > sf::seconds(pinkyExitDelaySeconds)) { + pinkyReleased = true; + } + if (!inkyReleased && inkyExitClock.getElapsedTime() > sf::seconds(inkyExitDelaySeconds)) { + inkyReleased = true; + } + if (!clydeReleased && clydeExitClock.getElapsedTime() > sf::seconds(clydeExitDelaySeconds)) { + clydeReleased = true; + } + + // updates ghost behavior modes when time is up + sf::Time modeElapsed = modeClock.getElapsedTime(); + if (currentlyScatter && modeElapsed > sf::seconds(scatterDurationSeconds)) { + // switch to chase + currentlyScatter = false; + modeClock.restart(); + blinky.setMode(Ghost::Mode::CHASE); + pinky.setMode(Ghost::Mode::CHASE); + inky.setMode(Ghost::Mode::CHASE); + clyde.setMode(Ghost::Mode::CHASE); + } else if (!currentlyScatter && modeElapsed > sf::seconds(chaseDurationSeconds)) { + // switch to scatter + currentlyScatter = true; + modeClock.restart(); + blinky.setMode(Ghost::Mode::SCATTER); + pinky.setMode(Ghost::Mode::SCATTER); + inky.setMode(Ghost::Mode::SCATTER); + clyde.setMode(Ghost::Mode::SCATTER); + } + sf::Vector2i currentPacmanTile = map.getTileCoords(pacman.getPosition()); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || @@ -332,6 +409,34 @@ void Game::run() { map.handleTunnelWrapping(pacman); + // Update Blinky's AI and movement (Blinky is the red ghost) - released immediately + if (blinkyReleased) { + blinky.updateAI(map, pacman); + blinky.update(); + map.handleTunnelWrapping(blinky); + } + + // Update Pinky's AI and movement (Pinky is the pink ghost) - released after delay + if (pinkyReleased) { + pinky.updateAI(map, pacman); + pinky.update(); + map.handleTunnelWrapping(pinky); + } + + // Update Inky's AI and movement (Inky is the blue ghost) - released after delay + if (inkyReleased) { + inky.updateAI(map, pacman); + inky.update(); + map.handleTunnelWrapping(inky); + } + + // Update Clyde's AI and movement (Clyde is the orange ghost) - released after delay + if (clydeReleased) { + clyde.updateAI(map, pacman); + clyde.update(); + map.handleTunnelWrapping(clyde); + } + if (map.hasPellet(currentPacmanTile.x, currentPacmanTile.y)) { map.eatPellet(currentPacmanTile.x, currentPacmanTile.y); diff --git a/src/Ghost.cpp b/src/Ghost.cpp index a16bc4f..4704902 100644 --- a/src/Ghost.cpp +++ b/src/Ghost.cpp @@ -1 +1,198 @@ #include "Ghost.h" +#include "MazeMap.h" +#include "Pacman.h" +#include + +static MovementDir oppositeDirection(MovementDir d) { + switch (d) { + case MovementDir::UP: return MovementDir::DOWN; + case MovementDir::DOWN: return MovementDir::UP; + case MovementDir::LEFT: return MovementDir::RIGHT; + case MovementDir::RIGHT: return MovementDir::LEFT; + case MovementDir::STATIC: return MovementDir::STATIC; + } + return MovementDir::STATIC; +} + +void Ghost::updateAI(MazeMap& map, const Pacman& pacman) { + // Only decide a new move when the ghost is not currently moving + if (isCurrentlyMoving()) return; + + sf::Vector2i currentTile = map.getTileCoords(getPosition()); + + sf::Vector2i targetTile; + + // If ghost hasn't exited the box yet, force it to target the exit + if (!hasExitedBox) { + targetTile = boxExitTile; + // Check if we've reached or passed the exit + if (currentTile.y <= boxBoundaryY) { + hasExitedBox = true; + } + } else { + // Prevent re-entry into box + if (currentTile.y > boxBoundaryY) { + // Force target back toward exit + targetTile = boxExitTile; + } else if (currentMode == Mode::CHASE) { + // Normal chase/scatter AI + sf::Vector2i pacmanTile = map.getTileCoords(pacman.getPosition()); + + if (aiType == AIType::BLINKY) { + // Blinky: direct chase to Pacman + targetTile = pacmanTile; + } else if (aiType == AIType::PINKY) { + // Pinky: ambush 4 tiles ahead of Pacman + MovementDir pacmanDir = pacman.getCurrentDirection(); + targetTile = pacmanTile; + + // Look 4 tiles ahead in Pacman's direction of travel + for (int i = 0; i < 4; i++) { + switch (pacmanDir) { + case MovementDir::UP: + targetTile.y -= 1; + if (targetTile.y < 0) targetTile.y = static_cast(map.getHeight()) - 1; + break; + case MovementDir::DOWN: + targetTile.y += 1; + if (targetTile.y >= static_cast(map.getHeight())) targetTile.y = 0; + break; + case MovementDir::LEFT: + targetTile.x -= 1; + if (targetTile.x < 0) targetTile.x = static_cast(map.getWidth()) - 1; + break; + case MovementDir::RIGHT: + targetTile.x += 1; + if (targetTile.x >= static_cast(map.getWidth())) targetTile.x = 0; + break; + case MovementDir::STATIC: + break; + } + } + } else if (aiType == AIType::INKY) { + // Inky: targets 2 tiles ahead of Pacman + MovementDir pacmanDir = pacman.getCurrentDirection(); + targetTile = pacmanTile; + + // Look 2 tiles ahead in Pacman's direction of travel + for (int i = 0; i < 2; i++) { + switch (pacmanDir) { + case MovementDir::UP: + targetTile.y -= 1; + if (targetTile.y < 0) targetTile.y = static_cast(map.getHeight()) - 1; + break; + case MovementDir::DOWN: + targetTile.y += 1; + if (targetTile.y >= static_cast(map.getHeight())) targetTile.y = 0; + break; + case MovementDir::LEFT: + targetTile.x -= 1; + if (targetTile.x < 0) targetTile.x = static_cast(map.getWidth()) - 1; + break; + case MovementDir::RIGHT: + targetTile.x += 1; + if (targetTile.x >= static_cast(map.getWidth())) targetTile.x = 0; + break; + case MovementDir::STATIC: + break; + } + } + } else { + // Clyde: direct chase to Pacman + targetTile = pacmanTile; + } + } else { + // Scatter: each ghost goes to their corner + if (aiType == AIType::BLINKY) { + // Blinky: top-right corner + targetTile = {static_cast(map.getWidth()) - 1, 0}; + } else if (aiType == AIType::PINKY) { + // Pinky: top-left corner + targetTile = {0, 0}; + } else if (aiType == AIType::INKY) { + // Inky: bottom-right corner + targetTile = {static_cast(map.getWidth()) - 1, static_cast(map.getHeight()) - 1}; + } else { + // Clyde: bottom-left corner + targetTile = {0, static_cast(map.getHeight()) - 1}; + } + } + } + + int dx = targetTile.x - currentTile.x; + int dy = targetTile.y - currentTile.y; + + MovementDir primary, secondary; + if (std::abs(dx) >= std::abs(dy)) { + primary = dx > 0 ? MovementDir::RIGHT : MovementDir::LEFT; + secondary = dy > 0 ? MovementDir::DOWN : MovementDir::UP; + } else { + primary = dy > 0 ? MovementDir::DOWN : MovementDir::UP; + secondary = dx > 0 ? MovementDir::RIGHT : MovementDir::LEFT; + } + + // Build try order. If we're on the box exit tile (or the tile directly below it) + // prefer moving away from the box to avoid circling: prefer UP first, then LEFT/RIGHT. + bool onExitOrBelow = (currentTile == boxExitTile) || (currentTile == sf::Vector2i(boxExitTile.x, boxExitTile.y + 1)); + MovementDir tryOrder[4]; + if (onExitOrBelow) { + tryOrder[0] = MovementDir::UP; + tryOrder[1] = MovementDir::LEFT; + tryOrder[2] = MovementDir::RIGHT; + tryOrder[3] = MovementDir::DOWN; + } else { + // Default behaviour: head primarily in the direction closer to target + tryOrder[0] = primary; + tryOrder[1] = secondary; + tryOrder[2] = oppositeDirection(secondary); + tryOrder[3] = oppositeDirection(primary); + } + MovementDir oppositeOfLast = oppositeDirection(lastDirection); + + for (auto dir : tryOrder) { + if (dir == MovementDir::STATIC) continue; + + // Prevent moving down from the box exit tile (don't allow re-entry) + if (currentTile == boxExitTile && dir == MovementDir::DOWN) { + continue; + } + + // Skip reverse move unless we just changed modes or it's the first move + if (!allowReversal && dir == oppositeOfLast && lastDirection != MovementDir::STATIC) { + continue; + } + + if (map.entityCanMove(*this, dir)) { + 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; + if (nextTile.x < 0) nextTile.x = static_cast(map.getWidth()) - 1; + break; + case MovementDir::RIGHT: + nextTile.x += 1; + if (nextTile.x >= static_cast(map.getWidth())) nextTile.x = 0; + break; + case MovementDir::STATIC: break; + } + + // Set appropriate sprite and start the move toward the tile center + switch (dir) { + case MovementDir::UP: setActiveSprite("up_walking", 0); break; + case MovementDir::DOWN: setActiveSprite("down_walking", 0); break; + case MovementDir::LEFT: setActiveSprite("left_walking", 0); break; + case MovementDir::RIGHT: setActiveSprite("right_walking", 0); break; + default: break; + } + + lastDirection = dir; + allowReversal = false; // Reset reversal flag after making a move + sf::Vector2f center = map.getTargetTileCenter(nextTile); + startMove(dir, center); + return; + } + } +} + diff --git a/src/Ghost.h b/src/Ghost.h index ac40815..69f5726 100644 --- a/src/Ghost.h +++ b/src/Ghost.h @@ -3,8 +3,49 @@ #include "Entity.h" -class Ghost : public Entity { +class MazeMap; +class Pacman; +class Ghost : public Entity { +public: + enum class Mode { + SCATTER, + CHASE + }; + + enum class AIType { + BLINKY, // Red: direct chase + PINKY, // Pink: ambush (4 tiles ahead) + INKY, // Blue: complex targeting (placeholder for now) + CLYDE // Orange: placeholder + }; + + Ghost(AIType type = AIType::BLINKY) + : currentMode(Mode::SCATTER), lastDirection(MovementDir::STATIC), allowReversal(false), aiType(type), hasExitedBox(type == AIType::BLINKY) {} + + void setMode(Mode m) { + currentMode = m; + allowReversal = true; // Allow reversal when mode changes + } + Mode getMode() const { return currentMode; } + + // Set the exit tile for the ghost box (where ghost should exit toward) + void setBoxExitTile(sf::Vector2i exitTile) { boxExitTile = exitTile; } + + // Set the box boundary (ghosts can't re-enter beyond this Y coordinate) + void setBoxBoundaryY(int boundaryY) { boxBoundaryY = boundaryY; } + + // AI step for ghosts with type-specific targeting logic + void updateAI(MazeMap& map, const Pacman& pacman); + +private: + Mode currentMode; + MovementDir lastDirection; + bool allowReversal; + AIType aiType; + bool hasExitedBox; + sf::Vector2i boxExitTile; + int boxBoundaryY; }; #endif