World gestion : displaying and collisions

This commit is contained in:
Shadow15510 2021-07-04 22:44:08 +02:00
parent 240242c2ee
commit bd868143b8
13 changed files with 84 additions and 53 deletions

12
.gitignore vendored
View File

@ -1,12 +1,12 @@
# Build files
projet/build-fx
projet/build-cg
project/build-fx
project/build-cg
# Executables
projet/Odyssee.g1a
projet/Odyssee.g3a
project/Odyssee.g1a
project/Odyssee.g3a
# Python bytecode
projet/__pycache__/*
projet/assets-fx/__pycache__/*
project/__pycache__/*
project/assets-fx/__pycache__/*

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -25,7 +25,7 @@ set(ASSETS_fx
assets-fx/bgrounds.png
assets-fx/tileset.png
assets-fx/player.png
assets-fx/maps/one.csv
assets-fx/map/world.csv
# ...
)
set(ASSETS_cg

View File

@ -2,7 +2,7 @@ import fxconv
def convert(input_name, output, params, target):
if params["custom-type"] == "map_t":
if params["custom-type"] == "map":
convert_map(input_name, output, params, target)
return 0
else:
@ -13,12 +13,19 @@ def convert_map(input_name, output, params, target):
# Read informations
with open(input_name, "r") as file:
csv_data = file.read().splitlines()
walkable = [-1, 11, 12, 13, 14, 15, 16, 17, 18, 20, 24, 25, 26, 30, 31, 37, 43, 44, 45, 62, 63, 64]
# Convertion into bytes
data = bytes()
tiles = bytes()
collision = bytes()
for line in csv_data:
for value in line.split(","):
data += fxconv.u32(int(value))
value = int(value)
tiles += fxconv.u32(value)
collision += fxconv.u8(int(value in walkable))
data = fxconv.ObjectData()
data += tiles + collision
fxconv.elf(data, output, "_" + params["name"], **target)

View File

@ -1,3 +1,3 @@
*.csv:
custom-type: map_t
custom-type: map
name_regex: (.*)\.csv map_\1

View File

@ -0,0 +1,16 @@
-1,65,66,66,67,-1,50,-1,-1,-1,0,1,1,1,1,2,30,-1,-1,-1,27,28,27,28,29,29,29,29,29,-1,-1,-1
-1,84,85,84,84,-1,50,-1,22,23,19,30,64,-1,83,21,-1,-1,64,-1,46,47,46,47,44,-1,-1,-1,49,-1,-1,44
29,84,84,86,85,29,29,-1,41,42,38,-1,-1,11,-1,21,-1,-1,-1,-1,84,85,84,84,-1,-1,24,-1,-1,-1,-1,-1
-1,33,-1,11,-1,-1,-1,-1,-1,-1,57,58,58,58,-1,21,29,29,29,29,84,84,86,84,83,83,51,-1,49,-1,-1,-1
-1,-1,-1,-1,11,-1,-1,30,-1,-1,11,-1,-1,-1,-1,40,11,-1,64,-1,-1,-1,44,-1,29,29,29,29,29,-1,-1,-1
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,58,58,58,59,-1,-1,-1,-1,-1,-1,44,-1,-1,-1,-1,-1,-1,-1,-1,-1
-1,-1,13,-1,-1,-1,-1,13,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,-1,44,-1,-1
-1,-1,-1,-1,-1,-1,64,-1,-1,-1,-1,-1,-1,15,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1
-1,-1,26,-1,64,-1,-1,-1,-1,-1,26,24,-1,-1,-1,-1,34,35,36,-1,-1,-1,-1,18,-1,-1,18,-1,-1,-1,-1,-1
-1,-1,-1,-1,-1,-1,64,-1,-1,-1,-1,-1,-1,-1,0,1,53,54,55,2,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,-1
-1,-1,-1,64,30,-1,-1,0,1,1,1,1,1,1,19,33,-1,-1,44,21,2,18,-1,44,-1,-1,-1,-1,-1,-1,-1,-1
-1,-1,-1,-1,-1,-1,-1,19,22,23,52,-1,52,62,38,-1,-1,-1,44,40,21,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1
-1,15,-1,-1,-1,-1,15,19,41,42,-1,-1,-1,-1,57,58,58,58,20,59,21,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1
-1,-1,-1,-1,-1,-1,-1,38,-1,-1,49,-1,-1,-1,-1,-1,-1,-1,-1,52,21,-1,-1,44,-1,-1,-1,-1,-1,-1,-1,-1
-1,-1,15,-1,-1,-1,-1,57,58,58,58,58,20,58,58,58,58,58,58,58,59,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
1 -1 65 66 66 67 -1 50 -1 -1 -1 0 1 1 1 1 2 30 -1 -1 -1 27 28 27 28 29 29 29 29 29 -1 -1 -1
2 -1 84 85 84 84 -1 50 -1 22 23 19 30 64 -1 83 21 -1 -1 64 -1 46 47 46 47 44 -1 -1 -1 49 -1 -1 44
3 29 84 84 86 85 29 29 -1 41 42 38 -1 -1 11 -1 21 -1 -1 -1 -1 84 85 84 84 -1 -1 24 -1 -1 -1 -1 -1
4 -1 33 -1 11 -1 -1 -1 -1 -1 -1 57 58 58 58 -1 21 29 29 29 29 84 84 86 84 83 83 51 -1 49 -1 -1 -1
5 -1 -1 -1 -1 11 -1 -1 30 -1 -1 11 -1 -1 -1 -1 40 11 -1 64 -1 -1 -1 44 -1 29 29 29 29 29 -1 -1 -1
6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 58 58 58 59 -1 -1 -1 -1 -1 -1 44 -1 -1 -1 -1 -1 -1 -1 -1 -1
7 -1 -1 13 -1 -1 -1 -1 13 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 62 -1 -1 -1 -1 44 -1 -1
8 -1 -1 -1 -1 -1 -1 64 -1 -1 -1 -1 -1 -1 15 -1 -1 -1 -1 -1 -1 62 -1 -1 -1 -1 -1 -1 -1 62 -1 -1 -1
9 -1 -1 26 -1 64 -1 -1 -1 -1 -1 26 24 -1 -1 -1 -1 34 35 36 -1 -1 -1 -1 18 -1 -1 18 -1 -1 -1 -1 -1
10 -1 -1 -1 -1 -1 -1 64 -1 -1 -1 -1 -1 -1 -1 0 1 53 54 55 2 -1 -1 -1 -1 -1 -1 -1 62 -1 -1 -1 -1
11 -1 -1 -1 64 30 -1 -1 0 1 1 1 1 1 1 19 33 -1 -1 44 21 2 18 -1 44 -1 -1 -1 -1 -1 -1 -1 -1
12 -1 -1 -1 -1 -1 -1 -1 19 22 23 52 -1 52 62 38 -1 -1 -1 44 40 21 -1 -1 -1 -1 11 -1 -1 -1 -1 -1 -1
13 -1 15 -1 -1 -1 -1 15 19 41 42 -1 -1 -1 -1 57 58 58 58 20 59 21 -1 -1 -1 -1 -1 -1 -1 -1 -1 62 -1
14 -1 -1 -1 -1 -1 -1 -1 38 -1 -1 49 -1 -1 -1 -1 -1 -1 -1 -1 52 21 -1 -1 44 -1 -1 -1 -1 -1 -1 -1 -1
15 -1 -1 15 -1 -1 -1 -1 57 58 58 58 58 20 58 58 58 58 58 58 58 59 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
16 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

View File

@ -1,8 +0,0 @@
19,-1,0,0,1,1,1,1,1,1,2,1,1,1,2,2
19,43,19,19,-1,62,3,-1,-1,62,21,-1,-1,-1,21,21
19,-1,19,19,62,-1,-1,44,43,-1,21,-1,-1,-1,21,21
19,-1,19,38,-1,-1,64,64,-1,-1,40,-1,-1,-1,21,21
19,-1,38,57,58,20,58,58,58,58,59,-1,-1,-1,40,21
19,62,57,58,58,20,58,58,58,58,58,58,20,58,59,21
38,-1,-1,-1,30,-1,-1,43,-1,83,-1,-1,-1,-1,64,40
57,58,58,58,58,58,58,58,58,58,58,58,58,58,58,59
1 19 -1 0 0 1 1 1 1 1 1 2 1 1 1 2 2
2 19 43 19 19 -1 62 3 -1 -1 62 21 -1 -1 -1 21 21
3 19 -1 19 19 62 -1 -1 44 43 -1 21 -1 -1 -1 21 21
4 19 -1 19 38 -1 -1 64 64 -1 -1 40 -1 -1 -1 21 21
5 19 -1 38 57 58 20 58 58 58 58 59 -1 -1 -1 40 21
6 19 62 57 58 58 20 58 58 58 58 58 58 20 58 59 21
7 38 -1 -1 -1 30 -1 -1 43 -1 83 -1 -1 -1 -1 64 40
8 57 58 58 58 58 58 58 58 58 58 58 58 58 58 58 59

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 B

View File

@ -9,28 +9,24 @@ void analyze_input(struct game *game, const int last_key)
game->total_tick += ENGINE_TICK;
switch (last_key)
{
case 0:
if (!(game->total_tick % 500)) game->player.animation_frame = (game->player.animation_frame + 1) % 2;
break;
case KEY_UP:
if (is_walkable(*game, UP)) game->world_y -= 1;
game->player.direction = UP;
game->player.y -= 2;
break;
case KEY_RIGHT:
if (is_walkable(*game, RIGHT)) game->world_x += 1;
game->player.direction = RIGHT;
game->player.x += 2;
break;
case KEY_DOWN:
if (is_walkable(*game, DOWN)) game->world_y += 1;
game->player.direction = DOWN;
game->player.y += 2;
break;
case KEY_LEFT:
if (is_walkable(*game, LEFT)) game->world_x -= 1;
game->player.direction = LEFT;
game->player.x -= 2;
break;
}
}
@ -51,4 +47,29 @@ int callback_tick(volatile int *tick)
{
*tick = 1;
return TIMER_CONTINUE;
}
int is_walkable(const struct game game, const int direction)
{
switch (direction)
{
case UP:
return game.map.collision[game.world_y + 4][game.world_x + 7];
break;
case RIGHT:
return game.map.collision[game.world_y + 5][game.world_x + 8];
break;
case DOWN:
return game.map.collision[game.world_y + 6][game.world_x + 7];
break;
case LEFT:
return game.map.collision[game.world_y + 5][game.world_x + 6];
break;
}
return 0;
}

View File

@ -8,8 +8,10 @@
#define TILESET_WIDTH 19
#define TILE_SIZE 8
#define MAP_WIDTH 16
#define MAP_HEIGHT 8
#define MAP_WIDTH 32
#define MAP_HEIGHT 16
#define SCREEN_WIDTH 16
#define SCREEN_HEIGHT 8
#define UP 0
#define RIGHT 1
@ -18,21 +20,21 @@
struct player
{
int x, y;
int direction, animation_frame;
int direction;
};
struct map_t
struct map
{
int data[MAP_HEIGHT][MAP_WIDTH];
uint8_t collision[MAP_HEIGHT][MAP_WIDTH];
};
struct game
{
// Current map and coord's map in the world
struct map_t map;
struct map map;
int world_x, world_y;
struct player player;
@ -49,4 +51,6 @@ int rtc_key(void);
// callback_tick : timer's function
int callback_tick(volatile int *tick);
int is_walkable(const struct game game, const int direction);
#endif /* _CORE_H */

