]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
created base for indoor areas
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 26 Sep 2015 20:07:07 +0000 (16:07 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 26 Sep 2015 20:07:07 +0000 (16:07 -0400)
include/world.h
src/main.cpp
src/world.cpp

index ff1a3ef24cfe3ad1b25ea0c5ef1343af34f98792..444b2c480bbe93d3803ad14a06b30e0c420e0d95 100644 (file)
@@ -7,7 +7,7 @@
  *     World - creates and handles an area of land
 */
 class World {
-private:
+protected:
        /*
         *      struct line_t
         * 
@@ -32,13 +32,15 @@ public:
        World *toLeft,*toRight;         // Pointers to areas to the left and right of this world. These are made public
                                                                // so that they can easily be set without a function.
                                                                
-       World(unsigned int width);      // Generates a world of width 'width'.
+       World(void);
        ~World(void);                           // Frees the 'line' array.
        
+       virtual void generate(unsigned int width);                      // Generate the world
+       
        void addLayer(unsigned int width);                                      // Generates a new world and makes 'behind' point to it. If 'behind'
                                                                                                                // already points to a world, the new world will be set to be behind 'behind'.
                                                                                                                
-       void draw(vec2 *vec);                                                           // Draws the world around the coordinates 'vec'
+       virtual void draw(vec2 *vec);                                           // Draws the world around the coordinates 'vec'
        
        
        void detect(Player *p);                                                         // Insures objects/entities stored in an Entity class stay outside of the
@@ -58,4 +60,16 @@ public:
        
 };
 
+/*
+ *     IndoorWorld - Indoor settings stored in a World class ;)
+ */
+class IndoorWorld : public World {
+public:
+       IndoorWorld(void);
+       ~IndoorWorld(void);
+       
+       void generate(unsigned int width);
+       void draw(vec2 *vec);
+};
+
 #endif // WORLD_H
index 6049d2cb069f9c8bb46a10a89e1791639e98d387..1747915be8b64e4b7de9fae91aabb5eb8a39f119 100644 (file)
@@ -86,11 +86,18 @@ int main(int argc, char *argv[]){
        //************************************************************************//
 
        // Make a world
-       World *test =new World(SCREEN_WIDTH/2);
+       World *test=new World();
+       test->generate(SCREEN_WIDTH/2);
        test->addLayer(400);
        test->addLayer(100);    
        currentWorld=test;
        
+       IndoorWorld *iw=new IndoorWorld();
+       iw->generate(200);
+       
+       test->toRight=iw;
+       iw->toLeft=test;
+       
        // Make the player
        player=new Player();
        player->spawn(0,100);
@@ -105,13 +112,6 @@ int main(int argc, char *argv[]){
        for(i=0;i<entity.size()+1;i++){
                entity[i]->inWorld=test;
        }
-       for(i=0;i<entity.size();i++){
-               std::cout<<(unsigned)&*entity[i]<<std::endl;
-       }
-       std::cout<<std::endl;
-       for(i=0;i<npc.size();i++){
-               std::cout<<(unsigned)&npc[i]<<std::endl;
-       }
        
        //************************************************************************//
        //      END WORLD GENERATION STUFF                                                                                        //
index fde75bba4f1e57ddee139e34dc0bf96c72e9b3e6..44ea10d973df54a1ad9ddb78cb0e575bcbe4c1ee 100644 (file)
@@ -12,6 +12,8 @@
 #define DRAW_Y_OFFSET 50       // Defines how many pixels each layer should be offset from each other on the y axis when drawn.
 #define DRAW_SHADE       40    // Defines a shade increment for draw()
 
+#define INDOOR_FLOOR_HEIGHT 100 // Defines how high the base floor of an IndoorWorld should be
+
 extern std::vector<Entity *>entity;
 
 void safeSetColor(int r,int g,int b){  // safeSetColor() is an alternative to directly using glColor3ub() to set
@@ -24,7 +26,10 @@ void safeSetColor(int r,int g,int b){        // safeSetColor() is an alternative to dir
        glColor3ub(r,g,b);
 }
 
-World::World(unsigned int width){              // Generates the world and sets all variables contained in the World class.
+World::World(void){
+}
+
+void World::generate(unsigned int width){      // Generates the world and sets all variables contained in the World class.
        unsigned int i;                                         // Used for 'for' loops 
        float inc;                                                      // See line 40
        lineCount=width+GEN_INC;                        // Sets line count to the desired width plus GEN_INC to remove incorrect line calculations.
@@ -149,7 +154,8 @@ void World::addLayer(unsigned int width){
                behind->addLayer(width);
                return;
        }
-       behind=new World(width);
+       behind=new World();
+       behind->generate(width);
        behind->infront=this;
 }
 
@@ -184,3 +190,50 @@ World *World::goWorldFront(Player *p){
        }
        return this;
 }
+
+
+
+
+IndoorWorld::IndoorWorld(void){
+}
+
+IndoorWorld::~IndoorWorld(void){
+       free(line);
+}
+
+void IndoorWorld::generate(unsigned int width){                // Generates a flat area of width 'width'
+       unsigned int i;                                         // Used for 'for' loops 
+       lineCount=width+GEN_INC;                        // Sets line count to the desired width plus GEN_INC to remove incorrect line calculations.
+       
+       line=(struct line_t *)calloc(lineCount,sizeof(struct line_t));  // Allocate memory for the array 'line'
+       
+       for(i=0;i<lineCount;i++){                       // Indoor areas don't have to be directly on the ground (i.e. 0)...
+               line[i].y=INDOOR_FLOOR_HEIGHT;
+       }
+       behind=infront=NULL;                                            // Set pointers to other worlds to NULL
+       toLeft=toRight=NULL;                                            // to avoid accidental calls to goWorld... functions
+       x_start=0-getWidth(this)/2+GEN_INC/2*HLINE;     // Calculate x_start (explained in world.h)
+}
+
+void IndoorWorld::draw(vec2 *vec){
+       int i,ie,v_offset;
+       v_offset=(vec->x-x_start)/HLINE;                                        // Calculate the player's offset in the array 'line' using the player's location 'vec'
+       i=v_offset-SCREEN_WIDTH/2;                                                      // um
+       if(i<0)i=0;                                                                                     // If the player is past the start of that world 'i' should start at the beginning
+                                                                                                               // of the world
+       ie=v_offset+SCREEN_WIDTH/2;                                                     // Set how many lines should be drawn (the drawing for loop loops from 'i' to 'ie')
+       if(ie>lineCount)ie=lineCount;                                           // If the player is past the end of that world 'ie' should contain the end of that world
+       glBegin(GL_QUADS);
+               for(i=i;i<ie-GEN_INC;i++){                                              // For lines in array 'line' from 'i' to 'ie'
+                       safeSetColor(150,100,50);
+                       glVertex2i(x_start+i*HLINE      ,line[i].y);    // Draw the base floor
+                       glVertex2i(x_start+i*HLINE+HLINE,line[i].y);
+                       glVertex2i(x_start+i*HLINE+HLINE,line[i].y-50);
+                       glVertex2i(x_start+i*HLINE              ,line[i].y-50);
+               }
+       glEnd();
+       for(i=0;i<entity.size()+1;i++){
+               if(entity[i]->inWorld==this)
+                       entity[i]->draw();
+       }
+}