]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/sword/sword.c
Add charged hits
[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 __attribute__((constructor)) static void init()
59 {
60         inventory_add(&player_inventory, (struct itemstack) {
61                 .item = &sword,
62                 .count = 1,
63                 .meta = NULL,
64         });
65 }