Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ResourceManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "ResourceManager.h"

sf::Texture& ResourceManager::getTexture(const std::string& filepath) {
auto& textures = getInstance().mTextures;
if (textures.find(filepath) != textures.end())
return textures.at(filepath);

sf::Texture& texture = textures[filepath];
if (!texture.loadFromFile(filepath))
std::cerr << "Failed to load texture: " << filepath << std::endl;
return texture;
}
20 changes: 20 additions & 0 deletions src/ResourceManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <map>
#include <string>
#include <iostream>

class ResourceManager {
public:
static ResourceManager& getInstance() {
static ResourceManager instance;
return instance;
}

sf::Texture& getTexture(const std::string& filepath);

private:
std::map<std::string, sf::Texture> mTextures;
ResourceManager() {};
};

29 changes: 17 additions & 12 deletions src/Rpg/gamengine/RPGEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ RPGEngine::RPGEngine(sf::RenderWindow& window, GameManager* gameManager)
mShowAnalyzeMenu(false),
mShowInteract(false),
mIsInsideAStructure(false),
mStructureIndex(-1),
mCrystals(100),
mStorageCapacity(500),
mCurrentLevel(1),
Expand All @@ -83,7 +84,8 @@ RPGEngine::RPGEngine(sf::RenderWindow& window, GameManager* gameManager)
mIsInitialized(false),
mTransitionSystem(sf::Vector2f(window.getSize().x, window.getSize().y)),
mChestMenu(window, mInventory, mChestInventory, sf::Vector2f(70.f, 70.f)),
mGameContext{ mCharacter, mIsInsideAStructure, mCameraFixedPosition, mShowChestMenu, mTimeSystem, mTransitionSystem, gameManager,
mGameContext{ mCharacter, mIsInsideAStructure, mStructureIndex, mCameraFixedPosition,
mShowChestMenu, mTimeSystem, mTransitionSystem, gameManager,
[this](const std::string& name) {
const Waypoint* waypoint = mWaypointManager.getWaypoint(name);

Expand All @@ -99,7 +101,7 @@ RPGEngine::RPGEngine(sf::RenderWindow& window, GameManager* gameManager)
}
}
}, mWaypointManager},
mZoneManager(mGameContext, 2),
mZoneManager(mGameContext, 1),
mStartTowerDefenseMenu(window, mAvailableTowers, this, gameManager, mCurrentLevel, mCrystals),
mBankMenu(window, mCrystals, mStorageCapacity, mTimeSystem),
mShopMenu(window, mInventory, mTimeSystem, 5, mCrystals),
Expand Down Expand Up @@ -664,8 +666,7 @@ void RPGEngine::render() {

mTransitionSystem.render(mWindow);

if (!mShowBankMenu && !mShowMenu && !mShowStartMenu && !mShowShopMenu && !mShowAnalyzeMenu && !mShowChestMenu)
renderDateTime(mWindow, mFont, currentDate, currentTime);
renderDateTime(mWindow, mFont, currentDate, currentTime);

mWindow.display();
}
Expand Down Expand Up @@ -880,8 +881,8 @@ void RPGEngine::saveGame() {
chestItemId, chestItemQuantity, droppedItemId, droppedItemXPos,
droppedItemYPos, droppedItemQuantity, extracting, inSlot, completed,
timerActive, startYear1, startDay1, startHour1, startMinute1,
slotItemId, mIsInsideAStructure, mCameraFixedPosition, chapter,
flagKeys, flagValues);
slotItemId, mIsInsideAStructure, mStructureIndex, mCameraFixedPosition,
chapter, flagKeys, flagValues);
}

