Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added smooth flashlight transitions #118

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/js/Flashlight.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let darkness;
let flashlightRotation;
//revert
const ROTATION_SPEED = 0.1;
let darkness
let currentRotation = 0, targetRotation = 0;

function darknessSetup() {
darkness = new Sprite(width/2, height/2, 1920, 1080); // creates a sprite that is the size of the canvas and at the center of the canvas
darkness.img = "assets/darknessEyesight.svg"
Expand All @@ -11,14 +12,25 @@ function darknessSetup() {
function darknessDraw(playerX, playerY, playerVelocityX, playerVelocityY, hasFlashlight) {
darkness.y = playerY;
darkness.x = playerX;
if(playerVelocityX != 0 || playerVelocityY != 0) { // if player is moving
darkness.rotation = atan2(playerVelocityX, -playerVelocityY); // calculates the angle formed between two points

if(playerVelocityX !== 0 || playerVelocityY !== 0) { // if player is moving
targetRotation = (Math.atan2(playerVelocityX, -playerVelocityY) * 180 / Math.PI + 360) % 360; // calculates the angle formed between two points, converts to degrees, range of 0-359

// calculate the shortest rotation direction
let diff = (targetRotation - currentRotation + 540) % 360 - 180; // finds difference between angles, add 540 to ensure positive before modulo, range of -180-179

// adjust targetRotation to rotate in the shortest direction
targetRotation = currentRotation + diff;
}

currentRotation = lerp(currentRotation, targetRotation, ROTATION_SPEED); // currentRotation gets ROTATION_SPEED times closer to the target

darkness.rotation = currentRotation;

if(hasFlashlight){
darkness.img = "assets/darknessFlashlight.svg"
}
else{
darkness.img = "assets/darknessEyesight.svg"
}
}
}
Loading