diff options
author | Andy Belle-Isle <abelleisle@roadrunner.com> | 2015-09-12 01:24:33 -0400 |
---|---|---|
committer | Andy Belle-Isle <abelleisle@roadrunner.com> | 2015-09-12 01:24:33 -0400 |
commit | 6a28f446c501f09153ca77eb22951de7bb144e18 (patch) | |
tree | 4cfff74de1469689602e4e3864a406be9acd7cc7 /src | |
parent | 3782c6ae9581063b3ed9e45467ce43f0b320bb19 (diff) |
Fixed movement and did a SUPER BETA version of NPC's
As much as I didn't want to, I had to get rid of SDL_AddTimer. Since it
runs in a separate thread and does calculations at the same time as the
main thread, a bunch of the data was getting messed up during the loop,
so it was just easiest to remove it. (Just refresh the world a lot to
see a village house spawn)
Diffstat (limited to 'src')
-rw-r--r-- | src/UIClass.cpp | 46 | ||||
-rw-r--r-- | src/World.cpp | 1 | ||||
-rw-r--r-- | src/entities.cpp | 21 | ||||
-rw-r--r-- | src/main.cpp | 82 |
4 files changed, 80 insertions, 70 deletions
diff --git a/src/UIClass.cpp b/src/UIClass.cpp index 731e387..319db5c 100644 --- a/src/UIClass.cpp +++ b/src/UIClass.cpp @@ -7,40 +7,22 @@ void UIClass::handleEvents(){ SDL_Event e; while(SDL_PollEvent(&e)){ switch(e.type){ - case SDL_QUIT: - gameRunning=false; - break; - case SDL_KEYDOWN: - switch(e.key.keysym.sym){ - case 27: ///ESCAPE - gameRunning=false; - break; - case SDLK_d: ///D - player.velg.x = 10; - break; - case SDLK_a: ///A - player.velg.x = -10; - break; - case SDLK_i: - if(currentWorld->behind)currentWorld=currentWorld->behind; - break; - case SDLK_k: - if(currentWorld->infront)currentWorld=currentWorld->infront; - break; - default: + case SDL_WINDOWEVENT: + switch(e.window.event){ + case SDL_WINDOWEVENT_CLOSE: + gameRunning = false; break; } + case SDL_KEYDOWN: + 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; + break; case SDL_KEYUP: - switch(e.key.keysym.sym){ - /*case SDLK_d: ///D - break; - case SDLK_a: ///A - break;*/ - default: - break; - } - default: + if(e.key.keysym.sym == SDLK_d) player.right = false; + if(e.key.keysym.sym == SDLK_a) player.left = false; + if(e.key.keysym.sym == SDLK_ESCAPE) gameRunning = false; break; - } + } } -} +}
\ No newline at end of file diff --git a/src/World.cpp b/src/World.cpp index f6bb70e..e8a5468 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -86,6 +86,7 @@ void World::draw(void){ void World::detect(vec2 *v,const float width){
unsigned int i;
// hey
+ // oh hai
for(i=0;i<lineCount-10;i++){
if(v->y<line[i].start){
if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){
diff --git a/src/entities.cpp b/src/entities.cpp index 8d582a1..754c777 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -3,19 +3,26 @@ void Entities::spawn(float x, float y){ loc.x = x; loc.y = y; - loci.x = loc.x; - loci.y = loc.y; vel.x = 0; vel.y = 0; - velg.x = 0; - velg.y = 0; + right = false; + left = false; } Player::Player(){ - width = HLINE * 6; - height = HLINE * 16; + width = HLINE * 8; + height = HLINE * 18; speed = 1; type = 0; } -Player::~Player(){} +Player::~Player(){ + +} + +NPC::NPC(){ + width = HLINE * 8; + height = HLINE * 18; + speed = 1; + type = 0; +} diff --git a/src/main.cpp b/src/main.cpp index 51b5c07..674acea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,10 +16,12 @@ static unsigned int tickCount = 0, currentTime = 0, deltaTime = 0; -Entities *entit1; -Player player; -UIClass ui; -World *currentWorld; +Entities *entPlay; //The player base +Entities *entnpc; //The NPC base +Player player; //The actual player object +NPC npc; +UIClass ui; //Yep +World *currentWorld;//u-huh //static int randNext=1; @@ -31,7 +33,7 @@ int grand(void){ return rand(); } -unsigned int logic(unsigned int interval,void *param); +void logic(); float interpolate(float goal, float current, float dt){ float difference = goal - current; @@ -75,7 +77,7 @@ int main(int argc,char **argv){ std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl; return -1; } - SDL_AddTimer(MSEC_PER_TICK,logic,NULL); + glClearColor(.3,.5,.8,0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); @@ -84,13 +86,18 @@ int main(int argc,char **argv){ **** GAMELOOP **** **************************/ - entit1 = &player; - entit1->spawn(0, 0); + irand(time(NULL)); + + entPlay = &player; + entPlay->spawn(0, 0); + entnpc = &npc; + npc.type = -1; //this will make the NPC spawn the start of a village + entnpc->spawn( (grand()%20)-10 ,0); //this will spawn the start of a village // Generate the world World *w=NULL,*w2=NULL; w2=new World(4,w,NULL); - w=new World(2,NULL,w2); + w=new World(10,NULL,w2); currentWorld=w; currentWorld->setRoot(); @@ -112,37 +119,39 @@ int main(int argc,char **argv){ fread(&fSave,sizeof(unsigned int),1,f); fclose(f); }*/ - irand(time(NULL)); float gw; while(gameRunning){ prevTime = currentTime; - currentTime = tickCount; + currentTime = SDL_GetTicks(); deltaTime = currentTime - prevTime; + if(prevTime + MSEC_PER_TICK >= SDL_GetTicks()){ //the logic loop to run at a dedicated time + logic(); + prevTime = SDL_GetTicks(); + } + + player.loc.x += player.vel.x * (deltaTime / 2); //update the player's x based on + gw=currentWorld->getWidth(); - if(player.loci.x+player.width>-1+gw){ + if(player.loc.x+player.width>-1+gw){ if(currentWorld->toRight){ goWorldRight(currentWorld) - player.loci.x=-1+HLINE; + player.loc.x=-1+HLINE; }else{ - player.loci.x=gw-1-player.width-HLINE; + player.loc.x=gw-1-player.width-HLINE; } } - if(player.loci.x<-1){ + if(player.loc.x<-1){ if(currentWorld->toLeft){ goWorldLeft(currentWorld); - player.loci.x=currentWorld->getWidth()-1-player.width-HLINE; + player.loc.x=currentWorld->getWidth()-1-player.width-HLINE; }else{ - player.loci.x=-1+HLINE; + player.loc.x=-1+HLINE; } } - player.vel.x = interpolate(player.velg.x, player.vel.x, deltaTime) * .005; - if(player.vel.x > .05) player.vel.x = .05; - if(player.vel.x < -.05) player.vel.x = -.05; - player.loci.x += player.vel.x; render(); } @@ -163,7 +172,7 @@ void render(){ glMatrixMode(GL_PROJECTION); //set the matrix mode as projection so we can set the ortho size and the camera settings later on glPushMatrix(); //push the matrix to the top of the matrix stack glLoadIdentity(); //replace the entire matrix stack with the updated GL_PROJECTION mode - glOrtho(-1 + player.loci.x, 1 + player.loci.x , -1, 1, -1,1); //set the the size of the screen + glOrtho(-1 + player.loc.x, 1 + player.loc.x , -1, 1, -1,1); //set the the size of the screen glMatrixMode(GL_MODELVIEW); //set the matrix to modelview so we can draw objects glPushMatrix(); //push the matrix to the top of the matrix stack glLoadIdentity(); //replace the entire matrix stack with the updated GL_MODELVIEW mode @@ -175,8 +184,19 @@ void render(){ **************************/ currentWorld->draw(); // layers dont scale x correctly... - glColor3ub(120,30,30); - glRectf(player.loci.x, player.loci.y, player.loci.x + player.width, player.loci.y + player.height); + glColor3ub(120,30,30); //render the player + glRectf(player.loc.x, player.loc.y, player.loc.x + player.width, player.loc.y + player.height); + + ///TEMP NPC RENDER!!!!!! + glColor3ub(98, 78, 44); //render the NPC(s) + glRectf(npc.loc.x, npc.loc.y, npc.loc.x + .25, npc.loc.y + .25); + glColor3ub(83, 49, 24); + glBegin(GL_TRIANGLES); + glVertex2f(npc.loc.x, npc.loc.y + .25); + glVertex2f(npc.loc.x + .25, npc.loc.y + .25); + glVertex2f(npc.loc.x + .125, npc.loc.y + .35); + glEnd(); + ///BWAHHHHHHHHHHHH /************************** **** CLOSE THE LOOP **** @@ -186,16 +206,16 @@ void render(){ SDL_GL_SwapWindow(window); //give the stack to SDL to render it } -unsigned int logic(unsigned int interval,void *param){ +void logic(){ ui.handleEvents(); // Handle events - player.vel.x=0; - currentWorld->detect(&player.loci,player.width); + if(player.right == true) {player.vel.x = .002;} + if(player.left == true) {player.vel.x = -.002;} + if(player.right == false && player.left == false) {player.vel.x = 0;} + - //std::cout << player.vel.x << std::endl; - //std::cout << player.velg.y << std::endl; - //std::cout << "d:" << deltaTime << std::endl; + currentWorld->detect(&player.loc,player.width); + currentWorld->detect(&npc.loc,npc.height); tickCount++; - return interval; } |