diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-01-09 21:27:53 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-01-09 21:53:12 +0100 |
commit | 8c37dc0c5e6f4e588d37f5900764125716bc7f77 (patch) | |
tree | 7fbddeceef42e306142bdcda4c365fd8277fe955 /jouer.c | |
parent | b4038deb223e309fe886c71b3765a7e82e65f9bb (diff) |
select a target using a structure instead of an index
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
Diffstat (limited to 'jouer.c')
-rw-r--r-- | jouer.c | 105 |
1 files changed, 55 insertions, 50 deletions
@@ -21,86 +21,94 @@ static inline void unhighlight_prev_character(struct team_t *team) chr->surf = chr->def_surf; } -static int find_next_team_member(const struct team_t *team, int current) +static struct character_t *find_next_team_member(const struct character_t *current) { - for (int i = current + 1; i < team->chr_cnt; ++i) { - const struct character_t *chr = &team->chrs[i]; + for (int i = current->idx + 1; i < current->team->chr_cnt; ++i) { + struct character_t *chr = ¤t->team->chrs[i]; if (chr->alive) - return i; + return chr; } - for (int i = 0; i <= current; ++i) { - const struct character_t *chr = &team->chrs[i]; + for (int i = 0; i <= current->idx; ++i) { + struct character_t *chr = ¤t->team->chrs[i]; if (chr->alive) - return i; + return chr; } - return -1; + return NULL; } -static int find_prev_team_member(const struct team_t *team, int current) +static struct character_t *find_prev_team_member(const struct character_t *current) { - for (int i = current - 1; i >= 0; --i) { - const struct character_t *chr = &team->chrs[i]; + for (int i = current->idx - 1; i >= 0; --i) { + struct character_t *chr = ¤t->team->chrs[i]; if (chr->alive) - return i; + return chr; } - for (int i = team->chr_cnt - 1; i >= current; ++i) { - const struct character_t *chr = &team->chrs[i]; + for (int i = current->team->chr_cnt - 1; i >= current->idx; ++i) { + struct character_t *chr = ¤t->team->chrs[i]; if (chr->alive) - return i; + return chr; } - return -1; + return NULL; } -static int get_alive_character(const struct team_t *team) +static struct character_t *get_first_alive_character(const struct team_t *team) { for (int i = 0; i < team->chr_cnt; ++i) { if (team->chrs[i].alive) - return i; + return &team->chrs[i]; } - return -1; + return NULL; } /* function called after an action has been performed */ static void update_current_character(struct team_t *t1, struct team_t *t2, struct team_t **playing) { struct team_t *current; - int next; + struct character_t *next; unhighlight_prev_character(*playing); current = *playing; - next = find_next_team_member(current, current->chr_cur); + next = find_next_team_member(¤t->chrs[current->chr_cur]); - if (next == -1) { + if (next == NULL) { /* if our team is dead */ current = *playing = (current == t1) ? t2 : t1; - if (!current->chrs[current->chr_cur].alive) - current->chr_cur = find_next_team_member(current, current->chr_cur); - } else if (next > current->chr_cur) { + if (!current->chrs[current->chr_cur].alive) { + next = find_next_team_member(¤t->chrs[current->chr_cur]); + + /* if both team are dead */ + if (next == NULL) + return; + + current->chr_cur = next->idx; + } + } else if (next->idx > current->chr_cur) { /* we still have some players to use */ - current->chr_cur = next; + current->chr_cur = next->idx; } else { - current->chr_cur = next; + current->chr_cur = next->idx; current = *playing = (current == t1) ? t2 : t1; if (!current->chrs[current->chr_cur].alive) { - current->chr_cur = find_next_team_member(current, current->chr_cur); + next = find_next_team_member(¤t->chrs[current->chr_cur]); - if (current->chr_cur < 0) { - /* there is no alive character in the other team */ + if (next == NULL) { current = *playing = (current == t1) ? t2 : t1; + } else { + current->chr_cur = next->idx; } } } @@ -115,13 +123,14 @@ enum action_state_t select_target(struct action_params_t *params, void *data, SURFACES *surfaces = params->surfaces; POSITIONS *positions = params->positions; /* select our own character because he exists no matter what */ - struct team_t *target_team = params->src->team; struct target_t target; - int selection = target_team->chr_cur; - int new_selection; + struct character_t *new_selection; SDL_Event event; - update_selected_target(surfaces, positions, &target_team->chrs[selection]); + target.is_chr = true; + target.chr = params->src; + + update_selected_target(surfaces, positions, target.chr); SDL_Flip(surfaces->Pecran); for (;;) { @@ -138,44 +147,40 @@ enum action_state_t select_target(struct action_params_t *params, void *data, return ACTION_CANCELED; case SDLK_UP: case SDLK_k: - new_selection = find_prev_team_member(target_team, selection); + new_selection = find_prev_team_member(target.chr); - if (new_selection == selection) + if (new_selection->idx == target.chr->idx) continue; - selection = new_selection; - update_selected_target(surfaces, positions, &target_team->chrs[selection]); + target.chr = new_selection; + update_selected_target(surfaces, positions, target.chr); SDL_Flip(surfaces->Pecran); break; case SDLK_DOWN: case SDLK_j: - new_selection = find_next_team_member(target_team, selection); + new_selection = find_next_team_member(target.chr); - if (new_selection == selection) + if (new_selection->idx == target.chr->idx) continue; - selection = new_selection; - update_selected_target(surfaces, positions, &target_team->chrs[selection]); + target.chr = new_selection; + update_selected_target(surfaces, positions, target.chr); SDL_Flip(surfaces->Pecran); break; case SDLK_LEFT: case SDLK_h: case SDLK_RIGHT: case SDLK_l: - new_selection = get_alive_character((target_team == params->t1) ? params->t2 : params->t1); - if (new_selection < 0) + new_selection = get_first_alive_character((target.chr->team == params->t1) ? params->t2 : params->t1); + if (new_selection == NULL) continue; - selection = new_selection; - target_team = (target_team == params->t1) ? params->t2 : params->t1; - update_selected_target(surfaces, positions, &target_team->chrs[selection]); + target.chr = new_selection; + update_selected_target(surfaces, positions, target.chr); SDL_Flip(surfaces->Pecran); break; case SDLK_RETURN: case SDLK_f: - target.is_chr = true; - target.chr = &target_team->chrs[selection]; - update_selected_target(surfaces, positions, NULL); (*cb)(surfaces, positions, params->src, &target, data); |