如何将结构的矢量数据保存到文件然后加载/检索它?

时间:2014-03-23 22:54:59

标签: c++ vector struct load save

我正致力于制作一个简单的'基于控制台的C ++游戏(VS 2012),我已经能够获得“玩家”的数据(xpos等)。要保存的类(通过将数据放入结构然后保存结构)。但是对于Enemies和Holes类,我无法保存数据。这两个类用作向量,并有自己的结构。

SaveGameData结构:这个存储有关游戏的基本数据(有多少活跃的僵尸,玩家的位置,有多少个洞,地图的大小和难度水平。

ZombieData struct:存储每个僵尸的X和Y位置

HoleData结构:存储每个孔的X和Y位置(我可能会使用ZombieData结构,稍后可能会将它们合并,但暂时忽略它们:P)

我的计划是通过它的类访问每个现有僵尸的数据,然后在每个类中使用get / set方法,从类向量中获取相关数据,并将其存储在结构中。然后以二进制格式将其写入.dat文件。

我无法确定这是否有效,因为......好吧......我无法打开文件。但是当我尝试加载它时,玩家会移动到他/她保存的状态。位置。因为孔不动,我不知道它们是否保存得当。但僵尸不能正常保存/加载。有时候,僵尸会加倍(IE如果一个人死亡,加载时,它会将“死亡的人”粘在一个现有的僵尸身边)或者他们根本不会恢复到他们保存的位置。

我注意到Holes的一个(或更多 - 无法分辨)在地图的左上角产生(没有任何东西可以到达的边界),我不知道为什么

我尝试在加载时删除它们,开始一个空白的平板,然后加载文件中的数据,但这不起作用。我无法弄清楚出了什么问题。

的Structs:

struct SaveGameData{

    int playerX;
    int playerY;
    int zombieCount;
    int holeCount;
    int sizeOfMap;
    int difficultyLevel;

};

struct ZombieData{

    int zombieX;
    int zombieY;

};

struct HoleData{

    int holeX;
    int holeY;

};


void saveGameState(SaveGameData&, vector<ZombieData>&, vector<HoleData>&, vector<Holes>&, vector<Enemies>&, int, int, int, int);
void loadGameState(SaveGameData&, fstream &, Player&, vector<Enemies>&, vector<Holes>&, vector<ZombieData>&, vector<HoleData>&);

主游戏(While)循环;

decision = ' ';

            cycle = cycle + 1;

            hSize = holes.size();
            eSize = zombies.size();
            pX = human.getX();
            pY = human.getY();
            pAlive = human.getAlive();  

            if(eSize == 0 || !pAlive)
            {
                loadFile.open("saveDataFile.dat", ios::in | ios::binary);
                if(loadFile)
                {
                    HNDCONSOLE_H gotoxy(10,10);
                    HNDCONSOLE_H setrgb(0);
                    cout << "Would you like to load from the save point? Y/N" << endl;
                    decision = _getch();
                    if(decision == 'y' || decision == 'Y'){
                        loadGameState(saveGame, loadFile, human, zombies, holes, zombieSaveData, holeSaveData);
                        gameover = false;
                    }
                    else
                        gameover = true;
                }
                else                    
                    gameover = true;
            }


            PLAYER_H human.movePlayer(mapSize, pAlive); 
            HOLES_H myHoles.drawHole(holes);
            ENEMIES_H myZombies.enemyMove(zombies, level, mapSize);
            PLAYER_H human.playerCollision(zombies, holes);
            ENEMIES_H myZombies.zombieCollide(zombies, holes);
            ENEMIES_H myZombies.zombieDelete(zombies);

            if(cycle % 15 == 0){
                HNDCONSOLE_H gotoxy(35,10);
                cout << "Would you like to save?" << endl;
                HNDCONSOLE_H gotoxy(38,11);
                cout << "Y/N" << endl;
                HNDCONSOLE_H gotoxy(35,12);
                decision = _getch();

                if(decision == 'y' || decision == 'Y')
                {
                    saveGameState(saveGame, zombieSaveData, holeSaveData, holes, zombies, mapSize, difficulty, pX, pY);
                    cout << "Game saved. Carry one playing as normal";
                }
                else
                    cout << "Your decision was No. Carry on playing as normal";
            }

保存/加载功能;

void saveGameState(SaveGameData& newSave, vector<ZombieData>& newZombieData, vector<HoleData>& newHoleData, vector<Holes>& hole, vector<Enemies>& zombie, int mapSize, int difficulty, int pX, int pY){

    fstream saveFile, zombieFile, holeFile;


    saveFile.open("saveDataFile.dat", ios::out | ios::binary);

    newSave.sizeOfMap = mapSize;
    newSave.difficultyLevel = difficulty;
    newSave.playerX = pX;
    newSave.playerY = pY;
    newSave.holeCount = hole.size();
    newSave.zombieCount = zombie.size();

    saveFile.write(reinterpret_cast<char *>(&newSave), sizeof(newSave));

    saveFile.close();


    zombieFile.open("saveZombieFile.dat", ios::out | ios::binary);
    for(int i = 0; i < newSave.zombieCount; i++)
    {
        newZombieData.push_back(ZombieData());
        newZombieData[i].zombieX = zombie[i].getX();
        newZombieData[i].zombieY = zombie[i].getY();
    }
    zombieFile.close();


    holeFile.open("saveZombieFile.dat", ios::out | ios::binary);
    for(int i = 0; i < newSave.holeCount; i++)
    {
        newHoleData.push_back(HoleData());
        newHoleData[i].holeX = hole[i].getX();
        newHoleData[i].holeY = hole[i].getY();
    }
    holeFile.close();

}

我遗忘的名字(我添加了它,以便您可以更轻松地看到变量类型?)

void loadGameState(SaveGameData& newSave, fstream &loadFile, Player& newHuman, vector<Enemies>& newZombie, vector<Holes>& newHole, vector<ZombieData>& newZombieData, vector<HoleData>& newHoleData){


    loadFile.read(reinterpret_cast<char *>(&newSave), sizeof(newSave));

    int zombieCount = newSave.zombieCount;
    int holeCount = newSave.holeCount;

    newHuman.setX(newSave.playerX);
    newHuman.setY(newSave.playerY);

    for(int k = 0; k < newZombie.size(); k++)
        newZombie.erase(newZombie.begin() + k);

    for(int l = 0; l < newHole.size(); l++)     
        newHole.erase(newHole.begin() + l);

    for(int i = 0; i < zombieCount; i++)
    {
        newZombie.push_back(Enemies());
        newZombie[i].setX(newZombieData[i].zombieX);
        newZombie[i].setY(newZombieData[i].zombieY);
    }

    for(int j = 0; j < holeCount; j++)
    {
        newHole.push_back(Holes());
        newHole[j].setX(newHoleData[j].holeX);
        newHole[j].setY(newHoleData[j].holeY);
    }

    loadFile.close();
}

0 个答案:

没有答案