]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
npc world walking
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 24 Mar 2016 13:22:30 +0000 (09:22 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 24 Mar 2016 13:22:30 +0000 (09:22 -0400)
Changelog
include/entities.h
include/world.h
src/entities.cpp
src/ui.cpp
src/world.cpp
xml/playerSpawnHill2.xml

index 00709fd8898783c01e3010f4c8669756fb0ca8b5..1a54ec34e2b4bd80ef98bf4994f73f0345b1b099 100644 (file)
--- a/Changelog
+++ b/Changelog
        - fixed abort bug on exit
        - added light that follows player
 
+3/21/2016,
+3/22/2016:
+==========
+
+       - andy did stuff
+
+       ~ 6 month Changelog-aversary!
+
 3/23/2016:
 ==========
 
        - flickery lights
        - ui font loading is smooth
        - entities can be flung
+
+3/24/2016:
+==========
+
+       - npcs walking through worlds?
+       - found brazzier glitch
+       - merchants inside stalls
+       - z renders????
index 450975f32579fc3a78ad23fb96aeaae1022ae1b8..acb2986f36cc51c72585dfc1aa8bcf0a4751fda3 100644 (file)
@@ -139,10 +139,10 @@ public:
 
        vec2 loc;
        vec2 vel;
-       
+
        float width;
        float height;
-       
+
        float speed;    // A speed factor for X movement
 
        /*
@@ -151,6 +151,7 @@ public:
 
        bool near;                              // Causes name to display
        bool canMove;                   // Enables movement
+       bool canJape;                   // Enables world leaving
        bool right,left;                // Direction faced by Entity
        bool alive;
        bool hit;
@@ -172,7 +173,7 @@ public:
 
        char   *name;
        GENDER  gender;
-       
+
        Texturec *tex;
        Texturec *ntex;
 
@@ -180,14 +181,14 @@ public:
 
        void draw(void);
        void spawn(float, float);
-       
+
        int ticksToUse;                         // Used by wander()
-       
+
        virtual void wander(int){}
        virtual void interact(){}
 
        void follow(Entity *e);
-       
+
        virtual ~Entity(){}
 };
 
@@ -195,21 +196,22 @@ class Player : public Entity{
 public:
        QuestHandler qh;
        bool light = false;
-       
+
        Player();
        ~Player();
        void save(void);
        void sspawn(float x,float y);
 };
 
-class NPC : public Entity{
+class NPC : public Entity {
 public:
        std::vector<int (*)(NPC *)>aiFunc;
        int dialogIndex;
-       
+
        NPC();
+       NPC(NPC *n);
        ~NPC();
-       
+
        void addAIFunc(int (*func)(NPC *),bool preload);
        void clearAIFunc(void);
        virtual void interact();
@@ -233,10 +235,10 @@ public:
        World *inWorld;
        std::string inside;
        std::string textureLoc;
-       
+
        Structures();
        ~Structures();
-       
+
        unsigned int spawn(BUILD_SUB, float, float);
 };
 
@@ -246,10 +248,10 @@ public:
        double init_y;
        void (*hey)(Mob *callee);
        std::string heyid;
-       
+
        Mob(int);
        ~Mob();
-       
+
        void wander(int);
 };
 
@@ -259,13 +261,13 @@ private:
 public:
        std::string pickupDialog;
        bool questObject = false;
-       
+
        Object();
        Object(std::string in,std::string pd);
        ~Object();
-       
+
        void reloadTexture(void);
-       
+
        void interact(void);
 };
 #endif // ENTITIES_H
index 152d654e3de7cf21376aa02a2b32128cc364f1c4..5b02c87ca633f7de330c9a565efc3eaee06d1d23 100644 (file)
@@ -115,7 +115,7 @@ protected:
         * of elements provided by the function.
         */
 
-       std::vector<WorldData> worldData;
+        std::vector<WorldData> worldData;
 
        /**
         * Starting x coordinate.
@@ -404,6 +404,7 @@ public:
         */
 
        World *goWorldLeft(Player *p);
+       bool   goWorldLeft( NPC *e );
 
        /**
         * Attempts to let the player enter the right-linked world specified by
index c047eff870af2c49f31a815ead54fbaa9d5face1..e97464e93c3598551572b21129eca2129a64efa3 100644 (file)
@@ -41,32 +41,32 @@ void initEntity(){
 void getRandomName(Entity *e){
        unsigned int tempNum,max=0;
        char *bufs;
-       
+
        std::ifstream names ("assets/names_en-us",std::ios::in);
-       
+
        names.seekg(0,names.beg);
-       
+
        bufs = new char[32];
-       
+
        for(;!names.eof();max++)
                names.getline(bufs,32);
-       
+
        tempNum = rand() % max;
        names.seekg(0,names.beg);
 
        for(unsigned int i=0;i<tempNum;i++)
                names.getline(bufs,32);
-       
+
        names.close();
-       
+
        switch(bufs[0]){
        default :
        case 'm': e->gender = MALE;  break;
        case 'f': e->gender = FEMALE;break;
        }
-       
+
        strcpy(e->name,bufs+1);
-       
+
        delete[] bufs;
 }
 
@@ -75,7 +75,7 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o
        loc.y = y;
        vel.x = 0;
        vel.y = 0;
-       
+
        alive   = true;
        right   = true;
        left    = false;
@@ -83,29 +83,29 @@ void Entity::spawn(float x, float y){       //spawns the entity you pass to it based o
        //canMove       = true;
        ground  = false;
        hit     = false;
-       
+
        ticksToUse = 0;
-       
+
        if(!maxHealth)health = maxHealth = 1;
-       
+
        if(type==MOBT){
                if(Mobp(this)->subtype == MS_BIRD){
                        Mobp(this)->init_y=loc.y;
                }
        }
-       
+
        name = new char[32];
        getRandomName(this);
-       
+
        followee = NULL;
 }
 
 Player::Player(){ //sets all of the player specific traits on object creation
        width = HLINE * 10;
        height = HLINE * 15;
-       
+
        type = PLAYERT; //set type to player
-       subtype = 0;    
+       subtype = 0;
        health = maxHealth = 100;
        speed = 1;
        canMove = true;
@@ -130,18 +130,18 @@ Player::~Player(){
 NPC::NPC(){    //sets all of the NPC specific traits on object creation
        width = HLINE * 10;
        height = HLINE * 16;
-       
+
        type    = NPCT; //sets type to npc
        subtype = 0;
 
        health = maxHealth = 100;
-       
+
        maxHealth = health = 100;
        canMove = true;
-       
+
        tex = new Texturec(1,"assets/NPC.png");
        inv = new Inventory(NPC_INV_SIZE);
-       
+
        randDialog = rand() % RAND_DIALOG_COUNT - 1;
        dialogIndex = 0;
 }
@@ -150,7 +150,7 @@ NPC::~NPC(){
        while(!aiFunc.empty()){
                aiFunc.pop_back();
        }
-       
+
        delete inv;
        delete tex;
        delete[] name;
@@ -159,22 +159,22 @@ NPC::~NPC(){
 Merchant::Merchant(){  //sets all of the Merchant specific traits on object creation
        width = HLINE * 10;
        height = HLINE * 16;
-       
+
        type    = MERCHT; //sets type to merchant
        subtype = 0;
 
        health = maxHealth = 100;
-       
+
        maxHealth = health = 100;
        canMove = true;
 
        trade.reserve(100);
        currTrade = 0;
-       
+
        //tex = new Texturec(1,"assets/NPC.png");
        //inv = new Inventory(NPC_INV_SIZE);
        //inv = new Inventory(1);
-                                                                                                            
+
        //randDialog = rand() % RAND_DIALOG_COUNT - 1;
        dialogIndex = 0;
 }
@@ -183,7 +183,7 @@ Merchant::~Merchant(){
        /*while(!aiFunc.empty()){
                aiFunc.pop_back();
        }*/
-       
+
        //delete inv;
        //delete tex;
        //delete[] name;
@@ -191,18 +191,18 @@ Merchant::~Merchant(){
 
 Structures::Structures(){ //sets the structure type
        health = maxHealth = 1;
-       
+
        alive = false;
        near  = false;
-       
+
        name = NULL;
-       
+
        //inv = NULL;
        canMove = false;
 }
 Structures::~Structures(){
        delete tex;
-       
+
        if(name)
                delete[] name;
 }
@@ -210,10 +210,10 @@ Structures::~Structures(){
 Mob::Mob(int sub){
        type = MOBT;
        aggressive = false;
-       
+
        maxHealth = health = 50;
        canMove = true;
-       
+
        switch((subtype = sub)){
        case MS_RABBIT:
                width  = HLINE * 10;
@@ -241,7 +241,7 @@ Mob::Mob(int sub){
                tex = new Texturec(1,"assets/items/ITEM_PAGE.png");
                break;
        }
-       
+
        inv = new Inventory(NPC_INV_SIZE);
 }
 Mob::~Mob(){
@@ -259,7 +259,7 @@ Object::Object(){
        canMove = false;
 
        maxHealth = health = 1;
-       
+
        tex = NULL;
        inv = NULL;
 }
@@ -288,7 +288,7 @@ Object::~Object(){
 void Object::reloadTexture(void){
        if(tex)
                delete tex;
-               
+
        tex = new Texturec(1,getItemTexturePath(iname));
        width  = getItemWidth(iname);
        height = getItemHeight(iname);
@@ -297,10 +297,10 @@ void Object::reloadTexture(void){
 void Entity::draw(void){               //draws the entities
        glPushMatrix();
        glColor3ub(255,255,255);
-       
+
        if ( !alive )
                return;
-       
+
        if(type==NPCT){
                if(NPCp(this)->aiFunc.size()){
                        glColor3ub(255,255,0);
@@ -363,7 +363,7 @@ void Entity::draw(void){            //draws the entities
                                tex->bind(0);
                                break;
                        }
-               } 
+               }
                break;
        default:
                glActiveTexture(GL_TEXTURE0);
@@ -395,7 +395,7 @@ void NPC::
 wander( int timeRun )
 {
        static int direction;
-       
+
        if ( followee ) {
                if ( loc.x < followee->loc.x - 40 )
                        direction = 1;
@@ -403,25 +403,28 @@ wander( int timeRun )
                        direction = -1;
                else
                        direction = 0;
-               
+
                vel.x = .018 * HLINE * direction;
        } else if ( ticksToUse == 0 ) {
                ticksToUse = timeRun;
-               
+
                vel.x = .008 * HLINE;
                direction = (getRand() % 3 - 1);
-               
+
                if ( direction == 0 )
                        ticksToUse *= 2;
-               
+
                vel.x *= direction;
        }
-       
+
+       if( vel.x < 0)
+               currentWorld->goWorldLeft( this );
+
        ticksToUse--;
 }
 
 void NPC::addAIFunc(int (*func)(NPC *),bool preload){
-       if(preload){                                                                            // Preload AI functions so that they're given after 
+       if(preload){                                                                            // Preload AI functions so that they're given after
                                                                                                                // the current dialog box is closed
                AIpreload.push_back(func);
                AIpreaddr.push_back(this);
@@ -437,14 +440,14 @@ void NPC::interact(){ //have the npc's interact back to the player
        std::thread([this]{
                int (*func)(NPC *);
                loc.y += 5;
-               
+
                canMove=false;
                left = (player->loc.x < loc.x);
                right = !left;
-               
+
                if(aiFunc.size()){
                        func=aiFunc.front();
-                       
+
                        if(!func(this)){
                                if(aiFunc.size())aiFunc.erase(aiFunc.begin());
                        }
@@ -525,17 +528,17 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){
 
        bsubtype = sub;
        dim2 dim;
-       
+
        /*
         *      tempN is the amount of entities that will be spawned in the village. Currently the village
         *      will spawn bewteen 2 and 7 villagers for the starting hut.
        */
 
        //unsigned int tempN = (getRand() % 5 + 2);
-       
+
        if ( textureLoc.empty() )
                textureLoc = inWorld->sTexLoc[sub];
-       
+
        switch(sub){
                case STALL_MARKET:
                        tex = new Texturec({ textureLoc });
@@ -551,7 +554,7 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){
                        inv = NULL;
                        break;
        }
-       
+
        return 0;
 }
 
@@ -565,7 +568,7 @@ void Mob::wander(int timeRun){
        static int direction;   //variable to decide what direction the entity moves
        static unsigned int heya=0,hi=0;
        static bool YAYA = false;
-       
+
        if ( followee ) {
                if ( loc.x < followee->loc.x - 40 )
                        direction = 1;
@@ -573,11 +576,11 @@ void Mob::wander(int timeRun){
                        direction = -1;
                else
                        direction = 0;
-               
+
                vel.x = .018 * HLINE * direction;
                return;
        }
-       
+
        if(aggressive && !YAYA &&
           player->loc.x + (width / 2)  > loc.x && player->loc.x + (width / 2)  < loc.x + width  &&
           player->loc.y + (height / 3) > loc.y && player->loc.y + (height / 3) < loc.y + height ){
@@ -586,7 +589,7 @@ void Mob::wander(int timeRun){
                        a->setStyle("");
                        a->setBackground( WorldBGType::Forest );
                        a->setBGM("assets/music/embark.wav");
-               
+
                        ui::toggleWhiteFast();
                        YAYA = true;
                        ui::waitForCover();
@@ -595,7 +598,7 @@ void Mob::wander(int timeRun){
                        ui::toggleWhiteFast();
                }
        }
-       
+
        switch(subtype){
        case MS_RABBIT:
                if(!ticksToUse){
@@ -654,16 +657,16 @@ void Player::save(void){
        data.append(std::to_string((int)health) + "\n");
        data.append(std::to_string((int)maxHealth) + "\n");
        data.append(std::to_string((int)tickCount) + "\n");
-       
+
        data.append(std::to_string((int)inv->items.size()) + "\n");
        for(auto &i : inv->items)
                data.append(std::to_string((int)i.count) + "\n" + std::to_string((int)i.id) + "\n");
-       
+
        data.append((std::string)(currentXML.c_str() + 4) + "\n");
-       
+
        data.append("dOnE\0");
        out.write(data.c_str(),data.size());
-       out.close();    
+       out.close();
 }
 
 void Player::sspawn(float x,float y){
@@ -671,21 +674,21 @@ void Player::sspawn(float x,float y){
        uint count;
        std::ifstream in ("xml/main.dat",std::ios::in | std::ios::binary);
        spawn(x,y);
-       
+
        if(in.good()){
                std::istringstream data;
                std::string ddata;
                std::streampos len;
-               
+
                in.seekg(0,std::ios::end);
                len = in.tellg();
                in.seekg(0,std::ios::beg);
-               
+
                std::vector<char> buf (len,'\0');
                in.read(buf.data(),buf.size());
-               
+
                data.rdbuf()->pubsetbuf(buf.data(),buf.size());
-               
+
                std::getline(data,ddata);
                loc.x = std::stoi(ddata);
                std::getline(data,ddata);
@@ -696,7 +699,7 @@ void Player::sspawn(float x,float y){
                maxHealth = std::stoi(ddata);
                std::getline(data,ddata);
                tickCount = std::stoi(ddata);
-       
+
                std::getline(data,ddata);
                for(i = std::stoi(ddata);i;i--){
                        std::getline(data,ddata);
@@ -704,10 +707,10 @@ void Player::sspawn(float x,float y){
                        std::getline(data,ddata);
                        inv->items.push_back(item_t{count,(uint)std::stoi(ddata)});
                }
-               
+
                std::getline(data,ddata);
                currentWorld = loadWorldFromXMLNoSave(ddata.c_str());
-               
+
                in.close();
        }
 }
index 8e5a304d55b268c9bb92e1e69f55c22a6eaa1bbb..48bd2b67a235b4e890dec88acee78a4c5171c2a1 100644 (file)
@@ -1488,7 +1488,7 @@ EXIT:
                                        heyOhLetsGo = 0;
                                        break;
                                case SDLK_l:
-                                       player->light^=true;
+                                       player->light ^= true;
                                        break;
                                case SDLK_f:
                                        currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
index a8e6e50fa568ad32b3a2175fb3c1d9d14c463a86..357e52762dcc6edf9f45a703c7e09a6d11e2db60 100644 (file)
@@ -803,15 +803,11 @@ singleDetect( Entity *e )
                */
 
                if(e->loc.x < worldStart){                                                                                              // Left bound
-
                        e->vel.x=0;
                        e->loc.x=(float)worldStart + HLINE / 2;
-
                }else if(e->loc.x + e->width + HLINE > worldStart + worldStart * -2){   // Right bound
-
                        e->vel.x=0;
                        e->loc.x=worldStart + worldStart * -2 - e->width - HLINE;
-
                }
        }
 }
@@ -1010,6 +1006,24 @@ goWorldLeft( Player *p )
        return this;
 }
 
+bool World::
+goWorldLeft( NPC *e )
+{
+       // check if entity is at world edge
+       if( !toLeft.empty() && e->loc.x < worldStart + HLINE * 15.0f ) {
+
+        currentWorldToLeft->addNPC(e->loc.x,e->loc.y);
+        e->alive = false;
+
+               currentWorldToLeft->npc.back()->loc.x = 0;
+               currentWorldToLeft->npc.back()->loc.y = GROUND_HEIGHT_MAXIMUM;
+
+               return true;
+       }
+
+       return false;
+}
+
 World *World::
 goWorldRight( Player *p )
 {
@@ -1379,6 +1393,8 @@ World *loadWorldFromPtr( World *ptr )
     currentWorldToRight = loadWorldFromXML( tmp->toRight );
     loadedRight = false;
 
+    std::cout<<tmp->npc.back()->name<<std::endl;
+
     return tmp;
 }
 
index 8d3d328551af0b7d740e2bb0c19898ed5fbc7741..482908702e0c90c15405e79f79ca638b3ec081bf 100644 (file)
@@ -4,6 +4,8 @@
        <generation type="Random" width="1000" />
        <structure type="5" inside="playerSpawnHill1_Building1.xml"/>
 
+       <npc name="Swag" x="0" />
+
        <link left="playerSpawnHill1.xml" />
 
 </World>