diff --git a/project/assets-fx/converters.py b/project/assets-fx/converters.py index 315f68a..2f274f7 100644 --- a/project/assets-fx/converters.py +++ b/project/assets-fx/converters.py @@ -23,7 +23,7 @@ def convert_map(input_name, output, params, target): for value in line.split(","): value = int(value) tiles += fxconv.u32(value) - collision += fxconv.u8(int(value in walkable)) + collision += fxconv.u8(value in walkable) data = fxconv.ObjectData() data += tiles + collision diff --git a/project/assets-fx/map/world.csv b/project/assets-fx/map/world.csv index dd902c8..95126fb 100644 --- a/project/assets-fx/map/world.csv +++ b/project/assets-fx/map/world.csv @@ -29,4 +29,4 @@ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,63,-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,63,-1,-1,-1,-1,-1,-1,-1,-1,-1,25,-1,-1,-1,57,20,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,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,25,-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,-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,-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,-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,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 \ No newline at end of file diff --git a/project/assets-fx/player.png b/project/assets-fx/player.png index 4e1a81c..32a0bc2 100644 Binary files a/project/assets-fx/player.png and b/project/assets-fx/player.png differ diff --git a/project/assets-fx/tileset.png b/project/assets-fx/tileset.png index 74c3e4e..45d6e2d 100644 Binary files a/project/assets-fx/tileset.png and b/project/assets-fx/tileset.png differ diff --git a/project/src/core.c b/project/src/core.c index 4096231..edbf8f8 100644 --- a/project/src/core.c +++ b/project/src/core.c @@ -6,56 +6,78 @@ void next_frame(struct game *game) { - game->water_frame = (game->water_frame + 1) % 2; + game->total_tick += ENGINE_TICK; + + if (!(game->total_tick % 200)) game->water_frame = (game->water_frame + 1) % 8; + if (!(game->total_tick % 500)) + { + switch (game->player->animation_frame) + { + case 2: + game->player->animation_frame = 0; + break; + + case 3: + game->player->animation_frame = 1; + break; + + default: + game->player->animation_frame = (game->player->animation_frame + 1) % 2; + break; + } + } } void analyze_input(struct game *game, const int last_key) { - game->total_tick += ENGINE_TICK; switch (last_key) { case KEY_UP: - if (is_walkable(*game, UP)) game->world_y -= 1; - game->player.direction = UP; + if (is_walkable(game, UP)) game->world_y -= 1; + game->player->direction = UP; + game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2; break; case KEY_RIGHT: - if (is_walkable(*game, RIGHT)) game->world_x += 1; - game->player.direction = RIGHT; + if (is_walkable(game, RIGHT)) game->world_x += 1; + game->player->direction = RIGHT; + game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2; break; case KEY_DOWN: - if (is_walkable(*game, DOWN)) game->world_y += 1; - game->player.direction = DOWN; + if (is_walkable(game, DOWN)) game->world_y += 1; + game->player->direction = DOWN; + game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2; break; case KEY_LEFT: - if (is_walkable(*game, LEFT)) game->world_x -= 1; - game->player.direction = LEFT; + if (is_walkable(game, LEFT)) game->world_x -= 1; + game->player->direction = LEFT; + game->player->animation_frame = 2 + (game->player->animation_frame + 1) % 2; break; } } -int is_walkable(const struct game game, const int direction) +uint8_t 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]; + if (game->world_y + 4 >= 0) 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]; + if (game->world_x + 8 < MAP_WIDTH) 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]; + if (game->world_y + 6 < MAP_HEIGHT) 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]; + if (game->world_x + 5 >= 0) return game->map->collision[game->world_y + 5][game->world_x + 6]; break; } return 0; diff --git a/project/src/core.h b/project/src/core.h index d985f51..25fffbb 100644 --- a/project/src/core.h +++ b/project/src/core.h @@ -7,11 +7,12 @@ #define TILESET_WIDTH 19 #define TILE_SIZE 8 -#define MAP_WIDTH 64 -#define MAP_HEIGHT 32 #define SCREEN_WIDTH 16 #define SCREEN_HEIGHT 8 +#define MAP_WIDTH 64 +#define MAP_HEIGHT 32 + #define UP 0 #define RIGHT 1 #define DOWN 2 @@ -20,6 +21,7 @@ struct player { int direction; + int animation_frame; }; struct map @@ -33,10 +35,10 @@ struct map struct game { // Current map and coord's map in the world - struct map map; + const struct map *map; int world_x, world_y; - struct player player; + struct player *player; int total_tick; int water_frame; @@ -49,7 +51,7 @@ void next_frame(struct game *game); void analyze_input(struct game *game, const int last_key); // is_walkable : check if the player can go on the targeted case -int is_walkable(const struct game game, const int direction); +uint8_t is_walkable(const struct game *game, const int direction); // rtc_key : returns the key code with RTC system int rtc_key(void); diff --git a/project/src/display_engine.c b/project/src/display_engine.c index cfbf6be..6c882f7 100644 --- a/project/src/display_engine.c +++ b/project/src/display_engine.c @@ -4,7 +4,7 @@ #include "display_engine.h" -void draw_map(const struct game game) +void draw_map(const struct game *game) { extern const bopti_image_t img_tileset; extern const bopti_image_t img_water; @@ -13,11 +13,13 @@ void draw_map(const struct game game) { for (int j = 0; j < SCREEN_WIDTH; j++) { - 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[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); - if (game.map.data[game.world_y + i][game.world_x + j] != 56) dsubimage(j * TILE_SIZE, i * TILE_SIZE, &img_tileset, tile_x, tile_y, 8, 8, DIMAGE_NONE); - else dsubimage(j * TILE_SIZE, i * TILE_SIZE, &img_water, 9 * game.water_frame, 0, 8, 8, DIMAGE_NONE); + if ((game->world_y + i < MAP_HEIGHT && game->world_y + i >= 0 && game->world_x + j < MAP_WIDTH && game->world_x + j >= 0) && (game->map->data[game->world_y + i][game->world_x + j] != -1)) + { + unsigned int tile_x = (TILE_SIZE + 1) * (game->map->data[game->world_y + i][game->world_x + j] % TILESET_WIDTH); + unsigned int tile_y = (TILE_SIZE + 1) * (game->map->data[game->world_y + i][game->world_x + j] / TILESET_WIDTH); + + if (game->map->data[game->world_y + i][game->world_x + j] != 56) dsubimage(j * TILE_SIZE, i * TILE_SIZE, &img_tileset, tile_x, tile_y, 8, 8, DIMAGE_NONE); + else dsubimage(j * TILE_SIZE, i * TILE_SIZE, &img_water, 9 * game->water_frame, 0, 8, 8, DIMAGE_NONE); } } @@ -25,9 +27,9 @@ void draw_map(const struct game game) } -void draw_player(const struct game game) +void draw_player(const struct game *game) { extern const bopti_image_t img_player; - dsubimage(56, 35, &img_player, 0, 13 * game.player.direction, 8, 12, DIMAGE_NONE); + dsubimage(56, 35, &img_player, 9 * game->player->animation_frame, 13 * game->player->direction, 8, 12, DIMAGE_NONE); } diff --git a/project/src/display_engine.h b/project/src/display_engine.h index 6f8b7fe..7928b70 100644 --- a/project/src/display_engine.h +++ b/project/src/display_engine.h @@ -4,10 +4,10 @@ #include "core.h" // draw_map : display a map on the screen -void draw_map(const struct game game); +void draw_map(const struct game *game); //draw_player : display the player with animation -void draw_player(const struct game game); +void draw_player(const struct game *game); #endif /* _DISPLAY_ENGINE_H */ diff --git a/project/src/main.c b/project/src/main.c index 24ab675..f71b850 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -57,24 +57,23 @@ void main_loop(void) extern const struct map map_world; int key = 0; - struct player player = {DOWN}; - struct game game = {map_world, 0, 0, player, 0, 0}; + struct player player = {DOWN, 0}; + struct game game = {&map_world, 0, 0, &player, 0, 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); + if (t >= 0) timer_start(t); while (key != KEY_EXIT) { while (!tick) sleep(); tick = 0; - dclear(C_WHITE); next_frame(&game); - draw_map(game); - draw_player(game); + draw_map(&game); + draw_player(&game); dupdate();