]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Fixed movement and did a SUPER BETA version of NPC's
authorAndy Belle-Isle <abelleisle@roadrunner.com>
Sat, 12 Sep 2015 05:24:33 +0000 (01:24 -0400)
committerAndy Belle-Isle <abelleisle@roadrunner.com>
Sat, 12 Sep 2015 05:24:33 +0000 (01:24 -0400)
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)

include/entities.h
src/UIClass.cpp
src/World.cpp
src/entities.cpp
src/main.cpp

index 5deaa00ab1d7fe8557968c3c87ef5f70c91cf583..e50c77a32579c58e5da3e61e7bfdb8d50dd4b39b 100644 (file)
@@ -10,9 +10,9 @@ public:
        float speed;
        int type;
        vec2 loc;
-       vec2 loci;
        vec2 vel;
-       vec2 velg;
+       bool right,left;
+
        void spawn(float, float);
 };
 
@@ -22,4 +22,9 @@ public:
        ~Player();
 };
 
+class NPC : public Entities{
+public:
+       NPC();
+};
+
 #endif // ENTITIES_H
index 731e3879d28828a572bbce4b56353f4b19050fb2..319db5c36229da30a5c5ebc78ad837a4e38f6d0b 100644 (file)
@@ -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
index f6bb70eecccd8c7e3be1a1c39fb33cec7c5e0adc..e8a5468aaf496ca71e050aaa36b3c1280c1306b0 100644 (file)
@@ -86,6 +86,7 @@ void World::draw(void){
 void World::detect(vec2 *v,const float width){\r
        unsigned int i;\r
        // hey\r
+       // oh hai\r
        for(i=0;i<lineCount-10;i++){\r
                if(v->y<line[i].start){\r
                        if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){\r
index 8d582a1207b9e76fdd2645b561303edba9bc168f..754c77756dec23a8b61ead401833d1f8e3bb3608 100644 (file)
@@ -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;
+}
index 51b5c070356fb230641c3239abec343131aab69c..674acea83f9f7f96e53882f735444477d87451f7 100644 (file)
@@ -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;
 }