From 6ff8a3fd1a771d2981cc75938c0b7e112a77e539 Mon Sep 17 00:00:00 2001 From: Mottributo <87079566+Mottributo@users.noreply.github.com> Date: Sat, 25 Mar 2023 22:39:36 +0300 Subject: [PATCH] camera movement made fluid --- sketch.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sketch.js b/sketch.js index a775947..d2257ea 100644 --- a/sketch.js +++ b/sketch.js @@ -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();