diff options
-rw-r--r-- | include/common.h | 6 | ||||
-rw-r--r-- | include/entities.h | 12 | ||||
-rw-r--r-- | src/World.cpp | 2 | ||||
-rw-r--r-- | src/entities.cpp | 23 | ||||
-rw-r--r-- | src/main.cpp | 38 |
5 files changed, 59 insertions, 22 deletions
diff --git a/include/common.h b/include/common.h index e1b536f..f32aad0 100644 --- a/include/common.h +++ b/include/common.h @@ -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; diff --git a/include/entities.h b/include/entities.h index 752908b..2ed97ee 100644 --- a/include/entities.h +++ b/include/entities.h @@ -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: diff --git a/src/World.cpp b/src/World.cpp index ce6a3c7..8083d47 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -116,7 +116,7 @@ void World::detect(vec2 *v,vec2 *vel,const float width){ return; // ;)
}
}else if(v->y>line[i].start+HLINE){ // Trashy gravity handling
- vel->y-=.00000001;
+ vel->y-=.0000001;
}
}
}
diff --git a/src/entities.cpp b/src/entities.cpp index 849bd7c..e9e69ce 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -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 diff --git a/src/main.cpp b/src/main.cpp index e53664b..bed75c8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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++; |