speed up camera, camera stays on player even inside a house

This commit is contained in:
bgiraudr 2021-08-25 15:46:05 +02:00
parent e8b5b1557a
commit 0477f1210c
8 changed files with 37 additions and 72 deletions

View File

@ -1,21 +1,8 @@
#pragma once
#include "camera.h"
#include "game.h"
#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 camera*/
struct Camera camera;
/*the background color*/
int background;
};
/*draw the current state of the game*/
void engine_draw(struct Game const *game);
/*draw the map around the player*/

View File

@ -1,4 +1,18 @@
#pragma once
#include "camera.h"
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 camera*/
struct Camera camera;
/*the background color*/
int background;
};
/*get the input with a timeout*/
int get_inputs(void);

View File

@ -27,6 +27,4 @@ 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);
bool is_map_larger(struct Map *map);
void generate_interior_map(struct Game *game);

View File

@ -11,6 +11,6 @@ struct Camera camera_new(struct Vec2f *target) {
}
void camera_update(struct Camera *c) {
c->pos = vec2f_lerp(c->pos, *c->target, 0.05);
c->pos = vec2f_lerp(c->pos, *c->target, 0.08);
c->offset = vec2f_vec2(c->pos);
}

View File

@ -8,6 +8,7 @@
#include "define.h"
#include "character.h"
#include "camera.h"
#include "vec2.h"
/*draw the current state of the game*/
void engine_draw(struct Game const *game) {
@ -40,16 +41,9 @@ void engine_draw_map(struct Game const *game) {
unsigned int tile_x = TILE_SIZE * (tile_id % TILESET_WIDTH);
unsigned int tile_y = TILE_SIZE * (tile_id / TILESET_WIDTH);
//provisoire le temps de trouver une manière propre
if(is_map_larger(game->map)) {
dsubimage(x * TILE_SIZE - x_offset%TILE_SIZE,
y * TILE_SIZE - y_offset%TILE_SIZE, game->map->tileset,
tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE);
} else {
dsubimage(x * TILE_SIZE - x_offset%TILE_SIZE,
y * TILE_SIZE - y_offset%TILE_SIZE, game->map->tileset,
tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE);
}
dsubimage(x * TILE_SIZE - x_offset%TILE_SIZE,
y * TILE_SIZE - y_offset%TILE_SIZE, game->map->tileset,
tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE);
}
}
}
@ -62,20 +56,11 @@ void engine_draw_player(struct Game const *g) {
struct Vec2f draw_offset = vec2f_sub(g->player->pos_visual, vec2_vec2f(g->camera.offset));
draw_offset.x += g->player->anim.dx * 3;
draw_offset.y += g->player->anim.dy * 3;
if(is_map_larger(g->map)) {
dframe(g->player->show_x * TILE_SIZE + draw_offset.x,
g->player->show_y * TILE_SIZE - 5 + draw_offset.y,
g->player->anim.img); //draw the player 5 pixel up
} else {
const int offset_map_x = (DWIDTH / TILE_SIZE - g->map->w + 1)/2;
const int offset_map_y = (DHEIGHT / TILE_SIZE - g->map->h + 1)/2;
dframe(g->player->show_x * TILE_SIZE + draw_offset.x + g->player->x_mid,
g->player->show_y * TILE_SIZE - 5 + draw_offset.y + g->player->y_mid,
g->player->anim.img); //draw the player 5 pixel up
dframe(
(g->player->pos.x + offset_map_x) * TILE_SIZE + g->player->anim.dx*3,
(g->player->pos.y + offset_map_y) * TILE_SIZE - 5 + g->player->anim.dy*3,
g->player->anim.img); //draw the player 5 pixel up
}
dprint(1,1,C_BLACK,"%d:%d",g->player->pos.x, g->player->pos.y);
}

View File

@ -19,8 +19,8 @@ struct Map *maps[] = {
};
static int callback_tick(volatile int *tick) {
*tick = 1;
return TIMER_CONTINUE;
*tick = 1;
return TIMER_CONTINUE;
}
int main(void) {

View File

@ -9,28 +9,18 @@ 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);
return 1;
}
/*get the tile under the player*/
int map_get_player_tile(struct Game const *game) {
return game->map->info_map[game->player->pos.x + game->map->w * game->player->pos.y];
return 0;
}
void generate_interior_map(struct Game *game) {
extern struct Map in_1;
game->map = &in_1;
set_player_xy(game->player, 3,3);
//game->camera.target = &VEC2F(DWIDTH/2, DHEIGHT/2);
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;
}
/*return true if the map is larger than the screen. false otherwise*/
bool is_map_larger(struct Map *map) {
if(map->w > DWIDTH / TILE_SIZE + 1 && map->h > DHEIGHT / TILE_SIZE + 1) {
return true;
} else {
return false;
}
}
}

View File

@ -1,55 +1,46 @@
#include "vec2.h"
struct Vec2
vec2_add(struct Vec2 v1, struct Vec2 v2)
struct Vec2 vec2_add(struct Vec2 v1, struct Vec2 v2)
{
return VEC2(v1.x + v2.x, v1.y + v2.y);
}
struct Vec2f
vec2f_add(struct Vec2f v1, struct Vec2f v2)
struct Vec2f vec2f_add(struct Vec2f v1, struct Vec2f v2)
{
return VEC2F(v1.x + v2.x, v1.y + v2.y);
}
struct Vec2
vec2_sub(struct Vec2 v1, struct Vec2 v2)
struct Vec2 vec2_sub(struct Vec2 v1, struct Vec2 v2)
{
return VEC2(v1.x - v2.x, v1.y - v2.y);
}
struct Vec2f
vec2f_sub(struct Vec2f v1, struct Vec2f v2)
struct Vec2f vec2f_sub(struct Vec2f v1, struct Vec2f v2)
{
return VEC2F(v1.x - v2.x, v1.y - v2.y);
}
struct Vec2
vec2_mul(struct Vec2 v, int scale)
struct Vec2 vec2_mul(struct Vec2 v, int scale)
{
return VEC2(v.x * scale, v.y * scale);
}
struct Vec2f
vec2f_mul(struct Vec2f v, int scale)
struct Vec2f vec2f_mul(struct Vec2f v, int scale)
{
return VEC2F(v.x * scale, v.y * scale);
}
struct Vec2
vec2f_vec2(struct Vec2f v)
struct Vec2 vec2f_vec2(struct Vec2f v)
{
return VEC2(v.x, v.y);
}
struct Vec2f
vec2_vec2f(struct Vec2 v)
struct Vec2f vec2_vec2f(struct Vec2 v)
{
return VEC2F(v.x, v.y);
}
struct Vec2f
vec2f_lerp(struct Vec2f from, struct Vec2f to, float scale) {
struct Vec2f vec2f_lerp(struct Vec2f from, struct Vec2f to, float scale) {
/* Linear interpolation: can be used for camera and animations.
* 'scale' is the transformation speed, 0 slow and 1 fast. */
return VEC2F(