aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2015-09-21 21:52:28 -0400
committerClyne Sullivan <tullivan99@gmail.com>2015-09-21 21:52:28 -0400
commitb3e625cf2e6fea860fc669f3a2a46cf96a01da0f (patch)
treebc01dccc2e7f148b3c291eb4e145c9263aeaa20d /src
parent1118f22a60dffeb204ec4845d009cf057b51f088 (diff)
added/fixed world linking
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp7
-rw-r--r--src/ui.cpp14
-rw-r--r--src/world.cpp88
3 files changed, 83 insertions, 26 deletions
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<<"("<<player->loc.x<<","<<player->loc.y<<")"<<std::endl;
}
diff --git a/src/ui.cpp b/src/ui.cpp
index 877dacb..a2fe7bf 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -1,10 +1,12 @@
#include <ui.h>
+#include <world.h>
#include <ft2build.h>
#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 <world.h>
-#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<<v_offset<<" "<<i<<" "<<ie<<yoff<<std::endl;
+ cline=current->line;
+ cx_start=current->x_start;
glBegin(GL_QUADS);
- for(i=i;i<ie;i++){
- line[i].y+=(yoff-50);
+ for(i=i;i<ie-GEN_INC;i++){
+ cline[i].y+=(yoff-50);
safeSetColor(shade,200+shade,shade);
- glVertex2i(x_start+i*HLINE ,line[i].y);
- glVertex2i(x_start+i*HLINE+HLINE,line[i].y);
- glVertex2i(x_start+i*HLINE+HLINE,line[i].y-GRASS_HEIGHT);
- glVertex2i(x_start+i*HLINE ,line[i].y-GRASS_HEIGHT);
- safeSetColor(line[i].color+shade,line[i].color-50+shade,line[i].color-100+shade);
- glVertex2i(x_start+i*HLINE ,line[i].y-GRASS_HEIGHT);
- glVertex2i(x_start+i*HLINE+HLINE,line[i].y-GRASS_HEIGHT);
- glVertex2i(x_start+i*HLINE+HLINE,0);
- glVertex2i(x_start+i*HLINE ,0);
- line[i].y-=(yoff-50);
+ glVertex2i(cx_start+i*HLINE ,cline[i].y);
+ glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y);
+ glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT);
+ glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT);
+ safeSetColor(cline[i].color+shade,cline[i].color-50+shade,cline[i].color-100+shade);
+ glVertex2i(cx_start+i*HLINE ,cline[i].y-GRASS_HEIGHT);
+ glVertex2i(cx_start+i*HLINE+HLINE,cline[i].y-GRASS_HEIGHT);
+ glVertex2i(cx_start+i*HLINE+HLINE,0);
+ glVertex2i(cx_start+i*HLINE ,0);
+ cline[i].y-=(yoff-50);
}
glEnd();
- if(infront){
+ if(current->infront){
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->x<x_start){
vel->x=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->x<x_start+HLINE*10){
+ loc->x=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->x<getWidth(behind)/2){
+ return behind;
+ }
+ return this;
+}
+
+World *World::goWorldFront(vec2 *loc,const float width){
+ if(infront&&loc->x>(int)(0-getWidth(infront)/2)&&loc->x<getWidth(infront)/2){
+ return infront;
+ }
+ return this;
+}