mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
add more game constants loaded from json
This commit is contained in:
@@ -6,7 +6,8 @@
|
||||
"ghostEatenPoints": [200, 400, 800, 1600],
|
||||
"frameRate": 60.606061,
|
||||
"pacmanEatDotFrames": 1,
|
||||
"pacmanEatEnergizerFrames": 3
|
||||
"pacmanEatEnergizerFrames": 3,
|
||||
"tileSize": 8
|
||||
},
|
||||
"bonusFruit": {
|
||||
"appearAfterDots": [70, 170],
|
||||
+32
-5
@@ -1,9 +1,11 @@
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#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<float>(config["gameConstants"]["frameRate"])),
|
||||
perPixelMove(static_cast<float>(config["gameConstants"]["baseSpeedPixelsPerSecond"]) / static_cast<float>(config["gameConstants"]["frameRate"])),
|
||||
baseTileSize(static_cast<int>(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;
|
||||
|
||||
+18
-3
@@ -3,17 +3,32 @@
|
||||
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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
|
||||
|
||||
+9
-2
@@ -1,8 +1,15 @@
|
||||
#include "Game.h"
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user