add loop for dialog + comment

This commit is contained in:
bgiraudr 2021-08-08 01:43:26 +02:00
parent 0e72369cf0
commit 4dedfef6f9
13 changed files with 90 additions and 6 deletions

View File

@ -1,6 +1,7 @@
tileset.png:
type: bopti-image
name: img_tileset
profile:p4
spritesheet.png:
type: bopti-image

View File

@ -9,18 +9,27 @@ anim_function_t anim_player_idle;
struct anim_frame
{
/*the image*/
bopti_image_t *source;
/*the left and top pixels to know where to start drawing*/
int left, top;
/*the width and height of one frame*/
int w, h;
};
struct anim_data
{
/*the function to call to update the frame*/
anim_function_t *function;
/*the anim frame*/
struct anim_frame img;
/*if the animation needs to move*/
int dx, dy;
/*the direction*/
int dir;
/*the current frame*/
int frame;
/*the duration of one frame*/
int duration;
};

View File

@ -1,6 +1,11 @@
#pragma once
struct character {
/*the position of the character*/
int x, y;
/*the name*/
char *name;
/*the text to say*/
char *dialog;
};

View File

@ -1,2 +1,4 @@
#define PLAYER_VIEW_X 5
#define PLAYER_VIEW_Y 5
/*the x radius the player is able to see*/
#define PLAYER_VIEW_X 13
/*the y radius the player is able to see*/
#define PLAYER_VIEW_Y 7

View File

@ -3,9 +3,13 @@
#define ENGINE_TICK 35
struct game {
/*the current map to display*/
struct map *map;
/*the player*/
struct player *player;
/*list of all the characters on the map*/
struct character **characters;
/*the background color*/
int background;
};

View File

