]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
c++'d more stuff
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 3 Mar 2016 20:45:10 +0000 (15:45 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 3 Mar 2016 20:45:10 +0000 (15:45 -0500)
Makefile
include/entities.h
include/world.h
main.cpp
src/Quest.cpp
src/entities.cpp
src/gameplay.cpp
src/world.cpp

index 305d0d638eaff5b09d75a4302e175aadd457db6e..1df683c0d8e8be6463ea31eb4c8f4b95feda70ee 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -25,6 +25,9 @@ EXEC = main
 
 all: $(EXEC)
 
+dirty:
+       rm -rf out/world.o
+
 clean:
        rm -f $(EXEC)
        rm -f out/*.o
index 3b8a19bb0a93c50bbf640902db88d16e73ca07b6..334bfeb101066b0cba07b46a0f3a1535cb2438e2 100644 (file)
@@ -254,11 +254,11 @@ class Object : public Entity{
 private:
        std::string iname;
 public:
-       char *pickupDialog;
+       std::string pickupDialog;
        bool questObject = false;
        
        Object();
-       Object(std::string in,const char *pd);
+       Object(std::string in,std::string pd);
        ~Object();
        
        void reloadTexture(void);
index 7b9383a2c90cc025a1f035b971eefc155a783618..825d83df25b887bc9b8ac9e1b2aba3067fc38b20 100644 (file)
  * in World::setBackground() to select the appropriate images.
  */
 
-typedef enum {
-       BG_FOREST,              /**< A forest theme. */
-       BG_WOODHOUSE,   /**< An indoor wooden house theme. */
-} WORLD_BG_TYPE;
+enum class WorldBGType : unsigned char {
+       Forest,         /**< A forest theme. */
+       WoodHouse       /**< An indoor wooden house theme. */
+};
 
 /**
  * The weather type enum.
@@ -40,11 +40,11 @@ typedef enum {
  * Weather is set by the world somewhere.
  */
 
-typedef enum {
-       SUNNY = 0,      /**< Sunny/daytime */
-       DARK,           /**< Nighttime */
-       RAIN            /**< Rain (to be implemented)*/
-} WEATHER;
+enum class WorldWeather : unsigned char {
+       Sunny = 0,      /**< Sunny/daytime */
+       Dark,           /**< Nighttime */
+       Rain            /**< Rain (to be implemented)*/
+};
 
 /**
  * The light structure, used to store light coordinates and color.
@@ -76,56 +76,16 @@ class World;
 
 class Village {
 public:
-
-       /**
-        * The name of the village.
-        */
-
        std::string name;
-       
-       /**
-        * The coordinate of where the village starts.
-        * 
-        * This is used to check if the player has entered the village's area.
-        */
-       
        vec2 start;
-       
-       /**
-        * The coordinate of where the village ends.
-        * 
-        * This is used to check if the player has entered the village's area.
-        */
-       
        vec2 end;
-       
-       /**
-        * TODO
-        */
-       
        bool in;
-
-       /**
-        * A vector of all structures that are associated with this village.
-        */
-
        std::vector<Structures *> build;
        
-       /**
-        * Creates a village of name `meme` in the world `w`.
-        */
-       
        Village(const char *meme, World *w);
-       
-       /**
-        * Destructor...
-        */
-        
        ~Village(void){}
 };
 
-extern Player *player;
-
 /**
  * The world class. This class does everything a world should do.
  */
@@ -194,7 +154,7 @@ protected:
         * Defines the set of background images that should be used for this world.
         */
        
-       WORLD_BG_TYPE bgType;
+       WorldBGType bgType;
        
        /**
         * The Mix_Music object that holds the background soundtrack for the world.
@@ -206,7 +166,8 @@ protected:
         * The file path of the song wished to be loaded by bgmObj.
         */
        
-       char *bgm;
+       std::string bgm;
+       
        std::vector<std::string>bgFiles;
        std::vector<std::string>bgFilesIndoors;
        
@@ -238,11 +199,6 @@ public:
        
        char *setToRight(const char *file);
 
-       void callUpdate(){
-               this->update(player,deltaTime);
-       }
-
-
        /**
         * A vector of pointers to every NPC, Structure, Mob, and Object in this
         * world.
@@ -345,7 +301,7 @@ public:
         * upon object interaction.
         */
        
-       void addObject(std::string in, const char *pickupDialog, float x, float y);
+       void addObject( std::string in, std::string pickupDialog, float x, float y);
        
        /**
         * Adds a particle to the world with the specified coordinates, dimensions,
@@ -359,13 +315,6 @@ public:
         */
        
        void addLight(vec2 xy, Color color);
-
-       /**
-        * Get the next NPC in the NPC vector that currently lacks a written dialog.
-        * Could be used to assign random NPCs non-random dialogs.
-        */
-
-       NPC *getAvailableNPC(void);
        
        /**
         * Updates the coordinates of everything in the world that has coordinates
@@ -387,7 +336,7 @@ public:
         * Texturec object.
         */
        
