From 367c61effd473bb456303d09031a33c95d5b9094 Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Sun, 22 May 2022 16:46:31 +0200 Subject: [PATCH] Only execute event once for area --- assets-cg/converters.py | 18 +++++--- assets-cg/maps/1.tmx | 5 ++- assets-cg/maps/inside/1.tmx | 5 ++- assets-cg/maps/inside/2.tmx | 5 ++- assets-cg/maps/testCarte.tmx | 3 ++ include/capacite.h | 6 +-- include/define.h | 5 +-- include/map.h | 4 +- include/player.h | 7 ++- include/util.h | 3 +- src/battle.c | 10 +++-- src/capacite.c | 14 +++--- src/engine.c | 7 ++- src/map.c | 4 ++ src/player.c | 87 +++++++++++++++++------------------- src/util.c | 38 ++++++++++++++++ 16 files changed, 146 insertions(+), 75 deletions(-) diff --git a/assets-cg/converters.py b/assets-cg/converters.py index e3bf888..0988110 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -22,8 +22,6 @@ def convert(input, output, params, target): elif params["custom-type"] == "table_type": convert_table_type(input, output, params, target) return 0 - elif params["custom-type"] == "test": - test(input, output, params, target) else: return 1 @@ -93,6 +91,11 @@ def convert_map(input, output, params, target): teleporter = fxconv.Structure() zone = fxconv.Structure() + try: + idmap = data["properties"][0]["value"] + except KeyError: + raise Exception("La carte n'a pas d'identifiant") + for layer in objectLayers: if layer.get("name") == DIALOG_LAYOUT: nbDialog = len(layer["objects"]) @@ -102,7 +105,7 @@ def convert_map(input, output, params, target): teleporter = parseTeleporter(layer) elif layer.get("name") == ZONE_LAYOUT: nbZone = len(layer["objects"]) - zone = parseZone(layer) + zone = parseZone(layer, idmap) else: print("UNKNOWN LAYER FOUND : " + layer.get("name")) @@ -183,17 +186,22 @@ def parseTeleporter(layer): raise Exception("parseTeleporter() : Un téléporteur est mal configuré") return teleporter -def parseZone(layer): +def parseZone(layer, idmap): zone = fxconv.Structure() + idZone = 0 for i in layer["objects"]: origin = (int(i['x']/16), int(i['y']/16)) to = (int(origin[0]+i['width']/16)-1, int(origin[1]+i['height']/16)-1) - + if len(i["properties"][0]["value"]) != 0: + zone += fxconv.u32(int(f"{idmap}{idZone}{idmap}")) #id zone is {mapid}{idzone}{mapid} + idZone+=1 + else: zone += fxconv.u32(0) zone += fxconv.u32(origin[0]) zone += fxconv.u32(origin[1]) zone += fxconv.u32(to[0]) zone += fxconv.u32(to[1]) + event = bytes(i["properties"][0]["value"], "utf-8") event += bytes(128 - len(event)) zone += event #event diff --git a/assets-cg/maps/1.tmx b/assets-cg/maps/1.tmx index cfe258f..68b9543 100644 --- a/assets-cg/maps/1.tmx +++ b/assets-cg/maps/1.tmx @@ -1,8 +1,11 @@ - + + + + diff --git a/assets-cg/maps/inside/1.tmx b/assets-cg/maps/inside/1.tmx index d183916..e9433f6 100644 --- a/assets-cg/maps/inside/1.tmx +++ b/assets-cg/maps/inside/1.tmx @@ -1,8 +1,11 @@ - + + + + diff --git a/assets-cg/maps/inside/2.tmx b/assets-cg/maps/inside/2.tmx index 29be832..d25b981 100644 --- a/assets-cg/maps/inside/2.tmx +++ b/assets-cg/maps/inside/2.tmx @@ -1,8 +1,11 @@ - + + + + diff --git a/assets-cg/maps/testCarte.tmx b/assets-cg/maps/testCarte.tmx index 373e8cb..a542319 100644 --- a/assets-cg/maps/testCarte.tmx +++ b/assets-cg/maps/testCarte.tmx @@ -3,6 +3,9 @@ + + + diff --git a/include/capacite.h b/include/capacite.h index 793d87d..f193098 100644 --- a/include/capacite.h +++ b/include/capacite.h @@ -25,7 +25,6 @@ struct Capacities { enum status { MISS, SUCCESS, - CRIT, HEAL, ATK, DEF, @@ -43,7 +42,7 @@ enum categorie { struct Move default_move(); struct Move get_move_id(int id); -void draw_move(int x, int y, int x2, int y2, struct Move *move); +void draw_move(int x, int y, int x2, int y2, struct Move *move, int selected); void draw_classic_move(int x, int y, struct Move *move); int execute_move(struct Stats *player_stats, struct Stats *monster_stats, struct Move *move, int ismonster); int calc_damage(struct Stats *attacker, struct Stats *target, struct Move *move); @@ -53,4 +52,5 @@ float crit(struct Stats *attacker); int is_crit(); int is_miss(struct Move *move); int self_effect(struct Stats *stats, struct Move *move); -float stab(char *type, char *move); \ No newline at end of file +float stab(char *type, char *move); +void draw_special_move(int x, int y, struct Move *move, int selected); \ No newline at end of file diff --git a/include/define.h b/include/define.h index d70b41e..1060fb1 100644 --- a/include/define.h +++ b/include/define.h @@ -1,9 +1,8 @@ #pragma once /*the size of one tile*/ #define TILE_SIZE 16 - #define NB_INTERIORS 2 - #define NB_PLAYER_MOVES 3 #define NB_PLAYER_ITEMS 30 -#define NB_TYPES 4 \ No newline at end of file +#define NB_TYPES 4 +#define NB_STO_ZONE 300 \ No newline at end of file diff --git a/include/map.h b/include/map.h index 1f4b511..6b5ac87 100644 --- a/include/map.h +++ b/include/map.h @@ -9,6 +9,7 @@ struct Teleporter { }; struct Zone { + int id; int start_x, start_y; int end_x, end_y; char event[128]; @@ -58,4 +59,5 @@ struct Vec2 locate_tile(struct Map const *map, int tile); struct Teleporter get_teleporter_xy(struct Map *map, struct Vec2 pos); int is_in_zone(struct Player *player, struct Map *map); -struct Zone get_zone(struct Player *player, struct Map *map); \ No newline at end of file +struct Zone get_zone(struct Player *player, struct Map *map); +void addZoneToPlayer(struct Player *player, struct Zone zone); \ No newline at end of file diff --git a/include/player.h b/include/player.h index f975fe6..2507448 100644 --- a/include/player.h +++ b/include/player.h @@ -32,6 +32,7 @@ struct Player { /*the current animation*/ int idle; struct AnimData anim; + int eventListZone[NB_STO_ZONE]; }; struct LevelUp { @@ -50,7 +51,7 @@ struct Player init_player(void); void add_move(struct Player *player, struct Move move); void draw_player_moves(struct Player *player); void replace_capacities(struct Player *player, struct Move move); -void draw_ui(struct Player *player); +void draw_ui(struct Player *player, int curr_select); int get_nb_moves(struct Player *player); void reset_pp(struct Player *player); void check_level(struct Player *player, int prec_level); @@ -58,4 +59,6 @@ void add_xp(struct Player *player, int xp); int select_capacity(struct Player *player, char* context, bool allow_back); void add_pp(struct Player *player, int amount); void change_type(struct Player *player, struct Type type); -int yes_no_question(char const *format, ...); \ No newline at end of file +bool has_move(struct Player *player, struct Move move); +int get_nb_eventzone(struct Player *player); +bool check_eventzone(struct Player *player, int id); \ No newline at end of file diff --git a/include/util.h b/include/util.h index f8a00c7..90be77d 100644 --- a/include/util.h +++ b/include/util.h @@ -5,4 +5,5 @@ void wait_for_input(int input); int rand_range(int low, int high); void format_text_opt(int x, int y, int width, int height, const int color, char const *format, ...); char *word_boundary_before(char *str, char *limit); -char *skip_spaces(char *str); \ No newline at end of file +char *skip_spaces(char *str); +int yes_no_question(char const *format, ...); \ No newline at end of file diff --git a/src/battle.c b/src/battle.c index 489e5a9..d0a241c 100644 --- a/src/battle.c +++ b/src/battle.c @@ -76,9 +76,6 @@ void check_move_status(int status, struct Player *player, struct Monster *monste draw_battle(player, monster); switch(status){ - case CRIT: - draw_status(name, "réalise un coup critique !"); - break; case MISS: draw_status(name, "rate son attaque !"); break; @@ -108,6 +105,11 @@ void check_move_status(int status, struct Player *player, struct Monster *monste dupdate(); wait_for_input(KEY_SHIFT); } + if(is_crit() && (status == LESS_EFFECTIVE || status == SUPER_EFFECTIVE)) { + draw_status(name, "réalise un coup critique !"); + dupdate(); + wait_for_input(KEY_SHIFT); + } } /*When a battle is finish, compute xp gain and gain level*/ @@ -153,7 +155,7 @@ int select_move(struct Player *player, struct Monster *monster, int prec_selecte dclear(C_RGB(25,25,25)); draw_battle(player, monster); - draw_ui(player); + draw_ui(player, selection); dtext(58 + (selection * 130), DHEIGHT-15 , C_RED, "[X]"); dupdate(); diff --git a/src/capacite.c b/src/capacite.c index 5c60ff9..b95da4e 100644 --- a/src/capacite.c +++ b/src/capacite.c @@ -49,12 +49,13 @@ struct Move *copy_move(struct Move move) { return copyMove; } -void draw_move(int x, int y, int x2, int y2, struct Move *move) { +void draw_move(int x, int y, int x2, int y2, struct Move *move, int selected) { extern bopti_image_t img_capacite; extern bopti_image_t img_categories; const int font_size = 8; - draw_change_one_color(x, y, &img_capacite, 0xE6D6, getTypeFromName(move->type).color); + if(!selected) draw_change_one_color(x, y, &img_capacite, 0xE6D6, getTypeFromName(move->type).color); + else draw_change_one_color(x, y, &img_capacite, 0xE6D6, 0x0FE0); dsubimage(x+96, y+7, &img_categories, 20*move->categorie, 0, 20, 10, DIMAGE_NONE); int color = move->pp > 0 ? C_BLACK : C_RED; @@ -73,8 +74,12 @@ void draw_move(int x, int y, int x2, int y2, struct Move *move) { } } +void draw_special_move(int x, int y, struct Move *move, int selected) { + draw_move(x, y, x+125, y+60, move, selected); +} + void draw_classic_move(int x, int y, struct Move *move) { - draw_move(x, y, x+125, y+60, move); + draw_move(x, y, x+125, y+60, move, 0); } int execute_move(struct Stats *player_stats, struct Stats *monster_stats, struct Move *move, int ismonster) { @@ -98,7 +103,6 @@ int execute_move(struct Stats *player_stats, struct Stats *monster_stats, struct if(typeEffect == 2) return SUPER_EFFECTIVE; if(typeEffect == 0.5) return LESS_EFFECTIVE; if(typeEffect == 0) return NOT_EFFECTIVE; - if(is_crit()) return CRIT; } else { if(ismonster) { return self_effect(monster_stats, move); @@ -137,7 +141,7 @@ float stab(char *type, char *move) { int is_crit() { //une chance sur 16 d'avoir un coup critique - const int proba_crit = 16; + const int proba_crit = 2; return rand_range(0,proba_crit)==0; } diff --git a/src/engine.c b/src/engine.c index 5885c2d..e0bb6f5 100644 --- a/src/engine.c +++ b/src/engine.c @@ -152,7 +152,12 @@ void engine_check_position(struct Game *game) { static struct Vec2 old_pos; if(is_in_zone(game->player, game->map)) { struct Zone zone = get_zone(game->player, game->map); - if(strcmp(zone.event, "")) handle_event(game, zone.event); + if(!check_eventzone(game->player, zone.id)) { + if(strcmp(zone.event, "")) { + addZoneToPlayer(game->player, zone); + handle_event(game, zone.event); + } + } } int player_curr_tile = map_get_player_tile(game); diff --git a/src/map.c b/src/map.c index 51de169..f780667 100644 --- a/src/map.c +++ b/src/map.c @@ -95,4 +95,8 @@ struct Zone get_zone(struct Player *player, struct Map *map) { if(zone.start_x <= posx && zone.start_y <= posy && zone.end_x >= posx && zone.end_y >= posy) return zone; } return default_zone; +} + +void addZoneToPlayer(struct Player *player, struct Zone zone) { + player->eventListZone[get_nb_eventzone(player)] = zone.id; } \ No newline at end of file diff --git a/src/player.c b/src/player.c index 57e2bcc..d3914ce 100644 --- a/src/player.c +++ b/src/player.c @@ -98,18 +98,32 @@ int get_nb_moves(struct Player *player) { return NB_PLAYER_MOVES; } +bool has_move(struct Player *player, struct Move move) { + int index = get_nb_moves(player); + for(int i = 0; i < index; i++) { + if(player->moves[i]->id == move.id) return true; + } + return false; +} + void add_move(struct Player *player, struct Move move) { int index = get_nb_moves(player); - if(index != NB_PLAYER_MOVES) { - draw_text(50, DHEIGHT-47, C_BLACK, "Vous apprenez %s !", move.name); - dupdate(); - wait_for_input(KEY_SHIFT); - player->moves[index] = copy_move(move); + if(!has_move(player, move)) { + if(index != NB_PLAYER_MOVES) { + draw_text(50, DHEIGHT-47, C_BLACK, "Vous apprenez %s !", move.name); + dupdate(); + wait_for_input(KEY_SHIFT); + player->moves[index] = copy_move(move); + } else { + draw_text(50, DHEIGHT-47, C_BLACK, "Vous pouvez apprendre %s !", move.name); + dupdate(); + wait_for_input(KEY_SHIFT); + replace_capacities(player, move); + } } else { - draw_text(50, DHEIGHT-47, C_BLACK, "Vous pouvez apprendre %s !", move.name); + draw_text(50, DHEIGHT-47, C_BLACK, "Vous connaissez déjà la capacité %s !", move.name); dupdate(); wait_for_input(KEY_SHIFT); - replace_capacities(player, move); } } @@ -184,11 +198,11 @@ int select_capacity(struct Player *player, char* context, bool allow_back) { return selection; } -void draw_ui(struct Player *player) { +void draw_ui(struct Player *player, int curr_select) { int index = get_nb_moves(player); for(int i = 0; i < index; i++) { - draw_classic_move(2+132*i,DHEIGHT-70, player->moves[i]); + draw_special_move(2+132*i,DHEIGHT-70, player->moves[i], i == curr_select); } } @@ -226,44 +240,6 @@ void add_pp(struct Player *player, int amount) { wait_for_input(KEY_SHIFT); } -int yes_no_question(char const *format, ...) { - char text_arg[512]; - va_list args; - va_start(args, format); - vsnprintf(text_arg, 512, format, args); - va_end(args); - - int selection = 0; - int buffer = keydown(KEY_SHIFT); - while(1) { - clearevents(); - dclear(C_WHITE); - - selection += keydown(KEY_RIGHT) - keydown(KEY_LEFT); - if(selection > 1) selection = 1; - if(selection < 0) selection = 0; - - format_text_opt(95,10, 200, 13, C_BLACK, text_arg); - - dtext(95,150,C_BLACK, "NON"); - dtext(285,150,C_BLACK, "OUI"); - - dtext(95 + (selection * 190), DHEIGHT-47, C_RED, "[X]"); - dupdate(); - - if(keydown(KEY_SHIFT)) { - if(buffer) buffer = 0; - else break; - } - if(keydown(KEY_EXIT)) { - selection = 0; - break; - } - while(keydown_any(KEY_LEFT,KEY_RIGHT, KEY_SHIFT,0)) clearevents(); - } - return selection; -} - void change_type(struct Player *player, struct Type type) { if(strcmp(player->stats.type, type.name) != 0) { int selection = yes_no_question("Voulez vous changer votre type %s en %s ?", player->stats.type, type.name); @@ -273,4 +249,21 @@ void change_type(struct Player *player, struct Type type) { dupdate(); wait_for_input(KEY_SHIFT); } +} + +int get_nb_eventzone(struct Player *player) { + for(int i = 0; i < 300; i++) { + if(player->eventListZone[i] == 0) { + return i; + } + } + return 0; +} + +bool check_eventzone(struct Player *player, int id) { + for(int i = 0; i < 300; i++) { + if(player->eventListZone[i] == id) return true; + if(player->eventListZone[i] == 0) return false; + } + return false; } \ No newline at end of file diff --git a/src/util.c b/src/util.c index cf659f3..73901c3 100644 --- a/src/util.c +++ b/src/util.c @@ -66,4 +66,42 @@ void format_text_opt(int x, int y, int width, int height, const int color, char text = skip_spaces(last_word); y += LINE_HEIGHT; } +} + +int yes_no_question(char const *format, ...) { + char text_arg[512]; + va_list args; + va_start(args, format); + vsnprintf(text_arg, 512, format, args); + va_end(args); + + int selection = 0; + int buffer = keydown(KEY_SHIFT); + while(1) { + clearevents(); + dclear(C_WHITE); + + selection += keydown(KEY_RIGHT) - keydown(KEY_LEFT); + if(selection > 1) selection = 1; + if(selection < 0) selection = 0; + + format_text_opt(95,10, 200, 13, C_BLACK, text_arg); + + dtext(95,150,C_BLACK, "NON"); + dtext(285,150,C_BLACK, "OUI"); + + dtext(95 + (selection * 190), DHEIGHT-47, C_RED, "[X]"); + dupdate(); + + if(keydown(KEY_SHIFT)) { + if(buffer) buffer = 0; + else break; + } + if(keydown(KEY_EXIT)) { + selection = 0; + break; + } + while(keydown_any(KEY_LEFT,KEY_RIGHT, KEY_SHIFT,0)) clearevents(); + } + return selection; } \ No newline at end of file