Add animated player and animated water

This commit is contained in:
Shadow15510 2021-07-17 15:32:47 +02:00
parent 8778623b1c
commit 3c98e0168e
9 changed files with 63 additions and 38 deletions

View File

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

View File

@ -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,-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 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Binary file not shown.

Before

Width:  |  Height:  |  Size: 897 B

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

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

View File

@ -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);

View File

@ -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);
}

View File

@ -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 */

View File

@ -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();