From db2480a067063c03253e3f33ea54bea4c3f136d6 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Tue, 25 Apr 2023 16:06:58 -0700 Subject: [PATCH] Clicking on item gives it --- buff.h | 1 + main.c | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/buff.h b/buff.h index 727dd8c..9b75fd4 100644 --- a/buff.h +++ b/buff.h @@ -18,4 +18,5 @@ #define BUFF_PUSH_FRONT(buff_ptr, value) { (buff_ptr)->cur_index++; BUFF_VALID(buff_ptr); for(int i = (buff_ptr)->cur_index - 1; i > 0; i--) { (buff_ptr)->data[i] = (buff_ptr)->data[i - 1]; }; (buff_ptr)->data[0] = value; } #define BUFF_REMOVE_BACK(buff_ptr) {assert( (buff_ptr)->cur_index > 0); (buff_ptr)->cur_index--;} #define BUFF_REMOVE_FRONT(buff_ptr) {if((buff_ptr)->cur_index > 0) {for(int i = 0; i < (buff_ptr)->cur_index - 1; i++) { (buff_ptr)->data[i] = (buff_ptr)->data[i+1]; }; (buff_ptr)->cur_index--;}} +#define BUFF_REMOVE_AT_INDEX(buff_ptr, index) { BUFF_VALID(buff_ptr); assert(index >= 0); assert(index < (buff_ptr)->cur_index); for(int i = index; i < (buff_ptr)->cur_index - 1; i++) (buff_ptr)->data[i] = (buff_ptr)->data[i + 1]; (buff_ptr)->cur_index -= 1; } #define BUFF_CLEAR(buff_ptr) {memset((buff_ptr), 0, sizeof(*(buff_ptr))); ((buff_ptr)->cur_index = 0);} diff --git a/main.c b/main.c index 7711959..ec07486 100644 --- a/main.c +++ b/main.c @@ -3806,6 +3806,7 @@ F cost: G + H Vec2 item_icon_size = V2(item_icon_width, item_icon_width); Vec2 cursor = AddV2(grid_aabb.upper_left, V2(padding, -padding)); + int to_give = -1; // don't modify the item array while iterating BUFF_ITER_I(ItemKind, &player->held_items, i) { Vec2 real_size = LerpV2(item_icon_size, hovered_state[i], MulV2F(item_icon_size, 1.25f)); @@ -3816,9 +3817,19 @@ F cost: G + H float target = 0.0f; if(aabb_is_valid(item_icon)) { - draw_quad((DrawParams){false, quad_aabb(item_icon), IMG(image_white_square), blendalpha(WHITE, Lerp(0.0f, hovered_state[i], 0.4f)), .layer = LAYER_UI_FG }); + draw_quad((DrawParams){false, quad_aabb(item_icon), IMG(image_white_square), blendalpha(WHITE, Lerp(0.0f, hovered_state[i], 0.4f)), .layer = LAYER_UI_FG}); bool hovered = has_point(item_icon, mouse_pos); - if(hovered) target = 1.0f; + if(hovered) + { + target = 1.0f; + if(pressed.mouse_down) + { + if(gete(player->talking_to)) + { + to_give = i; + } + } + } in_screen_space = true; dbgrect(item_icon); @@ -3835,6 +3846,15 @@ F cost: G + H cursor.x = grid_aabb.upper_left.x + padding; } } + if(to_give > -1) + { + choosing_item_grid = false; + assert(gete(player->talking_to)); + ItemKind given_item_kind = player->held_items.data[to_give]; + BUFF_REMOVE_AT_INDEX(&player->held_items, to_give); + BUFF_APPEND(&gete(player->talking_to)->held_items, given_item_kind); + } + } }