From 6303b713d70a664a211205ebbded939dee58a6b2 Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Fri, 27 Aug 2021 22:50:00 +0200 Subject: [PATCH] now can walk out of house + random --- CMakeLists.txt | 16 ++++-- assets-cg/maps/inside/1.tmx | 68 +++++++++++++++++++++++ assets-cg/maps/inside/2.tmx | 35 ++++++++++++ assets-cg/maps/inside/fxconv-metadata.txt | 4 +- assets-cg/maps/inside/interior_1.tmx | 58 ------------------- include/engine.h | 1 + include/map.h | 6 +- include/player.h | 1 - include/util.h | 4 +- src/camera.c | 5 +- src/engine.c | 15 +++++ src/map.c | 41 ++++++++++++-- src/player.c | 7 +-- src/util.c | 5 +- 14 files changed, 186 insertions(+), 80 deletions(-) create mode 100644 assets-cg/maps/inside/1.tmx create mode 100644 assets-cg/maps/inside/2.tmx delete mode 100644 assets-cg/maps/inside/interior_1.tmx diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fd19ec..8f0e08d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,13 +13,20 @@ fxconv_declare_converters(assets-cg/converters.py) add_custom_command( OUTPUT "${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/testCarte.json" - "${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/inside/interior_1.json" + "${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/inside/1.json" + "${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/inside/2.json" COMMENT "Convert tmx map to json" COMMAND tiled --export-tileset json tileset.tsx tileset.json COMMAND tiled --export-map json testCarte.tmx testCarte.json - COMMAND tiled --export-map json inside/interior_1.tmx inside/interior_1.json + COMMAND tiled --export-map json inside/1.tmx inside/1.json + COMMAND tiled --export-map json inside/2.tmx inside/2.json WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/ - DEPENDS assets-cg/maps/testCarte.tmx assets-cg/tileset.png assets-cg/maps/tileset.tsx assets-cg/converters.py assets-cg/maps/inside/interior_1.tmx) + DEPENDS assets-cg/maps/testCarte.tmx + assets-cg/tileset.png + assets-cg/maps/tileset.tsx + assets-cg/converters.py + assets-cg/maps/inside/1.tmx + assets-cg/maps/inside/2.tmx) set(SOURCES src/main.c @@ -37,7 +44,8 @@ set(SOURCES set(ASSETS_cg assets-cg/tileset.png assets-cg/maps/testCarte.json - assets-cg/maps/inside/interior_1.json + assets-cg/maps/inside/1.json + assets-cg/maps/inside/2.json assets-cg/spritesheet.png ) diff --git a/assets-cg/maps/inside/1.tmx b/assets-cg/maps/inside/1.tmx new file mode 100644 index 0000000..9b318b5 --- /dev/null +++ b/assets-cg/maps/inside/1.tmx @@ -0,0 +1,68 @@ + + + + + + + + +2,3,2,1,3,3,2,3,2,1,1,3, +1,6,6,6,156,157,157,158,2,1,2,2, +3,6,156,157,159,233,235,160,158,3,1,1, +1,6,185,233,234,236,237,235,187,1,6,1, +1,6,185,291,292,265,301,264,187,1,6,1, +1,6,214,215,188,291,292,293,160,158,6,1, +2,6,3,1,214,215,215,215,215,216,6,1, +2,3,2,2,1,5,3,109,2,6,6,3, +2,3,2,1,3,2,3,2,6,6,6,3, +1,2,0,1,2,2,2,1,3,3,1,3 + + + + +248,249,249,249,249,249,249,249,249,249,249,251, +277,0,0,0,0,0,0,0,0,20,21,280, +277,0,0,0,0,0,0,0,0,49,50,280, +277,0,0,0,0,0,0,0,0,0,0,280, +277,0,0,0,0,0,0,0,0,0,0,280, +277,0,0,26,0,0,0,0,0,0,0,280, +277,0,0,55,0,0,0,0,0,0,0,280, +277,0,0,0,0,0,0,0,0,0,0,280, +277,0,0,0,0,0,0,0,0,0,0,280, +306,278,238,279,250,250,250,250,250,250,250,309 + + + + +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, +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,29,0,0,0,0, +0,0,0,0,0,0,0,58,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,0,0,0,0 + + + + + + + Salutation ! +Ça va ? + + + + + + + + + + + + + + diff --git a/assets-cg/maps/inside/2.tmx b/assets-cg/maps/inside/2.tmx new file mode 100644 index 0000000..6ac5db0 --- /dev/null +++ b/assets-cg/maps/inside/2.tmx @@ -0,0 +1,35 @@ + + + + + + + + +94,94,94,94,94,94,94,94,94,94,94,94,94,94,94, +94,94,94,94,94,94,94,94,94,94,94,94,94,94,94, +94,94,94,94,94,94,94,94,94,94,94,94,94,94,94, +94,94,97,123,123,123,123,123,123,123,123,123,96,94,94, +94,94,95,161,162,162,162,162,162,162,162,163,93,94,94, +94,94,95,190,191,191,191,223,191,223,223,192,93,94,94, +94,94,95,219,220,220,193,223,194,220,220,221,93,94,94, +94,94,68,65,65,66,190,191,192,64,65,65,67,94,94, +94,94,94,94,94,95,190,191,192,93,94,94,94,94,94, +94,94,94,94,94,95,190,222,192,93,94,94,94,94,94 + + + + +224,225,225,225,225,225,225,225,225,225,225,225,225,225,226, +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,323,324,325,246,0,0,0,0,0, +0,0,0,0,22,23,352,353,354,275,0,0,0,0,0, +0,0,0,0,51,52,381,382,383,304,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,238,0,0,0,0,0,0,0 + + + diff --git a/assets-cg/maps/inside/fxconv-metadata.txt b/assets-cg/maps/inside/fxconv-metadata.txt index 00cb15c..7eaaba9 100644 --- a/assets-cg/maps/inside/fxconv-metadata.txt +++ b/assets-cg/maps/inside/fxconv-metadata.txt @@ -1,3 +1,3 @@ -interior_1.json: +*.json: custom-type: map - name: in_1 + name_regex: (.*)\.json in_\1 diff --git a/assets-cg/maps/inside/interior_1.tmx b/assets-cg/maps/inside/interior_1.tmx deleted file mode 100644 index d084a18..0000000 --- a/assets-cg/maps/inside/interior_1.tmx +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - -2,3,2,1,3,3,2,3,2,1,1,3, -1,6,6,6,1,3,2,3,2,1,2,2, -3,6,6,74,76,1,1,74,76,3,1,1, -1,6,2,103,105,1,1,103,105,1,1,1, -1,6,2,103,105,39,1,103,105,33,1,1, -1,6,1,103,78,75,75,77,105,1,1,1, -2,6,3,132,133,106,107,133,134,1,3,1, -2,3,2,2,1,132,134,3,2,2,1,3, -2,3,2,1,3,2,3,2,2,1,1,3, -1,2,0,1,2,2,2,1,3,3,1,3 - - - - -248,249,249,249,249,249,249,249,249,249,249,251, -277,0,0,0,635,636,637,638,0,0,0,280, -277,0,0,0,664,665,666,667,0,0,0,280, -277,0,0,0,693,694,695,696,0,0,0,280, -277,0,0,245,722,723,724,725,245,0,0,280, -277,0,0,274,751,752,753,754,274,0,0,280, -277,0,0,303,0,0,0,0,303,0,0,280, -277,0,0,0,0,0,0,0,0,0,0,280, -277,0,0,0,0,0,0,0,0,0,0,280, -306,307,238,308,250,250,250,250,250,250,250,309 - - - - -0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,294,294,294,0, -0,0,0,0,0,0,0,0,0,294,294,0, -0,0,0,0,0,0,0,0,0,294,294,0, -0,0,0,0,0,0,0,0,0,0,294,0, -0,0,0,0,0,0,0,29,0,0,294,0, -0,0,0,0,0,0,0,58,0,0,294,0, -0,0,0,0,0,0,0,0,0,0,294,0, -0,0,0,0,0,0,0,0,0,0,294,0, -0,0,0,0,0,0,0,0,0,0,0,0 - - - - - - - Salutation ! -Ça va ? - - - - diff --git a/include/engine.h b/include/engine.h index 1a7a6d6..a1d9480 100644 --- a/include/engine.h +++ b/include/engine.h @@ -19,3 +19,4 @@ void engine_set_background(struct Game *game, int color); void engine_action(struct Game const *game, int action); /*check the current position of the player. To perform action depends of his location*/ void engine_check_position(struct Game *game); +void engine_center_camera(struct Game *game); \ No newline at end of file diff --git a/include/map.h b/include/map.h index c72f950..b268f57 100644 --- a/include/map.h +++ b/include/map.h @@ -28,4 +28,8 @@ int map_walkable(struct Map const *map, int x, int y); /*get the tile under the player*/ int map_get_player_tile(struct Game const *game); -void generate_interior_map(struct Game *game); \ No newline at end of file +void generate_interior_map(struct Game *game); + +void set_map(struct Game *game, int id); + +struct Vec2 locate_tile(struct Map const *map, int tile); \ No newline at end of file diff --git a/include/player.h b/include/player.h index a24d674..9277993 100644 --- a/include/player.h +++ b/include/player.h @@ -23,5 +23,4 @@ struct Player { /*return the info tile value the player is facing to*/ int player_facing(struct Game const *game); -void set_player_xy(struct Player *player, int x, int y); struct Player init_player(void); diff --git a/include/util.h b/include/util.h index dbb22a5..b431c80 100644 --- a/include/util.h +++ b/include/util.h @@ -1,4 +1,6 @@ #pragma once /*wait for a specified input key*/ -void wait_for_input(int input); \ No newline at end of file +void wait_for_input(int input); + +int rand_range(int low, int high); \ No newline at end of file diff --git a/src/camera.c b/src/camera.c index d17f8fc..892f75b 100644 --- a/src/camera.c +++ b/src/camera.c @@ -1,5 +1,8 @@ #include "vec2.h" #include "camera.h" +#include "game.h" +#include "define.h" +#include "player.h" struct Camera camera_new(struct Vec2f *target) { return (struct Camera) { @@ -12,4 +15,4 @@ struct Camera camera_new(struct Vec2f *target) { void camera_update(struct Camera *c) { c->pos = vec2f_lerp(c->pos, *c->target, 0.08); c->offset = vec2f_vec2(c->pos); -} +} \ No newline at end of file diff --git a/src/engine.c b/src/engine.c index 99ff590..9513584 100644 --- a/src/engine.c +++ b/src/engine.c @@ -119,12 +119,27 @@ void engine_action(struct Game const *game, int action) { /*check the current position of the player. To perform action depends of his location*/ void engine_check_position(struct Game *game) { + static struct Map *old_map; + static struct Vec2 old_pos; + int player_curr_tile = map_get_player_tile(game); if(player_curr_tile == TILE_DOOR_IN) { + old_map = game->map; + old_pos = game->player->pos; + engine_set_background(game, C_BLACK); generate_interior_map(game); } if(player_curr_tile == TILE_DOOR_OUT) { + game->map = old_map; + game->player->pos = old_pos; + engine_center_camera(game); + engine_set_background(game, C_WHITE); } } + +void engine_center_camera(struct Game *game) { + game->camera.pos.x = game->player->pos.x * TILE_SIZE + game->player->x_mid; + game->camera.pos.y = game->player->pos.y * TILE_SIZE + game->player->y_mid; +} \ No newline at end of file diff --git a/src/map.c b/src/map.c index f7cbfe2..c06956b 100644 --- a/src/map.c +++ b/src/map.c @@ -1,6 +1,9 @@ +#include + #include "map.h" #include "player.h" #include "define.h" +#include "util.h" extern struct Map map_1; @@ -22,10 +25,38 @@ int map_get_player_tile(struct Game const *game) { /*generate the interior*/ void generate_interior_map(struct Game *game) { - extern struct Map in_1; + const int NB_INTERIORS = 2; - game->map = &in_1; - set_player_xy(game->player, 3,3); - game->camera.pos.x = in_1.w/2 * TILE_SIZE + game->player->x_mid; - game->camera.pos.y = in_1.h/2 * TILE_SIZE + game->player->y_mid; + extern struct Map in_1; + extern struct Map in_2; + + struct Map *interiors[] = { + &in_1, + &in_2 + }; + + srand(game->player->pos.x * game->player->pos.y); + + game->map = interiors[rand_range(0,NB_INTERIORS)]; + game->player->pos = locate_tile(game->map, TILE_DOOR_OUT); + engine_center_camera(game); +} + +/*may be useful later*/ +void set_map(struct Game *game, int id) { + game->map = maps[id]; + game->camera.pos = vec2f_lerp(game->camera.pos, *game->camera.target, 1); +} + +/*locate the first occurence of a tile in a specified map*/ +struct Vec2 locate_tile(struct Map const *map, int tile) { + for(int x = 0; x < map->w; x++) { + for(int y = 0; y < map->h; y++) { + int tile_id = map->info_map[x + y * map->w]; + if(tile_id == tile) { + return VEC2(x,y); + } + } + } + return VEC2(0,0); } \ No newline at end of file diff --git a/src/player.c b/src/player.c index a0d2757..69884c6 100644 --- a/src/player.c +++ b/src/player.c @@ -36,9 +36,4 @@ int player_facing(struct Game const *game) { return game->map->info_map[index]; } return TILE_SOLID; -} - -void set_player_xy(struct Player *p, int x, int y) { - p->pos.x = x; - p->pos.y = y; -} +} \ No newline at end of file diff --git a/src/util.c b/src/util.c index 1f3c940..5eb934e 100644 --- a/src/util.c +++ b/src/util.c @@ -1,4 +1,5 @@ #include +#include #include "util.h" @@ -13,4 +14,6 @@ void wait_for_input(int input) { } while(keydown(input)) clearevents(); } -} \ No newline at end of file +} + +int rand_range(int low, int high) { return (rand() % (high - low)) + low; } \ No newline at end of file