]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/sword/sword.c
Add sword
[dungeon_game.git] / plugins / sword / sword.c
1 #include <stdlib.h>
2 #include <stddef.h>
3 #include "../game/game.h"
4 #include "../movement/movement.h"
5 #include "../inventory/inventory.h"
6
7 static bool use_broken_sword(struct itemstack *stack)
8 {
9         (void) stack;
10
11         return true;
12 }
13
14 static struct item broken_sword = {
15         .name = "Broken Sword",
16         .stackable = false,
17
18         .on_use = &use_broken_sword,
19         .on_destroy = NULL,
20         .on_create = NULL,
21 };
22
23 static bool use_sword(struct itemstack *stack)
24 {
25         int x, y;
26         x = player.x;
27         y = player.y;
28
29         dir_to_xy(last_player_move, &x, &y);
30
31         struct entity *entity = entity_collision_map[x][y];
32
33         if (entity) {
34                 add_health(entity, -1);
35
36                 if (rand() % 100 == 0)
37                         stack->item = &broken_sword;
38         }
39
40         return false;
41 }
42
43 static struct item sword = {
44         .name = "Sword",
45         .stackable = false,
46
47         .on_use = &use_sword,
48         .on_destroy = NULL,
49         .on_create = NULL,
50 };
51
52 __attribute__((constructor)) static void init()
53 {
54         inventory_add(&player_inventory, (struct itemstack) {
55                 .item = &sword,
56                 .count = 1,
57                 .meta = NULL,
58         });
59 }