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 @@
+
+
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 @@
+
+
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 @@
-
-
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