From b3e625cf2e6fea860fc669f3a2a46cf96a01da0f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 21 Sep 2015 21:52:28 -0400 Subject: added/fixed world linking --- src/main.cpp | 7 +++-- src/ui.cpp | 14 ++++++++-- src/world.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 83 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index d31b704..4f4c13a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,8 +77,12 @@ int main(int argc, char *argv[]){ **** GAMELOOP **** **************************/ - World *test=new World(SCREEN_WIDTH/2); + World *test =new World(SCREEN_WIDTH/2), + *test2=new World(SCREEN_WIDTH*2); test->addLayer(400); + test->addLayer(100); + test->toLeft=test2; + test2->toRight=test; currentWorld=test; player=new Player(); player->spawn(0,100); @@ -143,5 +147,4 @@ void logic(){ currentWorld->detect(&player->loc,&player->vel,player->width); player->loc.y+=player->vel.y*deltaTime; player->loc.x+=player->vel.x*deltaTime; - std::cout<<"("<loc.x<<","<loc.y<<")"< +#include #include #include FT_FREETYPE_H #define SDL_KEY e.key.keysym.sym extern Player *player; +extern World *currentWorld; static FT_Library ftl; static FT_Face ftf; @@ -89,9 +91,17 @@ namespace ui { break; case SDL_KEYDOWN: if(SDL_KEY==SDLK_ESCAPE)gameRunning=false; - if(SDL_KEY==SDLK_a)player->vel.x=-.15; - if(SDL_KEY==SDLK_d)player->vel.x=.15; + if(SDL_KEY==SDLK_a){ + player->vel.x=-.15; + currentWorld=currentWorld->goWorldLeft(&player->loc,player->width); + } + if(SDL_KEY==SDLK_d){ + player->vel.x=.15; + currentWorld=currentWorld->goWorldRight(&player->loc,player->width); + } if(SDL_KEY==SDLK_SPACE)player->vel.y=.25; + if(SDL_KEY==SDLK_i)currentWorld=currentWorld->goWorldBack(&player->loc,player->width); + if(SDL_KEY==SDLK_k)currentWorld=currentWorld->goWorldFront(&player->loc,player->width); break; case SDL_KEYUP: if(SDL_KEY==SDLK_a)player->vel.x=0; diff --git a/src/world.cpp b/src/world.cpp index d7aece1..d05d5c1 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,6 +1,6 @@ #include -#define getWidth() ((lineCount-GEN_INC)*HLINE) +#define getWidth(w) ((w->lineCount-GEN_INC)*HLINE) #define GEN_INC 10 #define GRASS_HEIGHT 4 @@ -34,8 +34,9 @@ World::World(unsigned int width){ } line[i].color=rand()%20+130; } - x_start=0-getWidth()/2+GEN_INC/2*HLINE; + x_start=0-getWidth(this)/2+GEN_INC/2*HLINE; behind=infront=NULL; + toLeft=toRight=NULL; } World::~World(void){ @@ -45,36 +46,47 @@ World::~World(void){ void World::draw(vec2 *vec){ static float yoff=50; static int shade=0; - int i,ie,v_offset; - if(behind){ + static World *root,*current; + int i,ie,v_offset,cx_start; + struct line_t *cline; + root=current=this; +LOOP1: + if(current->behind){ yoff+=50; shade+=40; - behind->draw(vec); + current=current->behind; + goto LOOP1; } - v_offset=(vec->x-x_start)/HLINE; +LOOP2: + v_offset=(vec->x-current->x_start)/HLINE; i=v_offset-SCREEN_WIDTH/(yoff/25); if(i<0)i=0; ie=v_offset+SCREEN_WIDTH/(yoff/25); - if(ie>lineCount)ie=lineCount; + if(ie>current->lineCount)ie=current->lineCount; + //std::cout<line; + cx_start=current->x_start; glBegin(GL_QUADS); - for(i=i;iinfront){ yoff-=50; shade-=40; + current=current->infront; + goto LOOP2; }else{ yoff=50; shade=40; @@ -95,9 +107,9 @@ void World::detect(vec2 *v,vec2 *vel,const float width){ if(v->xx=0; v->x=x_start+HLINE/2; - }else if(v->x+width+HLINE>x_start+getWidth()){ + }else if(v->x+width+HLINE>x_start+getWidth(this)){ vel->x=0; - v->x=x_start+getWidth()-width-HLINE; + v->x=x_start+getWidth(this)-width-HLINE; } } @@ -109,3 +121,35 @@ void World::addLayer(unsigned int width){ behind=new World(width); behind->infront=this; } + +World *World::goWorldLeft(vec2 *loc,const float width){ + if(toLeft&&loc->xx=toLeft->x_start+getWidth(toLeft)-HLINE*10; + loc->y=toLeft->line[0].y; + return toLeft; + } + return this; +} + +World *World::goWorldRight(vec2 *loc,const float width){ + if(toRight&&loc->x+width>x_start+getWidth(this)-HLINE*10){ + loc->x=toRight->x_start+HLINE*10; + loc->y=toRight->line[toRight->lineCount-GEN_INC-1].y; + return toRight; + } + return this; +} + +World *World::goWorldBack(vec2 *loc,const float width){ + if(behind&&loc->x>(int)(0-getWidth(behind)/2)&&loc->xx>(int)(0-getWidth(infront)/2)&&loc->x