modify methods to only use vectors

This commit is contained in:
2025-11-11 23:45:05 -05:00
parent abac3ee7ee
commit 2d80604362
3 changed files with 63 additions and 21 deletions
+3 -3
View File
@@ -252,7 +252,7 @@ void Game::run() {
break; break;
} }
if (!map.isWall(targetTile.x, targetTile.y)) { if (!map.isWall(targetTile)) {
map.snapEntityToGrid(pacman); map.snapEntityToGrid(pacman);
switch (queued) { switch (queued) {
@@ -328,8 +328,8 @@ void Game::run() {
map.handleTunnelWrapping(pacman); map.handleTunnelWrapping(pacman);
if (map.hasPellet(currentPacmanTile.x, currentPacmanTile.y)) { if (map.hasPellet(currentPacmanTile)) {
map.eatPellet(currentPacmanTile.x, currentPacmanTile.y); map.eatPellet(currentPacmanTile);
switch (map.getPelletType(currentPacmanTile)) { switch (map.getPelletType(currentPacmanTile)) {
case PelletType::NONE: case PelletType::NONE:
+47 -13
View File
@@ -60,10 +60,10 @@ bool MazeMap::loadMaze(const std::vector<int>& collisionData,
return true; return true;
} }
void MazeMap::eatPellet(int x, int y) { void MazeMap::eatPellet(sf::Vector2i tilePos) {
if (!isLegalTile(x, y)) return; if (!isLegalTile(tilePos)) return;
int tileIndex = x + y * width; int tileIndex = convert2DCoords(tilePos);
pelletEaten[tileIndex] = true; pelletEaten[tileIndex] = true;
@@ -73,14 +73,35 @@ void MazeMap::eatPellet(int x, int y) {
} }
} }
bool MazeMap::hasPellet(int x, int y) const { //void MazeMap::eatPellet(int x, int y) {
if (!isLegalTile(x, y)) return false; // 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; 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 { PelletType MazeMap::getPelletType(sf::Vector2i tilePos) const {
int tileIndex = tilePos.x + tilePos.y * width; int tileIndex = tilePos.x + tilePos.y * width;
int pellet = pelletMap[tileIndex]; int pellet = pelletMap[tileIndex];
@@ -91,18 +112,31 @@ PelletType MazeMap::getPelletType(sf::Vector2i tilePos) const {
return PelletType::NONE; return PelletType::NONE;
} }
bool MazeMap::isWall(int x, int y) const { bool MazeMap::isWall(sf::Vector2i tilePos) const {
if (!isLegalTile(x, y)) return true; if (!isLegalTile(tilePos)) return true;
int tileIndex = convert2DCoords(tilePos);
int tileIndex = x + y * width;
return collisionMap[tileIndex] == 1; return collisionMap[tileIndex] == 1;
} }
bool MazeMap::isLegalTile(int x, int y) const { //bool MazeMap::isWall(int x, int y) const {
return x >= 0 && x < static_cast<int>(width) && // if (!isLegalTile(x, y)) return true;
y >= 0 && y < static_cast<int>(height); //
// 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);
//}
void MazeMap::draw(sf::RenderTarget& target, sf::RenderStates states) const { void MazeMap::draw(sf::RenderTarget& target, sf::RenderStates states) const {
if (!texture || !baseMazeSprite.has_value()) return; if (!texture || !baseMazeSprite.has_value()) return;
@@ -196,7 +230,7 @@ bool MazeMap::entityCanMove(Entity& entity, MovementDir dir) {
return true; return true;
}; };
return !isWall(targetTile.x, targetTile.y); return !isWall(targetTile);
}; };
void MazeMap::handleTunnelWrapping(Entity& entity) { void MazeMap::handleTunnelWrapping(Entity& entity) {
+13 -5
View File
@@ -25,16 +25,22 @@ public:
sf::Vector2u baseMazeTexturePos, sf::Vector2u baseMazeTexturePos,
sf::Vector2u pelletMazeTexturePos); 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; 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; } //int convert2DCoords(int x, int y) const { return x + y * width; }
bool isLegalTile(int x, int y) const; 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); bool entityCanMove(Entity& entity, MovementDir dir);
@@ -48,6 +54,8 @@ public:
void handleTunnelWrapping(Entity& entity); void handleTunnelWrapping(Entity& entity);
bool isIntersectionTile(sf::Vector2i tileCoords) const;
unsigned int getWidth() const { return width; } unsigned int getWidth() const { return width; }
unsigned int getHeight() const { return height; } unsigned int getHeight() const { return height; }
unsigned int getTileSize() const { return tileSize; } unsigned int getTileSize() const { return tileSize; }