]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
inventory visuals / item drop (Q)
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 29 Oct 2015 19:51:07 +0000 (15:51 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 29 Oct 2015 19:51:07 +0000 (15:51 -0400)
Changelog
include/inventory.h
src/inventory.cpp
src/ui.cpp
src/world.cpp

index a5b5df97e820ced91bd55c3c24b99bb87d32feaf..6283ad9477fe65a541c31c71de9ca7c09adbf800 100644 (file)
--- a/Changelog
+++ b/Changelog
        - documented world.h
        - fixed camera ortho
        - began working on layer-switching animation
+       - added variable width backgrounds
+       - added a debug sprint speed
+
+       - the currently selected item is now drawn on the player
+       - pressing q discards (w/ visuals) the currently selected item
index cb3e59d14d5647c560aaf74dbfc85446a3703f06..e27391a2751267e5782d912f5b1ccc4d72f8c597 100644 (file)
@@ -5,8 +5,12 @@
 
 #define DEBUG
 
-enum ITEM_ID {         // Contains item IDs for every item in the game, this is how items are stored. IDs are also used to lookup item strings
-       TEST_ITEM = 1,  // A test item (duh)
+/*
+ * A list of all item IDs.
+*/
+
+enum ITEM_ID {
+       TEST_ITEM = 1,
        SWORD_ITEM      = 2
 };
 
@@ -26,10 +30,15 @@ public:
        
        int addItem(ITEM_ID id,unsigned char count);    // Add 'count' items with an id of 'id' to the inventory
        int takeItem(ITEM_ID id,unsigned char count);   // Take 'count' items with an id of 'id' from the inventory
+       int useItem(ITEM_ID id);
+       
+       bool tossd;
+       int itemToss(void);
        
        void setSelection(unsigned int s);
        
        void draw(void);        // Draws a text list of items in this inventory (should only be called for the player for now)
+       
 };
 
 unsigned int initInventorySprites(void);       // Loads as many inventory textures as it can find, returns count
index 2eb008713af5a37a7da003a1dad806718db497aa..3043e80b09e3d3281d755cfc552f9424811958b7 100644 (file)
@@ -20,6 +20,8 @@ const char *ITEM_SPRITE[]={
 
 GLuint *ITEM_TEX;
 
+void itemDraw(Player *p,ITEM_ID id);
+
 unsigned int initInventorySprites(void){
        unsigned int i,loadCount=0;
        ITEM_TEX=(GLuint *)calloc(ITEM_COUNT,sizeof(GLuint));
@@ -36,6 +38,7 @@ Inventory::Inventory(unsigned int s){
        sel=0;
        size=s;
        item=(struct item_t *)calloc(size,sizeof(struct item_t));
+       tossd=false;
 }
 
 Inventory::~Inventory(void){
@@ -74,6 +77,9 @@ int Inventory::takeItem(ITEM_ID id,unsigned char count){
        unsigned int i;
        for(i=0;i<size;i++){
                if(item[i].id==id){
+#ifdef DEBUG
+                       DEBUG_printf("Took %u of player's %s(s).\n",count,itemName[item[i].id]);
+#endif // DEBUG
                        item[i].count-=count;
                        if(item[i].count<0)
                                return item[i].count*-1;
@@ -105,4 +111,69 @@ void Inventory::draw(void){
                glDisable(GL_TEXTURE_2D);
                i++;
        }
+       if(item[sel].count)itemDraw(player,item[sel].id);
+}
+
+static vec2 item_coord = {0,0};
+static vec2 item_velcd = {0,0};
+static bool item_tossd = false;
+static bool yes=false;
+
+void itemDraw(Player *p,ITEM_ID id){
+       static vec2 p1,p2;
+       if(!id)return;
+       glEnable(GL_TEXTURE_2D);
+       glBindTexture(GL_TEXTURE_2D,ITEM_TEX[id-1]);
+       if(!yes){
+               p1 = {p->loc.x+p->width/2,
+                         p->loc.y+p->width/2+HLINE*3};
+               p2 = {(float)(p1.x+p->width*(p->left?-.5:.5)),
+                         p->loc.y+HLINE*3};
+       }
+       if(p->inv->tossd) yes=true;
+       std::cout<<item_coord.x<<" "<<item_coord.y<<std::endl;
+       glBegin(GL_QUADS);
+               glTexCoord2i(0,1);glVertex2f(item_coord.x+p1.x,item_coord.y+p2.y);
+               glTexCoord2i(1,1);glVertex2f(item_coord.x+p2.x,item_coord.y+p2.y);
+               glTexCoord2i(1,0);glVertex2f(item_coord.x+p2.x,item_coord.y+p1.y);
+               glTexCoord2i(0,0);glVertex2f(item_coord.x+p1.x,item_coord.y+p1.y);
+       glEnd();
+       glDisable(GL_TEXTURE_2D);
+}
+
+void itemUse(Player *p,ITEM_ID id){
+       switch(id){
+       default:
+               ui::dialogBox(itemName[id],"You cannot use this item.");
+               break;
+       }
+}
+
+int Inventory::itemToss(void){
+       if(!item_tossd && item[sel].count && item[sel].id){
+               item_tossd = true;
+               item_coord.x = HLINE;
+               item_velcd.x = 0;
+               item_velcd.y = 3;
+               tossd = true;
+               return 1;
+       }else if(item_tossd){
+               if(item_coord.y<0){
+                       memset(&item_coord,0,sizeof(vec2));
+                       memset(&item_velcd,0,sizeof(vec2));
+                       item_tossd = false;
+                       
+                       takeItem(item[sel].id,1);
+                       
+                       tossd = yes = false;
+                       
+                       return 0;
+               }else{
+                       item_coord.x += item_velcd.x;
+                       item_coord.y += item_velcd.y;
+                       //item_velcd.x -= .005;
+                       item_velcd.y -= .1;
+                       return 1;
+               }
+       }
 }
index 91da2e9d1d408616e7d892edca5a1dd955ae1d46..05b540e49a5114877c10c9ff0409672c1659d6f0 100644 (file)
@@ -262,6 +262,9 @@ namespace ui {
                                                player->ground=false;
                                        }
                                }
+                               if(SDL_KEY==SDLK_q){
+                                       player->inv->itemToss();
+                               }
                                if(SDL_KEY==SDLK_c){
                                        dialogBox("","You pressed `c`, but nothing happened.");
                                }
@@ -287,6 +290,8 @@ namespace ui {
                        }
                }
                
+               if(player->inv->tossd)player->inv->itemToss();
+               
                unsigned int i;
                if(!dialogBoxExists&&AIpreaddr.size()){ // Flush preloaded AI functions if necessary
                        for(i=0;i<AIpreaddr.size();i++){
index 641dbf92107710dd8ed5d0b873271d1d2ff17ad7..5ff1f0fd886d2bfbd3a7d37518f3d49c8a34d3df 100644 (file)
@@ -602,7 +602,13 @@ void World::addHole(unsigned int start,unsigned int end){
 }
 
 int World::getTheWidth(void){
-       return -x_start*2;
+       World *hey=this;
+LOOP:
+       if(hey->infront){
+               hey=hey->infront;
+               goto LOOP;
+       }
+       return -hey->x_start*2;
 }
 
 IndoorWorld::IndoorWorld(void){