From a277430f0ddde9ea2583f4b0c44fcafe8a2528bf Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 2 Oct 2015 08:47:11 -0400 Subject: added inventories --- src/Quest.cpp | 15 +++++++------- src/entities.cpp | 2 ++ src/gameplay.cpp | 3 ++- src/inventory.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 5 +++-- 5 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 src/inventory.cpp (limited to 'src') 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