]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Added NPC Stuff
authorAndy Belle-Isle <abelleisle@roadrunner.com>
Mon, 14 Sep 2015 15:51:05 +0000 (11:51 -0400)
committerAndy Belle-Isle <abelleisle@roadrunner.com>
Mon, 14 Sep 2015 15:51:05 +0000 (11:51 -0400)
Added Wandering, and variable spawning amounts

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

index e1b536f0b4ca2a3461cf1fc02f33bb6e0d773cdf..f32aad08bdc5fb318c6c1fe642687e63c6dfaeee 100644 (file)
@@ -15,7 +15,8 @@ typedef struct{float x; float y;}vec2;
 #include <World.h>
 
 #define SCREEN_WIDTH  1280
-#define SCREEN_HEIGHT 800
+#define SCREEN_HEIGHT 720
+#define SCREEN_RATIO (float)SCREEN_WIDTH/(float)SCREEN_HEIGHT
 //#define FULLSCREEN
 
 #define HLINE (2.0f / (SCREEN_WIDTH / 4))
@@ -23,6 +24,9 @@ typedef struct{float x; float y;}vec2;
 #define irand srand
 #define grand rand
 
+template<typename T, size_t N>
+int eAmt(T (&)[N]){return N;}
+
 //SDL VARIABLES
 extern SDL_Window    *window;
 extern SDL_Surface   *renderSurface;
index 752908bfc0391352d5c2dcbaea88e3aab41e4cbc..2ed97ee2f48330a8f3fb172eee140fd0cd3e864e 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <common.h>
 
+extern int npcAmt;
+
 class Entity{
 public:
        float width;
@@ -11,10 +13,14 @@ public:
        int type, subtype;
        vec2 loc;
        vec2 vel;
-       bool right,left;
+       bool right,left, canMove;
+       bool alive;
 
        void spawn(float, float);
        void draw(void);
+       void wander(int, vec2*);
+private:
+       int ticksToUse;
 };
 
 class Player : public Entity{
@@ -27,8 +33,8 @@ public:
        NPC();
 };
 
-extern Entity *entnpc[10];     //The NPC base
-extern NPC npc[10];
+extern Entity *entnpc[32];     //The NPC base
+extern NPC npc[32];
 
 class Structures : public Entity{
 public:
index ce6a3c7e2e5f6e7a6a7ed4bcc9c22acd777864ec..8083d479547cd0fa9a52765ff2584223ed545ab5 100644 (file)
@@ -116,7 +116,7 @@ void World::detect(vec2 *v,vec2 *vel,const float width){
                                return; // ;)\r
                        }\r
                }else if(v->y>line[i].start+HLINE){                                                                     // Trashy gravity handling\r
-                       vel->y-=.00000001;\r
+                       vel->y-=.0000001;\r
                }\r
        }\r
 }\r
