From 3432efe82387a6a4145d087c4af1a354d4063901 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 14 Jun 2021 10:30:45 +0200 Subject: [PATCH] Add charged hits --- plugins/recharge/Makefile | 4 ++ plugins/recharge/dependencies.txt | 1 + plugins/recharge/recharge.c | 72 +++++++++++++++++++++++++++++++ plugins/recharge/recharge.h | 9 ++++ plugins/sword/Makefile | 2 +- plugins/sword/dependencies.txt | 1 + plugins/sword/sword.c | 6 +++ 7 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 plugins/recharge/Makefile create mode 100644 plugins/recharge/dependencies.txt create mode 100644 plugins/recharge/recharge.c create mode 100644 plugins/recharge/recharge.h diff --git a/plugins/recharge/Makefile b/plugins/recharge/Makefile new file mode 100644 index 0000000..cb6e05e --- /dev/null +++ b/plugins/recharge/Makefile @@ -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 index 0000000..dc22e61 --- /dev/null +++ b/plugins/recharge/dependencies.txt @@ -0,0 +1 @@ +game diff --git a/plugins/recharge/recharge.c b/plugins/recharge/recharge.c new file mode 100644 index 0000000..37d0207 --- /dev/null +++ b/plugins/recharge/recharge.c @@ -0,0 +1,72 @@ +#include +#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 index 0000000..7c0dc88 --- /dev/null +++ b/plugins/recharge/recharge.h @@ -0,0 +1,9 @@ +#ifndef _RECHARGE_H_ +#define _RECHARGE_H_ + +#include + +bool is_charged(); +void recharge(double timer, const char *icon); + +#endif diff --git a/plugins/sword/Makefile b/plugins/sword/Makefile index ee28952..034ba74 100644 --- a/plugins/sword/Makefile +++ b/plugins/sword/Makefile @@ -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 diff --git a/plugins/sword/dependencies.txt b/plugins/sword/dependencies.txt index b19003b..ad140f4 100644 --- a/plugins/sword/dependencies.txt +++ b/plugins/sword/dependencies.txt @@ -1,3 +1,4 @@ game movement inventory +recharge diff --git a/plugins/sword/sword.c b/plugins/sword/sword.c index f399150..653692f 100644 --- a/plugins/sword/sword.c +++ b/plugins/sword/sword.c @@ -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; -- 2.44.0