-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveSystem.cpp
More file actions
75 lines (59 loc) · 1.77 KB
/
SaveSystem.cpp
File metadata and controls
75 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "SaveSystem.h"
#include "Game.h"
SaveSystem::SaveSystem()
{
}
SaveSystem::~SaveSystem()
{
}
// ******************************************Outside Method (Stevie)****************************************
// This function uses the posix function stat() to check and see if a file exists.
// stat() can be two to four times faster than checking with fopen().
// http://www.zentut.com/c-tutorial/c-file-exists/
// http://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c
// *********************************************************************************************************
bool SaveSystem::saveFileExists()
{
if (!SAVE_SYSTEM_ENABLED)
return false;
struct stat buffer;
return (stat(SAVE_FILE.c_str(), &buffer) == 0);
}
void SaveSystem::saveGame()
{
if (!SAVE_SYSTEM_ENABLED)
{
cout << "saveGame() failed - SAVE_SYSTEM_ENABLED is false" << endl;
return;
}
if (Game::getInstance()->getScore() == 0 && Game::getInstance()->getPlayer()->getNumLives() == 3)
return;
ofstream ofs(SAVE_FILE);
boost::archive::text_oarchive oa(ofs);
oa & *Game::getInstance()
& *Game::getInstance()->getEnemyManager()
& *Game::getInstance()->getPlayer();
}
void SaveSystem::destroySaveFile()
{
if (!saveFileExists())
return;
remove(SAVE_FILE.c_str());
}
void SaveSystem::loadGame()
{
Game::getInstance()->setLagTime(0.0f);
if (!SAVE_SYSTEM_ENABLED)
{
cout << "loadGame() failed - SAVE_SYSTEM_ENABLED is false" << endl;
return;
}
Game::getInstance()->resetTimer();
ifstream ifs(SAVE_FILE);
boost::archive::text_iarchive ia(ifs);
ia & *Game::getInstance()
& *Game::getInstance()->getEnemyManager()
& *Game::getInstance()->getPlayer();
// Prevent weird deaths on loading
Game::getInstance()->getPlayer()->becomeInvincible();
}