From 66a80f534ed84e5117078c3dc111ecb8fec3ffaa Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Thu, 10 Feb 2022 15:20:42 +0100 Subject: [PATCH] generate monster from json --- CMakeLists.txt | 1 + assets-cg/capacites/capacites.txt | 2 +- assets-cg/converters.py | 24 ++++++++++++++++++++++++ assets-cg/monsters/fxconv-metadata.txt | 3 +++ include/battle.h | 3 ++- include/monster.h | 4 ++-- src/battle.c | 21 +++++++++++++++++---- src/capacite.c | 4 ++-- src/monster.c | 23 ++--------------------- 9 files changed, 54 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 705ebc0..b3d4bab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ set(ASSETS_cg assets-cg/uf8x9 assets-cg/capacites/capacites.txt assets-cg/monsters/test.png + assets-cg/monsters/test.json ) fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA) diff --git a/assets-cg/capacites/capacites.txt b/assets-cg/capacites/capacites.txt index 76c1c0f..66f3664 100644 --- a/assets-cg/capacites/capacites.txt +++ b/assets-cg/capacites/capacites.txt @@ -1,3 +1,3 @@ Test capacité;5;3 -Deuxième;10;3 +Deuxième;100;100 Charge;25;15 diff --git a/assets-cg/converters.py b/assets-cg/converters.py index 3465311..9f382e8 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -8,6 +8,9 @@ def convert(input, output, params, target): elif params["custom-type"] == "capacities": convert_capa(input, output, params, target) return 0 + elif params["custom-type"] == "monster": + convert_monster(input, output, params, target) + return 0 else: return 1 @@ -185,3 +188,24 @@ def convert_capa(input, output, params, target): capacities += fxconv.ptr(moves) fxconv.elf(capacities, output, "_" + params["name"], **target) + +def convert_monster(input, output, params, target): + file = open(input, "r") + data = json.load(file) + + stats = fxconv.Structure() + if len(data["stats"]) != 6: raise Exception(f"convert_monster : Les statistiques de {data['name']} sont mauvaises") + for i in data["stats"].values(): + stats+=fxconv.u32(i) + + moves = bytes() + for i in data["moves"]: + moves+=fxconv.u16(i) + + monster = fxconv.Structure() + monster += fxconv.string(data["name"]) + monster += fxconv.ptr(f"img_{data['sprite']}") + monster += fxconv.ptr(stats) + monster += fxconv.ptr(moves) + + fxconv.elf(monster, output, "_" + params["name"], **target) diff --git a/assets-cg/monsters/fxconv-metadata.txt b/assets-cg/monsters/fxconv-metadata.txt index ac5552c..74a2ef7 100644 --- a/assets-cg/monsters/fxconv-metadata.txt +++ b/assets-cg/monsters/fxconv-metadata.txt @@ -2,3 +2,6 @@ test.png: type: bopti-image name: img_test +*.json: + custom-type: monster + name_regex: (.*)\.json monster_\1 diff --git a/include/battle.h b/include/battle.h index f29dbe4..7e3e127 100644 --- a/include/battle.h +++ b/include/battle.h @@ -11,4 +11,5 @@ enum battle_state { void create_battle(struct Game *game); int during_battle(struct Player *player, struct Monster monster); int select_move(struct Player *player, struct Monster *monster, int prec_selected); -void draw_battle(struct Player *player, struct Monster *monster); \ No newline at end of file +void draw_battle(struct Player *player, struct Monster *monster); +void draw_executed_move(struct Move move, struct Monster *monster, int is_monster); \ No newline at end of file diff --git a/include/monster.h b/include/monster.h index 0c4acca..198a831 100644 --- a/include/monster.h +++ b/include/monster.h @@ -7,9 +7,9 @@ struct Monster { char *name; - struct Stats stats; - struct Move moves[NB_PLAYER_MOVES]; bopti_image_t *sprite; + struct Stats *stats; + short *moves; }; struct Monster generate_monster(struct Game *game); \ No newline at end of file diff --git a/src/battle.c b/src/battle.c index 56c8ba0..bc61153 100644 --- a/src/battle.c +++ b/src/battle.c @@ -21,11 +21,13 @@ int during_battle(struct Player *player, struct Monster monster) { draw_battle(player, &monster); dupdate(); selection = select_move(player, &monster, selection); - execute_move(&player->stats, &monster.stats, player->moves[selection]); + draw_executed_move(player->moves[selection], &monster, 0); + wait_for_input(KEY_SHIFT); + execute_move(&player->stats, monster.stats, player->moves[selection]); if(player->stats.pv <= 0) { return LOSE; } - if(monster.stats.pv <= 0) { + if(monster.stats->pv <= 0) { return WIN; } tour++; @@ -73,11 +75,22 @@ void draw_battle(struct Player *player, struct Monster *monster) { dprint(15+WIDTH_HP,15,C_BLACK,"%d/%d", player->stats.pv, player->stats.max_pv); - int posHPmonster = (float)monster->stats.pv / monster->stats.max_pv * WIDTH_HP; + int posHPmonster = (float)monster->stats->pv / monster->stats->max_pv * WIDTH_HP; dtext(240,2,C_BLACK,monster->name); drect(240,15,240+WIDTH_HP,25,C_BLACK); drect(240,15,240+posHPmonster,25,C_GREEN); - dprint(245+WIDTH_HP,15,C_BLACK,"%d/%d", monster->stats.pv, monster->stats.max_pv); + dprint(245+WIDTH_HP,15,C_BLACK,"%d/%d", monster->stats->pv, monster->stats->max_pv); dimage(260,30,monster->sprite); +} + +void draw_executed_move(struct Move move, struct Monster *monster, int is_monster) { + const int rect_size = 100; + drect(0,DHEIGHT,DWIDTH,DHEIGHT-rect_size,C_WHITE); + if(is_monster) { + dprint(10,DHEIGHT-rect_size/2-8, C_BLACK, "%s lance %s !", monster->name, move.name); + } else { + dprint(10,DHEIGHT-rect_size/2-8, C_BLACK, "Vous lancez %s !", move.name); + } + dupdate(); } \ No newline at end of file diff --git a/src/capacite.c b/src/capacite.c index c9d2d16..ef610ea 100644 --- a/src/capacite.c +++ b/src/capacite.c @@ -29,6 +29,6 @@ void draw_classic_move(int x, int y, struct Move move) { } void execute_move(struct Stats *player_stats, struct Stats *monster_stats, struct Move move) { - // player_stats->pv-=move.atk; - monster_stats->pv-=move.atk; + player_stats->pv-=move.atk; + //monster_stats->pv-=move.atk; } \ No newline at end of file diff --git a/src/monster.c b/src/monster.c index cf6ddba..764e2d4 100644 --- a/src/monster.c +++ b/src/monster.c @@ -4,25 +4,6 @@ struct Monster generate_monster(struct Game *game) { - extern bopti_image_t img_test; - - struct Stats s = { - .atk=10, - .def=10, - .pv=20, - .level=1, - .max_pv=20, - }; - - struct Monster m = { - .name="pas content", - .stats = s, - .sprite = &img_test, - }; - - m.moves[0] = get_move_id(0); - m.moves[1] = get_move_id(1); - m.moves[2] = get_move_id(2); - - return m; + extern struct Monster monster_test; + return monster_test; } \ No newline at end of file