Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush-848 committed Jul 25, 2024
1 parent 8843fd2 commit 9f4c9d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 55 deletions.
69 changes: 15 additions & 54 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Game {
this.timeLeft = 30;
this.timer = null;
this.timerDisplay = null;
this.timerStarted = false;
this.isTimerStarted = false;

this.addPlayersUI();
this.updateScoreboard();
Expand All @@ -50,6 +50,7 @@ class Game {
this.addEventListener("boxFill", () => this.onBoxFill());
this.addEventListener("playerSwitch", () => this.onPlayerSwitch());
this.addEventListener("playerWin", () => this.onPlayerWin());
this.addEventListener("edgeFill", () => this.onEdgeFill());
}

// Create timer UI
Expand All @@ -62,55 +63,6 @@ class Game {
`;
document.body.appendChild(timerContainer);
this.timerDisplay = document.getElementById('timer');
this.makeTimerDraggable(timerContainer);
}

// Make timer draggable
makeTimerDraggable(timerContainer) {
let isDragging = false;
let currentX;
let currentY;
let initialX;
let initialY;
let xOffset = 0;
let yOffset = 0;

timerContainer.addEventListener("mousedown", dragStart);
document.addEventListener("mousemove", drag);
document.addEventListener("mouseup", dragEnd);

function dragStart(e) {
initialX = e.clientX - xOffset;
initialY = e.clientY - yOffset;

if (e.target === timerContainer) {
isDragging = true;
}
}

function drag(e) {
if (isDragging) {
e.preventDefault();
currentX = e.clientX - initialX;
currentY = e.clientY - initialY;

xOffset = currentX;
yOffset = currentY;

setTranslate(currentX, currentY, timerContainer);
}
}

function dragEnd(e) {
initialX = currentX;
initialY = currentY;

isDragging = false;
}

function setTranslate(xPos, yPos, el) {
el.style.transform = `translate3d(${xPos}px, ${yPos}px, 0)`;
}
}

// Start or restart the timer
Expand Down Expand Up @@ -178,7 +130,7 @@ class Game {

onPlayerSwitch() {
this.updatePlayerNameUI();
if (this.timerStarted) {
if (this.isTimerStarted) {
this.startTimer(); // Restart timer for the new player
}
}
Expand All @@ -188,10 +140,19 @@ class Game {
this.currentPlayer.filledBoxes++;
this.updatePlayerScoreUI();
this.updateScoreboard();
if (!this.timerStarted) {
this.timerStarted = true;
if (this.isTimerStarted) {
this.startTimer(); // Restart timer when a move is made
}
}

// New method to handle edge fill event
onEdgeFill() {
if (!this.isTimerStarted) {
this.isTimerStarted = true;
this.startTimer();
} else {
this.startTimer(); // Restart timer when a move is made
}
this.startTimer(); // Restart timer when a move is made
}

// Add players to UI
Expand Down
2 changes: 1 addition & 1 deletion pages/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
text-align: center;
z-index: 1000;
cursor: move;
margin-right: 10vw;
}

#timer {
Expand Down

0 comments on commit 9f4c9d9

Please sign in to comment.