added 'vulnerability' state to ghosts that so far does nothing but changes their texture and pathing.

This commit is contained in:
BoogieChild
2025-11-17 11:13:53 -05:00
parent edc1f9fc9c
commit 673e0b3bc9
3 changed files with 81 additions and 9 deletions
+25
View File
@@ -112,6 +112,7 @@ void Game::run() {
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.setActiveSprite("left_walking", 0);
blinky.setOrigin({30, 30});
blinky.setPosition({14.5 * 32.0f - 16.0f, 12 * 32.0f - 16.0f});
@@ -121,6 +122,7 @@ void Game::run() {
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.setActiveSprite("down_walking", 0);
pinky.setOrigin({30, 30});
pinky.setPosition({14.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
@@ -130,6 +132,7 @@ void Game::run() {
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.setActiveSprite("up_walking", 0);
inky.setOrigin({30, 30});
inky.setPosition({12.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
@@ -139,6 +142,7 @@ void Game::run() {
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.setActiveSprite("up_walking", 0);
clyde.setOrigin({30, 30});
clyde.setPosition({16.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
@@ -186,6 +190,11 @@ void Game::run() {
bool inkyReleased = false;
bool clydeReleased = false;
// Vulnerable mode timer
sf::Clock vulnerableTimer;
bool vulnerableModeActive = false;
const float vulnerableDurationSeconds = 8.0f; // Duration of vulnerable mode
auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName);
sf::Sound sound(*resources.getSound("start"));
@@ -256,6 +265,15 @@ void Game::run() {
clydeReleased = true;
}
// Check if vulnerable mode should end
if (vulnerableModeActive && vulnerableTimer.getElapsedTime() > sf::seconds(vulnerableDurationSeconds)) {
vulnerableModeActive = false;
blinky.setVulnerable(false);
pinky.setVulnerable(false);
inky.setVulnerable(false);
clyde.setVulnerable(false);
}
// updates ghost behavior modes when time is up
sf::Time modeElapsed = modeClock.getElapsedTime();
if (currentlyScatter && modeElapsed > sf::seconds(scatterDurationSeconds)) {
@@ -449,6 +467,13 @@ void Game::run() {
case PelletType::ENERGIZER:
score += 50;
fright.play();
// Activate vulnerable mode for all ghosts
vulnerableModeActive = true;
vulnerableTimer.restart();
blinky.setVulnerable(true);
pinky.setVulnerable(true);
inky.setVulnerable(true);
clyde.setVulnerable(true);
break;
};
+40 -1
View File
@@ -2,6 +2,7 @@
#include "MazeMap.h"
#include "Pacman.h"
#include <cmath>
#include <SFML/System/Clock.hpp>
static MovementDir oppositeDirection(MovementDir d) {
switch (d) {
@@ -14,7 +15,7 @@ static MovementDir oppositeDirection(MovementDir d) {
return MovementDir::STATIC;
}
void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
// Only decide a new move when the ghost is not currently moving
if (isCurrentlyMoving()) return;
@@ -34,6 +35,20 @@ void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
if (currentTile.y > boxBoundaryY) {
// Force target back toward exit
targetTile = boxExitTile;
} else if (currentMode == Mode::VULNERABLE) {
// In vulnerable mode, run away from Pacman
sf::Vector2i pacmanTile = map.getTileCoords(pacman.getPosition());
// Target a tile far away from Pacman (simple implementation: go to opposite corner)
if (pacmanTile.x < map.getWidth() / 2) {
targetTile.x = map.getWidth() - 1; // Far right
} else {
targetTile.x = 0; // Far left
}
if (pacmanTile.y < map.getHeight() / 2) {
targetTile.y = map.getHeight() - 1; // Far bottom
} else {
targetTile.y = 0; // Far top
}
} else if (currentMode == Mode::CHASE) {
// Normal chase/scatter AI
sf::Vector2i pacmanTile = map.getTileCoords(pacman.getPosition());
@@ -179,6 +194,10 @@ void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
}
// Set appropriate sprite and start the move toward the tile center
if (isVulnerable) {
// Use vulnerable sprite regardless of direction
setActiveSprite("vulnerable", 0);
} else {
switch (dir) {
case MovementDir::UP: setActiveSprite("up_walking", 0); break;
case MovementDir::DOWN: setActiveSprite("down_walking", 0); break;
@@ -186,6 +205,7 @@ void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
case MovementDir::RIGHT: setActiveSprite("right_walking", 0); break;
default: break;
}
}
lastDirection = dir;
allowReversal = false; // Reset reversal flag after making a move
@@ -196,3 +216,22 @@ void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
}
}
void Ghost::setVulnerable(bool vulnerable) {
if (vulnerable && !isVulnerable) {
// Entering vulnerable mode
previousMode = currentMode; // Save current mode
currentMode = Mode::VULNERABLE;
isVulnerable = true;
allowReversal = true; // Allow direction reversal when becoming vulnerable
} else if (!vulnerable && isVulnerable) {
// Exiting vulnerable mode
isVulnerable = false;
currentMode = previousMode; // Restore previous mode
allowReversal = true; // Allow direction reversal when exiting vulnerable
}
}
void Ghost::endVulnerable() {
setVulnerable(false);
}
+10 -2
View File
@@ -10,7 +10,8 @@ class Ghost : public Entity {
public:
enum class Mode {
SCATTER,
CHASE
CHASE,
VULNERABLE
};
enum class AIType {
@@ -21,7 +22,7 @@ public:
};
Ghost(AIType type = AIType::BLINKY)
: currentMode(Mode::SCATTER), lastDirection(MovementDir::STATIC), allowReversal(false), aiType(type), hasExitedBox(type == AIType::BLINKY) {}
: currentMode(Mode::SCATTER), lastDirection(MovementDir::STATIC), allowReversal(false), aiType(type), hasExitedBox(type == AIType::BLINKY), isVulnerable(false), previousMode(Mode::SCATTER) {}
void setMode(Mode m) {
currentMode = m;
@@ -29,6 +30,11 @@ public:
}
Mode getMode() const { return currentMode; }
// Vulnerable mode methods
void setVulnerable(bool vulnerable);
bool getIsVulnerable() const { return isVulnerable; }
void endVulnerable();
// Set the exit tile for the ghost box (where ghost should exit toward)
void setBoxExitTile(sf::Vector2i exitTile) { boxExitTile = exitTile; }
@@ -46,6 +52,8 @@ private:
bool hasExitedBox;
sf::Vector2i boxExitTile;
int boxBoundaryY;
bool isVulnerable;
Mode previousMode; // Mode to return to after vulnerable ends
};
#endif