diff --git a/.vscode/settings.json b/.vscode/settings.json index 3850c1e..cf7570e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -25,6 +25,8 @@ "unistd.h": "c", "pthread.h": "c", "maeth.h": "c", - "music.h": "c" + "music.h": "c", + "inventory.h": "c", + "invfcts.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 6d73e15..88a0c02 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ test: bin/back mem: bin/back valgrind --leak-check=full ./bin/back -bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/inventory.o obj/music.o obj/maeth.o obj/move.o obj/base.o obj/hash.o +bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/invFcts.o obj/inventory.o obj/music.o obj/maeth.o obj/move.o obj/base.o obj/hash.o mkdir -p bin $(CC) $(FLAGS) $^ $(LFLAGS) -o $@ @@ -27,6 +27,7 @@ obj/move.o: src/move.c obj/base.o: src/base.c obj/proj.o: src/proj.c obj/inventory.o: src/inventory.c +obj/invFcts.o: src/invFcts.c obj/music.o: src/music.c obj/maeth.o: src/maeth.c obj/menus.o: src/menus.c diff --git a/bin/back b/bin/back index 8a8284a..23f4718 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/invFcts.o b/obj/invFcts.o new file mode 100644 index 0000000..f1558f4 Binary files /dev/null and b/obj/invFcts.o differ diff --git a/obj/inventory.o b/obj/inventory.o index 82f4878..6430416 100644 Binary files a/obj/inventory.o and b/obj/inventory.o differ diff --git a/obj/main.o b/obj/main.o index 1c1186c..4215332 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/src/invFcts.c b/src/invFcts.c new file mode 100644 index 0000000..04ca240 --- /dev/null +++ b/src/invFcts.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hash.h" +#include "base.h" +#include "display.h" +#include "inventory.h" +#include "invFcts.h" + +void shoot(float dtime) { + for(int l = 0; l < Lines; l++) { + for(int c = 0; c < Cols; c++) { + if(playerInventory->data[l][c]->itm->id == 1 && playerInventory->data[l][c]->count > 0) { + + } + } + } +} \ No newline at end of file diff --git a/src/invFcts.h b/src/invFcts.h new file mode 100644 index 0000000..8035d89 --- /dev/null +++ b/src/invFcts.h @@ -0,0 +1,6 @@ +#ifndef INV_HH +#define INV_HH + +void shoot(float dtime); + +#endif \ No newline at end of file diff --git a/src/inventory.c b/src/inventory.c index dcec9fc..37ba381 100644 --- a/src/inventory.c +++ b/src/inventory.c @@ -14,12 +14,15 @@ #include "hash.h" #include "base.h" #include "display.h" +#include "invFcts.h" #include "inventory.h" inventory* playerInventory; const int Lines = 9; const int Cols = 3; +item* itemList; + void inv_init() { playerInventory = malloc(sizeof(inventory)); playerInventory->lines = Lines; @@ -28,15 +31,94 @@ void inv_init() { for(int k = 0; k < Lines; k++) { playerInventory->data[k] = malloc(sizeof(stack)*Cols); for(int l = 0; l < Cols; l++) { - playerInventory->data[k][l] = NULL; + playerInventory->data[k][l] = malloc(sizeof(stack)); + playerInventory->data[k][l]->count = 0; + } + } + itemList = malloc(sizeof(item)*128); + + build_itemList(); +} + +void build_itemList() { + itemList[0].name = "gun"; + itemList[0].id = 0; + itemList[0].maxCount = 1; + itemList[0].maxDur = 100; + itemList[0].texID = 0; + itemList[0].onCollect = NULL; + itemList[0].onUse = &shoot; + + itemList[1].name = "gunAmmo"; + itemList[1].id = 1; + itemList[1].maxCount = 256; + itemList[1].maxDur = 1; + itemList[1].texID = 0; + itemList[1].onCollect = NULL; + itemList[1].onUse = NULL; +} + +// return value = success +bool inv_addItem(int id, float dtime) { + const char* name = itemList[id].name; + for(int l = 0; l < Lines; l++) { + for(int c = 0; c < Cols; c++) { + if( + (playerInventory->data[l][c]->itm->name == name && playerInventory->data[l][c]->count > 0) && + playerInventory->data[l][c]->count < playerInventory->data[l][c]->itm->maxCount + ) { + // stack exists and is not full + playerInventory->data[l][c]->count += 1; + if(playerInventory->data[l][c]->itm->onCollect != NULL) { + (*playerInventory->data[l][c]->itm->onCollect)(dtime); + } + return true; + } + } + } + for(int l = 0; l < Lines; l++) { + for(int c = 0; c < Cols; c++) { + if(playerInventory->data[l][c]->count == 0) { + // empty spot + playerInventory->data[l][c]->count = 1; + + playerInventory->data[l][c]->itm = &(itemList[id]); + playerInventory->data[l][c]->itm->durability = playerInventory->data[l][c]->itm->maxDur; + + if(playerInventory->data[l][c]->itm->onCollect != NULL) { + (*playerInventory->data[l][c]->itm->onCollect)(dtime); + } + return true; + } + } + } + return false; +} + +void useItem(int line, int col, float dtime) { + if(playerInventory->data[line][col]->count > 0 && playerInventory->data[line][col]->itm->onUse != NULL) { + (*playerInventory->data[line][col]->itm->onUse)(dtime); + if(playerInventory->data[line][col]->itm->maxCount == 1) { + // unstackable + playerInventory->data[line][col]->itm->durability -= 1; + if(playerInventory->data[line][col]->itm->durability == 0) { + playerInventory->data[line][col]->count = 0; + } + } else { + // stackable + playerInventory->data[line][col]->count -= 1; } } } void inv_destroy() { for(int k = 0; k < Lines; k++) { + for(int l = 0; l < Cols; l++) { + free(playerInventory->data[k][l]); + } free(playerInventory->data[k]); } free(playerInventory->data); free(playerInventory); + free(itemList); } \ No newline at end of file diff --git a/src/inventory.h b/src/inventory.h index 7fd1fda..835e360 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -3,8 +3,11 @@ typedef struct item { const char* name; + int id; int maxCount; int texID; + int durability; // 1 if its a stackable item + int maxDur; void (*onCollect)(float dtime); void (*onUse)(float dtime); @@ -22,8 +25,16 @@ typedef struct inventory { int cols; } inventory; +inventory* playerInventory; +const int Lines; +const int Cols; // -------------- // void inv_init(); +void build_itemList(); + +bool inv_addItem(int id, float dtime); +void useItem(int line, int col, float dtime); + void inv_destroy(); #endif \ No newline at end of file