diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-09 19:41:18 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-06-09 19:41:18 +0200 |
commit | 043c0a4c0ef016e3b49816d582cdf7227f6d3450 (patch) | |
tree | fe171f949c5dc59d47ecf882369ee3ae4cd905f1 /plugins/fireball | |
parent | 9a897a11d49b21f54e338cf82a6a79fd19f797a3 (diff) | |
download | dungeon_game-043c0a4c0ef016e3b49816d582cdf7227f6d3450.tar.xz |
Add fireballs
Diffstat (limited to 'plugins/fireball')
-rw-r--r-- | plugins/fireball/Makefile | 4 | ||||
-rw-r--r-- | plugins/fireball/fireball.c | 101 |
2 files changed, 105 insertions, 0 deletions
diff --git a/plugins/fireball/Makefile b/plugins/fireball/Makefile new file mode 100644 index 0000000..bc0a277 --- /dev/null +++ b/plugins/fireball/Makefile @@ -0,0 +1,4 @@ +plugins/fireball/fireball.so: plugins/fireball/fireball.c plugins/game/game.h + cc -g -shared -fpic -o plugins/fireball/fireball.so plugins/fireball/fireball.c + +PLUGINS := ${PLUGINS} plugins/fireball/fireball.so diff --git a/plugins/fireball/fireball.c b/plugins/fireball/fireball.c new file mode 100644 index 0000000..1c3eeb6 --- /dev/null +++ b/plugins/fireball/fireball.c @@ -0,0 +1,101 @@ +#include <stdlib.h> +#include <stddef.h> +#include "../game/game.h" + +static struct entity fireball; + +struct fireball_data +{ + double timer; + int vx; + int vy; +}; + +static void fireball_spawn(struct entity *self, void *data) +{ + self->meta = malloc(sizeof(struct fireball_data)); + *((struct fireball_data *) self->meta) = *((struct fireball_data *) data); +} + +static void fireball_step(struct entity *self, struct entity_step_data stepdata) +{ + struct fireball_data *data = self->meta; + + if (stepdata.visible && (data->timer -= stepdata.dtime) <= 0.0) { + data->timer = 0.1; + move(self, data->vx, data->vy); + } +} + +static void fireball_collide(struct entity *self, int x, int y) +{ + (void) x, y; + + self->remove = true; +} + +static void fireball_collide_with_entity(struct entity *self, struct entity *other) +{ + add_health(other, -(1 + rand() % 3)); + self->remove = true; +} + +static bool try_shoot(int x, int y, int vx, int vy) +{ + x += vx; + y += vy; + + return spawn(fireball, x, y, & (struct fireball_data) { + .timer = 0.1, + .vx = vx, + .vy = vy, + }); +} + +static void shoot_fireball() +{ + int x, y; + + x = player.x; + y = player.y; + + for (int tries = 10; tries > 0; tries--) { + int vx, vy; + + vx = vy = 0; + + dir_to_xy(rand() % 4, &vx, &vy); + + if (try_shoot(x, y, vx, vy)) + return; + } +} + +__attribute__((constructor)) static void init() +{ + fireball = (struct entity) { + .name = "fireball", + .x = 0, + .y = 0, + .color = get_color("#FF6611"), + .texture = "⬤ ", + .remove = false, + .meta = NULL, + .health = 1, + .max_health = 1, + .collide_with_entities = true, + + .on_step = &fireball_step, + .on_collide = &fireball_collide, + .on_collide_with_entity = &fireball_collide_with_entity, + .on_spawn = &fireball_spawn, + .on_remove = NULL, + .on_death = NULL, + .on_damage = NULL, + }; + + register_input_handler(' ', (struct input_handler) { + .run_if_dead = false, + .callback = &shoot_fireball, + }); +} |