#include #include #include #include "structures.h" #include "constantes.h" #include "prototypes.h" #include "priv_entries.h" static inline void highlight_current_character(struct team_t *team) { struct character_t *chr = &team->chrs[team->chr_cur]; chr->surf = chr->red_surf; } static inline void unhighlight_prev_character(struct team_t *team) { struct character_t *chr = &team->chrs[team->chr_cur]; chr->surf = chr->def_surf; } static int find_next_ally(const struct team_t *ally) { for (int i = ally->chr_cur + 1; i < ally->chr_cnt; ++i) { const struct character_t *chr = &ally->chrs[i]; if (chr->alive) return i; } for (int i = 0; i <= ally->chr_cur; ++i) { const struct character_t *chr = &ally->chrs[i]; if (chr->alive) return i; } return -1; } /* function called after an action has been performed */ static void update_current_character(struct team_t *ally, bool *ally_turn) { if (*ally_turn) { int next; unhighlight_prev_character(ally); next = find_next_ally(ally); /* if there is no next ally or they are dead */ if (next <= ally->chr_cur) { inverse_boolean(*ally_turn); } ally->chr_cur = next; if (*ally_turn) { highlight_current_character(ally); } } } enum action_state_t dig_entry(const struct entry_t *entries, int cnt_entries, struct action_params_t *params) { SDL_Event event; const struct entry_t *target; int selection = 0; update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection); for (;;) { SDL_WaitEvent(&event); if (event.type != SDL_KEYDOWN) continue; switch (event.key.keysym.sym) { case SDLK_a: case SDLK_ESCAPE: update_list_entries(params->surfaces, params->positions, NULL, 0, -1); return ACTION_CANCELED; case SDLK_k: case SDLK_UP: selection = (selection > 0) ? selection - 1 : cnt_entries - 1; update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection); break; case SDLK_j: case SDLK_DOWN: selection = (selection < cnt_entries - 1) ? selection + 1 : 0; update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection); break; case SDLK_f: case SDLK_RETURN: target = &entries[selection]; if (!target->children_cnt) { return target->f(params->surfaces, params->positions, params->t1, params->t2, target->data); } if (dig_entry(target->children, target->children_cnt, params) == ACTION_PERFORMED) return ACTION_PERFORMED; update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection); default: break; } } } int Fjouer (SURFACES *surfaces, POSITIONS *positions, struct team_t *ally, struct team_t *enemy) { unsigned int continuer=1; int i; int Vtourennemi=0; int Vtour; int Vnbennemis=0; unsigned int gagne,perdu; if(surfaces->Pfondjeu!=NULL) { SDL_FreeSurface(surfaces->Pfondjeu); surfaces->Pfondjeu=NULL; } Fblitterfond(surfaces); // on blit le fond du jeu Fremplirobjets(&ally->objects); // on monte les variables potions ether, etc /* compute whether the allies or the enemies should begin */ Vtour = rand() % 2; if (Vtour == ALLIE) { /* the current character will be highlighted in red */ /* TODO should be generic for an enemy or an ally */ highlight_current_character(ally); } blit_team(surfaces, ally); blit_team(surfaces, enemy); SDL_Flip(surfaces->Pecran); while (continuer) { if(Vtour==ALLIE) //si un player joue { struct action_params_t params = { .surfaces = surfaces, .positions = positions, .t1 = ally, .t2 = enemy, }; enum action_state_t state; while (!ally->chrs[ally->chr_cur].alive) //si le perso selectionné est mort { if (ally->chr_cur < ally->chr_cnt) // si ce n'est pas le dernier ally->chr_cur++; // on prend le perso suivant else // sinon si c'est le dernier ally->chr_cur = 0; // on reprend le 1er } state = dig_entry(action_entries_g, countof(action_entries_g), ¶ms); switch (state) { case ACTION_CANCELED: continuer = 0; break; case ACTION_PERFORMED: update_current_character(ally, (bool *)&Vtour); blit_team(surfaces, ally); break; default: break; } } else if(Vtour==ENNEMI) // sinon si c'est le cpu qui joue { while (!enemy->chrs[Vtourennemi].alive) { if (Vtourennemi < Vnbennemis) { Vtourennemi++; } else if (Vtourennemi == Vnbennemis) { Vtourennemi = 0; } } /* TODO reactivate */ #if 0 Factionennemi(&Vtourennemi,surfaces,positions,ennemis,persos,Vnbennemis,&Vtour,Vtourallie); #else Vtour = ALLIE; #endif } // les actions sont faites // on vérifie à présent si on a gagné ou si on a perdu ! gagne=1; perdu=1; for(i = 0; i <= enemy->chr_cnt; i++) { if (enemy->chrs[i].alive) gagne=0; } for (i = 0; i < ally->chr_cnt; i++) { if (ally->chrs[i].alive) perdu=0; } // la y'a des trucs a mettre pour si on gagne ou on perd parce que sa fait assez pitié :p if(perdu) { SDL_Delay(2000); continuer=0; } else if(gagne) { SDL_Delay(2000); continuer=0; } } return 0; } void Fremplirobjets(OBJET *objets) { objets->potions=10; objets->ethers=10; objets->potionsplus=5; objets->ethersplus=5; }