Single screen level now use fixed camera.

This commit is contained in:
KikooDX 2020-09-23 15:56:49 +02:00
parent 52477daac0
commit 62ca2886e9
3 changed files with 26 additions and 21 deletions

View File

@ -14,4 +14,4 @@ local function create_random_level(width, height, layers)
end
end
create_random_level(50, 20, 1)
create_random_level(10, 4, 1)

View File

@ -21,14 +21,30 @@ void camera_step(Camera *camera)
void camera_init(Camera *camera, Player *player, const Level *level)
{
/* calculate min and max */
vec_cpy(&camera->min, VEC_SCALED_DCENTER);
vec_mul(&camera->min, VEC_PRECISION);
vec_cpy(&camera->max, (Vec){0, 0});
vec_sub(&camera->max, VEC_SCALED_DCENTER);
vec_mul(&camera->max, VEC_PRECISION);
vec_add(&camera->max, (Vec){TILE_SIZE * level->width, TILE_SIZE * level->height});
vec_cpy(&camera->pos, player->pos);
/* check level size */
const Vec level_dim = {level->width * (TILE_SIZE / VEC_PRECISION * SCALE),
level->height * (TILE_SIZE / VEC_PRECISION * SCALE)};
if (level_dim.x > DWIDTH || level_dim.y > DHEIGHT)
{
/* level cannot be displayed on a single screen */
/* calculate min and max */
vec_cpy(&camera->min, VEC_SCALED_DCENTER);
vec_mul(&camera->min, VEC_PRECISION);
vec_cpy(&camera->max, (Vec){0, 0});
vec_sub(&camera->max, VEC_SCALED_DCENTER);
vec_mul(&camera->max, VEC_PRECISION);
vec_add(&camera->max, (Vec){TILE_SIZE * level->width, TILE_SIZE * level->height});
vec_cpy(&camera->pos, player->pos);
}
else
{
/* level is single screen */
/* We calculate the offset and setup the camera to center everything. */
const Vec screen_center_precise = {level->width * TILE_SIZE / (2 * SCALE),
level->height * TILE_SIZE / (2 * SCALE)};
vec_cpy(&camera->min, screen_center_precise);
vec_cpy(&camera->max, screen_center_precise);
}
vec_clamp(&camera->pos, camera->min, camera->max);
}
@ -38,15 +54,4 @@ void camera_draw_debug(Camera *camera)
dprint(0, 10, C_BLACK, "+x: %d, +y: %d", camera->max.x, camera->max.y);
dprint(0, 20, C_BLACK, "cx: %d, cy: %d", camera->pos.x, camera->pos.y);
dprint(0, 30, C_BLACK, "px: %d, py: %d", camera->offset.x, camera->offset.y);
/*Vec draw_pos;
vec_cpy(&draw_pos, camera->pos);
vec_mul(&draw_pos, SCALE);
vec_div(&draw_pos, VEC_PRECISION);
#ifdef FX9860G
const int color = C_BLACK;
#endif /* FX9860G *//*
#ifdef FXCG50
const int color = C_RED;
#endif /* FXCG50 *//*
dpixel(draw_pos.x, draw_pos.y, color);*/
}

View File

@ -31,7 +31,7 @@ int play_level(uint level_id)
{
/* create player */
Player player = {
.pos = {64 * VEC_PRECISION, 64 * VEC_PRECISION},
.pos = {TILE_SIZE, TILE_SIZE},
.hbox = {7 * VEC_PRECISION, 7 * VEC_PRECISION},
.vbox = {7, 7},
.origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION}