]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Add charged hits
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 14 Jun 2021 08:30:45 +0000 (10:30 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 14 Jun 2021 08:30:45 +0000 (10:30 +0200)
plugins/recharge/Makefile [new file with mode: 0644]
plugins/recharge/dependencies.txt [new file with mode: 0644]
plugins/recharge/recharge.c [new file with mode: 0644]
plugins/recharge/recharge.h [new file with mode: 0644]
plugins/sword/Makefile
plugins/sword/dependencies.txt
plugins/sword/sword.c

diff --git a/plugins/recharge/Makefile b/plugins/recharge/Makefile
new file mode 100644 (file)
index 0000000..cb6e05e
--- /dev/null
@@ -0,0 +1,4 @@
+plugins/recharge/recharge.so: plugins/recharge/recharge.c plugins/recharge/recharge.h plugins/game/game.h
+       cc -g -shared -fpic -o plugins/recharge/recharge.so plugins/recharge/recharge.c
+
+PLUGINS := ${PLUGINS} plugins/recharge/recharge.so
diff --git a/plugins/recharge/dependencies.txt b/plugins/recharge/dependencies.txt
new file mode 100644 (file)
index 0000000..dc22e61
--- /dev/null
@@ -0,0 +1 @@
+game
diff --git a/plugins/recharge/recharge.c b/plugins/recharge/recharge.c
new file mode 100644 (file)
index 0000000..37d0207
--- /dev/null
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include "recharge.h"
+#include "../game/game.h"
+
+static double recharge_full_timer = 0.0;
+static double recharge_timer = 0.0;
+
+static const char *recharge_icon;
+
+bool is_charged()
+{
+       return recharge_timer <= 0;
+}
+
+void recharge(double timer, const char *icon)
+{
+       recharge_full_timer = recharge_timer = timer;
+       recharge_icon = icon;
+}
+
+static void render_recharge_meter(struct winsize ws)
+{
+       int y = ws.ws_row - 1;
+       int x = ws.ws_col - 14;
+
+       if (recharge_timer <= 0.0)
+               return;
+
+       double frac = (recharge_full_timer - recharge_timer) / recharge_full_timer;
+
+       printf("\e[%d;%dH", y, x);
+
+       printf("%s[", recharge_icon);
+
+       struct color color = {
+               (1.0 - frac) * 255,
+               frac * 255,
+               0,
+       };
+
+       set_color(color, true);
+
+       char bar[11];
+       sprintf(bar, "%9d%%", (int) (frac * 100));
+
+       int bars = frac * 10;
+
+       for (int i = 0; i < 10; i++) {
+               if (i == bars)
+                       set_color(black, true);
+               printf("%c", bar[i]);
+       }
+
+       set_color(black, true);
+
+       printf("]");
+}
+
+static void recharge_globalstep(double dtime)
+{
+       if (recharge_timer > 0.0)
+               recharge_timer -= dtime;
+}
+
+__attribute__ ((constructor)) static void init()
+{
+       register_render_component(&render_recharge_meter);
+       register_globalstep((struct globalstep) {
+               .run_if_dead = false,
+               .callback = &recharge_globalstep,
+       });
+}
diff --git a/plugins/recharge/recharge.h b/plugins/recharge/recharge.h
new file mode 100644 (file)
index 0000000..7c0dc88
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef _RECHARGE_H_
+#define _RECHARGE_H_
+
+#include <stdbool.h>
+
+bool is_charged();
+void recharge(double timer, const char *icon);
+
+#endif
index ee289521ebf44ff37c84f4c99f3a0853aa5b0f67..034ba74031876a5aec0d0ced4aea2c65d6962b70 100644 (file)
@@ -1,4 +1,4 @@
-plugins/sword/sword.so: plugins/sword/sword.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h
+plugins/sword/sword.so: plugins/sword/sword.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/recharge/recharge.h
        cc -g -shared -fpic -o plugins/sword/sword.so plugins/sword/sword.c
 
 PLUGINS := ${PLUGINS} plugins/sword/sword.so
index b19003bde03be8102a4864e0d05aea6178d989b0..ad140f49f1d70ae787d1b92b37e5142a1a1d8631 100644 (file)
@@ -1,3 +1,4 @@
 game
 movement
 inventory
+recharge
index f3991506b2a75aee55354f00d566107e0d221618..653692fcf58950a8d66f312f1a77d056d8fce8ea 100644 (file)
@@ -3,6 +3,7 @@
 #include "../game/game.h"
 #include "../movement/movement.h"
 #include "../inventory/inventory.h"
+#include "../recharge/recharge.h"
 
 static bool use_broken_sword(struct itemstack *stack)
 {
@@ -22,6 +23,9 @@ static struct item broken_sword = {
 
 static bool use_sword(struct itemstack *stack)
 {
+       if (! is_charged())
+               return false;
+
        int x, y;
        x = player.x;
        y = player.y;
@@ -35,6 +39,8 @@ static bool use_sword(struct itemstack *stack)
 
                if (rand() % 100 == 0)
                        stack->item = &broken_sword;
+
+               recharge(1.0, "⚔ ");
        }
 
        return false;