From c45f31bd33cc52ee05c5518b2001aea71b7b1d73 Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Sun, 22 May 2022 23:58:00 +0200 Subject: [PATCH] only handle event once for dialog --- assets-cg/converters.py | 48 +++++++++++++++++++++++++++++++++--- assets-cg/maps/testCarte.tmx | 2 ++ include/define.h | 3 ++- include/player.h | 5 +++- include/talkable.h | 11 ++++++--- src/engine.c | 7 ++++-- src/player.c | 21 ++++++++++++++-- src/talkable.c | 15 ++++++++++- 8 files changed, 97 insertions(+), 15 deletions(-) diff --git a/assets-cg/converters.py b/assets-cg/converters.py index 0988110..59f9e15 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -1,3 +1,4 @@ +from random import randint import fxconv import json import pathlib @@ -99,7 +100,7 @@ def convert_map(input, output, params, target): for layer in objectLayers: if layer.get("name") == DIALOG_LAYOUT: nbDialog = len(layer["objects"]) - dialogs = parseDialog(layer) + dialogs = parseDialog(layer, idmap) elif layer.get("name") == TELEPORTER_LAYOUT: nbTelep = len(layer["objects"]) teleporter = parseTeleporter(layer) @@ -151,20 +152,59 @@ def convert_map(input, output, params, target): #generate ! fxconv.elf(structMap, output, "_" + params["name"], **target) -def parseDialog(layer): +def parseDialog(layer, idmap): dialogs = fxconv.Structure() + idDialog = 0 + + base_dialog = [ + "Encore toi ? Tu n'as pas quelque chose d'autre à faire ?", + "Mais... Tu vas me lacher oui ?", + "re-bonjour, comment vas-tu depuis la dernière fois ?", + "Tu reviens me voir après tout ce temps ?", + "Toujours un plaisir de te revoir.", + "La vie est pleine de surprise, je ne m'attendais pas à te revoir !", + "Salut ! Belle journée n'est-ce pas ?", + "Il faut savoir apprécier les bonnes choses de la vie.", + "La dernière fois je suis tombé sur une horde de monstre, quelle panique !", + "As-tu visité notre belle région depuis la dernière fois ?", + "Prend le temps, on n'a qu'une seule vie n'est-ce pas !", + "Pour être honnête, je ne t'apprécie pas beaucoup." + ] + + for i in layer["objects"]: dialogs += fxconv.u32(int(i["x"]/i["width"])) #Tiled seem to start at the bottom y of the object dialogs += fxconv.u32(int(i["y"]/i["width"])-1) try: + dialogs += fxconv.u32(int(f"{idmap}{idDialog}{idmap}")) + listProper = set((a['name']) for a in i["properties"]) + idDialog += 1 + stoText = "" for j in i["properties"]: - if(j["value"] == ""): j["value"] = " " - dialogs += fxconv.string(j["value"]) + if(j["name"] == "name"): dialogs += fxconv.string(j["value"]) + if(j["name"] == "text"): + dialogs += fxconv.string(j["value"]) + stoText = j["value"] + if(j["name"] == "text2"): dialogs += fxconv.string(j["value"]) + if not "text2" in listProper: + if "~" in stoText: dialogs += fxconv.string(base_dialog[randint(0, len(base_dialog)-1)]) + else: dialogs += fxconv.string("") + if("exclusive" in listProper): + for j in i["properties"]: + if(j["name"] == "exclusive"): + if j["value"]: dialogs += fxconv.u32(1) + else: dialogs += fxconv.u32(0) + else: + if "~" in stoText: dialogs += fxconv.u32(1) + else: dialogs += fxconv.u32(0) except KeyError: dialogs += fxconv.string("default name") dialogs += fxconv.string("default text") + dialogs += fxconv.string("default text2") + dialogs += fxconv.u32(0) + return dialogs def parseTeleporter(layer): diff --git a/assets-cg/maps/testCarte.tmx b/assets-cg/maps/testCarte.tmx index a542319..7431556 100644 --- a/assets-cg/maps/testCarte.tmx +++ b/assets-cg/maps/testCarte.tmx @@ -190,8 +190,10 @@ + + diff --git a/include/define.h b/include/define.h index 1060fb1..719e612 100644 --- a/include/define.h +++ b/include/define.h @@ -5,4 +5,5 @@ #define NB_PLAYER_MOVES 3 #define NB_PLAYER_ITEMS 30 #define NB_TYPES 4 -#define NB_STO_ZONE 300 \ No newline at end of file +#define NB_STO_ZONE 300 +#define NB_STO_DIALOG 300 \ No newline at end of file diff --git a/include/player.h b/include/player.h index 2507448..35fd58b 100644 --- a/include/player.h +++ b/include/player.h @@ -33,6 +33,7 @@ struct Player { int idle; struct AnimData anim; int eventListZone[NB_STO_ZONE]; + int eventListDialog[NB_STO_DIALOG]; }; struct LevelUp { @@ -61,4 +62,6 @@ void add_pp(struct Player *player, int amount); void change_type(struct Player *player, struct Type type); 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 +bool check_eventzone(struct Player *player, int id); +int get_nb_eventdialog(struct Player *player); +bool check_eventdialog(struct Player *player, int id); \ No newline at end of file diff --git a/include/talkable.h b/include/talkable.h index df201e2..c79cc3a 100644 --- a/include/talkable.h +++ b/include/talkable.h @@ -1,20 +1,23 @@ #pragma once #include "map.h" #include "game.h" +#include struct Talkable { /*the position of the tile*/ - int x, y; + int x, y, id; /*the name*/ char *name; /*the text to display*/ char *text; + /*the text to display after the action*/ + char *text_after; + /*if event, repeat it or no*/ + int exclusive; }; /*draw the dialog of a specified talkable tile*/ void draw_dialog(struct Game *game); /*find the talkable tile using the player's position*/ struct Talkable* get_dialog_xy(struct Map *map, int x, int y); -char *word_boundary_before(char *str, char *limit); -char *skip_spaces(char *str); -void format_text_opt(int x, int y, int width, int height, const int color, char const *format, ...); \ No newline at end of file +void addDialogToPlayer(struct Player *player, int id); \ No newline at end of file diff --git a/src/engine.c b/src/engine.c index e0bb6f5..f9dbf5f 100644 --- a/src/engine.c +++ b/src/engine.c @@ -32,6 +32,8 @@ void engine_draw(struct Game const *game) { dprint(1,1,C_WHITE,"%d:%d",game->player->pos.x, game->player->pos.y); dprint(1,20,C_WHITE,"%d",game->player->sprint); + dprint(1,40,C_WHITE,"%d",get_nb_eventdialog(game->player)); + dprint(1,60,C_WHITE,"%d",game->player->eventListDialog[get_nb_eventdialog(game->player)-1 > 0 ? get_nb_eventdialog(game->player)-1 : 0]); } void engine_draw_map(struct Game const *game) { @@ -154,8 +156,9 @@ void engine_check_position(struct Game *game) { struct Zone zone = get_zone(game->player, game->map); if(!check_eventzone(game->player, zone.id)) { if(strcmp(zone.event, "")) { - addZoneToPlayer(game->player, zone); - handle_event(game, zone.event); + if(handle_event(game, zone.event)) { + addZoneToPlayer(game->player, zone); + } } } } diff --git a/src/player.c b/src/player.c index d3914ce..c7a0abc 100644 --- a/src/player.c +++ b/src/player.c @@ -252,7 +252,7 @@ void change_type(struct Player *player, struct Type type) { } int get_nb_eventzone(struct Player *player) { - for(int i = 0; i < 300; i++) { + for(int i = 0; i < NB_STO_ZONE; i++) { if(player->eventListZone[i] == 0) { return i; } @@ -260,10 +260,27 @@ int get_nb_eventzone(struct Player *player) { return 0; } +int get_nb_eventdialog(struct Player *player) { + for(int i = 0; i < NB_STO_DIALOG; i++) { + if(player->eventListDialog[i] == 0) { + return i; + } + } + return 0; +} + bool check_eventzone(struct Player *player, int id) { - for(int i = 0; i < 300; i++) { + for(int i = 0; i < NB_STO_ZONE; i++) { if(player->eventListZone[i] == id) return true; if(player->eventListZone[i] == 0) return false; } return false; +} + +bool check_eventdialog(struct Player *player, int id) { + for(int i = 0; i < NB_STO_DIALOG; i++) { + if(player->eventListDialog[i] == id) return true; + if(player->eventListDialog[i] == 0) return false; + } + return false; } \ No newline at end of file diff --git a/src/talkable.c b/src/talkable.c index 74d1167..8ca983f 100644 --- a/src/talkable.c +++ b/src/talkable.c @@ -17,7 +17,9 @@ struct Talkable default_value = { .x = 0, .y = 0, .name = "default name", - .text = "default dialog" + .text = "default dialog", + .text_after = "", + .exclusive = 0 }; /*draw the dialog of a specified talkable tile*/ @@ -30,6 +32,13 @@ void draw_dialog(struct Game *game) { struct Talkable *talk = get_dialog_xy(game->map, game->player->pos.x + dx, game->player->pos.y + dy); char *str = strdup(talk->text); + if(talk->exclusive) { + if(check_eventdialog(game->player, talk->id)) { + str = strdup(talk->text_after); + } else { + addDialogToPlayer(game->player, talk->id); + } + } char *curr_line = strtok(str, delim); while(curr_line != NULL) { @@ -54,4 +63,8 @@ struct Talkable* get_dialog_xy(struct Map *map, int x, int y) { i++; } return &default_value; +} + +void addDialogToPlayer(struct Player *player, int id) { + player->eventListDialog[get_nb_eventdialog(player)] = id; } \ No newline at end of file