diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Quest.cpp | 36 | ||||
-rw-r--r-- | src/entities.cpp | 21 | ||||
-rw-r--r-- | src/gameplay.cpp | 10 | ||||
-rw-r--r-- | src/world.cpp | 73 |
4 files changed, 65 insertions, 75 deletions
diff --git a/src/Quest.cpp b/src/Quest.cpp index 535afc5..e59d79f 100644 --- a/src/Quest.cpp +++ b/src/Quest.cpp @@ -33,39 +33,41 @@ int QuestHandler::assign(std::string title,std::string desc,std::string req){ return 0;
}
+#include <algorithm>
+
int QuestHandler::drop(std::string title){
- for(unsigned int i=0;i<current.size();i++){
- if(current[i].title == title){
- current.erase(current.begin()+i);
- return 0;
- }
- }
- return -1;
+ current.erase( std::remove_if( current.begin(),
+ current.end(),
+ [&](Quest q){ return q.title == title; }),
+ current.end() );
+
+ return 0;
}
int QuestHandler::finish(std::string t){
- for(unsigned int i=0;i<current.size();i++){
- if(current[i].title == t){
- for(auto &n : current[i].need){
- if(player->inv->hasItem(n.name) < n.n)
+ for ( auto c = current.begin(); c != current.end(); c++ ) {
+ if ( (*c).title == t ) {
+ for ( auto &n : (*c).need ) {
+ if ( player->inv->hasItem( n.name ) < n.n )
return 0;
}
- for(auto &n : current[i].need){
- player->inv->takeItem(n.name,n.n);
- }
+ for ( auto &n : (*c).need )
+ player->inv->takeItem( n.name, n.n );
- current.erase(current.begin()+i);
+ current.erase( c );
return 1;
}
}
+
return 0;
}
bool QuestHandler::hasQuest(std::string t){
- for(unsigned int i=0;i<current.size();i++){
- if(current[i].title == t)
+ for ( auto &c : current ) {
+ if ( c.title == t )
return true;
}
+
return false;
}
diff --git a/src/entities.cpp b/src/entities.cpp index 4291b0a..5c60386 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -241,18 +241,11 @@ Object::Object(){ inv = NULL; } -Object::Object(std::string in, const char *pd){ +Object::Object(std::string in, std::string pd){ iname = in; - if(pd){ - pickupDialog = new char[strlen(pd)+1]; - strcpy(pickupDialog,pd); - questObject = true; - }else{ - pickupDialog = new char[1]; - *pickupDialog = '\0'; - questObject = false; - } + pickupDialog = pd; + questObject = !pd.empty(); type = OBJECTT; alive = true; @@ -265,7 +258,6 @@ Object::Object(std::string in, const char *pd){ inv = NULL; } Object::~Object(){ - delete[] pickupDialog; delete tex; delete[] name; } @@ -464,10 +456,10 @@ void Merchant::interact(){ void Object::interact(void){ if(questObject && alive){ - ui::dialogBox(player->name,":Yes:No",false,pickupDialog); + ui::dialogBox( player->name, ":Yes:No", false, pickupDialog.c_str()); ui::waitForDialog(); if(ui::dialogOptChosen == 1){ - player->inv->addItem(/*(ITEM_ID)(identifier)*/iname, 1); + player->inv->addItem( iname, 1 ); alive = false; } }else{ @@ -523,6 +515,7 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ inv = NULL; break; } + return 0; } @@ -541,7 +534,7 @@ void Mob::wander(int timeRun){ player->loc.x + (width / 2) > loc.x && player->loc.x + (width / 2) < loc.x + width && player->loc.y + (height / 3) > loc.y && player->loc.y + (height / 3) < loc.y + height ){ Arena *a = new Arena(currentWorld,player,this); - a->setBackground(BG_FOREST); + a->setBackground( WorldBGType::Forest ); a->setBGM("assets/music/embark.wav"); ui::toggleWhiteFast(); YAYA = true; diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 3d2ea25..a286db9 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -68,12 +68,12 @@ int commonAIFunc(NPC *speaker){ */ if((oxml = exml->FirstChildElement("quest"))){ - const char *qname; + std::string qname; - while(oxml){ - if((qname = oxml->Attribute("assign"))) + while ( oxml ) { + if ( !(qname = oxml->StrAttribute("assign")).empty() ) player->qh.assign(qname,"None",(std::string)oxml->GetText()); - else if((qname = oxml->Attribute("check"))){ + else if( !(qname = oxml->StrAttribute("check")).empty() ){ if(player->qh.hasQuest(qname) && player->qh.finish(qname)){ goto CONT; }else{ @@ -283,7 +283,7 @@ void initEverything(void){ * Read in the XML file. */ - currentWorld = loadWorldFromXML(xmlFiles[i].c_str()); + currentWorld = loadWorldFromXML(xmlFiles[i]); break; } } diff --git a/src/world.cpp b/src/world.cpp index a9e0fd9..bef6fc0 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -15,11 +15,15 @@ #define INDOOR_FLOOR_HEIGHT 100 + +extern Player *player; + + /** * Contains the current weather, used in many other places/files. */ -WEATHER weather = SUNNY; +WorldWeather weather = WorldWeather::Sunny; const std::string bgPaths[2][9]={ {"bg.png", // Daytime background @@ -71,15 +75,15 @@ const float bgDraw[4][3]={ */ void World:: -setBackground( WORLD_BG_TYPE bgt ) +setBackground( WorldBGType bgt ) { // load textures with a limit check switch ( (bgType = bgt) ) { - case BG_FOREST: + case WorldBGType::Forest: bgTex = new Texturec( bgFiles ); break; - case BG_WOODHOUSE: + case WorldBGType::WoodHouse: bgTex = new Texturec( bgFilesIndoors ); break; @@ -97,12 +101,12 @@ setBackground( WORLD_BG_TYPE bgt ) */ void World:: -setStyle( const char *pre ) +setStyle( std::string pre ) { unsigned int i; // get folder prefix - std::string prefix = pre ? pre : "assets/style/classic/"; + std::string prefix = pre.empty() ? "assets/style/classic/" : pre; for ( i = 0; i < arrAmt(buildPaths); i++ ) sTexLoc.push_back( prefix + buildPaths[i] ); @@ -127,9 +131,7 @@ setStyle( const char *pre ) World:: World( void ) { - // nullify strings - bgm = NULL; - bgmObj = NULL; + bgmObj = NULL; toLeft = NULL; toRight = NULL; @@ -199,10 +201,6 @@ World:: if(bgmObj) Mix_FreeMusic(bgmObj); - // bgm path - if(bgm) - delete[] bgm; - delete bgTex; delete[] toLeft; @@ -339,7 +337,8 @@ update( Player *p, unsigned int delta ) void World:: setBGM( std::string path ) { - bgmObj = Mix_LoadMUS( strcpy( (bgm = new char[ path.size() + 1 ]), path.c_str()) ); + if( !path.empty() ) + bgmObj = Mix_LoadMUS( (bgm = path).c_str() ); } /** @@ -349,7 +348,7 @@ setBGM( std::string path ) */ void World:: -bgmPlay( World *prev ) +bgmPlay( World *prev ) const { if ( prev ) { if ( bgm != prev->bgm ) { @@ -432,8 +431,8 @@ draw( Player *p ) // draw the stars if the time deems it appropriate - if (((( weather == DARK ) & ( tickCount % DAY_CYCLE )) < DAY_CYCLE / 2) || - ((( weather == SUNNY ) & ( tickCount % DAY_CYCLE )) > DAY_CYCLE * .75) ){ + if (((( weather == WorldWeather::Dark ) & ( tickCount % DAY_CYCLE )) < DAY_CYCLE / 2) || + ((( weather == WorldWeather::Sunny ) & ( tickCount % DAY_CYCLE )) > DAY_CYCLE * .75) ){ if (tickCount % DAY_CYCLE) { // The above if statement doesn't check for exact midnight. @@ -872,6 +871,7 @@ void World::addStructure(BUILD_SUB sub, float x,float y, std::string tex, std::s build.push_back(new Structures()); build.back()->inWorld = this; build.back()->textureLoc = tex; + build.back()->spawn(sub,x,y); build.back()->inside = inside; @@ -909,7 +909,7 @@ void World::addMerchant(float x, float y){ entity.push_back(npc.back()); } -void World::addObject(/*ITEM_ID i*/std::string in,const char *p, float x, float y){ +void World::addObject( std::string in, std::string p, float x, float y){ object.push_back(new Object(in,p)); object.back()->spawn(x,y); @@ -925,20 +925,12 @@ addParticle( float x, float y, float w, float h, float vx, float vy, Color color void World::addLight(vec2 loc, Color color){ if(light.size() < 64){ - light.push_back(Light()); + light.emplace_back(); light.back().loc = loc; light.back().color = color; } } -NPC *World::getAvailableNPC(void){ - for(auto &n : npc){ - if(n->aiFunc.empty()) - return n; - } - return (NPC *)NULL; -} - char *World::setToLeft(const char *file){ if(toLeft) delete[] toLeft; @@ -1043,7 +1035,9 @@ void World::addHole(unsigned int start,unsigned int end){ } } -int World::getTheWidth(void){ +int World:: +getTheWidth( void ) const +{ return worldStart * -2; } @@ -1300,7 +1294,7 @@ std::string currentXML; extern World *currentWorld; -World *loadWorldFromXML(const char *path){ +World *loadWorldFromXML(std::string path){ if ( !currentXML.empty() ) currentWorld->save(); @@ -1312,7 +1306,7 @@ World *loadWorldFromXML(const char *path){ */ World * -loadWorldFromXMLNoSave( const char *path ) { +loadWorldFromXMLNoSave( std::string path ) { XMLDocument xml; XMLElement *wxml; XMLElement *vil; @@ -1351,9 +1345,9 @@ loadWorldFromXMLNoSave( const char *path ) { else abort(); }else if(!strcmp(name,"style")){ - tmp->setStyle(wxml->Attribute("folder")); - tmp->setBackground((WORLD_BG_TYPE)wxml->UnsignedAttribute("background")); - tmp->setBGM(wxml->Attribute("bgm")); + tmp->setStyle(wxml->StrAttribute("folder")); + tmp->setBackground((WorldBGType)wxml->UnsignedAttribute("background")); + tmp->setBGM(wxml->StrAttribute("bgm")); }else if(!strcmp(name,"generation")){ if(!strcmp(wxml->Attribute("type"),"Random")){ if(Indoor) @@ -1395,14 +1389,13 @@ loadWorldFromXMLNoSave( const char *path ) { else tmp->npc.back()->dialogIndex = 9999; }else if(!strcmp(name,"structure")){ - ptr = wxml->Attribute("inside"); tmp->addStructure((BUILD_SUB)wxml->UnsignedAttribute("type"), wxml->QueryFloatAttribute("x",&spawnx) != XML_NO_ERROR ? getRand() % tmp->getTheWidth() / 2.0f : spawnx, 100, - (char*)wxml->Attribute("texture"), - ptr); + wxml->StrAttribute("texture"), + wxml->StrAttribute("inside")); }else if(!strcmp(name,"trigger")){ tmp->addMob(MS_TRIGGER,wxml->FloatAttribute("x"),0,commonTriggerFunc); tmp->mob.back()->heyid = wxml->Attribute("id"); @@ -1428,13 +1421,12 @@ loadWorldFromXMLNoSave( const char *path ) { * READS DATA ABOUT STRUCTURE CONTAINED IN VILLAGE */ - if(!strcmp(name,"structure")){ + if(!strcmp(name,"structure")){ tmp->addStructure((BUILD_SUB)vil->UnsignedAttribute("type"), vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, vil->StrAttribute("texture"), vil->StrAttribute("inside")); - }else if(!strcmp(name, "stall")){ if(!strcmp(vil->Attribute("type"),"market")){ std::cout << "Market" << std::endl; @@ -1462,10 +1454,13 @@ loadWorldFromXMLNoSave( const char *path ) { vil->StrAttribute("inside")); } } - vptr->build.push_back(tmp->build.back()); + + vptr->build.push_back(tmp->build.back()); + if(vptr->build.back()->loc.x < vptr->start.x){ vptr->start.x = vptr->build.back()->loc.x; } + if(vptr->build.back()->loc.x + vptr->build.back()->width > vptr->end.x){ vptr->end.x = vptr->build.back()->loc.x + vptr->build.back()->width; } |