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