]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Real layered worlds n stuff
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 12 Sep 2015 22:44:23 +0000 (18:44 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 12 Sep 2015 22:44:23 +0000 (18:44 -0400)
include/World.h
src/UIClass.cpp
src/World.cpp
src/main.cpp

index 13ccafcabd5c924357dc1bc2d075e9ef31305649..09e06d9ee69efc70c1a212e34b107267745478c7 100644 (file)
@@ -6,8 +6,6 @@
 #define goWorldLeft(w)  if(w->toLeft){w=w->toLeft;}\r
 #define goWorldRight(w) if(w->toRight){w=w->toRight;}\r
 \r
-#define LAYER_SCALE 2\r
-\r
 class World {\r
 private:\r
        struct line_t {\r
@@ -15,7 +13,6 @@ private:
                double start; // Where to change to dirt, going down (y)\r
        } __attribute__ ((packed)) *line;\r
        unsigned int lineCount;\r
-       bool root,drawn;\r
 public:\r
        World *behind,*infront;\r
        World *toLeft,*toRight;\r
@@ -26,8 +23,7 @@ public:
        float getWidth(void);\r
        void saveToFile(FILE *f,World *parent);\r
        void loadFromFile(FILE *f,World *parent);\r
-       void addLayer(void);\r
-       void setRoot(void);\r
+       void addLayer(const float width);\r
 };\r
 \r
 #endif // WORLD_H
index 318ba006bd9705ee489ef77a8cec4c6e87a7ac9c..c1c118052ceddd33b933468e95473a9c0e1988cc 100644 (file)
@@ -17,8 +17,16 @@ void UIClass::handleEvents(){
                        if(e.key.keysym.sym == SDLK_d) player.right = true;
                        if(e.key.keysym.sym == SDLK_a) player.left = true;
                        if(e.key.keysym.sym == SDLK_SPACE) player.loc.y += 10;
-                       if(e.key.keysym.sym == SDLK_i)if(currentWorld->behind) currentWorld=currentWorld->behind;
-                       if(e.key.keysym.sym == SDLK_k)if(currentWorld->infront)currentWorld=currentWorld->infront;
+                       if(e.key.keysym.sym == SDLK_i)
+                               if(currentWorld->behind){
+                                       player.loc.x-=(currentWorld->getWidth()-currentWorld->behind->getWidth())/2;
+                                       currentWorld=currentWorld->behind;
+                               }
+                       if(e.key.keysym.sym == SDLK_k)
+                               if(currentWorld->infront){
+                                       player.loc.x+=(currentWorld->infront->getWidth()-currentWorld->getWidth())/2;
+                                       currentWorld=currentWorld->infront;
+                               }
                        break;
                case SDL_KEYUP:
                        if(e.key.keysym.sym == SDLK_d) player.right = false;
index fb9e31565a13b82ea7e25dc74cb5660c5a65214e..82735bdb53707014ce8bce556a2caf089544f7d5 100644 (file)
@@ -5,7 +5,6 @@ World::World(void){
        line=NULL;\r
        lineCount=0;\r
        toLeft=toRight=behind=infront=NULL;\r
-       root=false;\r
 }\r
 World::World(const float width,World *l,World *r){\r
        unsigned int i;\r
@@ -18,7 +17,6 @@ World::World(const float width,World *l,World *r){
        toLeft=l;\r
        toRight=r;\r
        behind=infront=NULL;\r
-       root=false;\r
        if(toLeft){\r
                if(toLeft->toRight){\r
                        std::cout<<"There's already a world to the left!"<<std::endl;\r
@@ -49,39 +47,47 @@ World::World(const float width,World *l,World *r){
                }\r
        }\r
 }\r
-static float hline=HLINE;\r
-static float back=0;\r
+static float drawOffsetX=0,\r
+                        drawOffsetY=0;\r
 void World::draw(void){\r
        unsigned int i;\r
-       if(behind){\r
-               hline*=.5;\r
-               back+=.2;\r
-               behind->draw();\r
-       }else{\r
-               hline*=.5;\r
-               back+=.2;\r
-       }\r
-       if(root){\r
-               hline=HLINE;\r
-               back=0;\r
-       }else{\r
-               hline*=2;\r
-               back-=.2;\r
+       float x,y;\r
+       static World *root,*cur;\r
+       root=cur=this;\r
+LOOP:\r
+       if(cur->behind){\r
+               drawOffsetX+=(cur->getWidth()-cur->behind->getWidth())/2;\r
+               drawOffsetY+=.3;\r
+               cur=cur->behind;\r
+               goto LOOP;\r
+               //behind->draw();\r
        }\r
+LOOP2:\r
        glBegin(GL_QUADS);\r
-               for(i=0;i<lineCount-10;i++){\r
-                       glColor3ub(0,255,0);\r
-                       glVertex2f((hline*i)-1      ,line[i].start+back);\r
-                       glVertex2f((hline*i)-1+hline,line[i].start+back);\r
-                       glVertex2f((hline*i)-1+hline,line[i].start-hline*2+back);\r
-                       glVertex2f((hline*i)-1      ,line[i].start-hline*2+back);\r
+               for(i=0;i<cur->lineCount-10;i++){\r
+                       x=(HLINE*i)-1+drawOffsetX;\r
+                       y=cur->line[i].start+drawOffsetY;\r
+                       glColor3ub(0,200,0);\r
+                       glVertex2f(x      ,y);\r
+                       glVertex2f(x+HLINE,y);\r
+                       y-=HLINE*2;\r
+                       glVertex2f(x+HLINE,y);\r
+                       glVertex2f(x      ,y);\r
                        glColor3ub(150,100,50);\r
-                       glVertex2f((hline*i)-1      ,line[i].start-hline*2+back);\r
-                       glVertex2f((hline*i)-1+hline,line[i].start-hline*2+back);\r
-                       glVertex2f((hline*i)-1+hline,-1+back);\r
-                       glVertex2f((hline*i)-1      ,-1+back);\r
+                       glVertex2f(x      ,y);\r
+                       glVertex2f(x+HLINE,y);\r
+                       glVertex2f(x+HLINE,-1);\r
+                       glVertex2f(x      ,-1);\r
                }\r
        glEnd();\r
+       if(root!=cur){\r
+               cur=cur->infront;\r
+               drawOffsetX-=(cur->getWidth()-cur->behind->getWidth())/2;\r
+               drawOffsetY-=.3;\r
+               goto LOOP2;\r
+       }else{\r
+               drawOffsetX=drawOffsetY=0;\r
+       }\r
 }\r
 void World::detect(vec2 *v,const float width){\r
        unsigned int i;\r
@@ -127,14 +133,11 @@ void World::loadFromFile(FILE *f,World *parent){
                toRight->loadFromFile(f,toRight);\r
        }\r
 }\r
-void World::addLayer(void){\r
+void World::addLayer(const float width){\r
        if(behind){\r
-               behind->addLayer();\r
+               behind->addLayer(width);\r
        }else{\r
-               behind=new World((lineCount-1)*HLINE+LAYER_SCALE,NULL,NULL);\r
+               behind=new World(width,NULL,NULL);\r
                behind->infront=this;\r
        }\r
 }\r
-void World::setRoot(void){\r
-       root=true;\r
-}\r
index 3e8d04f698bf080979053c98e9935f0cfe067ad2..60564c416e202a89a90db86b4ca4e98c1dc388ac 100644 (file)
@@ -97,12 +97,10 @@ int main(int argc,char **argv){
        // Generate the world
        World *w=NULL,*w2=NULL;
        w2=new World(4,w,NULL);
-       w=new World(10,NULL,w2);
+       w=new World(2,NULL,w2);
        
        currentWorld=w;
-       currentWorld->setRoot();
-       currentWorld->addLayer();
-       //currentWorld->addLayer();
+       currentWorld->addLayer(3);
        //currentWorld->addLayer();
        
        // Save the world if necessary
@@ -213,6 +211,7 @@ void logic(){
        if(player.left == true) {player.vel.x = -.002;}
        if(player.right == false && player.left == false) {player.vel.x = 0;}
 
+       std::cout<<"\r("<<player.loc.x<<","<<player.loc.y<<")";
 
        currentWorld->detect(&player.loc,player.width);
        currentWorld->detect(&npc.loc,npc.height);