WIP: fire bullets
This commit is contained in:
@@ -57,8 +57,11 @@
|
|||||||
<line x1="0" y1="0" x2="0" y2="8" stroke="black"/>
|
<line x1="0" y1="0" x2="0" y2="8" stroke="black"/>
|
||||||
</g>
|
</g>
|
||||||
</g>
|
</g>
|
||||||
|
<g id="bullets">
|
||||||
|
<!-- <circle r="10" x="10" cy="10"/> -->
|
||||||
|
</g>
|
||||||
|
|
||||||
<circle id="bullet" cx="0" cy="0" r="1"/>
|
<!-- <circle id="bullet" cx="0" cy="0" r="1"/> -->
|
||||||
|
|
||||||
<!-- <g id="crosshair"> -->
|
<!-- <g id="crosshair"> -->
|
||||||
<!-- <line x1="-2" y1="0" x2="2" y2="0" stroke="red"/> -->
|
<!-- <line x1="-2" y1="0" x2="2" y2="0" stroke="red"/> -->
|
||||||
@@ -81,20 +84,13 @@
|
|||||||
</foreignObject>
|
</foreignObject>
|
||||||
|
|
||||||
<script type="text/javascript">//<![CDATA[
|
<script type="text/javascript">//<![CDATA[
|
||||||
const rules = document.styleSheets[0].cssRules;
|
const namespaceURIsvg = 'http://www.w3.org/2000/svg';
|
||||||
const tank = document.querySelector(".tank");
|
const degsRegex = /(-?\d*\.{0,1}\d+)deg/g;
|
||||||
const gun = tank.querySelector('line');
|
const regex = /(-?\d*\.{0,1}\d+)px/g;
|
||||||
const hitbox = document.querySelector(".hitbox");
|
const def = ["0px", "0"];
|
||||||
const leftTurnButton = document.querySelector("#turn-left");
|
let bullets = [];
|
||||||
const rightTurnButton = document.querySelector("#turn-right");
|
|
||||||
const reverseMoveButton = document.querySelector("#move-backward");
|
|
||||||
const forwardMoveButton = document.querySelector("#move-forward");
|
|
||||||
const bullet = document.querySelector("#bullet");
|
|
||||||
|
|
||||||
const rotateCWButton = document.querySelector("#rotate-cw");
|
let position = [0, 0]; // meters
|
||||||
const rotateCCWButton = document.querySelector("#rotate-ccw");
|
|
||||||
|
|
||||||
const fps = document.querySelector("#fps");
|
|
||||||
let velocity = [0, 0]; // meters per second
|
let velocity = [0, 0]; // meters per second
|
||||||
let acceleration = [0, 0]; // meters per second per second
|
let acceleration = [0, 0]; // meters per second per second
|
||||||
let previous, zero, frameCount = 0;
|
let previous, zero, frameCount = 0;
|
||||||
@@ -102,28 +98,64 @@
|
|||||||
let rotate = 0;
|
let rotate = 0;
|
||||||
let rotationSpeed = 0.25;
|
let rotationSpeed = 0.25;
|
||||||
|
|
||||||
let bullets = [
|
const rules = document.styleSheets[0].cssRules;
|
||||||
{ x: 0, y: 0, vx: 0, vy: 1, time: 5000 }
|
|
||||||
];
|
const fps = document.querySelector("#fps");
|
||||||
|
const tank = document.querySelector(".tank");
|
||||||
|
const gun = tank.querySelector('line');
|
||||||
|
const hitbox = document.querySelector(".hitbox");
|
||||||
|
const bulletsContainer = document.querySelector("#bullets");
|
||||||
|
|
||||||
|
const leftTurnButton = document.querySelector("#turn-left");
|
||||||
|
const rightTurnButton = document.querySelector("#turn-right");
|
||||||
|
const reverseMoveButton = document.querySelector("#move-backward");
|
||||||
|
const forwardMoveButton = document.querySelector("#move-forward");
|
||||||
|
|
||||||
|
const rotateCWButton = document.querySelector("#rotate-cw");
|
||||||
|
const rotateCCWButton = document.querySelector("#rotate-ccw");
|
||||||
|
|
||||||
|
const fireButton = document.querySelector("#fire");
|
||||||
|
|
||||||
gun.style.transform = "rotate(0deg)";
|
gun.style.transform = "rotate(0deg)";
|
||||||
|
|
||||||
function wrapPos(positionX, positionY) {
|
function wrapPos(positionX, positionY) {
|
||||||
let x, y;
|
let x, y;
|
||||||
|
|
||||||
// if (positionY > 150) positionY += -300;
|
|
||||||
if (positionY > 150) y = positionY - 300;
|
if (positionY > 150) y = positionY - 300;
|
||||||
// else if (positionY < -150) positionY += 300;
|
|
||||||
else if (positionY < -150) y = positionY + 300;
|
else if (positionY < -150) y = positionY + 300;
|
||||||
else y = positionY;
|
else y = positionY;
|
||||||
|
|
||||||
if (positionX > 200) positionX += -400;
|
if (positionX > 200) x = positionX - 400;
|
||||||
else if (positionX < -200) positionX += 400;
|
else if (positionX < -200) x = positionX + 400;
|
||||||
else x = positionX;
|
else x = positionX;
|
||||||
|
|
||||||
return [x, y];
|
return [x, y];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fireBullet(x, y) {
|
||||||
|
const el = document.createElementNS(namespaceURIsvg, 'circle');
|
||||||
|
el.setAttribute('r', 1);
|
||||||
|
el.setAttribute('cx', x);
|
||||||
|
el.setAttribute('cy', y);
|
||||||
|
const node = bulletsContainer.appendChild(el);
|
||||||
|
const bullet = { x: x, y: y, vx: 1, vy: 1, time: 5000, node: node }
|
||||||
|
bullets.push(bullet);
|
||||||
|
console.log("bullet fired", bullet, bullets);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTransform(el) {
|
||||||
|
let x, y;
|
||||||
|
|
||||||
|
if (el.style.transform.length === 0) {
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
} else {
|
||||||
|
[[, x], [, y] = def] = [...el.style.transform.matchAll(regex)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [x, y];
|
||||||
|
}
|
||||||
|
|
||||||
requestAnimationFrame(firstFrame);
|
requestAnimationFrame(firstFrame);
|
||||||
|
|
||||||
function firstFrame(timestamp) {
|
function firstFrame(timestamp) {
|
||||||
@@ -145,8 +177,6 @@
|
|||||||
frameCount++;
|
frameCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
const degsRegex = /(-?\d*\.{0,1}\d+)deg/g;
|
|
||||||
|
|
||||||
let [[, degrees]] = [...gun.style.transform.matchAll(degsRegex)];
|
let [[, degrees]] = [...gun.style.transform.matchAll(degsRegex)];
|
||||||
|
|
||||||
if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`;
|
if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`;
|
||||||
@@ -168,21 +198,22 @@
|
|||||||
const changeX = 0.001 * elapsed * velocityX;
|
const changeX = 0.001 * elapsed * velocityX;
|
||||||
const changeY = 0.001 * elapsed * velocityY;
|
const changeY = 0.001 * elapsed * velocityY;
|
||||||
|
|
||||||
const regex = /(-?\d*\.{0,1}\d+)px/g;
|
bullets.forEach((bullet, index) => {
|
||||||
const def = ["0px", "0"];
|
bullets[index].time -= elapsed;
|
||||||
|
|
||||||
bullets[0].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;
|
||||||
|
|
||||||
if (bullets[0].time > 0) {
|
let [bx, by] = wrapPos(bullets[index].x, bullets[index].y)
|
||||||
bullets[0].y += 0.1 * elapsed * bullets[0].vy;
|
bullets[index].x = bx;
|
||||||
|
bullets[index].y = by;
|
||||||
let [bx, by] = wrapPos(bullets[0].x, bullets[0].y)
|
bullet.node.style.transform = `translate(${bx}px, ${by}px)`;
|
||||||
bullets[0].x = bx;
|
} else {
|
||||||
bullets[0].y = by;
|
bullet.node.remove();
|
||||||
bullet.style.transform = `translate(${bx}px, ${by}px)`;
|
bullets.splice(index, 1);
|
||||||
} else {
|
}
|
||||||
bullet.remove();
|
});
|
||||||
}
|
|
||||||
|
|
||||||
let x, y;
|
let x, y;
|
||||||
if (hitbox.style.transform.length === 0) {
|
if (hitbox.style.transform.length === 0) {
|
||||||
@@ -261,6 +292,10 @@
|
|||||||
rotate = 0;
|
rotate = 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
fireButton.addEventListener("click", function (e) {
|
||||||
|
const [x, y] = getTransform(hitbox);
|
||||||
|
fireBullet(+x, +y);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// tankTurn.addEventListener("finish", function (e) {
|
// tankTurn.addEventListener("finish", function (e) {
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 7.8 KiB After Width: | Height: | Size: 8.9 KiB |
Reference in New Issue
Block a user