mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
added 'vulnerability' state to ghosts that so far does nothing but changes their texture and pathing.
This commit is contained in:
@@ -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"), {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"), {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"), {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.setActiveSprite("left_walking", 0);
|
||||||
blinky.setOrigin({30, 30});
|
blinky.setOrigin({30, 30});
|
||||||
blinky.setPosition({14.5 * 32.0f - 16.0f, 12 * 32.0f - 16.0f});
|
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"), {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"), {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"), {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.setActiveSprite("down_walking", 0);
|
||||||
pinky.setOrigin({30, 30});
|
pinky.setOrigin({30, 30});
|
||||||
pinky.setPosition({14.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
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"), {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"), {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"), {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.setActiveSprite("up_walking", 0);
|
||||||
inky.setOrigin({30, 30});
|
inky.setOrigin({30, 30});
|
||||||
inky.setPosition({12.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
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"), {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"), {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"), {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.setActiveSprite("up_walking", 0);
|
||||||
clyde.setOrigin({30, 30});
|
clyde.setOrigin({30, 30});
|
||||||
clyde.setPosition({16.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
clyde.setPosition({16.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f});
|
||||||
@@ -186,6 +190,11 @@ void Game::run() {
|
|||||||
bool inkyReleased = false;
|
bool inkyReleased = false;
|
||||||
bool clydeReleased = 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);
|
auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName);
|
||||||
|
|
||||||
sf::Sound sound(*resources.getSound("start"));
|
sf::Sound sound(*resources.getSound("start"));
|
||||||
@@ -256,6 +265,15 @@ void Game::run() {
|
|||||||
clydeReleased = true;
|
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
|
// updates ghost behavior modes when time is up
|
||||||
sf::Time modeElapsed = modeClock.getElapsedTime();
|
sf::Time modeElapsed = modeClock.getElapsedTime();
|
||||||
if (currentlyScatter && modeElapsed > sf::seconds(scatterDurationSeconds)) {
|
if (currentlyScatter && modeElapsed > sf::seconds(scatterDurationSeconds)) {
|
||||||
@@ -449,6 +467,13 @@ void Game::run() {
|
|||||||
case PelletType::ENERGIZER:
|
case PelletType::ENERGIZER:
|
||||||
score += 50;
|
score += 50;
|
||||||
fright.play();
|
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;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "MazeMap.h"
|
#include "MazeMap.h"
|
||||||
#include "Pacman.h"
|
#include "Pacman.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <SFML/System/Clock.hpp>
|
||||||
|
|
||||||
static MovementDir oppositeDirection(MovementDir d) {
|
static MovementDir oppositeDirection(MovementDir d) {
|
||||||
switch (d) {
|
switch (d) {
|
||||||
@@ -34,6 +35,20 @@ void Ghost::updateAI(MazeMap& map, const Pacman& pacman) {
|
|||||||
if (currentTile.y > boxBoundaryY) {
|
if (currentTile.y > boxBoundaryY) {
|
||||||
// Force target back toward exit
|
// Force target back toward exit
|
||||||
targetTile = boxExitTile;
|
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) {
|
} else if (currentMode == Mode::CHASE) {
|
||||||
// Normal chase/scatter AI
|
// Normal chase/scatter AI
|
||||||
sf::Vector2i pacmanTile = map.getTileCoords(pacman.getPosition());
|
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
|
// 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) {
|
switch (dir) {
|
||||||
case MovementDir::UP: setActiveSprite("up_walking", 0); break;
|
case MovementDir::UP: setActiveSprite("up_walking", 0); break;
|
||||||
case MovementDir::DOWN: setActiveSprite("down_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;
|
case MovementDir::RIGHT: setActiveSprite("right_walking", 0); break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lastDirection = dir;
|
lastDirection = dir;
|
||||||
allowReversal = false; // Reset reversal flag after making a move
|
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
@@ -10,7 +10,8 @@ class Ghost : public Entity {
|
|||||||
public:
|
public:
|
||||||
enum class Mode {
|
enum class Mode {
|
||||||
SCATTER,
|
SCATTER,
|
||||||
CHASE
|
CHASE,
|
||||||
|
VULNERABLE
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class AIType {
|
enum class AIType {
|
||||||
@@ -21,7 +22,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ghost(AIType type = AIType::BLINKY)
|
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) {
|
void setMode(Mode m) {
|
||||||
currentMode = m;
|
currentMode = m;
|
||||||
@@ -29,6 +30,11 @@ public:
|
|||||||
}
|
}
|
||||||
Mode getMode() const { return currentMode; }
|
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)
|
// Set the exit tile for the ghost box (where ghost should exit toward)
|
||||||
void setBoxExitTile(sf::Vector2i exitTile) { boxExitTile = exitTile; }
|
void setBoxExitTile(sf::Vector2i exitTile) { boxExitTile = exitTile; }
|
||||||
|
|
||||||
@@ -46,6 +52,8 @@ private:
|
|||||||
bool hasExitedBox;
|
bool hasExitedBox;
|
||||||
sf::Vector2i boxExitTile;
|
sf::Vector2i boxExitTile;
|
||||||
int boxBoundaryY;
|
int boxBoundaryY;
|
||||||
|
bool isVulnerable;
|
||||||
|
Mode previousMode; // Mode to return to after vulnerable ends
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user