]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
added platforms
authorClyne Sullivan <tullivan99@gmail.com>
Sun, 27 Sep 2015 14:35:40 +0000 (10:35 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sun, 27 Sep 2015 14:35:40 +0000 (10:35 -0400)
include/world.h
src/entities.cpp
src/main.cpp
src/ui.cpp
src/world.cpp

index e826ba2b9697d6b7badc84aad475a11061df68b9..0e8dced482e3d1611c480b32a30cdd8730c92eed 100644 (file)
@@ -3,6 +3,10 @@
 
 #include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
 
+typedef struct {
+       vec2 p1,p2;
+} __attribute__ ((packed)) Platform;
+
 /*
  *     World - creates and handles an area of land
 */
@@ -24,10 +28,11 @@ protected:
                unsigned char color;
        } __attribute__ ((packed)) *line;
        unsigned int lineCount; // Size of the array 'line' (aka the width of the world)
+       std::vector<Platform> platform; // An array (vector thing) of platforms
        int x_start;                    // Worlds are centered on the x axis (0,n), this contains
                                                        // where to start drawing the world to have it centered properly.
        World *behind,*infront; // Pointers to other areas of land that are behind or in front of this one, respectively.
-       void singleDetect(Entity *e);
+       void singleDetect(Entity *e);   // Handles an individual entity (gravity n' stuff)
 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.
@@ -57,7 +62,7 @@ public:
                                                                                                                // world is drawn the world has to appear directly behind the player)
        World *goWorldFront(Player *p);                                         // Functions the same as goWorldBack(), but checks/returns the world in front of
                                                                                                                // this one.
-       
+       void addPlatform(float x,float y,float w,float h);      
 };
 
 /*
index 3c2120b0421c930380d9588fee8d75cf283f8481..da28851676e7085ea2a3c9dca4595d9355fbc5f6 100644 (file)
@@ -85,7 +85,7 @@ unsigned int Structures::spawn(int t, float x, float y){
                        entity[entity.size()] = &npc[npc.size()-1];
                        entity[entity.size()]->alive=true;
                        entity[entity.size()]->type = 1;
-                       entity[entity.size()]->spawn(loc.x + (float)(i - 5) / 8,0);
+                       entity[entity.size()]->spawn(loc.x + (float)(i - 5) / 8,100);
                }
                return entity.size();
        }
index 1747915be8b64e4b7de9fae91aabb5eb8a39f119..0ba715fa98d031224c2a259c818d5bb9f79dc6ba 100644 (file)
@@ -89,7 +89,8 @@ int main(int argc, char *argv[]){
        World *test=new World();
        test->generate(SCREEN_WIDTH/2);
        test->addLayer(400);
-       test->addLayer(100);    
+       test->addLayer(100);
+       test->addPlatform(150,100,100,10);
        currentWorld=test;
        
        IndoorWorld *iw=new IndoorWorld();
index f849cf8bd84c8a8d542223c42b5ed48ad93f48da..1e78dca49c1100a690098dfca1d2c9622aeb1dd0 100644 (file)
@@ -145,6 +145,7 @@ namespace ui {
                                if(SDL_KEY==SDLK_SPACE){                                                                                        // Jump
                                        if(player->ground){
                                                player->vel.y=.25;
+                                               //player->loc.y+=HL
                                                player->ground=false;
                                        }
                                }
index cab9ecb2e825f7b1b7c70c4a2137c90a42292717..14df3949a3a8c400d2c1ab9916909b07a289824d 100644 (file)
@@ -1,4 +1,5 @@
 #include <world.h>
+#include <ui.h>
 
 #define getWidth(w) ((w->lineCount-GEN_INC)*HLINE)     // Calculates the width of world 'w'
 
@@ -100,6 +101,17 @@ LOOP2:                                                                                                     // Draw each world
                        cline[i].y-=(yoff-DRAW_Y_OFFSET); // Reset 'cline[i]'`s y to what it was
                }
        glEnd();
+       for(i=0;i<entity.size()+1;i++){
+               if(entity[i]->inWorld==current){
+                       entity[i]->draw();
+                       ui::putText(entity[i]->loc.x,entity[i]->loc.y,"%d",i);
+               }
+       }
+       glColor3ub(255,0,0);
+       for(i=0;i<current->platform.size();i++){
+               glRectf(current->platform[i].p1.x,current->platform[i].p1.y,
+                               current->platform[i].p2.x,current->platform[i].p2.y);
+       }
        if(current->infront){                   // If there's a world in front of the one that was just drawn
                yoff-=DRAW_Y_OFFSET;            // draw it as well.
                shade-=DRAW_SHADE;
@@ -108,10 +120,6 @@ LOOP2:                                                                                                     // Draw each world
        }else{                                                  // Otherwise reset static values and return
                yoff=DRAW_Y_OFFSET;
                shade=0;
-               for(i=0;i<entity.size()+1;i++){
-                       if(entity[i]->inWorld==this)
-                               entity[i]->draw();
-               }
        }
 }
 
@@ -124,6 +132,18 @@ void World::singleDetect(Entity *e){
                        e->ground=true;
                        e->loc.y=line[i].y+HLINE/2;
                }else{                                                  // If the player is above the ground do some gravity stuff
+                       for(i=0;i<platform.size();i++){
+                               if(((e->loc.x+e->width>platform[i].p1.x)&(e->loc.x+e->width<platform[i].p2.x))||
+                                  ((e->loc.x<platform[i].p2.x)&(e->loc.x>platform[i].p1.x))){
+                                       if(e->loc.y>platform[i].p2.y-HLINE&&e->loc.y<platform[i].p2.y){
+                                               if(e->vel.y<0){
+                                                       e->vel.y=0;
+                                                       e->loc.y=platform[i].p2.y;
+                                                       e->ground=true;
+                                               }
+                                       }
+                               }
+                       }
                        e->vel.y-=.01;
                }
                if(e->loc.x<x_start){                           // Keep the player inside world bounds (ui.cpp handles world jumping)
@@ -192,6 +212,9 @@ World *World::goWorldFront(Player *p){
        return this;
 }
 
+void World::addPlatform(float x,float y,float w,float h){
+       platform.push_back((Platform){{x,y},{x+w,y+h}});
+}