Fixing attack timings

This commit is contained in:
Anonymus Raccoon
2020-04-27 17:59:13 +02:00
parent a2ea70f569
commit 2491d6a47c
7 changed files with 57 additions and 30 deletions
+3 -2
View File
@@ -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
+2
View File
@@ -9,6 +9,7 @@
#ifndef MY_RPG_COMBAT_MANAGER_H
#define MY_RPG_COMBAT_MANAGER_H
#include <components/attack_component.h>
#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;
};
+2 -1
View File
@@ -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);
+5 -3
View File
@@ -9,25 +9,27 @@
#include "utility.h"
#include <malloc.h>
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);
}
+8
View File
@@ -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)
+20 -7
View File
@@ -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;
}
+17 -17
View File
@@ -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;