rewrite more code for camera

This commit is contained in:
KikooDX 2021-08-25 01:30:30 +02:00
parent 0c5256d55c
commit 3917b9561a
8 changed files with 27 additions and 15 deletions

View File

@ -11,3 +11,4 @@ struct Camera {
};
struct Camera camera_new(struct Vec2f *target);
void camera_update(struct Camera*);

View File

@ -1,4 +1,4 @@
/*the width of the tileset*/
#define TILESET_WIDTH 29
/*the size of one tile*/
#define TILE_SIZE 16
#define TILE_SIZE 16

View File

@ -32,4 +32,3 @@ void engine_set_background(struct Game *game, int color);
void engine_action(struct Game const *game, int action);
/*check the current position of the player. To perform action depends of his location*/
void engine_check_position(struct Game *game);
void vec_lerp(struct Camera *from, struct Player const *to, float scale);

View File

@ -21,5 +21,6 @@ struct Vec2 vec2_add(struct Vec2, struct Vec2);
struct Vec2f vec2f_add(struct Vec2f, struct Vec2f);
struct Vec2 vec2_sub(struct Vec2, struct Vec2);
struct Vec2 vec2_mul(struct Vec2, int scale);
struct Vec2f vec2f_mul(struct Vec2f, int scale);
struct Vec2 vec2f_vec2(struct Vec2f);
struct Vec2f vec2_vec2f(struct Vec2);

View File

@ -1,4 +1,5 @@
#include "vec2.h"
#include "define.h"
#include "camera.h"
struct Camera camera_new(struct Vec2f *target) {
@ -8,3 +9,8 @@ struct Camera camera_new(struct Vec2f *target) {
.target = target,
};
}
void camera_update(struct Camera *c) {
c->pos = *c->target;
c->offset = vec2f_vec2(c->pos);
}

View File

@ -88,11 +88,6 @@ int engine_move(struct Game *game, int direction) {
game->player->pos.x += dx;
game->player->pos.y += dy;
if(is_map_larger(game->map)) {
game->camera.offset.x += dx*16;
game->camera.offset.y += dy*16;
}
game->player->idle = !anim_player_walking(&game->player->anim, 1);
engine_check_position(game);
} else {
@ -105,12 +100,15 @@ 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;
if(game->player->anim.duration <= 0) {
game->player->idle = !game->player->anim.function(&game->player->anim, 0);
void engine_tick(struct Game *g, int dt) {
/* update the player animation */
g->player->anim.duration -= dt;
if(g->player->anim.duration <= 0) {
g->player->idle = !g->player->anim.function(&g->player->anim, 0);
}
/* update camera */
g->player->pos_visual = vec2_vec2f(vec2_mul(g->player->pos, TILE_SIZE));
camera_update(&g->camera);
}
/*set the background color*/

View File

@ -1,4 +1,5 @@
#include "player.h"
#include "define.h"
#include "engine.h"
#include "map.h"
#include "game.h"
@ -23,7 +24,7 @@ int player_facing(struct Game const *game) {
}
/* lol */
void set_player_xy(struct Player *player, int x, int y) {
player->pos.x = x;
player->pos.y = y;
void set_player_xy(struct Player *p, int x, int y) {
p->pos.x = x;
p->pos.y = y;
}

View File

@ -24,6 +24,12 @@ vec2_mul(struct Vec2 v, int scale)
return VEC2(v.x * scale, v.y * 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)
{