]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Added dynamic npcs that spawn with th e world
authordrumsetmonkey <abelleisle@roadrunner.com>
Thu, 24 Sep 2015 21:41:40 +0000 (17:41 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Thu, 24 Sep 2015 21:41:40 +0000 (17:41 -0400)
include/common.h
include/entities.h
src/entities.cpp
src/main.cpp

index dde3c4acbafe41053849505d1e962153b3a159fb..aa175bcadc6f89f40c8c84f54f78bb85c2ca3d75 100644 (file)
@@ -4,6 +4,7 @@
 ///THIS FILE IS USED FOR VARIABLES THAT WILL BE ACCESED BY MULTIPLE CLASSES/FILES
 
 #include <iostream>
+#include <vector>
 #include <cstdlib>
 #include <SDL2/SDL.h>
 #include <SDL2/SDL_image.h>
index bc277cdb82904bda9999f0b3a2156800ccf2a3f6..e7389dc83bf9ff7ba55fd17119bc164ffad90769 100644 (file)
@@ -3,8 +3,6 @@
 
 #include <common.h>
 
-extern int npcAmt;
-
 class Entity{
 public:
        float width;
@@ -37,10 +35,6 @@ public:
        NPC();
        void interact();
 };
-
-extern Entity *entnpc[32];     //The NPC base
-extern NPC npc[32];
-
 class Structures : public Entity{
 public:
        Structures();
index 6058e416ed288a768a9e676c8981d89bf684e6de..9c18313272a390f3453d7cfb17777fd3e7f1ad69 100644 (file)
@@ -1,8 +1,8 @@
 #include <entities.h>
 
-int    npcAmt=0;
-Entity *entnpc[32];
-NPC    npc[32];
+extern std::vector<Entity*>entity;
+extern std::vector<NPC>npc;
+extern std::vector<Structures>build;
 
 void Entity::spawn(float x, float y){
        loc.x = x;
@@ -21,12 +21,12 @@ void Entity::draw(void){
 }
 
 void Entity::wander(int timeRun, vec2 *v){
-       static int hey;
+       static int direction;
        if(ticksToUse == 0){
                ticksToUse = timeRun;
-               v->x = .00010;
-               hey = (getRand() % 3 - 1);
-               v->x *= hey;
+               v->x = .01*HLINE;
+               direction = (getRand() % 3 - 1);
+               v->x *= direction;
        }
        ticksToUse--;
 }
@@ -46,7 +46,7 @@ void Player::interact(){
 
 NPC::NPC(){    
        width = HLINE * 8;
-       height = HLINE * 18;
+       height = HLINE * 12;
        speed = 1;
        type = 0;
        subtype = 0;
@@ -74,14 +74,17 @@ void Structures::spawn(int t, float x, float y){
                width =  4 * HLINE;
                height = 4 * HLINE;
 
-               int tempN = (getRand() % 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
+               //int tempN = (getRand() % 5 + 1);
+               int tempN = 2;
+               for(int i=0;i<tempN;i++){
+                       entity.push_back(new Entity());
+                       npc.push_back(NPC());
+                       std::cout<<"NPC:"<<npc.size()<<std::endl;
+                       std::cout<<"Entity:"<<entity.size()<<std::endl;
+                       entity[entity.size()] = &npc[npc.size()-1];
+                       entity[entity.size()]->alive=true;
+                       entity[entity.size()]->type = 1;
+                       entity[entity.size()]->spawn(loc.x + (float)(i - 5) / 8,0);
                }
        }
 }
index 4f4c13aed54ee95d5c98558f24f982ef03b37adf..a5bee95fae3e561f696d658f5796b8504fedfd75 100644 (file)
@@ -21,6 +21,10 @@ static unsigned int tickCount   = 0,
 World *currentWorld=NULL;
 Player *player;
 
+std::vector<Entity*>entity;
+std::vector<NPC>npc;
+std::vector<Structures>build;
+
 void logic();
 void render();
 
@@ -87,8 +91,12 @@ int main(int argc, char *argv[]){
        player=new Player();
        player->spawn(0,100);
 
-       currentTime=millis();
+       entity.push_back(new Entity());//create the blank first element for the player; 
+       build.push_back(Structures());
+       entity[0]=&build[0];
+       build[0].spawn(-1,0,10);
 
+       currentTime=millis();
        while(gameRunning){
                prevTime = currentTime;
                currentTime = millis();
@@ -99,6 +107,8 @@ int main(int argc, char *argv[]){
                        prevTime = millis();
                }
 
+               player->loc.y+=player->vel.y*deltaTime;
+               player->loc.x+=player->vel.x*deltaTime;
                render();
        }
        
@@ -134,6 +144,12 @@ void render(){
        player->draw();
        ui::putString(0,0,"Hello");
 
+       for(int i=0;i<=entity.size();i++){
+               entity[i]->draw();
+               entity[i]->loc.x += entity[i]->vel.x * deltaTime;
+               entity[i]->loc.y += entity[i]->vel.y * deltaTime;
+       }
+
        /**************************
        ****  CLOSE THE LOOP   ****
        **************************/
@@ -145,6 +161,11 @@ void render(){
 void logic(){
        ui::handleEvents();
        currentWorld->detect(&player->loc,&player->vel,player->width);
-       player->loc.y+=player->vel.y*deltaTime;
-       player->loc.x+=player->vel.x*deltaTime;
+       for(int i=0;i<=entity.size();i++){
+               currentWorld->detect(&entity[i]->loc,&entity[i]->vel,entity[i]->width);
+               if(entity[i]->alive==true&&entity[i]->type == 1){
+                       entity[i]->wander(90, &entity[i]->vel);
+               }
+       }
+
 }