adding guns

This commit is contained in:
Alexandre 2025-05-12 09:53:19 +02:00
parent 99566d2cee
commit ecd14cbe55
10 changed files with 130 additions and 3 deletions

View File

@ -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"
}
}

View File

@ -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

BIN
bin/back

Binary file not shown.

BIN
obj/invFcts.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

25
src/invFcts.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#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) {
}
}
}
}

6
src/invFcts.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef INV_HH
#define INV_HH
void shoot(float dtime);
#endif

View File

@ -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);
}

View File

@ -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