]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
added inventories
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 2 Oct 2015 12:47:11 +0000 (08:47 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 2 Oct 2015 12:47:11 +0000 (08:47 -0400)
Changelog
Goals.txt
include/Quest.h
include/entities.h
include/inventory.h [new file with mode: 0644]
src/Quest.cpp
src/entities.cpp
src/gameplay.cpp
src/inventory.cpp [new file with mode: 0644]
src/main.cpp

index fb9e42b749fdaa9830315cbaada084b2603cb34a..b8315487dec86716de6456cb88102d387fb1c77d 100644 (file)
--- a/Changelog
+++ b/Changelog
        - player can now complete assigned requests
        - player's name is displayed
        - improved gravity so entities don't shake on the ground
+
+10/2/2015:
+==========
+
+       - added a basic inventory
+       - quests can reward one type of item (but any quantity of that item)
+       - added texture loading, began working on player textures
index ede8de8fbe772113430b69e97d59f5dc20cc2078..9b259183210671f3d782ba5f018832302480d264 100644 (file)
--- a/Goals.txt
+++ b/Goals.txt
@@ -7,7 +7,29 @@ End of October:
        - have basic quest handling/player can interact with NPCs to take quests
        - have basic mobs/animals
 
-...
+End of November:
+================
+
+       - have a working player inventory
+       - receive items from things like quests and mobs
+       - weapons n stuff
+
+End of December:
+================
+
+       - have a basic world scripter thing to script worlds
+       - create world saving/loading functionality
+         (maybe work on prettying up inventory/save-load/quest stuff)
+
+January - February:
+===================
+
+       - work on a story line
+       
+February - March:
+=================
+
+       - work on sound effects / background music
 
 End of March:
 =============
@@ -26,3 +48,4 @@ End of May:
 ===========
 
        - Have game fully deployed with a way to receive feedback from the players
+       
index c4b878046c11028883d519de2040bfe81e4f4359..11d04a684a7832d0880b60722d5e7dc3e9f030d8 100644 (file)
@@ -5,13 +5,15 @@
 #include <cstdlib>\r
 #include <cstring>\r
 \r
+#include <inventory.h>\r
+\r
 #define TOTAL_QUESTS 1\r
 \r
 class Quest {\r
 public:\r
        char *title,*desc;\r
-       unsigned int reward;\r
-       Quest(const char *t,const char *d,unsigned int r);\r
+       struct item_t reward;\r
+       Quest(const char *t,const char *d,struct item_t r);\r
        ~Quest();\r
 };\r
 \r
@@ -20,7 +22,7 @@ public:
        std::vector<const Quest *>current;\r
        int assign(const char *t);\r
        int drop(const char *t);\r
-       int finish(const char *t);\r
+       int finish(const char *t,void *completer);\r
        bool hasQuest(const char *t);\r
 };\r
 \r
index b58d56946bd088798feba05e74aa03b3ee9d80d4..4c1b4849832a5693386d41dadf25f777bb1f627d 100644 (file)
@@ -2,18 +2,25 @@
 #define ENTITIES_H
 
 #include <common.h>
+#include <inventory.h>
 
 #define NPCp(n) ((NPC *)n)
 
+#define PLAYER_INV_SIZE        30      // The size of the player's inventory
+#define NPC_INV_SIZE   3       // Size of an NPC's inventory
+
 extern FILE* names;
 
 class Entity{
 public:
+       Inventory *inv;
+
        void *inWorld;
+       
        float width;    //width and height of the player
        float height;
        float speed;    //speed of the play
-       //type and subtype
+       
        int subtype;
        _TYPE type;
                        //example:
@@ -21,8 +28,10 @@ public:
                        //              |(subtype)
                        //              |->  0 Base NPC
                        //              |->  1 Merchant
+       
        vec2 loc; //location and velocity of the entity
        vec2 vel;
+       
        bool near;
        bool right,left, canMove; //movement variables
        bool alive;                               //the flag for whether or not the entity is alive
diff --git a/include/inventory.h b/include/inventory.h
new file mode 100644 (file)
index 0000000..8f42cc5
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef INVENTORY_H
+#define INVENTORY_H
+
+#include <cstdlib>
+
+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)
+};
+
+struct item_t {                                // Used to define entries in an entity's inventory
+       short count;                    // Quantity of the item in this slot
+       ITEM_ID id;                             // ID of the item
+} __attribute__ ((packed));
+
+class Inventory {
+private:
+       unsigned int size;              // Size of 'item' array
+       struct item_t *item;    // An array of the items contained in this inventory.
+public:
+       Inventory(unsigned int s);      // Creates an inventory of size 's'
+       ~Inventory(void);                       // Free's 'item'
+       
+       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
+       
+       void draw(void);        // Draws a text list of items in this inventory (should only be called for the player for now)
+};
+
+#endif // INVENTORY_H
index 28aade2215702163bb2af5fe5489608e70d3306e..c331e3226574b00ff86bfe0280c651e2ea0c8be1 100644 (file)
@@ -1,22 +1,23 @@
 #include <Quest.h>\r
+#include <entities.h>\r
 \r
 const Quest QuestList[TOTAL_QUESTS]={\r
-       Quest("Test","A test quest",0)\r
+       Quest("Test","A test quest",(struct item_t){1,TEST_ITEM})\r
 };\r
 \r
