Put player ball at mouse position on start

This commit is contained in:
Midgard 2022-01-29 11:17:41 +01:00
parent 3224b0b424
commit 98eb3ff321
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -397,15 +397,18 @@
return Math.max(min, Math.min(max, value)); return Math.max(min, Math.min(max, value));
} }
function ballPositionFromEvent(event) {
return new Vector(
clamp(event.clientX - document.getElementById("gamecontainer").offsetLeft, BALL_RADIUS, playfieldWidth - BALL_RADIUS),
clamp(event.clientY - document.getElementById("gamecontainer").offsetTop, BALL_RADIUS, playfieldHeight - BALL_RADIUS)
);
}
function moveMouseCallback(event) { function moveMouseCallback(event) {
if (isGameOver) return; if (isGameOver) return;
// move player // move player
var previousPosition = playerBall.outline.centre; var previousPosition = playerBall.outline.centre;
var newPosition = new Vector( var newPosition = ballPositionFromEvent(event);
clamp(event.clientX - document.getElementById("gamecontainer").offsetLeft, BALL_RADIUS, playfieldWidth - BALL_RADIUS),
clamp(event.clientY - document.getElementById("gamecontainer").offsetTop, BALL_RADIUS, playfieldHeight - BALL_RADIUS)
);
playerBall.outline.centre = newPosition; playerBall.outline.centre = newPosition;
playerBall.velocity = newPosition.sub(previousPosition).mul(0.1); playerBall.velocity = newPosition.sub(previousPosition).mul(0.1);
} }
@ -438,20 +441,14 @@
ballAddTimeout = window.setTimeout(ballAddTimeoutFunction, addMessage[1] * 1000); ballAddTimeout = window.setTimeout(ballAddTimeoutFunction, addMessage[1] * 1000);
} }
function startGame() { function startGame(mouseEvent) {
document.getElementById("music").currentTime = 0; document.getElementById("music").currentTime = 0;
document.getElementById("music").volume = 0.5; document.getElementById("music").volume = 0.5;
document.getElementById("music").play(); document.getElementById("music").play();
document.getElementById("playbutton").style.display = "none"; document.getElementById("playbutton").style.display = "none";
playerBall = new Ball( playerBall = new Ball(
new Circle( new Circle(ballPositionFromEvent(mouseEvent), BALL_RADIUS),
new Vector(
canvas.width / 2 - BALL_RADIUS,
canvas.height / 2 - BALL_RADIUS
),
BALL_RADIUS
),
new Vector(0, 0), new Vector(0, 0),
"blue", "blue",
0 0