Browse Source

only handle event once for dialog

master
bgiraudr 1 month ago
parent
commit
c45f31bd33
  1. 48
      assets-cg/converters.py
  2. 2
      assets-cg/maps/testCarte.tmx
  3. 3
      include/define.h
  4. 5
      include/player.h
  5. 11
      include/talkable.h
  6. 7
      src/engine.c
  7. 21
      src/player.c
  8. 15
      src/talkable.c

48
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):

2
assets-cg/maps/testCarte.tmx

@ -190,8 +190,10 @@
</object>
<object id="62" gid="267" x="448" y="432" width="16" height="16">
<properties>
<property name="exclusive" type="bool" value="false"/>
<property name="name" value="Pancarte"/>
<property name="text" value="Cadeau;~type:Administrateur"/>
<property name="text2" value="Hop hop hop, tu as déjà reçu mon cadeau"/>
</properties>
</object>
</objectgroup>

3
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
#define NB_STO_ZONE 300
#define NB_STO_DIALOG 300

5
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);
bool check_eventzone(struct Player *player, int id);
int get_nb_eventdialog(struct Player *player);
bool check_eventdialog(struct Player *player, int id);

11
include/talkable.h

@ -1,20 +1,23 @@
#pragma once
#include "map.h"
#include "game.h"
#include <stdbool.h>
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, ...);
void addDialogToPlayer(struct Player *player, int id);

7
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);
}
}
}
}

21
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;
}

15
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;
}
Loading…
Cancel
Save