]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/sword/sword.c
dc1118d2445f87bd3a6a0236c2fe21a58a0afa7c
[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 #include "../recharge/recharge.h"
7
8 static bool use_broken_sword(struct itemstack *stack)
9 {
10         (void) stack;
11
12         return true;
13 }
14
15 static struct item broken_sword = {
16         .name = "Broken Sword",
17         .stackable = false,
18
19         .on_use = &use_broken_sword,
20         .on_destroy = NULL,
21         .on_create = NULL,
22 };
23
24 static bool use_sword(struct itemstack *stack)
25 {
26         if (! is_charged())
27                 return false;
28
29         int x, y;
30         x = player.x;
31         y = player.y;
32
33         dir_to_xy(last_player_move, &x, &y);
34
35         struct entity *entity = entity_collision_map[x][y];
36
37         if (entity) {
38                 add_health(entity, -1);
39
40                 if (rand() % 100 == 0)
41                         stack->item = &broken_sword;
42
43                 recharge(1.0, "⚔ ");
44         }
45
46         return false;
47 }
48
49 static struct item sword = {
50         .name = "Sword",
51         .stackable = false,
52
53         .on_use = &use_sword,
54         .on_destroy = NULL,
55         .on_create = NULL,
56 };
57
58 static void handle_e()
59 {
60         struct itemstack *stack = inventory_find(&player_inventory, &sword);
61
62         if (stack)
63                 use_sword(stack);
64 }
65
66 __attribute__((constructor)) static void init()
67 {
68         inventory_add(&player_inventory, (struct itemstack) {
69                 .item = &sword,
70                 .count = 1,
71                 .meta = NULL,
72         });
73
74         register_input_handler('e', (struct input_handler) {
75                 .run_if_dead = false,
76                 .callback = &handle_e,
77         });
78 }