From c594ed0d4475dc40150cffd89c594aa5783a4917 Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Mon, 18 Apr 2022 02:14:07 +0200 Subject: [PATCH] Display of inventory + delete item + replace item --- assets-cg/converters.py | 15 ++++--- assets-cg/items/1.json | 3 +- assets-cg/items/2.json | 3 +- assets-cg/maps/testCarte.tmx | 8 +++- include/define.h | 2 +- include/game.h | 17 ++++---- include/inventory.h | 5 ++- src/engine.c | 4 +- src/event.c | 2 + src/game.c | 3 +- src/inventory.c | 79 +++++++++++++++++++++++++++++++++--- 11 files changed, 114 insertions(+), 27 deletions(-) diff --git a/assets-cg/converters.py b/assets-cg/converters.py index f295b19..0d5facb 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -172,7 +172,7 @@ def parseTeleporter(layer): raise Exception("parseTeleporter() : Un téléporteur est mal configuré") if len(i["properties"]) == 2: print("parseTeleporter() : passage automatique idMap = -1 sur téléporteur x = " + str(i["properties"][0]["value"]) + ", y = " + str(i["properties"][1]["value"])) - teleporter += fxconv.u32(-1); + teleporter += fxconv.u32(-1) for j in i["properties"]: teleporter += fxconv.u32(j["value"]) except KeyError : @@ -308,10 +308,13 @@ def convert_items(input, output, params, target): file = open(f,"r") data = json.load(file) item = fxconv.Structure() - - item += fxconv.string(data["name"]) - item += fxconv.u32(data["id"]) - item += fxconv.ptr(f"img_{data['sprite']}") - items += fxconv.ptr(item) + try: + item += fxconv.string(data["name"]) + item += fxconv.u32(data["id"]) + item += fxconv.string(data["description"]) + item += fxconv.ptr(f"img_{data['sprite']}") + items += fxconv.ptr(item) + except KeyError: + raise Exception(f"convert_items() : L'item {data['name']} est mal configuré") fxconv.elf(items, output, "_" + params["name"], **target) \ No newline at end of file diff --git a/assets-cg/items/1.json b/assets-cg/items/1.json index 693759e..4db56f1 100644 --- a/assets-cg/items/1.json +++ b/assets-cg/items/1.json @@ -1,5 +1,6 @@ { "name":"Test1", "id":1, - "sprite":"item" + "sprite":"item", + "description":"test1" } diff --git a/assets-cg/items/2.json b/assets-cg/items/2.json index 12b7e1c..0806044 100644 --- a/assets-cg/items/2.json +++ b/assets-cg/items/2.json @@ -1,5 +1,6 @@ { "name":"Obj2", "id":2, - "sprite":"item2" + "sprite":"item2", + "description":"test2" } diff --git a/assets-cg/maps/testCarte.tmx b/assets-cg/maps/testCarte.tmx index ee1852b..5292cb2 100644 --- a/assets-cg/maps/testCarte.tmx +++ b/assets-cg/maps/testCarte.tmx @@ -1,5 +1,5 @@ - + @@ -185,6 +185,12 @@ + + + + + + diff --git a/include/define.h b/include/define.h index d57eaac..cb41b78 100644 --- a/include/define.h +++ b/include/define.h @@ -5,4 +5,4 @@ #define NB_INTERIORS 2 #define NB_PLAYER_MOVES 3 -#define NB_PLAYER_ITEMS 10 \ No newline at end of file +#define NB_PLAYER_ITEMS 30 \ No newline at end of file diff --git a/include/game.h b/include/game.h index 7eb42fa..b658727 100644 --- a/include/game.h +++ b/include/game.h @@ -13,14 +13,15 @@ struct Game { }; enum action { - DIR_DOWN = 0, - DIR_LEFT = 1, - DIR_UP = 2, - DIR_RIGHT = 3, - ACTION_SHIFT = 4, - ACTION_ALPHA = 5, - ACTION_F1 = 6, - ACTION_OPTN = 7 + DIR_DOWN, + DIR_LEFT, + DIR_UP, + DIR_RIGHT, + ACTION_SHIFT, + ACTION_ALPHA, + ACTION_OPTN, + ACTION_F1, + ACTION_F2, }; /*get the input with a timeout*/ diff --git a/include/inventory.h b/include/inventory.h index 73ec93f..f8d84d1 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -5,8 +5,8 @@ struct Item { char *name; int id; - bopti_image_t *sprite; char *description; + bopti_image_t *sprite; }; struct Items { @@ -23,4 +23,5 @@ struct Item *get_item_id(int id); bool add_item_to_inventory(struct Inventory *inventory, struct Item *item); int get_first_free_space(struct Inventory *inventory); void remove_item_pos(struct Inventory *inventory, int pos); -void display_inventory(struct Inventory *inventory); \ No newline at end of file +void display_inventory(struct Inventory *inventory); +int open_inventory(struct Inventory *inventory, char* context); \ No newline at end of file diff --git a/src/engine.c b/src/engine.c index a2dce12..d2e5e75 100644 --- a/src/engine.c +++ b/src/engine.c @@ -127,8 +127,10 @@ void engine_action(struct Game *game, int action) { if(action == ACTION_F1) { /*game->player->sprint = game->player->sprint ? 0 : 1; add_move(game->player, get_move_id(3));*/ + open_inventory(&game->player->inventory, "Consultation"); + } + if(action == ACTION_F2) { add_item_to_inventory(&game->player->inventory, get_item_id(1)); - display_inventory(&game->player->inventory); } if(action == ACTION_OPTN) { draw_stats(game->player->stats); diff --git a/src/event.c b/src/event.c index 7648cd4..31e467d 100644 --- a/src/event.c +++ b/src/event.c @@ -5,6 +5,7 @@ #include "event.h" #include "player.h" #include "capacite.h" +#include "inventory.h" bool handle_event(struct Game *game, char const *event) { @@ -14,6 +15,7 @@ bool handle_event(struct Game *game, char const *event) int len=strlen(event), qty=1; char *star = strchr(event, '*'); if(star) len=star-event, qty=atoi(star+1); + add_item_to_inventory(&game->player->inventory,get_item_id(2)); return true; } else if(!strncmp(event, "xp:", 3)) { diff --git a/src/game.c b/src/game.c index 745d611..5d653f1 100644 --- a/src/game.c +++ b/src/game.c @@ -38,7 +38,8 @@ int get_inputs(void) { /*action key*/ if(key == KEY_SHIFT) return ACTION_SHIFT; if(key == KEY_ALPHA) return ACTION_ALPHA; - if(key == KEY_F1) return ACTION_F1; if(key == KEY_OPTN) return ACTION_OPTN; + if(key == KEY_F1) return ACTION_F1; + if(key == KEY_F2) return ACTION_F2; } } diff --git a/src/inventory.c b/src/inventory.c index 7953c3c..7673347 100644 --- a/src/inventory.c +++ b/src/inventory.c @@ -5,6 +5,7 @@ #include "inventory.h" #include "util.h" #include "define.h" +#include "vec2.h" extern struct Items items; @@ -26,26 +27,94 @@ int get_first_free_space(struct Inventory *inventory) { bool add_item_to_inventory(struct Inventory *inventory, struct Item *item) { int index = get_first_free_space(inventory); + + extern bopti_image_t img_dialogue; + dimage(42,DHEIGHT-75,&img_dialogue); + if(index < NB_PLAYER_ITEMS) { + dprint(50,DHEIGHT-47,C_BLACK,"Vous ajoutez %s à votre inventaire !", item->name); + dupdate(); + wait_for_input(KEY_SHIFT); inventory->items[index] = item; inventory->nbItems++; return true; + } else { + dprint(50,DHEIGHT-47,C_BLACK,"Plus de place pour ajouter %s à votre inventaire !", item->name); + dupdate(); + wait_for_input(KEY_SHIFT); + int pos = open_inventory(inventory, "Remplacer"); + if(pos != -1) { + inventory->items[pos] = item; + inventory->nbItems++; + return true; + } } return false; } void remove_item_pos(struct Inventory *inventory, int pos) { - inventory->items[pos] = NULL; + inventory->items[pos] = NULL; inventory->nbItems--; } void display_inventory(struct Inventory *inventory) { - dclear(C_WHITE); for(int i = 0 ; i < NB_PLAYER_ITEMS ; i++) { + int x = i%10; + int y = i/10; if(inventory->items[i] != NULL) { - dimage(0,18*i,inventory->items[i]->sprite); + dimage(18*x+50,25*y+50,inventory->items[i]->sprite); + } else { + drect(18*x+50,25*y+50,18*x+65,25*y+65,C_BLACK); } } - dupdate(); - wait_for_input(KEY_SHIFT); +} + +int open_inventory(struct Inventory *inventory, char* context) { + int buffer = keydown(KEY_SHIFT); + struct Vec2 cursor = VEC2(0,0); + int pos = 0; + int suppression = 0; + + while(1) { + clearevents(); + + cursor.x += keydown(KEY_RIGHT) - keydown(KEY_LEFT); + cursor.y += keydown(KEY_DOWN) - keydown(KEY_UP); + + if(cursor.x > 9) cursor.x = 9; + if(cursor.x < 0) cursor.x = 0; + if(cursor.y > 2) cursor.y = 2; + if(cursor.y < 0) cursor.y = 0; + + pos = cursor.x + cursor.y*10; + + dclear(C_WHITE); + dtext(130,15,C_BLACK,context); + drect(18*cursor.x+48, 25*cursor.y+48, 18*cursor.x+67, 25*cursor.y+67,suppression ? C_RED : C_GREEN); + if(suppression) dtext(130,35,C_RED,"Suppression"); + display_inventory(inventory); + + if(inventory->items[pos] != NULL) { + dprint(10, DHEIGHT-30, C_BLACK, "nom : %s", inventory->items[pos]->name); + dprint(10, DHEIGHT-15, C_BLACK, "desc : %s", inventory->items[pos]->description); + } + dupdate(); + + if(keydown(KEY_SHIFT)) { + if(buffer) buffer = 0; + else if(inventory->items[pos] != NULL) { + if(!suppression) break; + else remove_item_pos(inventory, pos); + } + } + if(keydown(KEY_OPTN)) { + suppression = suppression ? 0 : 1; + } + if(keydown(KEY_EXIT)) { + pos = -1; + break; + } + while(keydown_any(KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP, KEY_SHIFT, KEY_OPTN, 0)) clearevents(); + } + return pos; } \ No newline at end of file