From 2491d6a47cec944b44ea2f3b339bf2e1fa5f3139 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Mon, 27 Apr 2020 17:59:13 +0200 Subject: [PATCH] Fixing attack timings --- include/components/dialog_holder.h | 5 +++-- include/systems/combat_manager.h | 2 ++ src/components/dialog_holder.c | 3 ++- src/components/dialog_methods.c | 8 ++++--- src/game_loader.c | 8 +++++++ src/systems/combat_methods.c | 27 ++++++++++++++++++------ src/systems/dialog_methods.c | 34 +++++++++++++++--------------- 7 files changed, 57 insertions(+), 30 deletions(-) diff --git a/include/components/dialog_holder.h b/include/components/dialog_holder.h index ab8e9ef..dfc9e9d 100644 --- a/include/components/dialog_holder.h +++ b/include/components/dialog_holder.h @@ -20,6 +20,7 @@ struct dialog_line { char *text; int input_count; struct dialog_input *inputs; + void (*callback)(gc_engine *engine); }; struct dialog_holder { @@ -46,8 +47,8 @@ struct dialog_manager { const struct dialog_manager dialog_manager; void dialog_next(gc_engine *engine); -void dialog_add_line(struct dialog_holder *this, char *name, char *text, \ -struct dialog_input *inputs); +struct dialog_line *dialog_add_line(struct dialog_holder *this, char *name, \ +char *text, struct dialog_input *inputs); void show_dialog_if_hidden(gc_engine *engine); #endif //MY_RPG_DIALOG_HOLDER_H diff --git a/include/systems/combat_manager.h b/include/systems/combat_manager.h index de45f46..a3ebcee 100644 --- a/include/systems/combat_manager.h +++ b/include/systems/combat_manager.h @@ -9,6 +9,7 @@ #ifndef MY_RPG_COMBAT_MANAGER_H #define MY_RPG_COMBAT_MANAGER_H +#include #include "system.h" #include "components/combat_holder.h" #include "components/dialog_holder.h" @@ -26,6 +27,7 @@ struct combat_manager { gc_system base; gc_scene *game_scene; struct enemy *current_enemy; + struct attack_holder *next_enemy_attack; enum combat_state state; }; diff --git a/src/components/dialog_holder.c b/src/components/dialog_holder.c index 29a799b..7a5847a 100644 --- a/src/components/dialog_holder.c +++ b/src/components/dialog_holder.c @@ -65,7 +65,8 @@ struct dialog_line *dialog_parse_text(gc_scene *scene, node *n) return (NULL); txt->name = my_strdup(n->name); txt->text = xml_getproperty(n, "line"); - txt->input_count = xml_getchildcount_filtered(n, "input");; + txt->input_count = xml_getchildcount_filtered(n, "input"); + txt->callback = NULL; txt->inputs = NULL; if (txt->input_count == 0) return (txt); diff --git a/src/components/dialog_methods.c b/src/components/dialog_methods.c index c681d33..a870c0c 100644 --- a/src/components/dialog_methods.c +++ b/src/components/dialog_methods.c @@ -9,25 +9,27 @@ #include "utility.h" #include -void dialog_add_line(struct dialog_holder *this, char *name, char *text, \ +struct dialog_line *dialog_add_line(struct dialog_holder *this, char *name, char *text, \ struct dialog_input *inputs) { struct dialog_line *line = malloc(sizeof(*line)); int count = 0; if (!line) - return; + return (NULL); line->name = name; line->text = text; if (inputs) for (count = 0; inputs[count].text; count++); line->input_count = count; line->inputs = inputs; + line->callback = NULL; for (count = 0; this->text[count]; count++); this->text = my_realloc(this->text, (count + 1) * sizeof(void *), \ (count + 2) * sizeof(void *)); if (!this->text) - return; + return (NULL); this->text[count] = line; this->text[count + 1] = NULL; + return (line); } \ No newline at end of file diff --git a/src/game_loader.c b/src/game_loader.c index 53dfd54..47fe9f8 100644 --- a/src/game_loader.c +++ b/src/game_loader.c @@ -69,6 +69,8 @@ const struct gc_data attacks[] = { void load_attacks(gc_scene *scene) { gc_data *data; + gc_list *li = scene->get_entity_by_cmp(scene, "attack_component"); + struct attack_component *attack; for (int i = 0; attacks[i].name; i++) { data = malloc(sizeof(*data)); @@ -80,6 +82,12 @@ void load_attacks(gc_scene *scene) data->destroy = attacks[i].destroy; LISTADD(scene->data, data); } + for (; li; li = li->next) { + attack = GETCMP(li->data, attack_component); + for (int i = 0; attack->attacks && attack->attacks[i].name; i++) + attack->attacks[i].run = scene->get_data(scene, "attack", \ +attack->attacks[i].name); + } } int register_customcmps(gc_engine *engine, bool map_editor) diff --git a/src/systems/combat_methods.c b/src/systems/combat_methods.c index 85da902..26162ff 100644 --- a/src/systems/combat_methods.c +++ b/src/systems/combat_methods.c @@ -125,6 +125,19 @@ gc_scene *scene, gc_engine *engine) this->state = ATTACKING; } +void defend_callback(gc_engine *engine) +{ + struct combat_manager *this = GETSYS(engine, combat_manager); + gc_entity *player = NULL; + gc_entity *enemy = NULL; + + if (!get_player_and_enemy(engine->scene, &player, &enemy)) + return; + if (this->next_enemy_attack->run) + this->next_enemy_attack->run(engine, enemy, player); + this->state = ATTACK; +} + void defend(struct combat_manager *this, struct dialog_holder *dialog, \ gc_scene *scene, gc_engine *engine) { @@ -132,8 +145,8 @@ gc_scene *scene, gc_engine *engine) gc_entity *enemy = NULL; struct attack_component *enemy_attack; static char str[150]; - struct attack_holder *attack = NULL; int count; + struct dialog_line *line; if (!get_player_and_enemy(scene, &player, &enemy)) return; @@ -144,10 +157,10 @@ gc_scene *scene, gc_engine *engine) my_printf("No attack found for the enemy.\n"); return; } - attack = &enemy_attack->attacks[random() % count]; - snprintf(str, 150, "%s uses attack %s.", "The bee", attack->name); - if (attack->run) - attack->run(engine, enemy, player); - dialog_add_line(dialog, NULL, str, NULL); - this->state = ATTACK; + this->next_enemy_attack = &enemy_attack->attacks[random() % count]; + snprintf(str, 150, "%s uses attack %s.", "The bee", \ +this->next_enemy_attack->name); + if ((line = dialog_add_line(dialog, NULL, str, NULL))) + line->callback = &defend_callback; + this->state = DEFENDING; } \ No newline at end of file diff --git a/src/systems/dialog_methods.c b/src/systems/dialog_methods.c index be3164a..46fedf8 100644 --- a/src/systems/dialog_methods.c +++ b/src/systems/dialog_methods.c @@ -42,27 +42,27 @@ my_strcmp(link->tile->type, "dialog")) } bool update_dialog(struct dialog_manager *this, gc_entity *text, \ -gc_entity *name) +gc_entity *name, gc_engine *engine) { - struct renderer *rend = GETCMP(name, renderer); + struct renderer *name_rend = GETCMP(name, renderer); + struct renderer *txt_rend = GETCMP(text, renderer); this->current_text = this->current_dialog->text[this->current_line]; - if (!rend || rend->type != GC_TXTREND || !rend->data) + if (!name_rend || name_rend->type != GC_TXTREND || !name_rend->data + || !txt_rend || txt_rend->type != GC_TXTREND || !txt_rend->data) return (true); - rend->destroy = &text_safe_destroy; - if (this->current_text) - ((gc_text *)rend->data)->text = this->current_text->name; - else - ((gc_text *)rend->data)->text = NULL; - rend = GETCMP(text, renderer); - if (!rend || rend->type != GC_TXTREND || !rend->data) - return (true); - rend->destroy = &text_safe_destroy; + name_rend->destroy = &text_safe_destroy; + txt_rend->destroy = &text_safe_destroy; if (this->current_text) { - ((gc_text *) rend->data)->text = this->current_text->text; + ((gc_text *)name_rend->data)->text = this->current_text->name; + ((gc_text *)txt_rend->data)->text = this->current_text->text; this->current_input = &this->current_text->inputs[0]; - } else - ((gc_text *)rend->data)->text = NULL; + if (this->current_text->callback) + this->current_text->callback(engine); + } else { + ((gc_text *)txt_rend->data)->text = NULL; + ((gc_text *)name_rend->data)->text = NULL; + } this->current_line++; return (!this->current_text); } @@ -116,8 +116,8 @@ void dialog_next(gc_engine *engine) controllable_set_can_move(scene, false); holder_name = scene->get_entity(scene, 1336); entity = scene->get_entity(scene, 1337); - if (!entity || !holder_name || - (!update_dialog(this, entity, holder_name) && handle_input(engine, this))) + if (!entity || !holder_name || (!update_dialog(this, entity, \ +holder_name, engine) && handle_input(engine, this))) return; prefab_destroy(scene, this->dialog_id); this->dialog_id = -1;