@ -1,6 +1,5 @@
#pragma once
void play();
int get_inputs(void);
enum direction {
@ -8,5 +7,6 @@ enum direction {
DIR_LEFT = 1,
DIR_UP = 2,
DIR_RIGHT = 3,
ACTION_SHIFT = 4
ACTION_SHIFT = 4,
ACTION_ALPHA = 5
};

View File

@ -1,10 +1,15 @@
#pragma once
struct map {
/*width, height and the number of layer of the map (max 2)*/
int w, h, nb_layer;
/*the tileset to use*/
bopti_image_t *tileset;
/*list of all background tiles (layer 1)*/
short *tiles_layer1;
/*list of all "physical" tiles (layer 2)*/
short *tiles_layer2;
/*state of each tile on the map (solid, air ...)*/
short *info_map;
};

View File

@ -4,12 +4,18 @@
#include "engine.h"
struct player {
/*current position of the player on the map*/
int x, y;
/*the direction the player facing to*/
int direction;
/*current frame of the animation*/
int frame;
/*where to draw the player on the screen*/
int show_x, show_y;
/*the current animation*/
int idle;
struct anim_data anim;
};
/*return the info tile value the player is facing to*/
int player_facing(struct game const *game);

View File

@ -6,8 +6,11 @@
struct sheet
{
/*the sheet image of the animation*/
bopti_image_t *img;
/*the frame width*/
int frame_w;
/*the frame height*/
int frame_h;
};
@ -29,11 +32,13 @@ static struct anim_frame anim_frame(struct sheet const *sheet, int col, int row)
return f;
}
/*draw the frame*/
void dframe(int x, int y, struct anim_frame const frame) {
dsubimage(x, y, frame.source, frame.left, frame.top, frame.w, frame.h,
DIMAGE_NONE);
}
/*animation for player walking*/
int anim_player_walking(struct anim_data *data, int init) {
if(init) {
data->function = anim_player_walking;
@ -60,6 +65,7 @@ int anim_player_walking(struct anim_data *data, int init) {
return 1;
}
/*animation for player doing nothing*/
int anim_player_idle(struct anim_data *data, int init) {
if(init) {
data->function = anim_player_idle;

View File

@ -3,15 +3,25 @@
#include <string.h>
#include "character.h"
/*draw the dialog of a specified character*/
void draw_dialog(struct character *character) {
dclear(C_WHITE);
drect(1,1,400,80,C_WHITE);
dprint(1,20, C_BLACK, "(%d,%d)", character->x, character->y);
dprint(1,40, C_BLACK, "%s", character->name);
dprint(1,60, C_BLACK, "%s", character->dialog);
dupdate();
getkey();
int buffer = 1;
while(1) {
clearevents();
if(keydown(KEY_SHIFT)) {
if(buffer) buffer = 0;
else break;
}
while(keydown(KEY_SHIFT)) clearevents();
}
}
/*find the character using the player's position*/
struct character* get_character_xy(struct character *characters[], int x, int y) {
int i = 0;
while(strcmp(characters[i]->name,"default name") != 0) {

View File

@ -10,12 +10,14 @@
#define TILESET_WIDTH 29
/*draw the current state of the game*/
void engine_draw(struct game const *game) {
dclear(game->background);
engine_draw_map_around_player(game);
engine_draw_player(game->player);
}
/*draw the map around the player*/
void engine_draw_map_around_player(struct game const *game) {
const int level_width = game->map->w;
const int taillemap = game->map->w * game->map->h;
@ -89,11 +91,13 @@ void engine_draw_map_around_player(struct game const *game) {
}
}
/*draw the player*/
void engine_draw_player(struct player const *player) {
dframe(player->show_x * 16, player->show_y * 16 - 5, player->anim.img); //draw the player 5 pixel up
dprint(1,1,C_BLACK,"%d:%d",player->x, player->y);
}
/*move the player to the direction*/
int engine_move(struct game *game, int direction) {
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
@ -115,6 +119,7 @@ int engine_move(struct game *game, int direction) {
return 1;
}
/*update the player animation*/
void engine_tick(struct game *game, int dt) {
game->player->anim.duration -= dt;
@ -123,16 +128,19 @@ void engine_tick(struct game *game, int dt) {
}
}
/*check if a tile is walkable*/
int map_walkable(struct map const *map, int x, int y) {
int tile = map->info_map[x + map->w * y];
if(x < 0 || x > map->w-1 || y < 0 || y > map->h-1) return 0;
return (tile != TILE_SOLID && tile != TILE_CHARACTER);
}
/*set the background color*/
void engine_set_background(struct game *game, int color) {
game->background = color;
}
/*make an interaction with something*/
void engine_action(struct game const *game, int action) {
if(action == ACTION_SHIFT) {
if(player_facing(game) == TILE_CHARACTER) {

View File

@ -3,6 +3,7 @@
#include "game.h"
#include "player.h"
/*get the input with a timeout*/
int get_inputs(void) {
int opt = GETKEY_DEFAULT & GETKEY_REP_ARROWS;
int timeout = 1;
@ -13,10 +14,14 @@ int get_inputs(void) {
if(ev.type == KEYEV_NONE) return -1;
int key = ev.key;
/*direction*/
if(key == KEY_DOWN) return DIR_DOWN;
if(key == KEY_RIGHT) return DIR_RIGHT;
if(key == KEY_UP) return DIR_UP;
if(key == KEY_LEFT) return DIR_LEFT;
/*action key*/
if(key == KEY_SHIFT) return ACTION_SHIFT;
if(key == KEY_ALPHA) return ACTION_ALPHA;
}
}

23
src/player.c Normal file
View File

@ -0,0 +1,23 @@
#include "player.h"
#include "engine.h"
#include "map.h"
#include "game.h"
/*
return the info tile value the player is facing to
TILE_SOLID by default (out of bound)
*/
int player_facing(struct game const *game) {
int direction = game->player->direction;
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
int index = game->player->x + dx + game->map->w * (game->player->y + dy);
if(game->player->x + dx >= 0 &&
game->player->x + dx <= game->map->w &&
game->player->y + dy >= 0 &&
game->player->y + dy <= game->map->h) {
return game->map->info_map[index];
}
return TILE_SOLID;
}