#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
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
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
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;
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
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
}\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
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
// 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
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);