-Quest::Quest(const char *t,const char *d,unsigned int r){\r
+Quest::Quest(const char *t,const char *d,struct item_t r){\r
        size_t len;\r
        title=(char *)malloc((len=strlen(t)));\r
        strncpy(title,t,len);\r
        desc=(char *)malloc((len=strlen(d)));\r
        strncpy(desc,d,len);\r
-       reward=r;\r
+       memcpy(&reward,&r,sizeof(struct item_t));\r
 }\r
 \r
 Quest::~Quest(){\r
        free(title);\r
        free(desc);\r
-       reward=0;\r
+       memset(&reward,0,sizeof(struct item_t));\r
 }\r
 \r
 int QuestHandler::assign(const char *t){\r
@@ -46,14 +47,14 @@ int QuestHandler::drop(const char *t){
        return -1;\r
 }\r
 \r
-int QuestHandler::finish(const char *t){\r
+int QuestHandler::finish(const char *t,void *completer){\r
        unsigned char i;\r
        unsigned int r;\r
        for(i=0;i<current.size();i++){\r
                if(!strcmp(current[i]->title,t)){\r
-                       r=current[i]->reward;\r
+                       ((Entity *)completer)->inv->addItem(current[i]->reward.id,current[i]->reward.count);\r
                        current.erase(current.begin()+i);\r
-                       return r;\r
+                       return 0;\r
                }\r
        }\r
        return -1;\r
index 9d7ce95ddb863587c0144614b0400eb461b431c0..d5328f0303a398368b4bf807c45adf5819a04f42 100644 (file)
@@ -89,6 +89,7 @@ Player::Player(){ //sets all of the player specific traits on object creation
        alive = true;
        ground = false;
        near = true;
+       inv = new Inventory(PLAYER_INV_SIZE);
 }
 
 void Player::interact(){ //the function that will cause the player to search for things to interact with
@@ -104,6 +105,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation
        alive = true;
        canMove = true;
        near = false;
+       inv = new Inventory(NPC_INV_SIZE);
 }
 
 void NPC::addAIFunc(int (*func)(NPC *)){
index ad44eb20d606b6919e964c0ba3ee6b16ac927fdf..93318281b24f77b7e8e9107648aede60a7da4e37 100644 (file)
@@ -18,7 +18,7 @@ int giveTestQuest(NPC *speaker){
 int compTestQuest(NPC *speaker){
        if(player->qh.hasQuest("Test")){
                ui::dialogBox(speaker->name,"Ooo, that's a nice quest you got there. Lemme finish that for you ;).");
-               player->qh.finish("Test");
+               player->qh.finish("Test",player);
                return 0;
        }else{
                ui::dialogBox(speaker->name,"You need to get a quest from %s first.",entity[1]->name);
@@ -56,6 +56,7 @@ void initEverything(void){
                switch(i){
                case 1:
                        NPCp(entity[i])->addAIFunc(giveTestQuest);
+                       entity[i]->inv->addItem(TEST_ITEM,3);
                        break;
                case 2:
                        NPCp(entity[i])->addAIFunc(compTestQuest);
diff --git a/src/inventory.cpp b/src/inventory.cpp
new file mode 100644 (file)
index 0000000..b8905dd
--- /dev/null
@@ -0,0 +1,58 @@
+#include <inventory.h>
+#include <ui.h>
+
+const char *itemName[]={
+       "\0",
+       "Dank Maymay"
+};
+
+Inventory::Inventory(unsigned int s){
+       size=s;
+       item=(struct item_t *)calloc(size,sizeof(struct item_t));
+}
+
+Inventory::~Inventory(void){
+       free(item);
+}
+       
+int Inventory::addItem(ITEM_ID id,unsigned char count){
+       unsigned int i;
+       for(i=0;i<size;i++){
+               if(item[i].id==id){
+                       item[i].count+=count;
+                       return 0;
+               }else if(!item[i].count){
+                       item[i].id=id;
+                       item[i].count=count;
+                       return 0;
+               }
+       }
+       return -1;
+}
+
+int Inventory::takeItem(ITEM_ID id,unsigned char count){
+       unsigned int i;
+       for(i=0;i<size;i++){
+               if(item[i].id==id){
+                       item[i].count-=count;
+                       if(item[i].count<0)
+                               return item[i].count*-1;
+                       return 0;
+               }
+       }
+       return -1;
+}
+
+#include <entities.h>
+extern Player *player;
+
+void Inventory::draw(void){
+       unsigned int i=0;
+       float y=SCREEN_HEIGHT/2;
+       ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"Inventory:");
+       while(item[i].count){
+               y-=ui::fontSize*1.15;
+               ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"%d x %s",item[i].count,itemName[(unsigned)item[i].id]);
+               i++;
+       }
+}
index 02ddc3cd72b7e746ab781c9e6c137c3b8bb3e27f..2419eebfb32b79e645f1df1dbe707f1b45e87674 100644 (file)
@@ -162,6 +162,9 @@ void render(){
        glColor3ub(0,0,0);
        player->near=true;
        player->draw();                                         // Draw the player
+       player->inv->draw();
+
+       ui::draw();                                                     // Draw any UI elements if they need to be
 
        if(ui::debug){
                static unsigned int debugDiv=0;
@@ -179,8 +182,6 @@ void render(){
                                        fps,d,player->ground,SCREEN_WIDTH,SCREEN_HEIGHT,entity.size(),player->loc.x,rndy,player->qh.current.size());
        }
 
-       ui::draw();                                                     // Draw any UI elements if they need to be
-
        for(int i=0;i<=entity.size();i++){
                //entity[i]->draw();
                entity[i]->loc.x += entity[i]->vel.x * deltaTime;