From a277430f0ddde9ea2583f4b0c44fcafe8a2528bf Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 2 Oct 2015 08:47:11 -0400 Subject: added inventories --- Changelog | 7 +++++++ Goals.txt | 25 ++++++++++++++++++++++- include/Quest.h | 8 +++++--- include/entities.h | 11 +++++++++- include/inventory.h | 29 +++++++++++++++++++++++++++ src/Quest.cpp | 15 +++++++------- src/entities.cpp | 2 ++ src/gameplay.cpp | 3 ++- src/inventory.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 5 +++-- 10 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 include/inventory.h create mode 100644 src/inventory.cpp diff --git a/Changelog b/Changelog index fb9e42b..b831548 100644 --- a/Changelog +++ b/Changelog @@ -64,3 +64,10 @@ - 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 diff --git a/Goals.txt b/Goals.txt index ede8de8..9b25918 100644 --- 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 + diff --git a/include/Quest.h b/include/Quest.h index c4b8780..11d04a6 100644 --- a/include/Quest.h +++ b/include/Quest.h @@ -5,13 +5,15 @@ #include #include +#include + #define TOTAL_QUESTS 1 class Quest { public: char *title,*desc; - unsigned int reward; - Quest(const char *t,const char *d,unsigned int r); + struct item_t reward; + Quest(const char *t,const char *d,struct item_t r); ~Quest(); }; @@ -20,7 +22,7 @@ public: std::vectorcurrent; int assign(const char *t); int drop(const char *t); - int finish(const char *t); + int finish(const char *t,void *completer); bool hasQuest(const char *t); }; diff --git a/include/entities.h b/include/entities.h index b58d569..4c1b484 100644 --- a/include/entities.h +++ b/include/entities.h @@ -2,18 +2,25 @@ #define ENTITIES_H #include +#include #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 index 0000000..8f42cc5 --- /dev/null +++ b/include/inventory.h @@ -0,0 +1,29 @@ +#ifndef INVENTORY_H +#define INVENTORY_H + +#include + +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 diff --git a/src/Quest.cpp b/src/Quest.cpp index 28aade2..c331e32 100644 --- a/src/Quest.cpp +++ b/src/Quest.cpp @@ -1,22 +1,23 @@ #include +#include const Quest QuestList[TOTAL_QUESTS]={ - Quest("Test","A test quest",0) + Quest("Test","A test quest",(struct item_t){1,TEST_ITEM}) }; -Quest::Quest(const char *t,const char *d,unsigned int r){ +Quest::Quest(const char *t,const char *d,struct item_t r){ size_t len; title=(char *)malloc((len=strlen(t))); strncpy(title,t,len); desc=(char *)malloc((len=strlen(d))); strncpy(desc,d,len); - reward=r; + memcpy(&reward,&r,sizeof(struct item_t)); } Quest::~Quest(){ free(title); free(desc); - reward=0; + memset(&reward,0,sizeof(struct item_t)); } int QuestHandler::assign(const char *t){ @@ -46,14 +47,14 @@ int QuestHandler::drop(const char *t){ return -1; } -int QuestHandler::finish(const char *t){ +int QuestHandler::finish(const char *t,void *completer){ unsigned char i; unsigned int r; for(i=0;ititle,t)){ - r=current[i]->reward; + ((Entity *)completer)->inv->addItem(current[i]->reward.id,current[i]->reward.count); current.erase(current.begin()+i); - return r; + return 0; } } return -1; diff --git a/src/entities.cpp b/src/entities.cpp index 9d7ce95..d5328f0 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -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 *)){ diff --git a/src/gameplay.cpp b/src/gameplay.cpp index ad44eb2..9331828 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -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 index 0000000..b8905dd --- /dev/null +++ b/src/inventory.cpp @@ -0,0 +1,58 @@ +#include +#include + +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 +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++; + } +} diff --git a/src/main.cpp b/src/main.cpp index 02ddc3c..2419eeb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; -- cgit v1.2.3