diff --git a/assets-cg/fxconv-metadata.txt b/assets-cg/fxconv-metadata.txt index 71c3743..0c5f699 100644 --- a/assets-cg/fxconv-metadata.txt +++ b/assets-cg/fxconv-metadata.txt @@ -1,6 +1,7 @@ tileset.png: type: bopti-image name: img_tileset + profile:p4 spritesheet.png: type: bopti-image diff --git a/include/animation.h b/include/animation.h index c7e58a2..d545d81 100644 --- a/include/animation.h +++ b/include/animation.h @@ -9,18 +9,27 @@ anim_function_t anim_player_idle; struct anim_frame { + /*the image*/ bopti_image_t *source; + /*the left and top pixels to know where to start drawing*/ int left, top; + /*the width and height of one frame*/ int w, h; }; struct anim_data { + /*the function to call to update the frame*/ anim_function_t *function; + /*the anim frame*/ struct anim_frame img; + /*if the animation needs to move*/ int dx, dy; + /*the direction*/ int dir; + /*the current frame*/ int frame; + /*the duration of one frame*/ int duration; }; diff --git a/include/character.h b/include/character.h index 83c40d2..1d0054d 100644 --- a/include/character.h +++ b/include/character.h @@ -1,6 +1,11 @@ +#pragma once + struct character { + /*the position of the character*/ int x, y; + /*the name*/ char *name; + /*the text to say*/ char *dialog; }; diff --git a/include/define.h b/include/define.h index cd7ebdf..77a57c9 100644 --- a/include/define.h +++ b/include/define.h @@ -1,2 +1,4 @@ -#define PLAYER_VIEW_X 5 -#define PLAYER_VIEW_Y 5 +/*the x radius the player is able to see*/ +#define PLAYER_VIEW_X 13 +/*the y radius the player is able to see*/ +#define PLAYER_VIEW_Y 7 diff --git a/include/engine.h b/include/engine.h index 806d0c6..b62179f 100644 --- a/include/engine.h +++ b/include/engine.h @@ -3,9 +3,13 @@ #define ENGINE_TICK 35 struct game { + /*the current map to display*/ struct map *map; + /*the player*/ struct player *player; + /*list of all the characters on the map*/ struct character **characters; + /*the background color*/ int background; }; diff --git a/include/game.h b/include/game.h index 12ab34b..aaa67b4 100644 --- a/include/game.h +++ b/include/game.h @@ -1,6 +1,5 @@ #pragma once -void play(); int get_inputs(void); enum direction { @@ -8,5 +7,6 @@ enum direction { DIR_LEFT = 1, DIR_UP = 2, DIR_RIGHT = 3, - ACTION_SHIFT = 4 + ACTION_SHIFT = 4, + ACTION_ALPHA = 5 }; \ No newline at end of file diff --git a/include/map.h b/include/map.h index 80e126b..b981cef 100644 --- a/include/map.h +++ b/include/map.h @@ -1,10 +1,15 @@ #pragma once struct map { + /*width, height and the number of layer of the map (max 2)*/ int w, h, nb_layer; + /*the tileset to use*/ bopti_image_t *tileset; + /*list of all background tiles (layer 1)*/ short *tiles_layer1; + /*list of all "physical" tiles (layer 2)*/ short *tiles_layer2; + /*state of each tile on the map (solid, air ...)*/ short *info_map; }; diff --git a/include/player.h b/include/player.h index 7c6615b..329ef1f 100644 --- a/include/player.h +++ b/include/player.h @@ -4,12 +4,18 @@ #include "engine.h" struct player { + /*current position of the player on the map*/ int x, y; + /*the direction the player facing to*/ int direction; + /*current frame of the animation*/ int frame; + /*where to draw the player on the screen*/ int show_x, show_y; + /*the current animation*/ int idle; struct anim_data anim; }; +/*return the info tile value the player is facing to*/ int player_facing(struct game const *game); \ No newline at end of file diff --git a/src/animation.c b/src/animation.c index 098a626..2c6055e 100644 --- a/src/animation.c +++ b/src/animation.c @@ -6,8 +6,11 @@ struct sheet { + /*the sheet image of the animation*/ bopti_image_t *img; + /*the frame width*/ int frame_w; + /*the frame height*/ int frame_h; }; @@ -29,11 +32,13 @@ static struct anim_frame anim_frame(struct sheet const *sheet, int col, int row) return f; } +/*draw the frame*/ void dframe(int x, int y, struct anim_frame const frame) { dsubimage(x, y, frame.source, frame.left, frame.top, frame.w, frame.h, DIMAGE_NONE); } +/*animation for player walking*/ int anim_player_walking(struct anim_data *data, int init) { if(init) { data->function = anim_player_walking; @@ -60,6 +65,7 @@ int anim_player_walking(struct anim_data *data, int init) { return 1; } +/*animation for player doing nothing*/ int anim_player_idle(struct anim_data *data, int init) { if(init) { data->function = anim_player_idle; diff --git a/src/character.c b/src/character.c index 7b628d0..a8b16ad 100644 --- a/src/character.c +++ b/src/character.c @@ -3,15 +3,25 @@ #include #include "character.h" +/*draw the dialog of a specified character*/ void draw_dialog(struct character *character) { - dclear(C_WHITE); + drect(1,1,400,80,C_WHITE); dprint(1,20, C_BLACK, "(%d,%d)", character->x, character->y); dprint(1,40, C_BLACK, "%s", character->name); dprint(1,60, C_BLACK, "%s", character->dialog); dupdate(); - getkey(); + int buffer = 1; + while(1) { + clearevents(); + if(keydown(KEY_SHIFT)) { + if(buffer) buffer = 0; + else break; + } + while(keydown(KEY_SHIFT)) clearevents(); + } } +/*find the character using the player's position*/ struct character* get_character_xy(struct character *characters[], int x, int y) { int i = 0; while(strcmp(characters[i]->name,"default name") != 0) { diff --git a/src/engine.c b/src/engine.c index 417e71c..3a213e8 100644 --- a/src/engine.c +++ b/src/engine.c @@ -10,12 +10,14 @@ #define TILESET_WIDTH 29 +/*draw the current state of the game*/ void engine_draw(struct game const *game) { dclear(game->background); engine_draw_map_around_player(game); engine_draw_player(game->player); } +/*draw the map around the player*/ void engine_draw_map_around_player(struct game const *game) { const int level_width = game->map->w; const int taillemap = game->map->w * game->map->h; @@ -89,11 +91,13 @@ void engine_draw_map_around_player(struct game const *game) { } } +/*draw the player*/ void engine_draw_player(struct player const *player) { dframe(player->show_x * 16, player->show_y * 16 - 5, player->anim.img); //draw the player 5 pixel up dprint(1,1,C_BLACK,"%d:%d",player->x, player->y); } +/*move the player to the direction*/ int engine_move(struct game *game, int direction) { int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT); int dy = (direction == DIR_DOWN) - (direction == DIR_UP); @@ -115,6 +119,7 @@ int engine_move(struct game *game, int direction) { return 1; } +/*update the player animation*/ void engine_tick(struct game *game, int dt) { game->player->anim.duration -= dt; @@ -123,16 +128,19 @@ void engine_tick(struct game *game, int dt) { } } +/*check if a tile is walkable*/ int map_walkable(struct map const *map, int x, int y) { int tile = map->info_map[x + map->w * y]; if(x < 0 || x > map->w-1 || y < 0 || y > map->h-1) return 0; return (tile != TILE_SOLID && tile != TILE_CHARACTER); } +/*set the background color*/ void engine_set_background(struct game *game, int color) { game->background = color; } +/*make an interaction with something*/ void engine_action(struct game const *game, int action) { if(action == ACTION_SHIFT) { if(player_facing(game) == TILE_CHARACTER) { diff --git a/src/game.c b/src/game.c index c31c48a..a69dd3b 100644 --- a/src/game.c +++ b/src/game.c @@ -3,6 +3,7 @@ #include "game.h" #include "player.h" +/*get the input with a timeout*/ int get_inputs(void) { int opt = GETKEY_DEFAULT & GETKEY_REP_ARROWS; int timeout = 1; @@ -13,10 +14,14 @@ int get_inputs(void) { if(ev.type == KEYEV_NONE) return -1; int key = ev.key; + /*direction*/ if(key == KEY_DOWN) return DIR_DOWN; if(key == KEY_RIGHT) return DIR_RIGHT; if(key == KEY_UP) return DIR_UP; if(key == KEY_LEFT) return DIR_LEFT; + + /*action key*/ if(key == KEY_SHIFT) return ACTION_SHIFT; + if(key == KEY_ALPHA) return ACTION_ALPHA; } } \ No newline at end of file diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..e33ad10 --- /dev/null +++ b/src/player.c @@ -0,0 +1,23 @@ +#include "player.h" +#include "engine.h" +#include "map.h" +#include "game.h" + +/* +return the info tile value the player is facing to +TILE_SOLID by default (out of bound) +*/ +int player_facing(struct game const *game) { + int direction = game->player->direction; + int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT); + int dy = (direction == DIR_DOWN) - (direction == DIR_UP); + + int index = game->player->x + dx + game->map->w * (game->player->y + dy); + if(game->player->x + dx >= 0 && + game->player->x + dx <= game->map->w && + game->player->y + dy >= 0 && + game->player->y + dy <= game->map->h) { + return game->map->info_map[index]; + } + return TILE_SOLID; +} \ No newline at end of file