From 2d1d11f8ae4d3e81e5538ae4e40da27b424f9ba8 Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Tue, 11 Nov 2025 19:12:13 -0500 Subject: [PATCH] add more game constants loaded from json --- assets/game/{gameplay.json => config.json} | 3 +- src/Game.cpp | 37 +++++++++++++++++++--- src/Game.h | 21 ++++++++++-- src/main.cpp | 11 +++++-- 4 files changed, 61 insertions(+), 11 deletions(-) rename assets/game/{gameplay.json => config.json} (99%) diff --git a/assets/game/gameplay.json b/assets/game/config.json similarity index 99% rename from assets/game/gameplay.json rename to assets/game/config.json index f41fbb4..2d94506 100644 --- a/assets/game/gameplay.json +++ b/assets/game/config.json @@ -6,7 +6,8 @@ "ghostEatenPoints": [200, 400, 800, 1600], "frameRate": 60.606061, "pacmanEatDotFrames": 1, - "pacmanEatEnergizerFrames": 3 + "pacmanEatEnergizerFrames": 3, + "tileSize": 8 }, "bonusFruit": { "appearAfterDots": [70, 170], diff --git a/src/Game.cpp b/src/Game.cpp index 402aff4..8e5d26a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,9 +1,11 @@ #include #include #include +#include #include #include #include +#include #include "Game.h" #include "MazeMap.h" @@ -11,13 +13,39 @@ #include "Pacman.h" #include "Ghost.h" +json Game::loadConfig(const std::filesystem::path& configPath) { + std::ifstream file(configPath); + if (!file.is_open()) { + throw std::runtime_error("Failed to open config file: " + configPath.string()); + } + + json data; + + try { + file >> data; + return data; + } catch (const json::parse_error& e) { + throw std::runtime_error("JSON parse error: " + std::string(e.what())); + } +} + +Game::Game(const std::filesystem::path& configPath) : + config(loadConfig(configPath)), + framerate(static_cast(config["gameConstants"]["frameRate"])), + perPixelMove(static_cast(config["gameConstants"]["baseSpeedPixelsPerSecond"]) / static_cast(config["gameConstants"]["frameRate"])), + baseTileSize(static_cast(config["gameConstants"]["tileSize"])) +{}; + void Game::run() { + const int tileSize = baseTileSize * scaleFactor; + const float perLoopMove = perPixelMove * scaleFactor; + ResourceManager resources; MazeMap map; resources.loadTexture("all_textures", "assets/textures/all_textures_transparent.png"); // resize all_textures to 32px - resources.resizeTexture("all_textures", 32); + resources.resizeTexture("all_textures", tileSize); // Collision data: 1=wall, 0=path resources.loadMap("mazeMap", "assets/game/maze.txt"); // Pellet data: 0=none, 1=pellet, 2=power @@ -56,7 +84,7 @@ void Game::run() { map.loadMaze(resources.getMazeMap(), resources.getPelletMap(), resources.getTexture("all_textures"), - 32, + tileSize, {912, 0}, {0, 0}); @@ -78,7 +106,7 @@ void Game::run() { pacman.setOrigin({30, 30}); // Tile center = tile * tileSize + tileSize/2 pacman.setPosition({14.5 * 32.0f - 16.0f, 24 * 32.0f - 16.0f}); - pacman.setMovementSpeed({(75.75757625 / 60.606061) * 4, (75.75757625 / 60.606061) * 4}); + pacman.setMovementSpeed({perLoopMove, perLoopMove}); blinky.setAnimationTiles(resources.getTexture("all_textures"), {1824, 256}, "right_walking", {60, 60}, 2, 4); blinky.setAnimationTiles(resources.getTexture("all_textures"), {1952, 256}, "left_walking", {60, 60}, 2, 4); @@ -113,7 +141,6 @@ void Game::run() { clyde.setPosition({16.5 * 32.0f - 16.0f, 15 * 32.0f - 16.0f}); auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName); - window.setFramerateLimit(maxFPS); sf::Sound sound(*resources.getSound("start")); sound.play(); @@ -125,7 +152,7 @@ void Game::run() { sf::Clock clock; - const sf::Time FIXED_TIMESTEP = sf::seconds(1.0f / 60.606061f); + const sf::Time FIXED_TIMESTEP = sf::seconds(1.0f / framerate); sf::Time accumulator = sf::Time::Zero; sf::Time initialGameplayPause; diff --git a/src/Game.h b/src/Game.h index 2f736d9..5e3c66c 100644 --- a/src/Game.h +++ b/src/Game.h @@ -3,17 +3,32 @@ #include #include +#include +#include + +using json = nlohmann::json; + class Game { public: - Game() {}; - Game(sf::Vector2u windowRes, std::string& windowName, int maxFPS) : windowRes(windowRes), windowName(windowName), maxFPS(maxFPS) {}; + Game(const std::filesystem::path& configPath); + + void setScaleFactor(int newScaleFactor) { scaleFactor = newScaleFactor; } void run(); private: + json loadConfig(const std::filesystem::path& configPath); + + const json config; + + const float framerate; + const float perPixelMove; + const int baseTileSize; + + int scaleFactor = 4; + sf::Vector2u windowRes = {1920, 1080}; std::string windowName = "Pacmen"; - unsigned int maxFPS = 60; }; #endif diff --git a/src/main.cpp b/src/main.cpp index f4b7581..1033d7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,15 @@ #include "Game.h" +#include +#include int main() { - Game game; - game.run(); + try { + Game game("assets/game/config.json"); + game.run(); + } catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } return 0; }