View File

@ -8,13 +8,13 @@ void draw_map(const struct game game)
{
extern const bopti_image_t img_tileset;
for (int i = 0; i < MAP_HEIGHT; i ++)
for (int i = 0; i < SCREEN_HEIGHT; i ++)
{
for (int j = 0; j < MAP_WIDTH; j++)
for (int j = 0; j < SCREEN_WIDTH; j++)
{
if (game.map.data[i][j] != -1)
if (game.map.data[game.world_y + i][game.world_x + j] != -1 && game.world_y + i < (MAP_HEIGHT) && game.world_y + i >= 0 && game.world_x + j < (MAP_WIDTH) && game.world_x + j >= 0)
{
int tile_x = (TILE_SIZE + 1) * (game.map.data[i][j] % TILESET_WIDTH), tile_y = (TILE_SIZE + 1) * (game.map.data[i][j] / TILESET_WIDTH);
int tile_x = (TILE_SIZE + 1) * (game.map.data[game.world_y + i][game.world_x + j] % TILESET_WIDTH), tile_y = (TILE_SIZE + 1) * (game.map.data[game.world_y + i][game.world_x + j] / TILESET_WIDTH);
dsubimage(j * TILE_SIZE, i * TILE_SIZE, &img_tileset, tile_x, tile_y, 8, 8, DIMAGE_NONE);
}
}
@ -26,5 +26,5 @@ void draw_player(const struct game game)
{
extern const bopti_image_t img_player;
dsubimage(game.player.x, game.player.y, &img_player, 9 * game.player.animation_frame, 13 * game.player.direction, 8, 12, DIMAGE_NONE);
dsubimage(56, 35, &img_player, 0, 13 * game.player.direction, 8, 12, DIMAGE_NONE);
}

View File

@ -88,21 +88,14 @@ void title_screen(void)
void main_loop(void)
{
extern const struct map_t map_one;
extern const struct map map_world;
int key = 0;
static volatile int tick = 0;
int t = timer_configure(TIMER_ANY, ENGINE_TICK*1000, GINT_CALL(callback_tick, &tick));
if (t >= 0) timer_start(t);
struct player player = {8, 0, 2, 0};
struct game game = {map_one, 0, 0, player, 0};
struct player player = {DOWN};
struct game game = {map_world, 0, 0, player, 0};
while (key != KEY_EXIT)
{
while (!tick) sleep();
tick = 0;
dclear(C_WHITE);
draw_map(game);
@ -110,9 +103,7 @@ void main_loop(void)
dupdate();
key = rtc_key();
key = getkey().key;
analyze_input(&game, key);
}
if (t >= 0) timer_stop(t);
}