• Fichier: Save.cpp
  • Path: /dungeon_ascii/DungeonASCII/Save.cpp
  • File size: 2.01 KB
  • MIME-type: text/x-c
  • Charset: utf-8
 
Retour
#include "Save.h"

Save::Save()
{
	m_gameFileCount = 0;

	std::ifstream gameFile;

	do
	{
		gameFile.open("Save/saveSlot_" + std::to_string(m_gameFileCount) + ".txt");

		if (gameFile)
		{
			m_gameFileCount++;

			gameFile.close();
		}
	} while (gameFile);
}

int Save::getGameFileCount()
{
	return m_gameFileCount;
}

void Save::loadGameFile(Player **pPlayer, Sector **pSector, short *pGameState, int pGameFileSlot)
{
	if(pGameFileSlot > -1)
		m_gameFileSlot = pGameFileSlot;

	std::ifstream gameFile("Save/saveSlot_" + std::to_string(m_gameFileSlot) + ".txt");

	if (gameFile)
	{
		//Nom du joueur
		std::string stringData;
		gameFile >> stringData;

		//Coordonnee du joueur
		Vector2 playerCoordinates;
		gameFile >> playerCoordinates.x;
		gameFile >> playerCoordinates.y;

		//Vies du joueur
		int playerHP;
		gameFile >> playerHP;

		if (*pPlayer != NULL)
			**pPlayer = Player(stringData, playerCoordinates.x, playerCoordinates.y, playerHP);
		else
			*pPlayer = new Player(stringData, playerCoordinates.x, playerCoordinates.y, playerHP);
			

		//Secteur
		gameFile >> stringData;

		if (*pSector != NULL)
			**pSector = Sector(stringData);
		else
			*pSector = new Sector(stringData);

		(*pSector)->loadMap(*pPlayer, pGameState);

		gameFile.close();
	}
}

void Save::saveGameFile(Player *pPlayer, Sector *pSector, bool pCreateNewFile)
{
	std::ofstream gameFile("Save/saveSlot_" + (pCreateNewFile ? std::to_string(m_gameFileCount) : std::to_string(m_gameFileSlot)) + ".txt", std::ios::trunc);

	if (gameFile)
	{
		if (pCreateNewFile)
		{
			m_gameFileSlot = m_gameFileCount;
			m_gameFileCount++;
		}

		//Nom du joueur
		gameFile << pPlayer->getName() << " ";

		//Coordonnee du joueur
		gameFile << pPlayer->getCoordinates()->x << " ";
		gameFile << pPlayer->getCoordinates()->y << " ";

		//Vies du joueur
		gameFile << pPlayer->getHP() << " ";

		//Secteur
		gameFile << pSector->getSectorName();

		gameFile.close();
	}
}

Save::~Save()
{
}