events in dialogs

This commit is contained in:
bgiraudr 2022-04-02 01:57:54 +02:00
parent 7a4f2c5ff3
commit 790af39fd2
10 changed files with 113 additions and 47 deletions

View File

@ -44,6 +44,7 @@ set(SOURCES
src/stats.c
src/capacite.c
src/monster.c
src/event.c
)
set(ASSETS_cg

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.8" tiledversion="1.8.2" orientation="orthogonal" renderorder="right-down" width="100" height="38" tilewidth="16" tileheight="16" infinite="0" nextlayerid="14" nextobjectid="61">
<map version="1.8" tiledversion="1.8.4" orientation="orthogonal" renderorder="right-down" width="100" height="38" tilewidth="16" tileheight="16" infinite="0" nextlayerid="14" nextobjectid="62">
<editorsettings>
<export target="testCarte.json" format="json"/>
</editorsettings>
@ -73,8 +73,8 @@
320,321,322,0,0,0,0,0,0,0,0,0,0,306,249,249,108,0,140,80,81,0,0,0,0,0,0,0,0,0,0,0,108,0,378,379,380,0,0,0,0,0,0,0,49,50,0,0,0,0,22,23,0,0,0,0,0,0,0,0,0,0,0,0,0,111,0,0,0,0,0,0,0,0,0,0,0,303,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
349,350,351,0,0,0,0,0,0,0,0,0,0,0,0,0,137,138,84,0,110,249,249,249,249,249,249,249,250,249,251,0,108,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,51,52,0,0,0,0,0,0,0,0,320,321,322,0,82,139,0,0,0,0,0,0,323,324,325,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
378,379,380,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,137,138,139,0,0,0,0,0,0,0,0,0,280,0,137,138,138,138,138,138,84,0,0,0,174,174,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,349,350,351,0,111,0,0,0,0,0,0,0,352,353,354,0,0,0,0,0,0,0,0,0,323,324,325,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,289,289,289,289,289,0,0,280,334,335,336,337,0,0,0,137,83,138,138,203,203,138,138,138,84,0,0,0,0,0,0,0,0,0,0,0,0,378,379,380,82,139,0,0,0,0,0,0,0,381,382,383,0,0,255,256,257,258,0,0,0,352,353,354,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,320,321,322,0,0,0,0,0,0,0,0,0,0,0,289,289,289,289,289,0,0,280,363,364,365,366,0,0,0,0,0,0,0,232,232,0,0,0,137,138,84,0,0,0,0,0,0,0,0,0,0,0,0,82,139,0,0,0,0,0,0,0,0,0,0,0,0,0,284,285,286,287,0,0,0,381,382,383,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,289,289,289,289,289,20,21,280,334,335,336,337,0,0,0,137,83,138,138,203,203,138,138,138,84,0,0,0,0,0,0,0,0,0,0,0,0,378,379,380,82,139,0,0,0,0,0,0,0,381,382,383,0,0,255,256,257,258,0,0,0,352,353,354,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,320,321,322,0,0,0,0,0,0,0,0,0,0,0,289,289,289,289,289,49,50,280,363,364,365,366,0,0,0,0,0,0,0,232,232,0,0,0,137,138,84,0,0,0,0,0,0,0,0,0,0,0,0,82,139,0,0,0,0,0,0,0,0,0,0,0,0,0,284,285,286,287,0,0,0,381,382,383,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,349,350,351,0,0,0,0,0,0,0,0,0,0,0,289,0,0,0,0,0,0,306,392,393,394,395,0,0,0,0,0,0,0,0,0,0,0,0,0,0,137,84,0,0,0,0,0,0,0,0,0,82,138,139,0,0,0,0,0,0,0,0,0,0,0,0,0,0,284,285,286,287,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,378,379,380,0,0,0,0,0,0,0,0,294,0,0,289,0,0,0,0,0,0,0,421,422,423,424,0,0,12,13,0,0,0,0,0,294,0,0,0,0,0,137,138,138,138,84,0,0,0,82,138,139,0,280,0,0,0,0,0,0,0,0,0,0,0,0,0,0,284,285,286,287,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,19,
0,0,0,0,0,0,0,0,0,294,294,0,0,0,0,0,0,0,0,0,294,0,0,289,0,294,0,0,0,0,0,450,451,452,453,0,0,41,42,0,0,0,0,0,294,294,0,0,0,0,29,0,0,0,137,138,138,138,139,0,0,0,280,0,0,0,0,0,0,0,0,0,0,0,0,0,0,284,285,286,287,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,47,48,
@ -179,6 +179,12 @@
<property name="text" value="Parlez à nos habitants !"/>
</properties>
</object>
<object id="61" gid="267" x="464" y="432" width="16" height="16">
<properties>
<property name="name" value="Pancarte"/>
<property name="text" value="De quoi bien commencer l'aventure !;~xp:500;De rien :)"/>
</properties>
</object>
</objectgroup>
<objectgroup id="11" name="teleporter">
<object id="50" gid="238" x="448" y="528" width="16" height="16">

6
include/event.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stdbool.h>
#include "game.h"
bool handle_event(struct Game *game, char const *event);

View File

@ -49,4 +49,5 @@ void replace_capacities(struct Player *player, struct Move move);
void draw_ui(struct Player *player);
int get_nb_moves(struct Player *player);
void reset_pp(struct Player *player);
void check_level(struct Player *player, int prec_level);
void check_level(struct Player *player, int prec_level);
void add_xp(struct Player *player, int xp);

View File

@ -1,5 +1,6 @@
#pragma once
#include "map.h"
#include "game.h"
struct Talkable {
/*the position of the tile*/
@ -11,7 +12,7 @@ struct Talkable {
};
/*draw the dialog of a specified talkable tile*/
void draw_dialog(struct Talkable *character);
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);

View File

@ -1,5 +1,6 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include <math.h>
#include "engine.h"
#include "battle.h"
@ -8,7 +9,6 @@
#include "player.h"
#include "monster.h"
#include <stdlib.h>
#include <math.h>
extern bopti_image_t img_dialogue;
@ -110,20 +110,7 @@ void finish_battle(int status, struct Game *game, struct Monster *monster) {
dupdate();
wait_for_input(KEY_SHIFT);
game->player->stats.xp += xp;
//niveau suivant une progession N³
int calc_level = (int)pow(game->player->stats.xp, 0.33);
for(int i = game->player->stats.level; i < calc_level; i++) {
draw_battle(game->player, monster);
dimage(42,DHEIGHT-75,&img_dialogue);
dprint(50,DHEIGHT-47,C_BLACK,"Vous passez au niveau %d !", i+1);
dupdate();
wait_for_input(KEY_SHIFT);
}
int prec = game->player->stats.level;
game->player->stats.level = calc_level;
check_level(game->player, prec);
add_xp(game->player, xp);
} else if(status == LOSE) {
draw_battle(game->player, monster);

View File

@ -120,10 +120,7 @@ void engine_set_background(struct Game *game, int color) {
void engine_action(struct Game const *game, int action) {
if(action == ACTION_SHIFT) {
if(player_facing(game) == TILE_TALKABLE) {
int direction = game->player->direction;
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
draw_dialog(get_dialog_xy(game->map, game->player->pos.x + dx, game->player->pos.y + dy));
draw_dialog(game);
}
}
if(action == ACTION_F1) {

28
src/event.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "game.h"
#include "event.h"
#include "player.h"
bool handle_event(struct Game *game, char const *event)
{
if(!strncmp(event, "give:", 5)) {
event += 5;
// Isoler le nom et la quantité
int len=strlen(event), qty=1;
char *star = strchr(event, '*');
if(star) len=star-event, qty=atoi(star+1);
return true;
}
else if(!strncmp(event, "xp:", 3)) {
event += 3;
add_xp(game->player, atoi(event));
return true;
}
else if(!strcmp(event, "test:test")) {
// etc, n'importe quoi de compliqué
return true;
}
return false;
}

View File

@ -1,5 +1,6 @@
#include <gint/keyboard.h>
#include <stdlib.h>
#include <math.h>
#include "player.h"
#include "define.h"
@ -9,6 +10,7 @@
#include "util.h"
extern struct LevelUpPlayer levelupplayer;
extern bopti_image_t img_dialogue;
struct Player init_player(void) {
@ -83,14 +85,24 @@ int get_nb_moves(struct Player *player) {
void add_move(struct Player *player, struct Move move) {
int index = get_nb_moves(player);
if(index != NB_PLAYER_MOVES) player->moves[index] = copy_move(move);
else replace_capacities(player, move);
dimage(42,DHEIGHT-75,&img_dialogue);
if(index != NB_PLAYER_MOVES) {
dprint(50,DHEIGHT-47,C_BLACK,"Vous apprenez %s !", move.name);
dupdate();
wait_for_input(KEY_SHIFT);
player->moves[index] = copy_move(move);
} else {
dprint(50,DHEIGHT-47,C_BLACK,"Vous pouvez apprendre %s !", move.name);
dupdate();
wait_for_input(KEY_SHIFT);
replace_capacities(player, move);
}
}
void draw_player_moves(struct Player *player) {
int index = get_nb_moves(player);
for(int i = 0; i < index; i++) {
draw_classic_move(0,65*i,player->moves[i]);
draw_classic_move(20,65*i+15,player->moves[i]);
}
}
@ -99,17 +111,16 @@ void replace_capacities(struct Player *player, struct Move move) {
int buffer = keydown(KEY_SHIFT);
while(1) {
clearevents();
dclear(C_WHITE);
selection += keydown(KEY_DOWN) - keydown(KEY_UP);
if(selection > NB_PLAYER_MOVES-1) selection = NB_PLAYER_MOVES-1;
if(selection < 0) selection = 0;
draw_classic_move(200,DHEIGHT/2-30, &move);
for(int i = 0; i < NB_PLAYER_MOVES; i++) {
draw_classic_move(0,65*i, player->moves[i]);
}
draw_player_moves(player);
dtext(105, 45+65*selection , C_RED, "[X]");
dtext(105, 42+65*selection , C_RED, "[X]");
dupdate();
if(keydown(KEY_SHIFT)) {
@ -141,4 +152,21 @@ void reset_pp(struct Player *player) {
for(int i = 0; i < index; i++) {
player->moves[i]->pp = player->moves[i]->init_pp;
}
}
void add_xp(struct Player *player, int xp) {
player->stats.xp += xp;
//niveau suivant une progession N³
int calc_level = (int)pow(player->stats.xp, 0.33);
for(int i = player->stats.level; i < calc_level; i++) {
// draw_battle(game->player, monster);
dimage(42,DHEIGHT-75,&img_dialogue);
dprint(50,DHEIGHT-47,C_BLACK,"Vous passez au niveau %d !", i+1);
dupdate();
wait_for_input(KEY_SHIFT);
}
int prec = player->stats.level;
player->stats.level = calc_level;
check_level(player, prec);
}

View File

@ -6,6 +6,8 @@
#include "talkable.h"
#include "util.h"
#include "event.h"
#include "player.h"
#define LIMIT 35
@ -37,32 +39,41 @@ char *skip_spaces(char *str)
}
/*draw the dialog of a specified talkable tile*/
void draw_dialog(struct Talkable *talkable) {
void draw_dialog(struct Game *game) {
extern bopti_image_t img_dialogue;
const char *delim = ";";
char *str = strdup(talkable->text);
int direction = game->player->direction;
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
struct Talkable *talk = get_dialog_xy(game->map, game->player->pos.x + dx, game->player->pos.y + dy);
char *str = strdup(talk->text);
char *curr_line = strtok(str, delim);
while(curr_line != NULL) {
dimage(43,31,&img_dialogue);
dprint(50,40, C_BLACK, "%s", talkable->name);
char *event = strchr(curr_line, '~');
if(event) handle_event(game, curr_line+1);
else {
dimage(43,31,&img_dialogue);
dprint(50,40, C_BLACK, "%s", talk->name);
int const DIALOG_WIDTH = 295, LINE_HEIGHT = 13;
int y = 45 + LINE_HEIGHT;
int const DIALOG_WIDTH = 295, LINE_HEIGHT = 13;
int y = 45 + LINE_HEIGHT;
while(*curr_line) {
char *end = (char *)drsize(curr_line, NULL, DIALOG_WIDTH, NULL);
char *last_word = word_boundary_before(curr_line, end);
dtext_opt(50, y, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_TOP,
curr_line, last_word - curr_line);
curr_line = skip_spaces(last_word);
y += LINE_HEIGHT;
while(*curr_line) {
char *end = (char *)drsize(curr_line, NULL, DIALOG_WIDTH, NULL);
char *last_word = word_boundary_before(curr_line, end);
dtext_opt(50, y, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_TOP,
curr_line, last_word - curr_line);
curr_line = skip_spaces(last_word);
y += LINE_HEIGHT;
}
dupdate();
wait_for_input(KEY_SHIFT);
}
dupdate();
curr_line = strtok(NULL, delim);
wait_for_input(KEY_SHIFT);
}
}