mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 00:17:01 +00:00
278 lines
8.9 KiB
C++
278 lines
8.9 KiB
C++
#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,
|
|
sf::Texture& sharedTexture,
|
|
unsigned int tileSizeParam,
|
|
sf::Vector2u baseMazeTexturePos,
|
|
sf::Vector2u pelletMazeTexturePos) {
|
|
|
|
this->tileSize = tileSizeParam;
|
|
this->texture = &sharedTexture;
|
|
this->collisionMap = collisionData;
|
|
this->pelletMap = pelletData;
|
|
this->baseMazeTexPos = baseMazeTexturePos;
|
|
this->pelletMazeTexPos = pelletMazeTexturePos;
|
|
|
|
pelletEaten.resize(width * height, false);
|
|
|
|
baseMazeSprite.emplace(*texture);
|
|
baseMazeSprite->setTextureRect(sf::IntRect(
|
|
sf::Vector2i(baseMazeTexturePos),
|
|
sf::Vector2i(width * tileSize, height * tileSize)
|
|
));
|
|
|
|
pelletVertices.setPrimitiveType(sf::PrimitiveType::Triangles);
|
|
pelletVertices.resize(width * height * 6);
|
|
|
|
for (unsigned int x = 0; x < width; ++x) {
|
|
for (unsigned int y = 0; y < height; ++y) {
|
|
int tileIndex = x + y * width;
|
|
int pelletValue = pelletMap[tileIndex];
|
|
|
|
sf::Vertex* triangles = &pelletVertices[tileIndex * 6];
|
|
|
|
triangles[0].position = sf::Vector2f(x * tileSize, y * tileSize);
|
|
triangles[1].position = sf::Vector2f((x + 1) * tileSize, y * tileSize);
|
|
triangles[2].position = sf::Vector2f(x * tileSize, (y + 1) * tileSize);
|
|
triangles[3].position = sf::Vector2f(x * tileSize, (y + 1) * tileSize);
|
|
triangles[4].position = sf::Vector2f((x + 1) * tileSize, y * tileSize);
|
|
triangles[5].position = sf::Vector2f((x + 1) * tileSize, (y + 1) * tileSize);
|
|
|
|
float texX = pelletMazeTexturePos.x + x * tileSize;
|
|
float texY = pelletMazeTexturePos.y + y * tileSize;
|
|
|
|
triangles[0].texCoords = sf::Vector2f(texX, texY);
|
|
triangles[1].texCoords = sf::Vector2f(texX + tileSize, texY);
|
|
triangles[2].texCoords = sf::Vector2f(texX, texY + tileSize);
|
|
triangles[3].texCoords = sf::Vector2f(texX, texY + tileSize);
|
|
triangles[4].texCoords = sf::Vector2f(texX + tileSize, texY);
|
|
triangles[5].texCoords = sf::Vector2f(texX + tileSize, texY + tileSize);
|
|
|
|
sf::Color color = (pelletValue > 0) ? sf::Color::White : sf::Color::Transparent;
|
|
for (int i = 0; i < 6; ++i) {
|
|
triangles[i].color = color;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void MazeMap::eatPellet(sf::Vector2i tilePos) {
|
|
if (!isLegalTile(tilePos)) return;
|
|
|
|
int tileIndex = convert2DCoords(tilePos);
|
|
|
|
pelletEaten[tileIndex] = true;
|
|
|
|
sf::Vertex* triangles = &pelletVertices[tileIndex * 6];
|
|
for (int i = 0; i < 6; ++i) {
|
|
triangles[i].color = sf::Color::Transparent;
|
|
}
|
|
}
|
|
|
|
//void MazeMap::eatPellet(int x, int y) {
|
|
// 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;
|
|
// }
|
|
//}
|
|
|
|
bool MazeMap::hasPellet(sf::Vector2i tilePos) const {
|
|
if (!isLegalTile(tilePos)) return false;
|
|
|
|
int tileIndex = convert2DCoords(tilePos);
|
|
|
|
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 {
|
|
int tileIndex = tilePos.x + tilePos.y * width;
|
|
int pellet = pelletMap[tileIndex];
|
|
|
|
if (pellet == 1) return PelletType::DOT;
|
|
if (pellet == 2) return PelletType::ENERGIZER;
|
|
|
|
return PelletType::NONE;
|
|
}
|
|
|
|
bool MazeMap::isWall(sf::Vector2i tilePos) const {
|
|
if (!isLegalTile(tilePos)) return true;
|
|
|
|
int tileIndex = convert2DCoords(tilePos);
|
|
|
|
return collisionMap[tileIndex] == 1;
|
|
}
|
|
|
|
//bool MazeMap::isWall(int x, int y) const {
|
|
// if (!isLegalTile(x, y)) return true;
|
|
//
|
|
// 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);
|
|
//}
|
|
|
|
bool MazeMap::isIntersectionTile(sf::Vector2i tilePos) const {
|
|
// true if more than two surrounding tiles are not walls
|
|
|
|
int surroundingMovementTiles = 0;
|
|
|
|
// +x (right)
|
|
if (!isWall({tilePos.x + 1, tilePos.y})) surroundingMovementTiles++;
|
|
// -x (left)
|
|
if (!isWall({tilePos.x - 1, tilePos.y})) surroundingMovementTiles++;
|
|
// +y (down)
|
|
if (!isWall({tilePos.x, tilePos.y + 1})) surroundingMovementTiles++;
|
|
// -y (up)
|
|
if (!isWall({tilePos.x, tilePos.y - 1})) surroundingMovementTiles++;
|
|
|
|
return surroundingMovementTiles > 2;
|
|
};
|
|
|
|
void MazeMap::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
|
if (!texture || !baseMazeSprite.has_value()) return;
|
|
|
|
states.transform *= getTransform();
|
|
states.texture = texture;
|
|
|
|
target.draw(*baseMazeSprite, states);
|
|
|
|
target.draw(pelletVertices, states);
|
|
}
|
|
|
|
// assumes maze is drawn at 0,0
|
|
sf::Vector2i MazeMap::getTileCoords(sf::Vector2f screenPos) {
|
|
int tileX = static_cast<int>(screenPos.x / tileSize);
|
|
int tileY = static_cast<int>(screenPos.y / tileSize);
|
|
|
|
return {tileX, tileY};
|
|
};
|
|
|
|
sf::Vector2f MazeMap::getTargetTileCenter(sf::Vector2i tileCoords) const {
|
|
float halfTile = tileSize / 2.0f;
|
|
return sf::Vector2f(tileCoords.x * tileSize + halfTile,
|
|
tileCoords.y * tileSize + halfTile);
|
|
};
|
|
|
|
bool MazeMap::isEntityCentered(const Entity& entity) const {
|
|
sf::Vector2f pos = entity.getPosition();
|
|
float halfTile = tileSize / 2.0f;
|
|
|
|
sf::Vector2i tileCoords = const_cast<MazeMap*>(this)->getTileCoords(pos);
|
|
sf::Vector2f tileCenter(tileCoords.x * tileSize + halfTile,
|
|
tileCoords.y * tileSize + halfTile);
|
|
|
|
float dx = std::abs(pos.x - tileCenter.x);
|
|
float dy = std::abs(pos.y - tileCenter.y);
|
|
|
|
return dx < 1.0f && dy < 1.0f;
|
|
}
|
|
|
|
void MazeMap::snapEntityToGrid(Entity& entity) {
|
|
sf::Vector2f pos = entity.getPosition();
|
|
float halfTile = tileSize / 2.0f;
|
|
|
|
sf::Vector2i tileCoords = getTileCoords(pos);
|
|
sf::Vector2f tileCenter(tileCoords.x * tileSize + halfTile,
|
|
tileCoords.y * tileSize + halfTile);
|
|
|
|
entity.setPosition(tileCenter);
|
|
}
|
|
|
|
bool MazeMap::entityCanMove(Entity& entity, MovementDir dir) {
|
|
sf::Vector2f currentPos = entity.getPosition();
|
|
sf::Vector2i currentTile = getTileCoords(currentPos);
|
|
|
|
if (dir == MovementDir::UP || dir == MovementDir::DOWN) {
|
|
float halfTile = tileSize / 2.0f;
|
|
float tileCenterX = currentTile.x * tileSize + halfTile;
|
|
if (std::abs(currentPos.x - tileCenterX) > 1.0f) {
|
|
return false;
|
|
}
|
|
} else if (dir == MovementDir::LEFT || dir == MovementDir::RIGHT) {
|
|
float halfTile = tileSize / 2.0f;
|
|
float tileCenterY = currentTile.y * tileSize + halfTile;
|
|
if (std::abs(currentPos.y - tileCenterY) > 1.0f) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
sf::Vector2i targetTile = currentTile;
|
|
|
|
switch (dir) {
|
|
case MovementDir::UP:
|
|
targetTile.y -= 1;
|
|
break;
|
|
case MovementDir::DOWN:
|
|
targetTile.y += 1;
|
|
break;
|
|
case MovementDir::LEFT:
|
|
targetTile.x -= 1;
|
|
if (targetTile.x < 0) {
|
|
targetTile.x = width - 1;
|
|
}
|
|
break;
|
|
case MovementDir::RIGHT:
|
|
targetTile.x += 1;
|
|
if (targetTile.x >= static_cast<int>(width)) {
|
|
targetTile.x = 0;
|
|
}
|
|
break;
|
|
case MovementDir::STATIC:
|
|
return true;
|
|
};
|
|
|
|
return !isWall(targetTile);
|
|
};
|
|
|
|
void MazeMap::handleTunnelWrapping(Entity& entity) {
|
|
sf::Vector2f pos = entity.getPosition();
|
|
sf::Vector2i tile = getTileCoords(pos);
|
|
MovementDir dir = entity.getCurrentDirection();
|
|
|
|
float halfTile = tileSize / 2.0f;
|
|
|
|
if (tile.x == 0 && dir == MovementDir::LEFT && !entity.isCurrentlyMoving()) {
|
|
float newX = (width - 1) * tileSize + halfTile;
|
|
entity.setPosition({newX, pos.y});
|
|
} else if (tile.x == static_cast<int>(width - 1) && dir == MovementDir::RIGHT && !entity.isCurrentlyMoving()) {
|
|
float newX = 0 * tileSize + halfTile;
|
|
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;
|
|
};
|