bounce duck at edge of screen so it doesn't fly off and expand the document

This commit is contained in:
Benjamin Wiegand 2024-09-10 09:22:10 -07:00
parent 45ee8f1134
commit 049affae6a

View File

@ -29,6 +29,7 @@ const turnSpeedCoeff = .08; // coefficient of turning
// other stuff
let brake = false;
const brakeDist = 70;
const duckSize = 50;
// animation
let alt = false;
@ -113,6 +114,26 @@ function update() {
xPos += xVel * deltaT;
yPos += yVel * deltaT;
// bounce duck at edge of screen
if (xPos > document.scrollingElement.scrollWidth - duckSize) {
xPos = document.scrollingElement.scrollWidth - duckSize;
angle = 2*Math.PI - angle;
} else if (xPos < 0) {
xPos = 0;
angle = 2*Math.PI - angle;
}
if (yPos > document.scrollingElement.scrollHeight - duckSize) {
yPos = document.scrollingElement.scrollHeight - duckSize;
angle = Math.PI - angle;
} else if (yPos < 0) {
yPos = 0;
angle = Math.PI - angle;
}
// limit angle again
if (angle > 2*Math.PI) angle -= 2*Math.PI;
else if (angle < 0) angle += 2*Math.PI;
// console.log(`accel: ${accel}`);
// console.log(`velocity: ${velocity} (${xVel}, ${yVel})`)
@ -128,19 +149,19 @@ function update() {
// thing.style['background-color'] = '#ff0000';
// }
let fuck = visualAngle + Math.PI/4;
if (fuck < Math.PI/2 || fuck > Math.PI * 2) {
let tAngle = visualAngle + Math.PI/4;
if (tAngle < Math.PI/2 || tAngle > Math.PI * 2) {
// front-facing
thing.style['background-position-x'] = '100px';
} else if (fuck < Math.PI) {
thing.style['background-position-x'] = (2*duckSize) + 'px';
} else if (tAngle < Math.PI) {
// left-facing
thing.style['background-position-x'] = '0px';
} else if (fuck < Math.PI*3/2) {
} else if (tAngle < Math.PI*3/2) {
// right-facing
thing.style['background-position-x'] = '150px';
thing.style['background-position-x'] = (3*duckSize) + 'px';
} else {
// back-facing
thing.style['background-position-x'] = '50px';
thing.style['background-position-x'] = duckSize + 'px';
}
if (thisUpdate - lastAlt > 70) {
@ -149,13 +170,14 @@ function update() {
}
if (velocity > 50) {
thing.style['background-position-y'] = alt ? '50px' : '100px';
thing.style['background-position-y'] = (duckSize * (alt + 1)) + 'px';
} else {
thing.style['background-position-y'] = '0px';
}
}
// scroll and mouse are separate so the cursor is followed when scrolled
document.addEventListener('mousemove', e => {
mouseX = e.clientX;
mouseY = e.clientY;
@ -167,6 +189,7 @@ document.addEventListener('scroll', e => {
scrollY = el.scrollTop;
});
// kick-start duck
let duck_start = false;
function click_duck() {
if (duck_start) return;