diff --git a/CMakeLists.txt b/CMakeLists.txt index 51c8685..f61f194 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,6 @@ FetchContent_Declare(json FetchContent_MakeAvailable(SFML json) -add_executable(main src/main.cpp src/TileMap.cpp src/Entity.cpp) +add_executable(main src/main.cpp src/Entity.cpp src/Game.cpp src/MazeMap.cpp src/ResourceManager.cpp) target_compile_features(main PRIVATE cxx_std_17) target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json) diff --git a/assets/all_textures.png b/assets/all_textures.png new file mode 100644 index 0000000..cf01851 Binary files /dev/null and b/assets/all_textures.png differ diff --git a/assets/maze.txt b/assets/maze.txt new file mode 100644 index 0000000..8a61ec5 --- /dev/null +++ b/assets/maze.txt @@ -0,0 +1,31 @@ +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 +1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 +1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 +1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 +1 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 +1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 +1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 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1,7 +1,6 @@ #ifndef ENTITY_H #define ENTITY_H -#include "TileMap.h" #include #include #include @@ -23,13 +22,13 @@ enum class MovementDir { class Entity : public sf::Drawable, public sf::Transformable { public: - Entity(TileMap& map) : map(map) {}; + //Entity(TileMap& map) : map(map) {}; bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles); void setTextureFrame(const sf::IntRect& rectangle); protected: - TileMap map; + //TileMap map; sf::VertexArray vertexArray; sf::Texture texture; diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..cfdf95b --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,52 @@ +#include + +#include "Game.h" +#include "MazeMap.h" +#include "ResourceManager.h" + +void Game::run() { + ResourceManager resources; + MazeMap map; + + resources.loadTexture("all_textures", "assets/all_textures.png"); + + // resize all_textures to 32px + resources.resizeTexture("all_textures", 32); + // Collision data: 1=wall, 0=path + resources.loadMap("mazeMap", "assets/maze.txt"); + // Pellet data: 0=none, 1=pellet, 2=power + resources.loadMap("pelletMap", "assets/pellets.txt"); + + // Texture layout: [Pellet Maze Image] + gap (16px) + [Base Maze Image] + // Original: 224px (28*8) + 4px gap at 8px tiles + // Scaled 4x: 896px (28*32) + 16px gap at 32px tiles + map.loadMaze(resources.getMazeMap(), + resources.getPelletMap(), + resources.getTexture("all_textures"), + 32, + {912, 0}, + {0, 0}); + + auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName); + window.setFramerateLimit(maxFPS); + + while (window.isOpen()) + { + while (const std::optional event = window.pollEvent()) + { + if (event->is()) + { + window.close(); + } + } + + window.clear(); + + window.draw(map); + + //window.draw(); + + window.display(); + } + +}; diff --git a/src/Game.h b/src/Game.h new file mode 100644 index 0000000..2f736d9 --- /dev/null +++ b/src/Game.h @@ -0,0 +1,19 @@ +#ifndef GAME_H +#define GAME_H + +#include +#include +class Game { +public: + Game() {}; + Game(sf::Vector2u windowRes, std::string& windowName, int maxFPS) : windowRes(windowRes), windowName(windowName), maxFPS(maxFPS) {}; + + void run(); + +private: + sf::Vector2u windowRes = {1920, 1080}; + std::string windowName = "Pacmen"; + unsigned int maxFPS = 60; +}; + +#endif diff --git a/src/MazeCharacter.h b/src/MazeCharacter.h deleted file mode 100644 index 5053794..0000000 --- a/src/MazeCharacter.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef MAZECHARACTER_H -#define MAZECHARACTER_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include "TileMap.h" - -enum class MovementDir { - STATIC, - UP, - DOWN, - LEFT, - RIGHT -}; - -class MazeCharacter : public sf::Transformable, public sf::Drawable { -public: - MazeCharacter(const std::filesystem::path& textureFile, TileMap& map, int tileSize = 32, int baseMovementSpeed = 5) : tileSize(tileSize), baseMovementSpeed(baseMovementSpeed), texture(textureFile), mazeMap(map) { - sprite = new sf::Sprite(texture); - }; - - void changeDirection(MovementDir dir) { - switch (dir) { - case MovementDir::UP: - veloDir = transformVeloUp; - break; - case MovementDir::DOWN: - veloDir = transformVeloDown; - break; - case MovementDir::LEFT: - veloDir = transformVeloLeft; - break; - case MovementDir::RIGHT: - veloDir = transformVeloRight; - break; - }; - }; - - ~MazeCharacter() { - delete sprite; - } - -private: - int tileSize = 0; - int halfTileSize = tileSize / 2; - std::vector legalTileIds; - - sf::Texture texture; - sf::Sprite* sprite; - - sf::Vector2i tilePos; - sf::Vector2f screenPos; - sf::Vector2f centerPos = {screenPos.x + halfTileSize, screenPos.y + halfTileSize}; - - sf::Vector2f velo; - sf::Vector2i veloDir; - - sf::Vector2i transformVeloUp{0, -1}; - sf::Vector2i transformVeloDown{0, 1}; - sf::Vector2i transformVeloRight{1, 0}; - sf::Vector2i transformVeloLeft{-1, 0}; - - float baseMovementSpeed = 0; - - TileMap mazeMap; - - void draw(sf::RenderTarget& target, sf::RenderStates states) const override { - states.transform *= getTransform(); // Apply MazeCharacter's transform - target.draw(*sprite, states); - }; -}; - -#endif diff --git a/src/MazeMap.cpp b/src/MazeMap.cpp new file mode 100644 index 0000000..b5c62de --- /dev/null +++ b/src/MazeMap.cpp @@ -0,0 +1,104 @@ +#include "MazeMap.h" + +bool MazeMap::loadMaze(const std::vector& collisionData, + const std::vector& 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(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(int x, int y) const { + if (!isLegalTile(x, y)) return false; + + int tileIndex = x + y * width; + + return !pelletEaten[tileIndex] && pelletMap[tileIndex] > 0; +} + +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(int x, int y) const { + return x >= 0 && x < static_cast(width) && + y >= 0 && y < static_cast(height); +} + +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); +} diff --git a/src/MazeMap.h b/src/MazeMap.h new file mode 100644 index 0000000..a66f195 --- /dev/null +++ b/src/MazeMap.h @@ -0,0 +1,52 @@ +#ifndef MAZEMAP_H +#define MAZEMAP_H + +#include + +#include +#include + +class MazeMap : public sf::Drawable, public sf::Transformable { +public: + MazeMap() : width(28), height(31), tileSize(8), texture(nullptr), baseMazeSprite(std::nullopt) {} + + bool loadMaze(const std::vector& collisionData, + const std::vector& pelletData, + sf::Texture& sharedTexture, + unsigned int tileSize, + sf::Vector2u baseMazeTexturePos, + sf::Vector2u pelletMazeTexturePos); + + void eatPellet(int x, int y); + + bool hasPellet(int x, int y) const; + + bool isWall(int x, int y) const; + + int convert2DCoords(int x, int y) const { return x + y * width; } + bool isLegalTile(int x, int y) const; + + unsigned int getWidth() const { return width; } + unsigned int getHeight() const { return height; } + unsigned int getTileSize() const { return tileSize; } + +private: + void draw(sf::RenderTarget& target, sf::RenderStates states) const override; + + std::vector collisionMap; // 1=wall, 0=path + std::vector pelletMap; // 0=none, 1=pellet, 2=power pellet + std::vector pelletEaten; + + std::optional baseMazeSprite; + sf::VertexArray pelletVertices; + sf::Texture* texture; + + unsigned int width; + unsigned int height; + unsigned int tileSize; + + sf::Vector2u baseMazeTexPos; + sf::Vector2u pelletMazeTexPos; +}; + +#endif diff --git a/src/ResourceManager.cpp b/src/ResourceManager.cpp new file mode 100644 index 0000000..3c9d0ca --- /dev/null +++ b/src/ResourceManager.cpp @@ -0,0 +1,76 @@ +#include "ResourceManager.h" +#include "MazeMap.h" +#include +#include +#include +#include +#include +#include +#include + +bool ResourceManager::loadTexture(const std::string& textureName, const std::filesystem::path& texturePath) { + auto texture = std::make_unique(); + if (!texture->loadFromFile(texturePath)) return false; + + textures[textureName] = std::move(texture); + return true; +}; + +bool ResourceManager::loadFont(const std::string& fontName, const std::filesystem::path& fontPath) { + sf::Font font; + if (!font.openFromFile(fontPath)) return false; + + fonts[fontName] = font; + + return true; +}; + +bool ResourceManager::loadMap(const std::string& mapName, const std::filesystem::path& mapPath) { + std::ifstream file(mapPath); + if (!file.is_open()) return false; + + std::vector mapData; + int value; + + while (file >> value) { + mapData.push_back(value); + } + + file.close(); + + if (mapName == "mazeMap") mazeMap = mapData; + if (mapName == "pelletMap") pelletMap = mapData; + + return !mapData.empty(); +}; + +void ResourceManager::resizeTexture(const std::string& textureName, unsigned int newTextureTileSize) { + auto it = textures.find(textureName); + if (it == textures.end()) return; + + sf::Texture& originalTexture = *it->second; + + originalTexture.setSmooth(false); + + float scaleFactor = static_cast(newTextureTileSize) / static_cast(textureTileSize); + + sf::Vector2u originalSize = originalTexture.getSize(); + sf::Vector2u newSize( + static_cast(originalSize.x * scaleFactor), + static_cast(originalSize.y * scaleFactor) + ); + + sf::RenderTexture renderTexture(newSize); + + renderTexture.clear(sf::Color::Transparent); + + sf::Sprite sprite(originalTexture); + sprite.setScale({scaleFactor, scaleFactor}); + renderTexture.draw(sprite); + renderTexture.display(); + + auto newTexture = std::make_unique(renderTexture.getTexture()); + newTexture->setSmooth(false); + + textures[textureName] = std::move(newTexture); +}; diff --git a/src/ResourceManager.h b/src/ResourceManager.h new file mode 100644 index 0000000..360f8c5 --- /dev/null +++ b/src/ResourceManager.h @@ -0,0 +1,61 @@ +#ifndef RESOURCEMANAGER_H +#define RESOURCEMANAGER_H + +#include +#include +#include +#include +#include + +#include +//#include + +#include "MazeMap.h" + +class ResourceManager { +public: + ResourceManager() : textureTileSize(8) {}; + + bool loadTexture(const std::string& textureName, const std::filesystem::path& texturePath); + + bool loadFont(const std::string& fontName, const std::filesystem::path& fontPath); + + bool loadMap(const std::string& mapName, const std::filesystem::path& mapPath); + + //bool loadSound(const std::filesystem::path& soundPath); + + sf::Texture& getTexture(const std::string& textureName) { + return *textures[textureName]; + }; + + void resizeTexture(const std::string& textureName, unsigned int newTextureTileSize); + + std::vector& getMazeMap() { + return mazeMap; + } + + std::vector& getPelletMap() { + return pelletMap; + } + + //load gameplay info + +private: + //textures and sprites + std::map> textures; + const int textureTileSize; + + //fonts + std::map fonts; + + //sounds + //std::map sounds; + + //map + std::vector mazeMap; + std::vector pelletMap; + + //gameplay info (speeds, scores, etc.) +}; + +#endif diff --git a/src/TileMap.cpp b/src/TileMap.cpp deleted file mode 100644 index 5530731..0000000 --- a/src/TileMap.cpp +++ /dev/null @@ -1,162 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include "TileMap.h" - -using json = nlohmann::json; - -void TileMap::loadFromJSON(const std::filesystem::path& jsonPath) { - std::ifstream file(jsonPath); - if (!file.is_open()) { - throw std::runtime_error("Failed to open JSON file: " + jsonPath.string()); - } - - json data = json::parse(file); - file.close(); - - tileSize = data["tileSize"].get(); - - // Get the first layer's tiles - if (data["layers"].empty() || data["layers"][0]["tiles"].empty()) { - throw std::runtime_error("No tiles found in JSON"); - } - - const auto& tilesArray = data["layers"][0]["tiles"]; - - // Find the bounds of the tilemap - int minX = std::numeric_limits::max(); - int minY = std::numeric_limits::max(); - int maxX = std::numeric_limits::min(); - int maxY = std::numeric_limits::min(); - - for (const auto& tile : tilesArray) { - int x = tile["x"].get(); - int y = tile["y"].get(); - minX = std::min(minX, x); - minY = std::min(minY, y); - maxX = std::max(maxX, x); - maxY = std::max(maxY, y); - } - - // Calculate grid dimensions - width = (maxX - minX) / tileSize + 1; - height = (maxY - minY) / tileSize + 1; - - // Initialize the grid with zeros (empty tiles) - tiles.resize(width * height, 0); - - // Fill in the tiles from the JSON - for (const auto& tile : tilesArray) { - int x = tile["x"].get(); - int y = tile["y"].get(); - int tileId = std::stoi(tile["id"].get()); - - // Convert pixel position to grid position - int gridX = (x - minX) / tileSize; - int gridY = (y - minY) / tileSize; - - // Store in the grid - tiles[gridX + gridY * width] = tileId; - } -} - -bool TileMap::load(const std::filesystem::path& tilesetPath, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height) -{ - // load the tileset texture - if (!tileset.loadFromFile(tilesetPath)) - return false; - - // resize the vertex array to fit the level size - vertices.setPrimitiveType(sf::PrimitiveType::Triangles); - vertices.resize(width * height * 6); - - // populate the vertex array, with two triangles per tile - for (unsigned int i = 0; i < width; ++i) - { - for (unsigned int j = 0; j < height; ++j) - { - // get the current tile number - const int tileNumber = tiles[i + j * width]; - - // find its position in the tileset texture - const int tu = tileNumber % (tileset.getSize().x / tileSize.x); - const int tv = tileNumber / (tileset.getSize().x / tileSize.x); - - // get a pointer to the triangles' vertices of the current tile - sf::Vertex* triangles = &vertices[(i + j * width) * 6]; - - // define the 6 corners of the two triangles - triangles[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y); - triangles[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y); - triangles[2].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y); - triangles[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y); - triangles[4].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y); - triangles[5].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y); - - // define the 6 matching texture coordinates - triangles[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); - triangles[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); - triangles[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); - triangles[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); - triangles[4].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); - triangles[5].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); - } - } - - return true; -} - -int TileMap::convert2DCoords(int row, int col) { - return col + (row * width); -} - -int TileMap::convert2DCoords(sf::Vector2i vec) { - return vec.y + (vec.x * width); -} - -int TileMap::getTileId(int tileX, int tileY) { - //tileX and tileY start from top left 0,0 - //tileX increases going right - //tileY increases going down - - int index = convert2DCoords(tileY, tileX); - - int tileId = tiles.at(index); - - return tileId; -} - -int TileMap::getTileId(sf::Vector2i vec) { - int index = convert2DCoords(vec); - - int tileId = tiles.at(index); - - return tileId; -} - -bool TileMap::isLegalTile(sf::Vector2i vec) { - int index = convert2DCoords(vec); - - int tileId = tiles.at(index); - - return tileId == 92; -} - -void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const { - // apply the transform - states.transform *= getTransform(); - - // apply the tileset texture - states.texture = &tileset; - - // draw the vertex array - target.draw(vertices, states); -} diff --git a/src/TileMap.h b/src/TileMap.h deleted file mode 100644 index e2773c1..0000000 --- a/src/TileMap.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef TILEMAP_H -#define TILEMAP_H - -#include - -#include -#include - -class TileMap : public sf::Drawable, public sf::Transformable { -public: - std::vector tiles; - unsigned int width; - unsigned int height; - unsigned int tileSize; - - void loadFromJSON(const std::filesystem::path& jsonPath); - - bool load(const std::filesystem::path& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height); - - int convert2DCoords(int row, int col); - - int convert2DCoords(sf::Vector2i vec); - - int getTileId(int tileX, int tileY); - - int getTileId(sf::Vector2i vec); - - bool isLegalTile(sf::Vector2i vec); - -private: - void draw(sf::RenderTarget& target, sf::RenderStates states) const override; - - sf::VertexArray vertices; - sf::Texture tileset; -}; - -#endif diff --git a/src/main.cpp b/src/main.cpp index 3b66b1e..f4b7581 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,188 +1,8 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "Game.h" -#include "Entity.h" -#include "TileMap.h" -//#include "MazeCharacter.h" +int main() { + Game game; + game.run(); -int main() -{ - // Load tilemap data from JSON - TileMap mazeMap; - try { - mazeMap.loadFromJSON("util/pacmantiles32.json"); - } catch (const std::exception& e) { - std::cerr << "Error loading tilemap: " << e.what() << std::endl; - return -1; - } - - if (!mazeMap.load("assets/maze32.png", - {mazeMap.tileSize, mazeMap.tileSize}, - mazeMap.tiles.data(), - mazeMap.width, - mazeMap.height)) - return -1; - - auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen"); - window.setFramerateLimit(60); - - int tileSize = mazeMap.tileSize; - int halfTile = tileSize / 2; - - int legalTileId = 92; - - const sf::Font bitFont("assets/PressStart2P.ttf"); - - sf::Text currTile(bitFont, "Current Tile: "); - currTile.move({1000, 50}); - - sf::Text currTileX(bitFont, "X: "); - currTileX.move({1000, 100}); - - sf::Text currTileY(bitFont, "Y: "); - currTileY.move({1000, 150}); - - sf::Text tileX(bitFont, ""); - tileX.move({1100, 100}); - - sf::Text tileY(bitFont, ""); - tileY.move({1100, 150}); - - sf::Text pacCent(bitFont, "Pacman Center:"); - pacCent.move({1000, 200}); - - sf::Text pacCentX(bitFont, ""); - pacCentX.move({1000, 250}); - - sf::Text pacCentY(bitFont, ""); - pacCentY.move({1000, 300}); - - sf::Text tileIdText(bitFont, "Tile ID: "); - tileIdText.move({1000, 350}); - - sf::Text tileId(bitFont, ""); - tileId.move({1000, 400}); - - sf::Text movementDirText(bitFont, "Movement Direction: "); - movementDirText.move({1000, 450}); - - sf::Text movementDir(bitFont, "STATIC"); - movementDir.move({1000, 500}); - - const sf::Texture pinkPacmanMovement("assets/pink_pac_man_movement48.png"); - sf::Sprite pinkPacmanSpriteOne(pinkPacmanMovement, {{0, 0}, {48, 48}}); - - // 24 tiles down, 13-14 tiles right - pinkPacmanSpriteOne.move({(13.0f * tileSize) + (halfTile / 2.0f), (23.0f * tileSize) - (halfTile / 2.0f)}); - - sf::Vector2f pacmanCenter; - - sf::Vector2i pacmanVelo; - - sf::Vector2i transformVeloUp({0, -1}); - sf::Vector2i transformVeloDown({0, 1}); - sf::Vector2i transformVeloRight({1, 0}); - sf::Vector2i transformVeloLeft({-1, 0}); - - float pacmanSpeed = 5; - - //MazeCharacter pacman("assets/pink_pac_man_movement48.png", mazeMap); - - Entity pacman(mazeMap); - pacman.loadTexture("assets/pink_pac_man_movement48.png", {48, 48}, 5); - - while (window.isOpen()) - { - while (const std::optional event = window.pollEvent()) - { - if (event->is()) - { - window.close(); - } - } - - window.clear(); - - window.draw(mazeMap); - window.draw(currTile); - window.draw(tileX); - window.draw(tileY); - window.draw(currTileX); - window.draw(currTileY); - window.draw(pinkPacmanSpriteOne); - window.draw(pacCent); - window.draw(pacCentX); - window.draw(pacCentY); - window.draw(tileIdText); - window.draw(tileId); - window.draw(movementDirText); - window.draw(movementDir); - - window.draw(pacman); - - pacmanCenter = {pinkPacmanSpriteOne.getPosition().x + 16.0f, pinkPacmanSpriteOne.getPosition().y + 16.0f}; - - sf::Vector2i pacmanTile = {(int)std::round((pacmanCenter.x - 16) / 32), (int)std::round((pacmanCenter.y - 16) / 32)}; - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) { - pacmanVelo = transformVeloUp; - movementDir.setString("UP"); - } - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) { - pacmanVelo = transformVeloLeft; - movementDir.setString("LEFT"); - } - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)|| sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) { - pacmanVelo = transformVeloDown; - movementDir.setString("DOWN"); - } - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) { - pacmanVelo = transformVeloRight; - movementDir.setString("RIGHT"); - } - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) { - pacmanVelo = {0, 0}; - movementDir.setString("STATIC"); - } - - // center coord 16, 16 - // tile 0, 0 - // center coord 48, 48 - // tile 1, 1 - // center coord 80, 80 - // tile 2, 2 - // (cent - 16) / 32 - - tileX.setString(std::to_string(pacmanTile.x)); - tileY.setString(std::to_string(pacmanTile.y)); - - pacCentX.setString(std::to_string(pacmanCenter.x)); - pacCentY.setString(std::to_string(pacmanCenter.y)); - - tileId.setString(std::to_string(mazeMap.getTileId(pacmanTile.x, pacmanTile.y))); - - //if (mazeMap.isLegalTile(pacmanTile + pacmanVelo)) { - // sf::Vector2f pacmanVeloFloat = {pacmanVelo.x * 1.0f, pacmanVelo.y * 1.0f}; - // pinkPacmanSpriteOne.move(pacmanSpeed * pacmanVeloFloat); - //} - - sf::Vector2f pacmanVeloFloat = {pacmanVelo.x * 1.0f, pacmanVelo.y * 1.0f}; - pinkPacmanSpriteOne.move(pacmanSpeed * pacmanVeloFloat); - pacman.move(pacmanSpeed * pacmanVeloFloat); - - window.display(); - } + return 0; } diff --git a/src/main.cpp.bak b/src/main.cpp.bak new file mode 100644 index 0000000..3b66b1e --- /dev/null +++ b/src/main.cpp.bak @@ -0,0 +1,188 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Entity.h" +#include "TileMap.h" +//#include "MazeCharacter.h" + +int main() +{ + // Load tilemap data from JSON + TileMap mazeMap; + try { + mazeMap.loadFromJSON("util/pacmantiles32.json"); + } catch (const std::exception& e) { + std::cerr << "Error loading tilemap: " << e.what() << std::endl; + return -1; + } + + if (!mazeMap.load("assets/maze32.png", + {mazeMap.tileSize, mazeMap.tileSize}, + mazeMap.tiles.data(), + mazeMap.width, + mazeMap.height)) + return -1; + + auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen"); + window.setFramerateLimit(60); + + int tileSize = mazeMap.tileSize; + int halfTile = tileSize / 2; + + int legalTileId = 92; + + const sf::Font bitFont("assets/PressStart2P.ttf"); + + sf::Text currTile(bitFont, "Current Tile: "); + currTile.move({1000, 50}); + + sf::Text currTileX(bitFont, "X: "); + currTileX.move({1000, 100}); + + sf::Text currTileY(bitFont, "Y: "); + currTileY.move({1000, 150}); + + sf::Text tileX(bitFont, ""); + tileX.move({1100, 100}); + + sf::Text tileY(bitFont, ""); + tileY.move({1100, 150}); + + sf::Text pacCent(bitFont, "Pacman Center:"); + pacCent.move({1000, 200}); + + sf::Text pacCentX(bitFont, ""); + pacCentX.move({1000, 250}); + + sf::Text pacCentY(bitFont, ""); + pacCentY.move({1000, 300}); + + sf::Text tileIdText(bitFont, "Tile ID: "); + tileIdText.move({1000, 350}); + + sf::Text tileId(bitFont, ""); + tileId.move({1000, 400}); + + sf::Text movementDirText(bitFont, "Movement Direction: "); + movementDirText.move({1000, 450}); + + sf::Text movementDir(bitFont, "STATIC"); + movementDir.move({1000, 500}); + + const sf::Texture pinkPacmanMovement("assets/pink_pac_man_movement48.png"); + sf::Sprite pinkPacmanSpriteOne(pinkPacmanMovement, {{0, 0}, {48, 48}}); + + // 24 tiles down, 13-14 tiles right + pinkPacmanSpriteOne.move({(13.0f * tileSize) + (halfTile / 2.0f), (23.0f * tileSize) - (halfTile / 2.0f)}); + + sf::Vector2f pacmanCenter; + + sf::Vector2i pacmanVelo; + + sf::Vector2i transformVeloUp({0, -1}); + sf::Vector2i transformVeloDown({0, 1}); + sf::Vector2i transformVeloRight({1, 0}); + sf::Vector2i transformVeloLeft({-1, 0}); + + float pacmanSpeed = 5; + + //MazeCharacter pacman("assets/pink_pac_man_movement48.png", mazeMap); + + Entity pacman(mazeMap); + pacman.loadTexture("assets/pink_pac_man_movement48.png", {48, 48}, 5); + + while (window.isOpen()) + { + while (const std::optional event = window.pollEvent()) + { + if (event->is()) + { + window.close(); + } + } + + window.clear(); + + window.draw(mazeMap); + window.draw(currTile); + window.draw(tileX); + window.draw(tileY); + window.draw(currTileX); + window.draw(currTileY); + window.draw(pinkPacmanSpriteOne); + window.draw(pacCent); + window.draw(pacCentX); + window.draw(pacCentY); + window.draw(tileIdText); + window.draw(tileId); + window.draw(movementDirText); + window.draw(movementDir); + + window.draw(pacman); + + pacmanCenter = {pinkPacmanSpriteOne.getPosition().x + 16.0f, pinkPacmanSpriteOne.getPosition().y + 16.0f}; + + sf::Vector2i pacmanTile = {(int)std::round((pacmanCenter.x - 16) / 32), (int)std::round((pacmanCenter.y - 16) / 32)}; + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) { + pacmanVelo = transformVeloUp; + movementDir.setString("UP"); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) { + pacmanVelo = transformVeloLeft; + movementDir.setString("LEFT"); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)|| sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) { + pacmanVelo = transformVeloDown; + movementDir.setString("DOWN"); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) { + pacmanVelo = transformVeloRight; + movementDir.setString("RIGHT"); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) { + pacmanVelo = {0, 0}; + movementDir.setString("STATIC"); + } + + // center coord 16, 16 + // tile 0, 0 + // center coord 48, 48 + // tile 1, 1 + // center coord 80, 80 + // tile 2, 2 + // (cent - 16) / 32 + + tileX.setString(std::to_string(pacmanTile.x)); + tileY.setString(std::to_string(pacmanTile.y)); + + pacCentX.setString(std::to_string(pacmanCenter.x)); + pacCentY.setString(std::to_string(pacmanCenter.y)); + + tileId.setString(std::to_string(mazeMap.getTileId(pacmanTile.x, pacmanTile.y))); + + //if (mazeMap.isLegalTile(pacmanTile + pacmanVelo)) { + // sf::Vector2f pacmanVeloFloat = {pacmanVelo.x * 1.0f, pacmanVelo.y * 1.0f}; + // pinkPacmanSpriteOne.move(pacmanSpeed * pacmanVeloFloat); + //} + + sf::Vector2f pacmanVeloFloat = {pacmanVelo.x * 1.0f, pacmanVelo.y * 1.0f}; + pinkPacmanSpriteOne.move(pacmanSpeed * pacmanVeloFloat); + pacman.move(pacmanSpeed * pacmanVeloFloat); + + window.display(); + } +}