]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Add on_create callback for items
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 13 Jun 2021 18:41:17 +0000 (20:41 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 13 Jun 2021 18:41:17 +0000 (20:41 +0200)
plugins/cherry/cherry.c
plugins/fireball/fireball.c
plugins/inventory/inventory.c
plugins/inventory/inventory.h

index 436fcf941bfd8036e024aee3dbe92ab6fc7bd378..f9987704b93af46fb7d79e9e557f6a255f8360cf 100644 (file)
@@ -18,6 +18,7 @@ static struct item cherry_item = {
 
        .on_use = &use_cherry,
        .on_destroy = NULL,
+       .on_create = NULL,
 };
 
 static void cherry_step(struct entity *self, struct entity_step_data stepdata)
index fcb1ac19a2a62e26f4f6d238aa80b4ce826f33a1..39e93fb586909d7c8429e6493ad3533b0f2d9713 100644 (file)
@@ -94,6 +94,7 @@ static struct item fireball_item = {
 
        .on_use = &shoot_fireball_item,
        .on_destroy = NULL,
+       .on_create = NULL,
 };
 
 static void shoot_if_has_fireball()
index c3b533d70380e6b93cebbc08499c5eb37abd372f..99235610499abb0a2eec2c78f56457af6a7207bc 100644 (file)
@@ -29,7 +29,11 @@ void inventory_add(struct inventory *self, struct itemstack stack)
                }
        }
 
-       *ptr = add_element(*ptr, make_buffer(&stack, sizeof(struct itemstack)));
+       struct itemstack *buf = make_buffer(&stack, sizeof(struct itemstack));
+       *ptr = add_element(*ptr, buf);
+
+       if (buf->item->on_create)
+               buf->item->on_create(buf);
 }
 
 /*
@@ -81,6 +85,9 @@ static void decrease_item_count(struct list **ptr, struct itemstack *stack)
                if (stack->item->on_destroy)
                        stack->item->on_destroy(stack);
 
+               if (stack->meta)
+                       free(stack->meta);
+
                free(stack);
                free(*ptr);
 
index 4b13f94fbf2d19953098eabdb86be5be69b66b8a..437ffb646d427c1cc071a3d8b212efbc043a116f 100644 (file)
@@ -16,6 +16,7 @@ struct item
 
        bool (*on_use)(struct itemstack *stack);
        void (*on_destroy)(struct itemstack *stack);
+       void (*on_create)(struct itemstack *stack);
 };
 
 struct inventory