From fe824d73405983bc048fe7a0cc9654f05ef1c3f5 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:29:27 +0200 Subject: [PATCH] Inventory management. --- src/game.c | 23 ++++++++++++++---- src/game.h | 9 +++++++ src/inventory.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ src/inventory.h | 3 +++ 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/game.c b/src/game.c index 61f8987..8aae513 100644 --- a/src/game.c +++ b/src/game.c @@ -37,6 +37,10 @@ void game_init(Game *game) { events_bind_variable(&game->handler, (int *)&game->player.life, "life"); events_bind_variable(&game->handler, &game->mana, "mana"); inventory_init(&game->inventory); + /* For debugging */ + game->inventory.slots[5].i = I_GLOVE; + game->inventory.slots[1].i = I_ARMOR; + game->inventory.slots[8].i = I_TALISMAN; // reload_npc(&game); } @@ -191,16 +195,27 @@ void game_get_inputs(Game *game) { clearevents(); sleep(); } - if(keydown(KEY_SHIFT)) - game->inventory.selected = game->inventory.selection; - if(keydown(KEY_OPTN)) { - game->player.is_male = !game->player.is_male; + if(keydown(KEY_OPTN)) + inventory_move_from_selected(&game->inventory); /* TODO: Make something cleaner */ while(keydown(KEY_OPTN)) { clearevents(); sleep(); } + if(keydown(KEY_SQUARE)) { + inventory_use(&game->inventory, &game->player); } + if(keydown(KEY_F1)) { + inventory_unequip(&game->inventory, IT_TALISMAN); + } + if(keydown(KEY_F2)) { + inventory_unequip(&game->inventory, IT_ARMOR); + } + if(keydown(KEY_F3)) { + inventory_unequip(&game->inventory, IT_WEAPON); + } + if(keydown(KEY_SHIFT)) + game->inventory.selected = game->inventory.selection; } else { /* Player actions - Prototypes in player.h and implementation in * player.c */ diff --git a/src/game.h b/src/game.h index 9670a4c..f42269a 100644 --- a/src/game.h +++ b/src/game.h @@ -32,6 +32,15 @@ typedef enum { I_AMOUNT } Item; +typedef enum { + IT_NONE, + IT_TALISMAN, + IT_ARMOR, + IT_WEAPON, + IT_FOOD, + IT_AMOUNT +} ItemType; + typedef struct { Item i : 4; unsigned char durability; diff --git a/src/inventory.c b/src/inventory.c index 5289fc1..ec1836a 100644 --- a/src/inventory.c +++ b/src/inventory.c @@ -10,6 +10,16 @@ extern bopti_image_t selected_img; extern bopti_image_t player_male_inv_img; extern bopti_image_t player_female_inv_img; +char item_types[I_AMOUNT] = { + IT_NONE, + IT_ARMOR, + IT_WEAPON, + IT_WEAPON, + IT_FOOD, + IT_FOOD, + IT_TALISMAN +}; + void inventory_init(Inventory *inventory) { inventory->open = 0; memset(inventory->slots, 0, sizeof(Slot)*SLOT_NUM); @@ -49,6 +59,7 @@ void inventory_draw(Inventory *inventory, Player *player) { player->is_male ? &player_male_inv_img : &player_female_inv_img); #endif } + /* TODO: Stats. */ } char inventory_add(Inventory *inventory, Item item) { @@ -63,6 +74,59 @@ char inventory_add(Inventory *inventory, Item item) { return 1; } +void inventory_move_from_selected(Inventory *inventory) { + if(inventory->selected < 0) return; + Slot current = inventory->slots[inventory->selection]; + Slot new = inventory->slots[inventory->selected]; + inventory->slots[inventory->selection] = new; + inventory->slots[inventory->selected] = current; +} + +void inventory_use(Inventory *inventory, Player *player) { + Item item = inventory->slots[inventory->selection].i; + switch(item_types[item]){ + case IT_TALISMAN: + inventory->equipped[0] = inventory->slots[inventory->selection]; + break; + case IT_ARMOR: + inventory->equipped[1] = inventory->slots[inventory->selection]; + break; + case IT_WEAPON: + inventory->equipped[2] = inventory->slots[inventory->selection]; + break; + case IT_FOOD: + /* TODO */ + break; + default: + break; + } + inventory->slots[inventory->selection].i = I_NONE; + inventory->slots[inventory->selection].durability = 255; +} + +void inventory_unequip(Inventory *inventory, ItemType type) { + if(inventory->slots[inventory->selection].i) return; + switch(type){ + case IT_TALISMAN: + inventory->slots[inventory->selection] = inventory->equipped[0]; + inventory->equipped[0].i = I_NONE; + inventory->equipped[0].durability = 255; + break; + case IT_ARMOR: + inventory->slots[inventory->selection] = inventory->equipped[1]; + inventory->equipped[1].i = I_NONE; + inventory->equipped[1].durability = 255; + break; + case IT_WEAPON: + inventory->slots[inventory->selection] = inventory->equipped[2]; + inventory->equipped[2].i = I_NONE; + inventory->equipped[2].durability = 255; + break; + default: + break; + } +} + void inventory_move_selection(Inventory *inventory, Direction direction) { switch(direction) { case D_UP: diff --git a/src/inventory.h b/src/inventory.h index 4e547f6..5bc3116 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -7,6 +7,9 @@ void inventory_init(Inventory *inventory); void inventory_draw(Inventory *inventory, Player *player); char inventory_add(Inventory *inventory, Item item); +void inventory_move_from_selected(Inventory *inventory); +void inventory_use(Inventory *inventory, Player *player); +void inventory_unequip(Inventory *inventory, ItemType type); void inventory_move_selection(Inventory *inventory, Direction direction); #endif