Restart after crashing with a keydown event
This commit is contained in:
@@ -65,6 +65,16 @@
|
|||||||
#legs {
|
#legs {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#triangles polygon.obtuse {
|
||||||
|
stroke: orangered;
|
||||||
|
stroke-dasharray: 5 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
#triangles polygon.anti-clockwise {
|
||||||
|
stroke: orange;
|
||||||
|
stroke-dasharray: 1 5;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<rect id="bg" x="-200" y="-150" width="400" height="300"/>
|
<rect id="bg" x="-200" y="-150" width="400" height="300"/>
|
||||||
@@ -123,6 +133,7 @@
|
|||||||
let rotate = 0;
|
let rotate = 0;
|
||||||
let rotationSpeed = 0.25;
|
let rotationSpeed = 0.25;
|
||||||
const maxSpeed = 100;
|
const maxSpeed = 100;
|
||||||
|
let started = false;
|
||||||
|
|
||||||
const fps = document.querySelector("#fps");
|
const fps = document.querySelector("#fps");
|
||||||
const time = document.querySelector("#time");
|
const time = document.querySelector("#time");
|
||||||
@@ -228,6 +239,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.classList[isClockwise && isAcute ? "add" : "remove"](className);
|
t.classList[isClockwise && isAcute ? "add" : "remove"](className);
|
||||||
|
t.classList[isClockwise && !isAcute ? "add" : "remove"]("obtuse");
|
||||||
|
t.classList[!isClockwise ? "add" : "remove"]("anti-clockwise");
|
||||||
if (isClockwise && isAcute) visible.push(t);
|
if (isClockwise && isAcute) visible.push(t);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -352,44 +365,7 @@
|
|||||||
return cornerCollision || sideCollision;
|
return cornerCollision || sideCollision;
|
||||||
}
|
}
|
||||||
|
|
||||||
requestAnimationFrame(firstFrame);
|
function updateShip(elapsed) {
|
||||||
|
|
||||||
let start;
|
|
||||||
let restart = false;
|
|
||||||
function firstFrame(timestamp) {
|
|
||||||
zero = timestamp;
|
|
||||||
zeroForTimer = timestamp;
|
|
||||||
previous = timestamp;
|
|
||||||
animate(timestamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
function distance(x1, y1, x2, y2) {
|
|
||||||
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
function animate(timestamp) {
|
|
||||||
const elapsed = timestamp - previous;
|
|
||||||
const delta = timestamp - zero;
|
|
||||||
let degrees = getRotate(gun);
|
|
||||||
|
|
||||||
previous = timestamp;
|
|
||||||
time.innerText = ((timestamp - zeroForTimer) * 0.001).toFixed(3);
|
|
||||||
|
|
||||||
if (delta >= 1000) {
|
|
||||||
fps.innerText = frameCount;
|
|
||||||
|
|
||||||
// info.innerText = `velocity ${velocity}\n`
|
|
||||||
// + 'bullets\nx\ty\tvx\tvy\n'
|
|
||||||
// + bullets.map(b => {
|
|
||||||
// return `${b.x.toFixed(2)}\t${b.y.toFixed(2)}\t${b.vx.toFixed(2)}\t${b.vy.toFixed(2)}`;
|
|
||||||
// }).join("\n");
|
|
||||||
|
|
||||||
zero = timestamp;
|
|
||||||
frameCount = 0;
|
|
||||||
} else {
|
|
||||||
frameCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`;
|
if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`;
|
||||||
else if (rotate < 0) gun.style.transform = `rotate(${(+degrees - rotationSpeed * elapsed) % 360}deg)`;
|
else if (rotate < 0) gun.style.transform = `rotate(${(+degrees - rotationSpeed * elapsed) % 360}deg)`;
|
||||||
|
|
||||||
@@ -417,7 +393,50 @@
|
|||||||
|
|
||||||
let [x, y] = getTranslate(hitbox);
|
let [x, y] = getTranslate(hitbox);
|
||||||
let position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y);
|
let position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y);
|
||||||
|
hitbox.style.transform = `translate(${positionX}px, ${positionY}px)`;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let start;
|
||||||
|
let restart = false;
|
||||||
|
function firstFrame(timestamp) {
|
||||||
|
zero = timestamp;
|
||||||
|
zeroForTimer = timestamp;
|
||||||
|
previous = timestamp;
|
||||||
|
animate(timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
function distance(x1, y1, x2, y2) {
|
||||||
|
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
let isReadingKeys = true;
|
||||||
|
|
||||||
|
function animate(timestamp) {
|
||||||
|
const elapsed = timestamp - previous;
|
||||||
|
const delta = timestamp - zero;
|
||||||
|
let degrees = getRotate(gun);
|
||||||
|
|
||||||
|
previous = timestamp;
|
||||||
|
time.innerText = ((timestamp - zeroForTimer) * 0.001).toFixed(3);
|
||||||
|
|
||||||
|
if (delta >= 1000) {
|
||||||
|
fps.innerText = frameCount;
|
||||||
|
|
||||||
|
// info.innerText = `velocity ${velocity}\n`
|
||||||
|
// + 'bullets\nx\ty\tvx\tvy\n'
|
||||||
|
// + bullets.map(b => {
|
||||||
|
// return `${b.x.toFixed(2)}\t${b.y.toFixed(2)}\t${b.vx.toFixed(2)}\t${b.vy.toFixed(2)}`;
|
||||||
|
// }).join("\n");
|
||||||
|
|
||||||
|
zero = timestamp;
|
||||||
|
frameCount = 0;
|
||||||
|
} else {
|
||||||
|
frameCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
position = updateShip(elapsed);
|
||||||
updateBullets(elapsed);
|
updateBullets(elapsed);
|
||||||
|
|
||||||
const visibleTriangles = updateTriangles(position);
|
const visibleTriangles = updateTriangles(position);
|
||||||
@@ -447,19 +466,19 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const collision = detectCollision(points, visibleTriangles);
|
const collision = detectCollision(points, visibleTriangles);
|
||||||
|
|
||||||
wall.setAttribute('fill', collision ? 'red' : 'black');
|
wall.setAttribute('fill', collision ? 'red' : 'black');
|
||||||
hitbox.style.transform = `translate(${positionX}px, ${positionY}px)`;
|
|
||||||
|
|
||||||
if (collision) {
|
if (collision) {
|
||||||
// restart game
|
// restart game
|
||||||
zeroForTimer = timestamp;
|
zeroForTimer = timestamp;
|
||||||
restart = true;
|
restart = true;
|
||||||
|
started = false;
|
||||||
|
isReadingKeys = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (+y < 200)
|
// if (+y < 200)
|
||||||
// if (timestamp < 10000)
|
// if (timestamp < 10000)
|
||||||
if (lineContainer.childElementCount > 0)
|
if (lineContainer.childElementCount > 0 && started)
|
||||||
requestAnimationFrame(t => animate(t));
|
requestAnimationFrame(t => animate(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,7 +492,33 @@
|
|||||||
let rotateCWPressed = false;
|
let rotateCWPressed = false;
|
||||||
let rotateCCWPressed = false;
|
let rotateCCWPressed = false;
|
||||||
|
|
||||||
|
const pressed = [
|
||||||
|
spacePressed,
|
||||||
|
upPressed,
|
||||||
|
downPressed,
|
||||||
|
leftPressed,
|
||||||
|
rightPressed,
|
||||||
|
rotateCWPressed,
|
||||||
|
rotateCCWPressed,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
document.addEventListener("keydown", function(e) {
|
document.addEventListener("keydown", function(e) {
|
||||||
|
// console.log(isReadingKeys, pressed.every(k => k));
|
||||||
|
|
||||||
|
if (!isReadingKeys && pressed.some(k => k)) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
isReadingKeys = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!started && !restart) {
|
||||||
|
started = true;
|
||||||
|
requestAnimationFrame(firstFrame);
|
||||||
|
} else if (restart) {
|
||||||
|
requestAnimationFrame(firstFrame);
|
||||||
|
}
|
||||||
|
|
||||||
switch (e.code) {
|
switch (e.code) {
|
||||||
case "Space":
|
case "Space":
|
||||||
if (!spacePressed) {
|
if (!spacePressed) {
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 18 KiB |
Reference in New Issue
Block a user