mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
modify entire game to use class based structure
This commit is contained in:
+1
-1
@@ -20,6 +20,6 @@ FetchContent_Declare(json
|
|||||||
|
|
||||||
FetchContent_MakeAvailable(SFML 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_compile_features(main PRIVATE cxx_std_17)
|
||||||
target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json)
|
target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json)
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
@@ -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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0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0
|
||||||
|
0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0
|
||||||
|
0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
|
||||||
|
0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
|
||||||
|
0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||||
+2
-3
@@ -1,7 +1,6 @@
|
|||||||
#ifndef ENTITY_H
|
#ifndef ENTITY_H
|
||||||
#define ENTITY_H
|
#define ENTITY_H
|
||||||
|
|
||||||
#include "TileMap.h"
|
|
||||||
#include <SFML/Graphics/Drawable.hpp>
|
#include <SFML/Graphics/Drawable.hpp>
|
||||||
#include <SFML/Graphics/PrimitiveType.hpp>
|
#include <SFML/Graphics/PrimitiveType.hpp>
|
||||||
#include <SFML/Graphics/Rect.hpp>
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
@@ -23,13 +22,13 @@ enum class MovementDir {
|
|||||||
|
|
||||||
class Entity : public sf::Drawable, public sf::Transformable {
|
class Entity : public sf::Drawable, public sf::Transformable {
|
||||||
public:
|
public:
|
||||||
Entity(TileMap& map) : map(map) {};
|
//Entity(TileMap& map) : map(map) {};
|
||||||
|
|
||||||
bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles);
|
bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles);
|
||||||
|
|
||||||
void setTextureFrame(const sf::IntRect& rectangle);
|
void setTextureFrame(const sf::IntRect& rectangle);
|
||||||
protected:
|
protected:
|
||||||
TileMap map;
|
//TileMap map;
|
||||||
|
|
||||||
sf::VertexArray vertexArray;
|
sf::VertexArray vertexArray;
|
||||||
sf::Texture texture;
|
sf::Texture texture;
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
|
||||||
|
#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<sf::Event::Closed>())
|
||||||
|
{
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.clear();
|
||||||
|
|
||||||
|
window.draw(map);
|
||||||
|
|
||||||
|
//window.draw();
|
||||||
|
|
||||||
|
window.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef GAME_H
|
||||||
|
#define GAME_H
|
||||||
|
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include <string>
|
||||||
|
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
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
#ifndef MAZECHARACTER_H
|
|
||||||
#define MAZECHARACTER_H
|
|
||||||
|
|
||||||
#include <SFML/Graphics/Drawable.hpp>
|
|
||||||
#include <SFML/Graphics/Rect.hpp>
|
|
||||||
#include <SFML/Graphics/RenderStates.hpp>
|
|
||||||
#include <SFML/Graphics/RenderTarget.hpp>
|
|
||||||
#include <SFML/Graphics/Texture.hpp>
|
|
||||||
#include <SFML/Graphics/Sprite.hpp>
|
|
||||||
#include <SFML/System/Vector2.hpp>
|
|
||||||
#include <vector>
|
|
||||||
#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<int> 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
|
|
||||||
+104
@@ -0,0 +1,104 @@
|
|||||||
|
#include "MazeMap.h"
|
||||||
|
|
||||||
|
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(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<int>(width) &&
|
||||||
|
y >= 0 && y < static_cast<int>(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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#ifndef MAZEMAP_H
|
||||||
|
#define MAZEMAP_H
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
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<int>& collisionData,
|
||||||
|
const std::vector<int>& 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<int> collisionMap; // 1=wall, 0=path
|
||||||
|
std::vector<int> pelletMap; // 0=none, 1=pellet, 2=power pellet
|
||||||
|
std::vector<bool> pelletEaten;
|
||||||
|
|
||||||
|
std::optional<sf::Sprite> baseMazeSprite;
|
||||||
|
sf::VertexArray pelletVertices;
|
||||||
|
sf::Texture* texture;
|
||||||
|
|
||||||
|
unsigned int width;
|
||||||
|
unsigned int height;
|
||||||
|
unsigned int tileSize;
|
||||||
|
|
||||||
|
sf::Vector2u baseMazeTexPos;
|
||||||
|
sf::Vector2u pelletMazeTexPos;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#include "ResourceManager.h"
|
||||||
|
#include "MazeMap.h"
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/RenderTexture.hpp>
|
||||||
|
#include <SFML/Graphics/Sprite.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
bool ResourceManager::loadTexture(const std::string& textureName, const std::filesystem::path& texturePath) {
|
||||||
|
auto texture = std::make_unique<sf::Texture>();
|
||||||
|
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<int> 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<float>(newTextureTileSize) / static_cast<float>(textureTileSize);
|
||||||
|
|
||||||
|
sf::Vector2u originalSize = originalTexture.getSize();
|
||||||
|
sf::Vector2u newSize(
|
||||||
|
static_cast<unsigned int>(originalSize.x * scaleFactor),
|
||||||
|
static_cast<unsigned int>(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<sf::Texture>(renderTexture.getTexture());
|
||||||
|
newTexture->setSmooth(false);
|
||||||
|
|
||||||
|
textures[textureName] = std::move(newTexture);
|
||||||
|
};
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#ifndef RESOURCEMANAGER_H
|
||||||
|
#define RESOURCEMANAGER_H
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Texture.hpp>
|
||||||
|
//#include <SFML/Audio/SoundBuffer.hpp>
|
||||||
|
|
||||||
|
#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<int>& getMazeMap() {
|
||||||
|
return mazeMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int>& getPelletMap() {
|
||||||
|
return pelletMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
//load gameplay info
|
||||||
|
|
||||||
|
private:
|
||||||
|
//textures and sprites
|
||||||
|
std::map<std::string, std::unique_ptr<sf::Texture>> textures;
|
||||||
|
const int textureTileSize;
|
||||||
|
|
||||||
|
//fonts
|
||||||
|
std::map<std::string, sf::Font> fonts;
|
||||||
|
|
||||||
|
//sounds
|
||||||
|
//std::map<std::string, sf::SoundBuffer> sounds;
|
||||||
|
|
||||||
|
//map
|
||||||
|
std::vector<int> mazeMap;
|
||||||
|
std::vector<int> pelletMap;
|
||||||
|
|
||||||
|
//gameplay info (speeds, scores, etc.)
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
-162
@@ -1,162 +0,0 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
#include <SFML/System/Vector2.hpp>
|
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
|
|
||||||
#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<unsigned int>();
|
|
||||||
|
|
||||||
// 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<int>::max();
|
|
||||||
int minY = std::numeric_limits<int>::max();
|
|
||||||
int maxX = std::numeric_limits<int>::min();
|
|
||||||
int maxY = std::numeric_limits<int>::min();
|
|
||||||
|
|
||||||
for (const auto& tile : tilesArray) {
|
|
||||||
int x = tile["x"].get<int>();
|
|
||||||
int y = tile["y"].get<int>();
|
|
||||||
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>();
|
|
||||||
int y = tile["y"].get<int>();
|
|
||||||
int tileId = std::stoi(tile["id"].get<std::string>());
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#ifndef TILEMAP_H
|
|
||||||
#define TILEMAP_H
|
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <filesystem>
|
|
||||||
|
|
||||||
class TileMap : public sf::Drawable, public sf::Transformable {
|
|
||||||
public:
|
|
||||||
std::vector<int> 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
|
|
||||||
+5
-185
@@ -1,188 +1,8 @@
|
|||||||
#include <SFML/Graphics.hpp>
|
#include "Game.h"
|
||||||
#include <SFML/Graphics/CircleShape.hpp>
|
|
||||||
#include <SFML/Graphics/Font.hpp>
|
|
||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
|
||||||
#include <SFML/Graphics/Sprite.hpp>
|
|
||||||
#include <SFML/Graphics/Text.hpp>
|
|
||||||
#include <SFML/System/Vector2.hpp>
|
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
|
||||||
#include <cmath>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "Entity.h"
|
int main() {
|
||||||
#include "TileMap.h"
|
Game game;
|
||||||
//#include "MazeCharacter.h"
|
game.run();
|
||||||
|
|
||||||
int main()
|
return 0;
|
||||||
{
|
|
||||||
// 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<sf::Event::Closed>())
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/CircleShape.hpp>
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
|
#include <SFML/Graphics/Sprite.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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<sf::Event::Closed>())
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user