index 849bd7ce7e831877f607ae33f07a9e83695cf7c8..e9e69ce5782bd5836f4cd175f30a9cf379b1b742 100644 (file)
@@ -7,6 +7,8 @@ void Entity::spawn(float x, float y){
        vel.y = 0;
        right = false;
        left = false;
+       ticksToUse = 0;
+       canMove = false;
 }
 
 void Entity::draw(void){
@@ -14,20 +16,33 @@ void Entity::draw(void){
        glRectf(loc.x,loc.y,loc.x+width,loc.y+height);
 }
 
+void Entity::wander(int timeRun, vec2 *v){
+       if(ticksToUse == 0){
+               ticksToUse = timeRun;
+               v->x = .00010;
+               int hey = (grand() % 3 - 1);
+               v->x *= hey;
+       }
+       ticksToUse--;
+}
+
 Player::Player(){
        width = HLINE * 8;
        height = HLINE * 18;
        speed = 1;
        type = 0;
        subtype = 5;
+       alive = true;
 }
 
-NPC::NPC(){
+NPC::NPC(){    
        width = HLINE * 8;
        height = HLINE * 18;
        speed = 1;
        type = 0;
        subtype = 0;
+       alive = false;
+       canMove = true;
 }
 
 Structures::Structures(){
@@ -45,7 +60,11 @@ void Structures::spawn(int t, float x, float y){
                width =  4 * HLINE;
                height = 4 * HLINE;
 
-               for(int i = 0;i<10;i++){
+               int tempN = (grand() % 5 + 1);
+               npcAmt = tempN;
+
+               for(int i = 0;i<eAmt(entnpc);i++){
+                       npc[i].alive = true;
                        entnpc[i] = &npc[i];
                        npc[i].type = -1;                                                //this will make the NPC spawn the start of a village
                        entnpc[i]->spawn(loc.x + (float)(i - 5) / 8,0); //this will spawn the start of a village
index e53664b7113eaa54a3c06b0d5ca8c47505a35fb8..bed75c8e94203e848679ab514f8cf66d991c2c55 100644 (file)
@@ -16,10 +16,11 @@ static unsigned int tickCount   = 0,
                                        currentTime = 0,
                                        deltaTime   = 0;
 
+int npcAmt = 0;
 Entity *entPlay;       //The player base
-Entity *entnpc[10];    //The NPC base
+Entity *entnpc[32];    //The NPC base
 Player player;         //The actual player object
-NPC npc[10];
+NPC npc[32];
 Structures build;
 UIClass ui;                    //Yep
 World *currentWorld;//u-huh
@@ -59,6 +60,7 @@ int main(int argc,char **argv){
                return -1;
        }
 
+       glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);
        glClearColor(.3,.5,.8,0);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
@@ -75,6 +77,8 @@ int main(int argc,char **argv){
        entPlay->spawn(0, 0);
 
        build.spawn(-1, (grand()%20)-10, 0);
+       //build.spawn(-1, 1.5, 0);
+
 
        // Generate the world
        World *w=NULL,*w2=NULL;
@@ -86,7 +90,7 @@ int main(int argc,char **argv){
        currentWorld->addLayer(4);
        // shh
        unsigned char jklasdf;
-       for(jklasdf=0;jklasdf<10;jklasdf++){
+       for(jklasdf=0;jklasdf<npcAmt;jklasdf++){
                currentWorld->addEntity((void *)entnpc[jklasdf]);
        }
 
@@ -105,6 +109,13 @@ int main(int argc,char **argv){
 
                player.loc.x += (player.vel.x * player.speed) * deltaTime;                                              //update the player's x based on 
                player.loc.y += player.vel.y * deltaTime;
+
+               for(int i = 0; i < eAmt(entnpc); i++){
+                       if(npc[i].alive == true){
+                               npc[i].loc.y += npc[i].vel.y * deltaTime;
+                               npc[i].loc.x += npc[i].vel.x * deltaTime;
+                       }
+           }
                
 
                gw=currentWorld->getWidth();
@@ -147,7 +158,7 @@ void render(){
                glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_PROJECTION mode
                //set the the size of the screen
                if(player.loc.x-1<-1){
-                       glOrtho(-1,1,-1,1,-1,1);
+                       glOrtho(-1,1, -1,1, -1,1);
                }else if(player.loc.x+1>-1+currentWorld->getWidth()){
                        glOrtho(-3+currentWorld->getWidth(),-1+currentWorld->getWidth(),-1,1,-1,1);
                }else{
@@ -167,14 +178,7 @@ void render(){
                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!!!!!!
-               for(int i = 0; i < 10; i++){
-                       npc[i].loc.y += npc[i].vel.y*deltaTime;
-
-                       glColor3ub(98, 78, 44);                                                 //render the NPC(s)
-                       glRectf(npc[i].loc.x, npc[i].loc.y, npc[i].loc.x + npc[i].width, npc[i].loc.y + npc[i].height);
-                       glEnd();
-           }
+               
            glColor3ub(255,0,0);
                glRectf(build.loc.x, build.loc.y, build.loc.x + build.width, build.loc.y + build.height);
                ///BWAHHHHHHHHHHHH
@@ -196,14 +200,18 @@ void logic(){
        if(player.left == true) {player.vel.x = -.00075;}
        if(player.right == false && player.left == false) {player.vel.x = 0;}
 
-       std::cout<<"\r("<<player.loc.x<<","<<player.loc.y<<")";
+       //std::cout<<"\r("<<player.loc.x<<","<<player.loc.y<<")";
+       //std::cout << tickCount << std::endl;
 
 
        currentWorld->detect(&player.loc,&player.vel,player.width);
 
        currentWorld->detect(&build.loc,&build.vel,build.width);
-       for(int i = 0; i < 10; i++){
-               currentWorld->detect(&npc[i].loc,&npc[i].vel,npc[i].width);
+       for(int i = 0; i < eAmt(entnpc); i++){
+               if(npc[i].alive == true){
+                       currentWorld->detect(&npc[i].loc,&npc[i].vel,npc[i].width);
+                       entnpc[i]->wander((grand()%91 + 1), &npc[i].vel);
+               }
        }
 
        tickCount++;