void RPGEngine::loadGame() {
Expand All @@ -892,7 +893,7 @@ void RPGEngine::loadGame() {
int crystals, year, day, hour, minute, bankBalance, penalty, interest,
amountToRepay, daysToRepayment, startYear, startDay, startHour, startMinute,
hasBorrowActive, extracting, inSlot, completed, timerActive, startYear1,
startDay1, startHour1, startMinute1, slotItemId, insideStructure, chapter;
startDay1, startHour1, startMinute1, slotItemId, insideStructure, structureIndex, chapter;
std::vector<int> droppedItemId;
std::vector<float> droppedItemXPos;
std::vector<float> droppedItemYPos;
Expand All @@ -912,21 +913,25 @@ void RPGEngine::loadGame() {
chestItemId, chestItemQuantity, droppedItemId, droppedItemXPos,
droppedItemYPos, droppedItemQuantity, extracting, inSlot,
completed, timerActive, startYear1, startDay1, startHour1,
startMinute1, slotItemId, insideStructure,
startMinute1, slotItemId, insideStructure, structureIndex,
mCameraFixedPosition, chapter, flagKeys, flagValues)) {

mInventory.clear();
mChestInventory.clear();
mDroppedItems.clear();

mCharacter.setPosition(playerPosition);
mCharacter.setAnimation(playerAnimation);

mIsInsideAStructure = insideStructure;
if (mIsInsideAStructure == true)
mGameContext.changeMap("assets/maps/interiors/house_interior.json");
else
mStructureIndex = structureIndex;
if (mIsInsideAStructure == true) {
if (mStructureIndex == 0)
mGameContext.changeMap("MCHouse_Interior");
mCharacter.setPosition(playerPosition);
} else {
mGameContext.changeMap("open_world");
mStructureIndex = -1;
}

mNPCManager.loadNPCStates(npcPositions, npcWaypoints);
mCrystals = crystals;
Expand Down
1 change: 1 addition & 0 deletions src/Rpg/gamengine/RPGEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class RPGEngine {
std::vector<sf::Text> mChoiceTexts;

bool mIsInsideAStructure;
int mStructureIndex;
sf::Vector2f mCameraFixedPosition;
sf::View mFixedCamera;

Expand Down
6 changes: 4 additions & 2 deletions src/Rpg/map/buildings/MCHouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
MCHouse::MCHouse(const sf::Vector2f& position, const std::string filename, const sf::Vector2f& collPosition,
const sf::Vector2f& collSize, int canInteract, const sf::Vector2f& interactPos,
const sf::Vector2f& interactSize, MainCharacter& mainCharacter, bool& isInsideAStructure,
sf::Vector2f& cameraFixedPosition, std::function<void(const std::string&)> changeMap)
sf::Vector2f& cameraFixedPosition, std::function<void(const std::string&)> changeMap,
int& structureIndex)
: Entity(position, filename, collPosition, collSize, canInteract, interactPos, interactSize), mMainCharacter(mainCharacter),
mIsInsideAStructure(isInsideAStructure), mCameraFixedPosition(cameraFixedPosition), mChangeMap(changeMap) {
mIsInsideAStructure(isInsideAStructure), mCameraFixedPosition(cameraFixedPosition), mChangeMap(changeMap), mStructureIndex(structureIndex) {

}

void MCHouse::interact() {
mMainCharacter.setAnimation(3);
mIsInsideAStructure = true;

mStructureIndex = 0;
mChangeMap("MCHouse_Interior");
}

3 changes: 2 additions & 1 deletion src/Rpg/map/buildings/MCHouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MCHouse : public Entity {
const sf::Vector2f& collPosition, const sf::Vector2f& collSize, int canInteract,
const sf::Vector2f& interactPos, const sf::Vector2f& interactSize,
MainCharacter& mainCharacter, bool& isInsideAStructure, sf::Vector2f& cameraFixedPosition,
std::function<void(const std::string&)> changeMap);
std::function<void(const std::string&)> changeMap, int& structureIndex);

void interact() override;
private:
Expand All @@ -18,5 +18,6 @@ class MCHouse : public Entity {
sf::Vector2f& mCameraFixedPosition;

std::function<void(const std::string&)> mChangeMap;
int& mStructureIndex;
};

10 changes: 4 additions & 6 deletions src/Rpg/map/entities/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ Entity::Entity(const sf::Vector2f& position, const std::string filename,
const sf::Vector2f& collPosition, const sf::Vector2f& collSize, int canInteract,
const sf::Vector2f& interactPos, const sf::Vector2f& interactSize)
: mCanInteract(canInteract) {
mTexture = new sf::Texture;
// If filename is empty than the entity is not drawable => dont load texture from file
if(filename != "")
if (!mTexture->loadFromFile(filename)) {
std::cerr << "Couldn't load texture for an entity!" << std::endl;
}
if(filename != "") {
sf::Texture& texture = ResourceManager::getInstance().getTexture(filename);
mSprite.setTexture(texture);
}
mSprite.setPosition(position);
mSprite.setTexture(*mTexture);

mCollisionZone.setSize(collSize);
mCollisionZone.setPosition(collPosition);
Expand Down
3 changes: 2 additions & 1 deletion src/Rpg/map/entities/Entity.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../../ResourceManager.h"
#include "DrawableEntity.h"

class Entity : public DrawableEntity {
Expand All @@ -21,7 +22,7 @@ class Entity : public DrawableEntity {

protected:
sf::Sprite mSprite;
sf::Texture *mTexture;

sf::RectangleShape mCollisionZone;
sf::RectangleShape mInteractableZone;
int mCanInteract;
Expand Down
3 changes: 2 additions & 1 deletion src/Rpg/map/entities/EntityFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ std::unique_ptr<Entity> EntityFactory::createEntity(const std::string& type, con
gameContext.mainCharacter,
gameContext.isInsideStructure,
gameContext.cameraFixedPosition,
gameContext.changeMap
gameContext.changeMap,
gameContext.structureIndex
);
} else if (type == "MCHouseInt") {
sf::Vector2f interactSize = sf::Vector2f(43.f, 15.f);
Expand Down
1 change: 1 addition & 0 deletions src/Rpg/map/entities/EntityFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GameManager;
struct GameContext {
MainCharacter& mainCharacter;
bool& isInsideStructure;
int &structureIndex;
sf::Vector2f& cameraFixedPosition;
bool& showChestMenu;
TimeSystem& timeSystem;
Expand Down
9 changes: 3 additions & 6 deletions src/Rpg/map/zones/Zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ void Zone::loadFromJson(const std::string& jsonPath, GameContext& gameContext) {
if (backgroundPath.find("..") != std::string::npos)
backgroundPath = "assets" + backgroundPath.substr(2);

if (mBackgroundTexture.loadFromFile(backgroundPath)) {
mBackgroundSprite.setTexture(mBackgroundTexture);
mBackgroundSprite.setPosition(mCoords.x * 1024.f, mCoords.y * 1024.f);
} else {
std::cerr << "Failed to load zone background: " << backgroundPath << std::endl;
}
sf::Texture& texture = ResourceManager::getInstance().getTexture(backgroundPath);
mBackgroundSprite.setTexture(texture);
mBackgroundSprite.setPosition(mCoords.x * 1024.f, mCoords.y * 1024.f);
} else if (type == "objectgroup") {
for (const auto& object: layer["objects"]) {
std::string entityType = object.value("type", "");
Expand Down
2 changes: 1 addition & 1 deletion src/Rpg/map/zones/Zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <map>
#include "../entities/Entity.h"
#include "../entities/EntityFactory.h"
#include "../../../ResourceManager.h"

class Zone {
public:
Expand All @@ -24,7 +25,6 @@ class Zone {
void loadFromJson(const std::string& jsonPath, GameContext& gameContext);

sf::Vector2i mCoords;
sf::Texture mBackgroundTexture;
sf::Sprite mBackgroundSprite;
std::vector<std::unique_ptr<Entity>> mEntities;

Expand Down
4 changes: 2 additions & 2 deletions src/Rpg/map/zones/ZoneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
const int CHUNK_SIZE = 1024;

ZoneManager::ZoneManager(GameContext& gameContext, int viewRange)
: mGameContext(gameContext), mViewRange(viewRange), mCurrentZoneCoords(0, 0), mIsInsideAStructure(gameContext.isInsideStructure) {
: mGameContext(gameContext), mViewRange(viewRange), mCurrentZoneCoords(0, 0) {
}

void ZoneManager::update(const sf::Vector2f& playerWorldPos) {
if (mIsInsideAStructure)
if (mGameContext.isInsideStructure)
return;

int zoneX = static_cast<int>(playerWorldPos.x / (CHUNK_SIZE));
Expand Down
2 changes: 0 additions & 2 deletions src/Rpg/map/zones/ZoneManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ class ZoneManager {
std::map<std::pair<int, int>, std::unique_ptr<Zone>> mZones;
sf::Vector2i mCurrentZoneCoords;
int mViewRange;

bool mIsInsideAStructure;
};

8 changes: 4 additions & 4 deletions src/Rpg/menues/AnalyzeMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ AnalyzeMenu::AnalyzeMenu(sf::RenderWindow& window, Inventory& inventory, TimeSys
if (!mFont.loadFromFile("assets/fonts/gameFont.ttf"))
std::cerr << "Failed to load font for AnalyzeMenu!" << std::endl;

mMenuShape.setSize(sf::Vector2f(window.getSize().x / 2.0f, window.getSize().y / 2.0f));
mMenuShape.setSize(sf::Vector2f(window.getSize().x / 2.f, window.getSize().y / 2.f));
mMenuShape.setFillColor(sf::Color(50, 50, 50, 255));
mMenuShape.setPosition((window.getSize().x - mMenuShape.getSize().x) / 2.f,
(window.getSize().y - mMenuShape.getSize().y) / 2.f);

mHoveredZoneShape.setSize(sf::Vector2f(window.getSize().x * 3.0f / 4.0f, 40));
mHoveredZoneShape.setSize(sf::Vector2f(window.getSize().x / 2.f, 40));
mHoveredZoneShape.setFillColor(sf::Color(10, 10, 10, 100));
mHoveredZoneShape.setPosition(sf::Vector2f((window.getSize().x - mMenuShape.getSize().x) / 2.0f,
(window.getSize().y - mMenuShape.getSize().y) / 2.0f));
mHoveredZoneShape.setPosition(sf::Vector2f((window.getSize().x - mMenuShape.getSize().x) / 2.f,
(window.getSize().y - mMenuShape.getSize().y) / 2.f));

mMenuText.setFont(mFont);
mMenuText.setCharacterSize(20);
Expand Down
4 changes: 2 additions & 2 deletions src/Rpg/menues/ShopMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ ShopMenu::ShopMenu(sf::RenderWindow& window, Inventory& inventory, TimeSystem& t
mItem3Button(sf::Vector2f(0.0f, 0.0f), sf::Vector2f(110.f, 150.f), "3"),
mCrystals(crystals), mNumItems(numItems) {

mMenuShape.setSize(sf::Vector2f(window.getSize().x / 2.0f, window.getSize().y / 2.f));
mMenuShape.setSize(sf::Vector2f(window.getSize().x / 2.f, window.getSize().y / 2.f));
mMenuShape.setFillColor(sf::Color(50, 50, 50, 255));
mMenuShape.setPosition((window.getSize().x - mMenuShape.getSize().x) / 2.f,
(window.getSize().y - mMenuShape.getSize().y) / 2.f);

mHoveredZoneShape.setSize(sf::Vector2f(window.getSize().x * 3.0f / 4.0f, 40));
mHoveredZoneShape.setSize(sf::Vector2f(window.getSize().x / 2.f, 40));
mHoveredZoneShape.setFillColor(sf::Color(10, 10, 10, 100));
mHoveredZoneShape.setPosition(mMenuShape.getPosition());

Expand Down
20 changes: 12 additions & 8 deletions src/Rpg/saveSystem/SaveSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void SaveSystem::save(const sf::Vector2f& playerPosition, const int& playerAnima
const std::vector<float>& droppedItemYPos, const std::vector<int>& droppedItemQuantity,
const int& extracting, const int& inSlot, const int& completed, const int& timerActive,
const int& startYear1, const int& startDay1, const int& startHour1, const int& startMinute1,
const int& slotItemId, const int& insideStructure, const sf::Vector2f& fixedCameraPos,
const int& chapter, const std::vector<std::string>& flagKeys,
const int& slotItemId, const int& insideStructure, const int& structureIndex,
const sf::Vector2f& fixedCameraPos, const int& chapter, const std::vector<std::string>& flagKeys,
const std::vector<int>& flagValues) {

std::ofstream outFile(mSaveFilePath);
Expand Down Expand Up @@ -62,7 +62,7 @@ void SaveSystem::save(const sf::Vector2f& playerPosition, const int& playerAnima
// std::cout << droppedItemId[i] << " " << droppedItemXPos[i] << " " << droppedItemYPos[i] << " " << droppedItemQuantity[i] << std::endl;
}

outFile << insideStructure << " " << fixedCameraPos.x << " " << fixedCameraPos.y
outFile << insideStructure << " " << structureIndex << " " << fixedCameraPos.x << " " << fixedCameraPos.y
<< " " << chapter << std::endl;

outFile << flagValues.size() << std::endl;
Expand All @@ -84,8 +84,8 @@ bool SaveSystem::load(sf::Vector2f& playerPosition, int& playerAnimation,
std::vector<float>& droppedItemYPos, std::vector<int>& droppedItemQuantity,
int& extracting, int& inSlot, int& completed, int& timerActive, int& startYear1,
int& startDay1, int& startHour1, int& startMinute1, int& slotItemId,
int& insideStructure, sf::Vector2f& fixedCameraPos, int& chapter,
std::vector<std::string>& flagKeys, std::vector<int>& flagValues) {
int& insideStructure, int& structureIndex, sf::Vector2f& fixedCameraPos,
int& chapter, std::vector<std::string>& flagKeys, std::vector<int>& flagValues) {

std::ifstream inFile(mSaveFilePath);
if (!inFile) {
Expand Down Expand Up @@ -255,6 +255,10 @@ bool SaveSystem::load(sf::Vector2f& playerPosition, int& playerAnimation,
std::cerr << "Error reading inside structure from save file." << std::endl;
return false;
}
if (!(inFile >> structureIndex)) {
std::cerr << "Error reading structure index from save file." << std::endl;
return false;
}
if (!(inFile >> fixedCameraPos.x)) {
std::cerr << "Error reading fixed camera pos X from save file." << std::endl;
return false;
Expand Down Expand Up @@ -297,7 +301,7 @@ bool SaveSystem::loadPartial(std::string saveFile, int& crystals, int& year, int
int bankBalance, penalty, interest, amountToRepay, daysToRepayment,
startYear, startDay, startHour, startMinute, hasBorrowActive, extracting,
inSlot, completed, timerActive, startYear1, startDay1, startHour1,
startMinute1, slotItemId, insideStructure, chapter;
startMinute1, slotItemId, insideStructure, structureIndex, chapter;
std::vector<int> droppedItemId;
std::vector<float> droppedItemXPos;
std::vector<float> droppedItemYPos;
Expand All @@ -321,8 +325,8 @@ bool SaveSystem::loadPartial(std::string saveFile, int& crystals, int& year, int
droppedItemId, droppedItemXPos, droppedItemYPos,
droppedItemQuantity, extracting, inSlot, completed, timerActive,
startYear1, startDay1, startHour1, startMinute1, slotItemId,
insideStructure, cameraFixedPosition, chapter, flagKeys,
flagValues);
insideStructure, structureIndex, cameraFixedPosition, chapter,
flagKeys, flagValues);

mSaveFilePath = tmp;
return success;
Expand Down
8 changes: 4 additions & 4 deletions src/Rpg/saveSystem/SaveSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SaveSystem {
const std::vector<float>& droppedItemYPos, const std::vector<int>& droppedItemQuantity,
const int& extracting, const int& inSlot, const int& completed, const int& timerActive,
const int& startYear1, const int& startDay1, const int& startHour1, const int& startMinute1,
const int& slotItemid, const int& insideStructure, const sf::Vector2f& fixedCameraPos,
const int& chapter, const std::vector<std::string>& flagKeys,
const int& slotItemid, const int& insideStructure, const int& structureIndex,
const sf::Vector2f& fixedCameraPos, const int& chapter, const std::vector<std::string>& flagKeys,
const std::vector<int>& flagValues);

bool load(sf::Vector2f& playerPosition, int& playerAnimation,
Expand All @@ -36,8 +36,8 @@ class SaveSystem {
std::vector<float>& droppedItemYPos, std::vector<int>& droppedItemQuantity,
int& extracting, int& inSlot, int& completed, int& timerActive, int& startYear1,
int& startDay1, int& startHour1, int& startMinute1, int& slotItemId,
int& insideStructure, sf::Vector2f& fixedCameraPos, int& chapter,
std::vector<std::string>& flagKeys, std::vector<int>& flagValues);
int& insideStructure, int& structureIndex, sf::Vector2f& fixedCameraPos,
int& chapter, std::vector<std::string>& flagKeys, std::vector<int>& flagValues);

// Method used for the extraction of some fields for MainMenu
bool loadPartial(std::string saveFile, int& crystals, int& year, int &day, int& hour, int& minute);
Expand Down
Loading