diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.cpp | 30 | ||||
-rw-r--r-- | src/entities.cpp | 67 | ||||
-rw-r--r-- | src/gameplay.cpp | 6 | ||||
-rw-r--r-- | src/ui.cpp | 3 | ||||
-rw-r--r-- | src/world.cpp | 41 |
5 files changed, 108 insertions, 39 deletions
diff --git a/src/config.cpp b/src/config.cpp index ca6db1b..efa2523 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -9,9 +9,9 @@ extern unsigned int SCREEN_WIDTH; extern unsigned int SCREEN_HEIGHT; extern bool FULLSCREEN; -extern float VOLUME_MASTER; -extern float VOLUME_MUSIC; -extern float VOLUME_SFX; +extern float VOLUME_MASTER; +extern float VOLUME_MUSIC; +extern float VOLUME_SFX; XMLDocument xml; XMLElement *scr; @@ -19,6 +19,7 @@ XMLElement *vol; void readConfig(){ unsigned int uval; + float fval; bool bval; xml.LoadFile("config/settings.xml"); @@ -36,18 +37,23 @@ void readConfig(){ if(xml.FirstChildElement("hline")->QueryUnsignedAttribute("size",&uval) == XML_NO_ERROR) HLINE = uval; else HLINE = 3; - - /*SCREEN_WIDTH = scr->UnsignedAttribute("width"); - SCREEN_HEIGHT = scr->UnsignedAttribute("height"); - FULLSCREEN = scr->BoolAttribute("fullscreen"); - HLINE = xml.FirstChildElement("hline")->UnsignedAttribute("size");*/ vol = xml.FirstChildElement("volume"); - VOLUME_MASTER = vol->FirstChildElement("master")->FloatAttribute("volume"); - VOLUME_MUSIC = vol->FirstChildElement("music")->FloatAttribute("volume"); - VOLUME_SFX = vol->FirstChildElement("sfx")->FloatAttribute("volume"); - std::cout<<"Loading font through settings.xml..."<<std::endl; + if(vol->FirstChildElement("master")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + VOLUME_MASTER = fval; + else VOLUME_MASTER = 50; + if(vol->FirstChildElement("music")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + VOLUME_MUSIC = fval; + else VOLUME_MUSIC = 50; + if(vol->FirstChildElement("sfx")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + VOLUME_SFX = fval; + else VOLUME_SFX = 50; + + Mix_Volume(0,VOLUME_MASTER); + Mix_Volume(1,VOLUME_SFX); + Mix_VolumeMusic(VOLUME_MUSIC); + ui::initFonts(); ui::setFontFace(xml.FirstChildElement("font")->Attribute("path")); } diff --git a/src/entities.cpp b/src/entities.cpp index e020f8b..71a0890 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -332,9 +332,6 @@ NOPE: if(near)ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-HLINE/2,name); } -void Player::interact(){ -} - /* * NPC::wander, this makes the npcs wander around the near area * @@ -546,3 +543,67 @@ void Mob::wander(int timeRun){ break; } } + +void Player::save(void){ + std::string data; + std::ofstream out ("xml/main.dat",std::ios::out | std::ios::binary); + std::cout<<"Saving player data..."<<std::endl; + data.append(std::to_string((int)loc.x) + "\n"); + data.append(std::to_string((int)loc.y) + "\n"); + data.append(std::to_string((int)health) + "\n"); + data.append(std::to_string((int)maxHealth) + "\n"); + + data.append(std::to_string((int)inv->items.size()) + "\n"); + for(auto &i : inv->items) + data.append(std::to_string((int)i.count) + "\n" + std::to_string((int)i.id) + "\n"); + + data.append((std::string)(currentXML+4) + "\n"); + + data.append("dOnE\0"); + out.write(data.c_str(),data.size()); + out.close(); +} + +void Player::sspawn(float x,float y){ + unsigned int i; + uint count; + std::ifstream in ("xml/main.dat",std::ios::in | std::ios::binary); + spawn(x,y); + + if(in.good()){ + std::istringstream data; + std::string ddata; + std::streampos len; + + in.seekg(0,std::ios::end); + len = in.tellg(); + in.seekg(0,std::ios::beg); + + std::vector<char> buf (len,'\0'); + in.read(buf.data(),buf.size()); + + data.rdbuf()->pubsetbuf(buf.data(),buf.size()); + + std::getline(data,ddata); + loc.x = std::stoi(ddata); + std::getline(data,ddata); + loc.y = std::stoi(ddata); + std::getline(data,ddata); + health = std::stoi(ddata); + std::getline(data,ddata); + maxHealth = std::stoi(ddata); + + std::getline(data,ddata); + for(i = std::stoi(ddata);i;i--){ + std::getline(data,ddata); + count = std::stoi(ddata); + std::getline(data,ddata); + inv->items.push_back((item_t){count,(uint)std::stoi(ddata)}); + } + + std::getline(data,ddata); + currentWorld = loadWorldFromXMLNoSave(ddata.c_str()); + + in.close(); + } +} diff --git a/src/gameplay.cpp b/src/gameplay.cpp index e252c26..4bbc672 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -278,7 +278,7 @@ void initEverything(void){ */ for(unsigned int i=0;i<xmlFiles.size();i++){ - if(strncmp(xmlFiles[i].c_str(),".",1) && strncmp(xmlFiles[i].c_str(),"..",2)){ + if(xmlFiles[i] != "." && xmlFiles[i] != ".." && strcmp(xmlFiles[i].c_str()+xmlFiles[i].size()-3,"dat")){ /* * Read in the XML file. @@ -307,9 +307,9 @@ void initEverything(void){ /* * Spawn the player and begin the game. */ - + player = new Player(); - player->spawn(200,100); + player->sspawn(0,100); currentWorld->bgmPlay(NULL); atexit(destroyEverything); @@ -59,7 +59,7 @@ extern Menu* currentMenu; extern Menu pauseMenu; -Mix_Chunk *dialogClick; +static Mix_Chunk *dialogClick; extern void mainLoop(void); @@ -1179,6 +1179,7 @@ DONE: if(SDL_KEY == SDLK_ESCAPE){ //gameRunning = false; currentMenu = &pauseMenu; + player->save(); return; } switch(SDL_KEY){ diff --git a/src/world.cpp b/src/world.cpp index 2012d32..25b7d35 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -329,10 +329,7 @@ void World::update(Player *p,unsigned int delta){ p->loc.y += p->vel.y * delta; p->loc.x +=(p->vel.x * p->speed) * delta; - /*if(p->inv->usingi){ - p->inv->useItem(); - }*/ - + /* * Update coordinates of all entities except for structures. */ @@ -374,20 +371,20 @@ void World::update(Player *p,unsigned int delta){ } } -void World::setBGM(const char *path){ - if(!bgm) delete[] bgm; - if(path != NULL){ - bgm = new char[strlen(path) + 1]; - strcpy(bgm,path); - bgmObj = Mix_LoadMUS(bgm); - }else{ - bgm = new char[1]; - bgm[0] = '\0'; - } +void World::setBGM(std::string path){ + bgm = new char[path.size()]; + strcpy(bgm,path.c_str()); + bgmObj = Mix_LoadMUS(bgm); } void World::bgmPlay(World *prev){ - if(prev && strcmp(bgm,prev->bgm)){ + if(prev){ + if(bgm != prev->bgm){ + Mix_FadeOutMusic(800); + Mix_VolumeMusic(50); + Mix_PlayMusic(bgmObj,-1); // Loop infinitely + } + }else{ Mix_FadeOutMusic(800); Mix_VolumeMusic(50); Mix_PlayMusic(bgmObj,-1); // Loop infinitely @@ -1477,6 +1474,15 @@ char *currentXML = NULL; extern World *currentWorld; World *loadWorldFromXML(const char *path){ + if(currentXML){ + currentWorld->save(); + delete[] currentXML; + } + + return loadWorldFromXMLNoSave(path); +} + +World *loadWorldFromXMLNoSave(const char *path){ XMLDocument xml; XMLElement *wxml; @@ -1488,11 +1494,6 @@ World *loadWorldFromXML(const char *path){ unsigned int size = 5 + strlen(path); - if(currentXML){ - currentWorld->save(); - delete[] currentXML; - } - memset((currentXML = new char[size]),0,size); strcpy(currentXML,"xml/"); strcat(currentXML,path); |