camera movement made fluid

This commit is contained in:
Mottributo 2023-03-25 22:39:36 +03:00
parent c7e5d9bb58
commit 6ff8a3fd1a

View File

@ -1,5 +1,6 @@
var floorPos_y;
var cameraPosX;
var camera_speed;
var processStop_timer;
var fps_recent_values = [];
var showDebugData = false;
@ -53,13 +54,13 @@ function setup() {
}
function startGame(level_start) {
floorPos_y = 432;
cameraPosX = 0;
// levels of depth where the character, collectable and enemies stand.
groundPositions = [
floorPos_y + (height - floorPos_y) / 6,
floorPos_y + (3 * (height - floorPos_y)) / 6,
floorPos_y + (5 * (height - floorPos_y)) / 6,
];
camera_speed = 0.9;
gravity = 1;
game_score = 0;
finish_position_x = 2000;
@ -73,7 +74,6 @@ function startGame(level_start) {
cell_size_v: 20,
cell_size_h: 20,
};
if (level_start) {
gameChar = {
x: width / 2,
@ -285,6 +285,7 @@ function startGame(level_start) {
}
},
};
cameraPosX = gameChar.x;
// Creating trees, clouds, mountains, canyons, collectables.
{
// Creating trees, clouds, mountains, canyons, collectables, platforms, enemies.
@ -397,11 +398,15 @@ function draw() {
// -------- GROUND -------------
drawGround();
push(); // Scrolling
push();
fill(0);
translate(-cameraPosX, 0); // Scrolling
if (gameChar.x < finish_position_x) cameraPosX = gameChar.x - width / 2; // Scrolling
translate(-cameraPosX, 0); // Scrolling everything
// Focusing on the character. lerp() ensures fluid camera movement.
if (gameChar.x < finish_position_x)
cameraPosX = lerp(gameChar.x - width/2, cameraPosX, camera_speed);
else
cameraPosX = lerp(finish_position_x - width/2, cameraPosX, camera_speed);
// -------- CLOUDS --------------
drawClouds();