Limit ship max speed
This commit is contained in:
@@ -76,6 +76,7 @@
|
||||
let friction = 0;
|
||||
let rotate = 0;
|
||||
let rotationSpeed = 0.25;
|
||||
const maxSpeed = 100;
|
||||
|
||||
const rules = document.styleSheets[0].cssRules;
|
||||
|
||||
@@ -112,9 +113,10 @@
|
||||
return [x, y];
|
||||
}
|
||||
|
||||
function fireBullet(x, y) {
|
||||
function fireBullet(x, y, velocity) {
|
||||
const degrees = getRotate(gun);
|
||||
const radians = degrees * Math.PI / 180; // toFixed(15)?
|
||||
const speed = 200; // meters per second
|
||||
const vx = -Math.sin(radians);
|
||||
const vy = Math.cos(radians);
|
||||
const bulletTimeout = 5000; // miliseconds
|
||||
@@ -129,8 +131,8 @@
|
||||
const bullet = {
|
||||
x: x + vx * cannonLength,
|
||||
y: y + vy * cannonLength,
|
||||
vx: vx,
|
||||
vy: vy,
|
||||
vx: vx * speed + velocity[0],
|
||||
vy: vy * speed + velocity[1],
|
||||
time: bulletTimeout,
|
||||
node: bulletsContainer.appendChild(el)
|
||||
}
|
||||
@@ -172,9 +174,11 @@
|
||||
if (delta >= 1000) {
|
||||
fps.innerText = frameCount;
|
||||
|
||||
info.innerText = 'bullets\nx\ty\tvx\tvy\n' + bullets.map((b, index) => {
|
||||
return `${b.x.toFixed(2)}\t${b.y.toFixed(2)}\t${b.vx.toFixed(2)}\t${b.vy.toFixed(2)}`;
|
||||
}).join("\n");
|
||||
info.innerText = `velocity ${velocity}\n`
|
||||
+ 'bullets\nx\ty\tvx\tvy\n'
|
||||
+ bullets.map((b, index) => {
|
||||
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;
|
||||
@@ -200,6 +204,13 @@
|
||||
velocityY = velocityY > 0 && velocityY + accelerationY < 0 ? 0 : velocityY + accelerationY;
|
||||
velocity = [velocityX, velocityY];
|
||||
|
||||
|
||||
if (velocity[0] > maxSpeed) velocity[0] = maxSpeed;
|
||||
else if (velocity[0] < -maxSpeed) velocity[0] = -maxSpeed
|
||||
|
||||
if (velocity[1] > maxSpeed) velocity[1] = maxSpeed;
|
||||
else if (velocity[1] < -maxSpeed) velocity[1] = -maxSpeed
|
||||
|
||||
const changeX = 0.001 * elapsed * velocityX;
|
||||
const changeY = 0.001 * elapsed * velocityY;
|
||||
|
||||
@@ -208,8 +219,8 @@
|
||||
bullets[index].time -= elapsed;
|
||||
|
||||
if (bullets[index].time > 0) {
|
||||
bullets[index].y += 0.1 * elapsed * bullets[index].vy;
|
||||
bullets[index].x += 0.1 * elapsed * bullets[index].vx;
|
||||
bullets[index].y += 0.001 * elapsed * bullets[index].vy;
|
||||
bullets[index].x += 0.001 * elapsed * bullets[index].vx;
|
||||
|
||||
let [bx, by] = wrapPos(bullets[index].x, bullets[index].y)
|
||||
bullets[index].x = bx;
|
||||
@@ -254,7 +265,8 @@
|
||||
if (!spacePressed) {
|
||||
spacePressed = true;
|
||||
const [x, y] = getTranslate(hitbox);
|
||||
fireBullet(x, y);
|
||||
// fireBullet(x, y);
|
||||
fireBullet(x, y, velocity);
|
||||
}
|
||||
break;
|
||||
case "KeyW":
|
||||
|
||||
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 12 KiB |
Reference in New Issue
Block a user