Put player ball at mouse position on start
This commit is contained in:
parent
3224b0b424
commit
98eb3ff321
1 changed files with 9 additions and 12 deletions
|
@ -397,15 +397,18 @@
|
|||
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) {
|
||||
if (isGameOver) return;
|
||||
// move player
|
||||
var previousPosition = playerBall.outline.centre;
|
||||
|
||||
var newPosition = 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)
|
||||
);
|
||||
var newPosition = ballPositionFromEvent(event);
|
||||
playerBall.outline.centre = newPosition;
|
||||
playerBall.velocity = newPosition.sub(previousPosition).mul(0.1);
|
||||
}
|
||||
|
@ -438,20 +441,14 @@
|
|||
ballAddTimeout = window.setTimeout(ballAddTimeoutFunction, addMessage[1] * 1000);
|
||||
}
|
||||
|
||||
function startGame() {
|
||||
function startGame(mouseEvent) {
|
||||
document.getElementById("music").currentTime = 0;
|
||||
document.getElementById("music").volume = 0.5;
|
||||
document.getElementById("music").play();
|
||||
document.getElementById("playbutton").style.display = "none";
|
||||
|
||||
playerBall = new Ball(
|
||||
new Circle(
|
||||
new Vector(
|
||||
canvas.width / 2 - BALL_RADIUS,
|
||||
canvas.height / 2 - BALL_RADIUS
|
||||
),
|
||||
BALL_RADIUS
|
||||
),
|
||||
new Circle(ballPositionFromEvent(mouseEvent), BALL_RADIUS),
|
||||
new Vector(0, 0),
|
||||
"blue",
|
||||
0
|
||||
|
|
Loading…
Reference in a new issue