-       void setBackground(WORLD_BG_TYPE bgt);
+       void setBackground(WorldBGType bgt);
        
        /**
         * Sets the background music for the world, required for the world to be
@@ -400,14 +349,14 @@ public:
         *      Sets the worlds style folder
         */
 
-       void setStyle(const char* pre); 
+       void setStyle(std::string pre); 
        
        /**
         * Plays/stops this world's BGM. If `prev` is not NULL, that world's BGM
         * will be faded out followed by the fading in of this world's BGM.
         */
        
-       void bgmPlay(World *prev);
+       void bgmPlay(World *prev) const;
        
        /**
         * Draw the world and entities based on the player's coordinates.
@@ -457,7 +406,7 @@ public:
         *      Get's the world's width.
         */
        
-       int getTheWidth(void);
+       int getTheWidth(void) const;
        
        void save(void);
        void load(void);
@@ -488,7 +437,7 @@ public:
 extern int worldShade;
 extern std::string currentXML;
 
-World *loadWorldFromXML(const char *path);
-World *loadWorldFromXMLNoSave(const char *path);
+World *loadWorldFromXML(std::string path);
+World *loadWorldFromXMLNoSave(std::string path);
 
 #endif // WORLD_H
index 3769379e15151be9f22007713b4257a6197a7cf3..e96a5557235c53f6d79cc7261638657b65fc7731 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -162,7 +162,7 @@ Menu *currentMenu;
 Menu optionsMenu;
 Menu pauseMenu;
 
-extern WEATHER weather;
+extern WorldWeather weather;
 
 extern int  fadeIntensity;
 extern bool fadeEnable;
@@ -933,10 +933,10 @@ void logic(){
         *      Switch between day and night (SUNNY and DARK) if necessary.
        */
        if(!(tickCount%DAY_CYCLE)||!tickCount){
-               if(weather==SUNNY){
-                       weather=DARK;
-               }else{
-                       weather=SUNNY;
+               if ( weather == WorldWeather::Sunny )
+                       weather = WorldWeather::Dark;
+               else {
+                       weather = WorldWeather::Sunny;
                        Mix_Pause(2);
                }
        }
index 535afc52032bb867b039b1ce560d658cce275b37..e59d79f76fb630d8351f4d1587df4cf2a53accb9 100644 (file)
@@ -33,39 +33,41 @@ int QuestHandler::assign(std::string title,std::string desc,std::string req){
        return 0;\r
 }\r
 \r
+#include <algorithm>\r
+\r
 int QuestHandler::drop(std::string title){\r
-       for(unsigned int i=0;i<current.size();i++){\r
-               if(current[i].title == title){\r
-                       current.erase(current.begin()+i);\r
-                       return 0;\r
-               }\r
-       }\r
-       return -1;\r
+       current.erase( std::remove_if( current.begin(),\r
+                                                                  current.end(),\r
+                                                                  [&](Quest q){ return q.title == title; }),\r
+                                  current.end() );\r
+       \r
+       return 0;\r
 }\r
 \r
 int QuestHandler::finish(std::string t){\r
-       for(unsigned int i=0;i<current.size();i++){\r
-               if(current[i].title == t){\r
-                       for(auto &n : current[i].need){\r
-                               if(player->inv->hasItem(n.name) < n.n)\r
+       for ( auto c = current.begin(); c != current.end(); c++ ) {\r
+               if ( (*c).title == t ) {\r
+                       for ( auto &n : (*c).need ) {\r
+                               if ( player->inv->hasItem( n.name ) < n.n )\r
                                        return 0;\r
                        }\r
                        \r
-                       for(auto &n : current[i].need){\r
-                               player->inv->takeItem(n.name,n.n);\r
-                       }\r
+                       for ( auto &n : (*c).need )\r
+                               player->inv->takeItem( n.name, n.n );\r
                        \r
-                       current.erase(current.begin()+i);\r
+                       current.erase( c );\r
                        return 1;\r
                }\r
        }\r
+       \r
        return 0;\r
 }\r
 \r
 bool QuestHandler::hasQuest(std::string t){\r
-       for(unsigned int i=0;i<current.size();i++){\r
-               if(current[i].title == t)\r
+       for ( auto &c : current ) {\r
+               if ( c.title == t )\r
                        return true;\r
        }\r
+       \r
        return false;\r
 }\r
index 4291b0a127c6fdbd0e6ccd8ab4406af61371ee46..5c60386b886d34eca713e3ed488149009e5b5b7a 100644 (file)
@@ -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;
index 3d2ea258db8e0bd938216ce7ac89816e3128ba00..a286db95a62505ed816046dca11f6d3fe07a12c8 100644 (file)
@@ -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;
                }
        }
index a9e0fd98e78cdf50820f8ef5afc75d44a0f3c726..bef6fc0d191932b6e1158cb70bc38e42857c1b16 100644 (file)
 
 #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;
                }