/**
 * Developer: Molla Samser
 * Organization: RSK World
 * Website: https://rskworld.in
 * Email: hello@rskworld.in, support@rskworld.in
 * Phone: +91 93305 39277
 * Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147
 * Copyright © 2026 RSK World. All rights reserved.
 */

function rectangularCollision({ rectangle1, rectangle2 }) {
  return (
    rectangle1.attackBox.position.x + rectangle1.attackBox.width >=
      rectangle2.position.x &&
    rectangle1.attackBox.position.x <=
      rectangle2.position.x + rectangle2.width &&
    rectangle1.attackBox.position.y + rectangle1.attackBox.height >=
      rectangle2.position.y &&
    rectangle1.attackBox.position.y <= rectangle2.position.y + rectangle2.height
  )
}

function determineWinner({ player, enemy, timerId }) {
  clearTimeout(timerId)
  const displayOverlay = document.querySelector('#displayText')
  const winnerName = document.querySelector('#winnerName')
  displayOverlay.style.display = 'flex'
  
  if (player.health === enemy.health) {
    winnerName.innerHTML = 'TIE'
  } else if (player.health > enemy.health) {
    winnerName.innerHTML = 'PLAYER 1 WINS'
  } else if (player.health < enemy.health) {
    winnerName.innerHTML = 'PLAYER 2 WINS'
  }
}

let timer = 60
let timerId
function decreaseTimer() {
  if (timer > 0) {
    timerId = setTimeout(decreaseTimer, 1000)
    timer--
    document.querySelector('#timer').innerHTML = timer
  }

  if (timer === 0) {
    determineWinner({ player, enemy, timerId })
  }
}
